blob: 58e7aea04a2e7214b5da4f1fd115c79688e2fedf [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;
20
21class TestScheduledHostTransition : public testing::Test
22{
23 public:
Carol Wang6a5db3d2020-02-21 10:12:01 +080024 sdeventplus::Event event;
Carol Wang4ca6f3f2020-02-19 16:28:59 +080025 sdbusplus::SdBusMock sdbusMock;
26 sdbusplus::bus::bus mockedBus = sdbusplus::get_mocked_new(&sdbusMock);
27 ScheduledHostTransition scheduledHostTransition;
28
Carol Wang6a5db3d2020-02-21 10:12:01 +080029 TestScheduledHostTransition() :
30 event(sdeventplus::Event::get_default()),
31 scheduledHostTransition(mockedBus, "", event)
Carol Wang4ca6f3f2020-02-19 16:28:59 +080032 {
33 // Empty
34 }
35
36 seconds getCurrentTime()
37 {
38 return scheduledHostTransition.getTime();
39 }
Carol Wang6a5db3d2020-02-21 10:12:01 +080040
41 bool isTimerEnabled()
42 {
43 return scheduledHostTransition.timer.isEnabled();
44 }
Carol Wang4ca6f3f2020-02-19 16:28:59 +080045};
46
47TEST_F(TestScheduledHostTransition, disableHostTransition)
48{
49 EXPECT_EQ(scheduledHostTransition.scheduledTime(0), 0);
Carol Wang6a5db3d2020-02-21 10:12:01 +080050 EXPECT_FALSE(isTimerEnabled());
Carol Wang4ca6f3f2020-02-19 16:28:59 +080051}
52
53TEST_F(TestScheduledHostTransition, invalidScheduledTime)
54{
55 // scheduled time is 1 min earlier than current time
56 uint64_t schTime =
57 static_cast<uint64_t>((getCurrentTime() - seconds(60)).count());
58 EXPECT_THROW(scheduledHostTransition.scheduledTime(schTime),
59 InvalidTimeError);
60}
61
Carol Wang6a5db3d2020-02-21 10:12:01 +080062TEST_F(TestScheduledHostTransition, validScheduledTime)
63{
64 // scheduled time is 1 min later than current time
65 uint64_t schTime =
66 static_cast<uint64_t>((getCurrentTime() + seconds(60)).count());
67 EXPECT_EQ(scheduledHostTransition.scheduledTime(schTime), schTime);
68 EXPECT_TRUE(isTimerEnabled());
69}
70
71TEST_F(TestScheduledHostTransition, hostTransitionStatus)
72{
73 // set requested transition to be on
74 scheduledHostTransition.scheduledTransition(Transition::On);
75 EXPECT_EQ(scheduledHostTransition.scheduledTransition(), Transition::On);
76 // set requested transition to be off
77 scheduledHostTransition.scheduledTransition(Transition::Off);
78 EXPECT_EQ(scheduledHostTransition.scheduledTransition(), Transition::Off);
79}
80
Carol Wang4ca6f3f2020-02-19 16:28:59 +080081} // namespace manager
82} // namespace state
83} // namespace phosphor