Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 1 | #include "scheduled_host_transition.hpp" |
| 2 | |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 3 | #include <sdbusplus/bus.hpp> |
| 4 | #include <sdbusplus/test/sdbus_mock.hpp> |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 5 | #include <sdeventplus/event.hpp> |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 6 | #include <xyz/openbmc_project/ScheduledTime/error.hpp> |
| 7 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 8 | #include <gmock/gmock.h> |
| 9 | #include <gtest/gtest.h> |
| 10 | |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 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; |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 21 | using HostTransition = |
Patrick Williams | 7e969cb | 2023-08-23 16:24:23 -0500 | [diff] [blame] | 22 | sdbusplus::server::xyz::openbmc_project::state::ScheduledHostTransition; |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 23 | |
| 24 | class TestScheduledHostTransition : public testing::Test |
| 25 | { |
| 26 | public: |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 27 | sdeventplus::Event event; |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 28 | sdbusplus::SdBusMock sdbusMock; |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 29 | sdbusplus::bus_t mockedBus = sdbusplus::get_mocked_new(&sdbusMock); |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 30 | ScheduledHostTransition scheduledHostTransition; |
| 31 | |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 32 | TestScheduledHostTransition() : |
| 33 | event(sdeventplus::Event::get_default()), |
Patrick Williams | 211d972 | 2022-04-07 21:24:33 -0500 | [diff] [blame] | 34 | scheduledHostTransition(mockedBus, "", 0, event) |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 35 | { |
| 36 | // Empty |
| 37 | } |
| 38 | |
Pavithra Barithaya | a1c0e5c | 2024-06-21 12:39:38 -0500 | [diff] [blame^] | 39 | static seconds getCurrentTime() |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 40 | { |
Pavithra Barithaya | a1c0e5c | 2024-06-21 12:39:38 -0500 | [diff] [blame^] | 41 | return ScheduledHostTransition::getTime(); |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 42 | } |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 43 | |
| 44 | bool isTimerEnabled() |
| 45 | { |
| 46 | return scheduledHostTransition.timer.isEnabled(); |
| 47 | } |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 48 | |
| 49 | void bmcTimeChange() |
| 50 | { |
| 51 | scheduledHostTransition.handleTimeUpdates(); |
| 52 | } |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | TEST_F(TestScheduledHostTransition, disableHostTransition) |
| 56 | { |
| 57 | EXPECT_EQ(scheduledHostTransition.scheduledTime(0), 0); |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 58 | EXPECT_FALSE(isTimerEnabled()); |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | TEST_F(TestScheduledHostTransition, invalidScheduledTime) |
| 62 | { |
Pavithra Barithaya | a1c0e5c | 2024-06-21 12:39:38 -0500 | [diff] [blame^] | 63 | seconds currentTime = getCurrentTime(); |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 64 | // scheduled time is 1 min earlier than current time |
| 65 | uint64_t schTime = |
Pavithra Barithaya | a1c0e5c | 2024-06-21 12:39:38 -0500 | [diff] [blame^] | 66 | static_cast<uint64_t>((currentTime - seconds(60)).count()); |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 67 | EXPECT_THROW(scheduledHostTransition.scheduledTime(schTime), |
| 68 | InvalidTimeError); |
| 69 | } |
| 70 | |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 71 | TEST_F(TestScheduledHostTransition, validScheduledTime) |
| 72 | { |
Pavithra Barithaya | a1c0e5c | 2024-06-21 12:39:38 -0500 | [diff] [blame^] | 73 | seconds currentTime = getCurrentTime(); |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 74 | // scheduled time is 1 min later than current time |
| 75 | uint64_t schTime = |
Pavithra Barithaya | a1c0e5c | 2024-06-21 12:39:38 -0500 | [diff] [blame^] | 76 | static_cast<uint64_t>((currentTime + seconds(60)).count()); |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 77 | EXPECT_EQ(scheduledHostTransition.scheduledTime(schTime), schTime); |
| 78 | EXPECT_TRUE(isTimerEnabled()); |
| 79 | } |
| 80 | |
| 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 | |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 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 | |
| 102 | TEST_F(TestScheduledHostTransition, bmcTimeChangeBackward) |
| 103 | { |
Pavithra Barithaya | a1c0e5c | 2024-06-21 12:39:38 -0500 | [diff] [blame^] | 104 | seconds currentTime = getCurrentTime(); |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 105 | // Current time is earlier than scheduled time due to BMC time changing |
| 106 | uint64_t schTime = |
Pavithra Barithaya | a1c0e5c | 2024-06-21 12:39:38 -0500 | [diff] [blame^] | 107 | static_cast<uint64_t>((currentTime + seconds(60)).count()); |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 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 | |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 118 | } // namespace manager |
| 119 | } // namespace state |
| 120 | } // namespace phosphor |