blob: 4807a8f78e06f516632f791d647123569a1d1486 [file] [log] [blame]
Carol Wang4ca6f3f2020-02-19 16:28:59 +08001#include "scheduled_host_transition.hpp"
2
Carol Wang4ca6f3f2020-02-19 16:28:59 +08003#include <sdbusplus/bus.hpp>
4#include <sdbusplus/test/sdbus_mock.hpp>
Carol Wang6a5db3d2020-02-21 10:12:01 +08005#include <sdeventplus/event.hpp>
Carol Wang4ca6f3f2020-02-19 16:28:59 +08006#include <xyz/openbmc_project/ScheduledTime/error.hpp>
7
Andrew Geisslere426b582020-05-28 12:40:55 -05008#include <gmock/gmock.h>
9#include <gtest/gtest.h>
10
Carol Wang4ca6f3f2020-02-19 16:28:59 +080011namespace phosphor
12{
13namespace state
14{
15namespace manager
16{
17
18using namespace std::chrono;
19using InvalidTimeError =
20 sdbusplus::xyz::openbmc_project::ScheduledTime::Error::InvalidTime;
Carol Wangef7abe12020-02-25 16:19:34 +080021using HostTransition =
Patrick Williams7e969cb2023-08-23 16:24:23 -050022 sdbusplus::server::xyz::openbmc_project::state::ScheduledHostTransition;
Carol Wang4ca6f3f2020-02-19 16:28:59 +080023
24class TestScheduledHostTransition : public testing::Test
25{
26 public:
Carol Wang6a5db3d2020-02-21 10:12:01 +080027 sdeventplus::Event event;
Carol Wang4ca6f3f2020-02-19 16:28:59 +080028 sdbusplus::SdBusMock sdbusMock;
Patrick Williamsf053e6f2022-07-22 19:26:54 -050029 sdbusplus::bus_t mockedBus = sdbusplus::get_mocked_new(&sdbusMock);
Carol Wang4ca6f3f2020-02-19 16:28:59 +080030 ScheduledHostTransition scheduledHostTransition;
31
Carol Wang6a5db3d2020-02-21 10:12:01 +080032 TestScheduledHostTransition() :
33 event(sdeventplus::Event::get_default()),
Patrick Williams211d9722022-04-07 21:24:33 -050034 scheduledHostTransition(mockedBus, "", 0, event)
Carol Wang4ca6f3f2020-02-19 16:28:59 +080035 {
36 // Empty
37 }
38
Pavithra Barithayaa1c0e5c2024-06-21 12:39:38 -050039 static seconds getCurrentTime()
Carol Wang4ca6f3f2020-02-19 16:28:59 +080040 {
Pavithra Barithayaa1c0e5c2024-06-21 12:39:38 -050041 return ScheduledHostTransition::getTime();
Carol Wang4ca6f3f2020-02-19 16:28:59 +080042 }
Carol Wang6a5db3d2020-02-21 10:12:01 +080043
44 bool isTimerEnabled()
45 {
46 return scheduledHostTransition.timer.isEnabled();
47 }
Carol Wangef7abe12020-02-25 16:19:34 +080048
49 void bmcTimeChange()
50 {
51 scheduledHostTransition.handleTimeUpdates();
52 }
Carol Wang4ca6f3f2020-02-19 16:28:59 +080053};
54
55TEST_F(TestScheduledHostTransition, disableHostTransition)
56{
57 EXPECT_EQ(scheduledHostTransition.scheduledTime(0), 0);
Carol Wang6a5db3d2020-02-21 10:12:01 +080058 EXPECT_FALSE(isTimerEnabled());
Carol Wang4ca6f3f2020-02-19 16:28:59 +080059}
60
61TEST_F(TestScheduledHostTransition, invalidScheduledTime)
62{
Pavithra Barithayaa1c0e5c2024-06-21 12:39:38 -050063 seconds currentTime = getCurrentTime();
Carol Wang4ca6f3f2020-02-19 16:28:59 +080064 // scheduled time is 1 min earlier than current time
65 uint64_t schTime =
Pavithra Barithayaa1c0e5c2024-06-21 12:39:38 -050066 static_cast<uint64_t>((currentTime - seconds(60)).count());
Carol Wang4ca6f3f2020-02-19 16:28:59 +080067 EXPECT_THROW(scheduledHostTransition.scheduledTime(schTime),
68 InvalidTimeError);
69}
70
Carol Wang6a5db3d2020-02-21 10:12:01 +080071TEST_F(TestScheduledHostTransition, validScheduledTime)
72{
Pavithra Barithayaa1c0e5c2024-06-21 12:39:38 -050073 seconds currentTime = getCurrentTime();
Carol Wang6a5db3d2020-02-21 10:12:01 +080074 // scheduled time is 1 min later than current time
75 uint64_t schTime =
Pavithra Barithayaa1c0e5c2024-06-21 12:39:38 -050076 static_cast<uint64_t>((currentTime + seconds(60)).count());
Carol Wang6a5db3d2020-02-21 10:12:01 +080077 EXPECT_EQ(scheduledHostTransition.scheduledTime(schTime), schTime);
78 EXPECT_TRUE(isTimerEnabled());
79}
80
81TEST_F(TestScheduledHostTransition, hostTransitionStatus)
82{
83 // set requested transition to be on
84 scheduledHostTransition.scheduledTransition(Transition::On);
85 EXPECT_EQ(scheduledHostTransition.scheduledTransition(), Transition::On);
86 // set requested transition to be off
87 scheduledHostTransition.scheduledTransition(Transition::Off);
88 EXPECT_EQ(scheduledHostTransition.scheduledTransition(), Transition::Off);
89}
90
Carol Wangef7abe12020-02-25 16:19:34 +080091TEST_F(TestScheduledHostTransition, bmcTimeChangeWithDisabledHostTransition)
92{
93 // Disable host transition
94 scheduledHostTransition.scheduledTime(0);
95 bmcTimeChange();
96 // Check timer
97 EXPECT_FALSE(isTimerEnabled());
98 // Check scheduled time
99 EXPECT_EQ(scheduledHostTransition.HostTransition::scheduledTime(), 0);
100}
101
102TEST_F(TestScheduledHostTransition, bmcTimeChangeBackward)
103{
Pavithra Barithayaa1c0e5c2024-06-21 12:39:38 -0500104 seconds currentTime = getCurrentTime();
Carol Wangef7abe12020-02-25 16:19:34 +0800105 // Current time is earlier than scheduled time due to BMC time changing
106 uint64_t schTime =
Pavithra Barithayaa1c0e5c2024-06-21 12:39:38 -0500107 static_cast<uint64_t>((currentTime + seconds(60)).count());
Carol Wangef7abe12020-02-25 16:19:34 +0800108 // Set scheduled time, which is the same as bmc time is changed.
109 // But can't use this method to write another case like
110 // bmcTimeChangeForward, because set a scheduled time earlier than current
111 // time will throw an error.
112 scheduledHostTransition.scheduledTime(schTime);
113 bmcTimeChange();
114 // Check timer
115 EXPECT_TRUE(isTimerEnabled());
116}
117
Carol Wang4ca6f3f2020-02-19 16:28:59 +0800118} // namespace manager
119} // namespace state
120} // namespace phosphor