Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 1 | #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 Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 7 | #include <sdeventplus/event.hpp> |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 8 | #include <xyz/openbmc_project/ScheduledTime/error.hpp> |
| 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace state |
| 13 | { |
| 14 | namespace manager |
| 15 | { |
| 16 | |
| 17 | using namespace std::chrono; |
| 18 | using InvalidTimeError = |
| 19 | sdbusplus::xyz::openbmc_project::ScheduledTime::Error::InvalidTime; |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 20 | using HostTransition = |
| 21 | sdbusplus::xyz::openbmc_project::State::server::ScheduledHostTransition; |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 22 | |
| 23 | class TestScheduledHostTransition : public testing::Test |
| 24 | { |
| 25 | public: |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 26 | sdeventplus::Event event; |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 27 | sdbusplus::SdBusMock sdbusMock; |
| 28 | sdbusplus::bus::bus mockedBus = sdbusplus::get_mocked_new(&sdbusMock); |
| 29 | ScheduledHostTransition scheduledHostTransition; |
| 30 | |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 31 | TestScheduledHostTransition() : |
| 32 | event(sdeventplus::Event::get_default()), |
| 33 | scheduledHostTransition(mockedBus, "", event) |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 34 | { |
| 35 | // Empty |
| 36 | } |
| 37 | |
| 38 | seconds getCurrentTime() |
| 39 | { |
| 40 | return scheduledHostTransition.getTime(); |
| 41 | } |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 42 | |
| 43 | bool isTimerEnabled() |
| 44 | { |
| 45 | return scheduledHostTransition.timer.isEnabled(); |
| 46 | } |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 47 | |
| 48 | void bmcTimeChange() |
| 49 | { |
| 50 | scheduledHostTransition.handleTimeUpdates(); |
| 51 | } |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | TEST_F(TestScheduledHostTransition, disableHostTransition) |
| 55 | { |
| 56 | EXPECT_EQ(scheduledHostTransition.scheduledTime(0), 0); |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 57 | EXPECT_FALSE(isTimerEnabled()); |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TEST_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 Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 69 | TEST_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 | |
| 78 | TEST_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 Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 88 | TEST_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 | |
| 99 | TEST_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 Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 114 | } // namespace manager |
| 115 | } // namespace state |
| 116 | } // namespace phosphor |