1 #include "scheduled_host_transition.hpp"
2
3 #include <sdbusplus/bus.hpp>
4 #include <sdbusplus/test/sdbus_mock.hpp>
5 #include <sdeventplus/event.hpp>
6 #include <xyz/openbmc_project/ScheduledTime/error.hpp>
7
8 #include <gmock/gmock.h>
9 #include <gtest/gtest.h>
10
11 namespace phosphor
12 {
13 namespace state
14 {
15 namespace manager
16 {
17
18 using namespace std::chrono;
19 using InvalidTimeError =
20 sdbusplus::xyz::openbmc_project::ScheduledTime::Error::InvalidTime;
21 using HostTransition =
22 sdbusplus::server::xyz::openbmc_project::state::ScheduledHostTransition;
23
24 class TestScheduledHostTransition : public testing::Test
25 {
26 public:
27 sdeventplus::Event event;
28 sdbusplus::SdBusMock sdbusMock;
29 sdbusplus::bus_t mockedBus = sdbusplus::get_mocked_new(&sdbusMock);
30 ScheduledHostTransition scheduledHostTransition;
31
TestScheduledHostTransition()32 TestScheduledHostTransition() :
33 event(sdeventplus::Event::get_default()),
34 scheduledHostTransition(mockedBus, "", 0, event)
35 {
36 // Empty
37 }
38
getCurrentTime()39 static seconds getCurrentTime()
40 {
41 return ScheduledHostTransition::getTime();
42 }
43
isTimerEnabled()44 bool isTimerEnabled()
45 {
46 return scheduledHostTransition.timer.isEnabled();
47 }
48
bmcTimeChange()49 void bmcTimeChange()
50 {
51 scheduledHostTransition.handleTimeUpdates();
52 }
53 };
54
TEST_F(TestScheduledHostTransition,disableHostTransition)55 TEST_F(TestScheduledHostTransition, disableHostTransition)
56 {
57 EXPECT_EQ(scheduledHostTransition.scheduledTime(0), 0);
58 EXPECT_FALSE(isTimerEnabled());
59 }
60
TEST_F(TestScheduledHostTransition,invalidScheduledTime)61 TEST_F(TestScheduledHostTransition, invalidScheduledTime)
62 {
63 seconds currentTime = getCurrentTime();
64 // scheduled time is 1 min earlier than current time
65 uint64_t schTime =
66 static_cast<uint64_t>((currentTime - seconds(60)).count());
67 EXPECT_THROW(scheduledHostTransition.scheduledTime(schTime),
68 InvalidTimeError);
69 }
70
TEST_F(TestScheduledHostTransition,validScheduledTime)71 TEST_F(TestScheduledHostTransition, validScheduledTime)
72 {
73 seconds currentTime = getCurrentTime();
74 // scheduled time is 1 min later than current time
75 uint64_t schTime =
76 static_cast<uint64_t>((currentTime + seconds(60)).count());
77 EXPECT_EQ(scheduledHostTransition.scheduledTime(schTime), schTime);
78 EXPECT_TRUE(isTimerEnabled());
79 }
80
TEST_F(TestScheduledHostTransition,hostTransitionStatus)81 TEST_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
TEST_F(TestScheduledHostTransition,bmcTimeChangeWithDisabledHostTransition)91 TEST_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
TEST_F(TestScheduledHostTransition,bmcTimeChangeBackward)102 TEST_F(TestScheduledHostTransition, bmcTimeChangeBackward)
103 {
104 seconds currentTime = getCurrentTime();
105 // Current time is earlier than scheduled time due to BMC time changing
106 uint64_t schTime =
107 static_cast<uint64_t>((currentTime + seconds(60)).count());
108 // 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
118 } // namespace manager
119 } // namespace state
120 } // namespace phosphor
121