blob: f148b5956a83e757231e06bcfdcb3a86271d51a2 [file] [log] [blame]
Carol Wang4ca6f3f2020-02-19 16:28:59 +08001#include "scheduled_host_transition.hpp"
2
3#include <gmock/gmock.h>
4#include <gtest/gtest.h>
5#include <sdbusplus/bus.hpp>
6#include <sdbusplus/test/sdbus_mock.hpp>
Carol Wang6a5db3d2020-02-21 10:12:01 +08007#include <sdeventplus/event.hpp>
Carol Wang4ca6f3f2020-02-19 16:28:59 +08008#include <xyz/openbmc_project/ScheduledTime/error.hpp>
9
10namespace phosphor
11{
12namespace state
13{
14namespace manager
15{
16
17using namespace std::chrono;
18using InvalidTimeError =
19 sdbusplus::xyz::openbmc_project::ScheduledTime::Error::InvalidTime;
Carol Wangef7abe12020-02-25 16:19:34 +080020using HostTransition =
21 sdbusplus::xyz::openbmc_project::State::server::ScheduledHostTransition;
Carol Wang4ca6f3f2020-02-19 16:28:59 +080022
23class TestScheduledHostTransition : public testing::Test
24{
25 public:
Carol Wang6a5db3d2020-02-21 10:12:01 +080026 sdeventplus::Event event;
Carol Wang4ca6f3f2020-02-19 16:28:59 +080027 sdbusplus::SdBusMock sdbusMock;
28 sdbusplus::bus::bus mockedBus = sdbusplus::get_mocked_new(&sdbusMock);
29 ScheduledHostTransition scheduledHostTransition;
30
Carol Wang6a5db3d2020-02-21 10:12:01 +080031 TestScheduledHostTransition() :
32 event(sdeventplus::Event::get_default()),
33 scheduledHostTransition(mockedBus, "", event)
Carol Wang4ca6f3f2020-02-19 16:28:59 +080034 {
35 // Empty
36 }
37
38 seconds getCurrentTime()
39 {
40 return scheduledHostTransition.getTime();
41 }
Carol Wang6a5db3d2020-02-21 10:12:01 +080042
43 bool isTimerEnabled()
44 {
45 return scheduledHostTransition.timer.isEnabled();
46 }
Carol Wangef7abe12020-02-25 16:19:34 +080047
48 void bmcTimeChange()
49 {
50 scheduledHostTransition.handleTimeUpdates();
51 }
Carol Wang4ca6f3f2020-02-19 16:28:59 +080052};
53
54TEST_F(TestScheduledHostTransition, disableHostTransition)
55{
56 EXPECT_EQ(scheduledHostTransition.scheduledTime(0), 0);
Carol Wang6a5db3d2020-02-21 10:12:01 +080057 EXPECT_FALSE(isTimerEnabled());
Carol Wang4ca6f3f2020-02-19 16:28:59 +080058}
59
60TEST_F(TestScheduledHostTransition, invalidScheduledTime)
61{
62 // scheduled time is 1 min earlier than current time
63 uint64_t schTime =
64 static_cast<uint64_t>((getCurrentTime() - seconds(60)).count());
65 EXPECT_THROW(scheduledHostTransition.scheduledTime(schTime),
66 InvalidTimeError);
67}
68
Carol Wang6a5db3d2020-02-21 10:12:01 +080069TEST_F(TestScheduledHostTransition, validScheduledTime)
70{
71 // scheduled time is 1 min later than current time
72 uint64_t schTime =
73 static_cast<uint64_t>((getCurrentTime() + seconds(60)).count());
74 EXPECT_EQ(scheduledHostTransition.scheduledTime(schTime), schTime);
75 EXPECT_TRUE(isTimerEnabled());
76}
77
78TEST_F(TestScheduledHostTransition, hostTransitionStatus)
79{
80 // set requested transition to be on
81 scheduledHostTransition.scheduledTransition(Transition::On);
82 EXPECT_EQ(scheduledHostTransition.scheduledTransition(), Transition::On);
83 // set requested transition to be off
84 scheduledHostTransition.scheduledTransition(Transition::Off);
85 EXPECT_EQ(scheduledHostTransition.scheduledTransition(), Transition::Off);
86}
87
Carol Wangef7abe12020-02-25 16:19:34 +080088TEST_F(TestScheduledHostTransition, bmcTimeChangeWithDisabledHostTransition)
89{
90 // Disable host transition
91 scheduledHostTransition.scheduledTime(0);
92 bmcTimeChange();
93 // Check timer
94 EXPECT_FALSE(isTimerEnabled());
95 // Check scheduled time
96 EXPECT_EQ(scheduledHostTransition.HostTransition::scheduledTime(), 0);
97}
98
99TEST_F(TestScheduledHostTransition, bmcTimeChangeBackward)
100{
101 // Current time is earlier than scheduled time due to BMC time changing
102 uint64_t schTime =
103 static_cast<uint64_t>((getCurrentTime() + seconds(60)).count());
104 // Set scheduled time, which is the same as bmc time is changed.
105 // But can't use this method to write another case like
106 // bmcTimeChangeForward, because set a scheduled time earlier than current
107 // time will throw an error.
108 scheduledHostTransition.scheduledTime(schTime);
109 bmcTimeChange();
110 // Check timer
111 EXPECT_TRUE(isTimerEnabled());
112}
113
Carol Wang4ca6f3f2020-02-19 16:28:59 +0800114} // namespace manager
115} // namespace state
116} // namespace phosphor