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; |
| 20 | |
| 21 | class TestScheduledHostTransition : public testing::Test |
| 22 | { |
| 23 | public: |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame^] | 24 | sdeventplus::Event event; |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 25 | sdbusplus::SdBusMock sdbusMock; |
| 26 | sdbusplus::bus::bus mockedBus = sdbusplus::get_mocked_new(&sdbusMock); |
| 27 | ScheduledHostTransition scheduledHostTransition; |
| 28 | |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame^] | 29 | TestScheduledHostTransition() : |
| 30 | event(sdeventplus::Event::get_default()), |
| 31 | scheduledHostTransition(mockedBus, "", event) |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 32 | { |
| 33 | // Empty |
| 34 | } |
| 35 | |
| 36 | seconds getCurrentTime() |
| 37 | { |
| 38 | return scheduledHostTransition.getTime(); |
| 39 | } |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame^] | 40 | |
| 41 | bool isTimerEnabled() |
| 42 | { |
| 43 | return scheduledHostTransition.timer.isEnabled(); |
| 44 | } |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | TEST_F(TestScheduledHostTransition, disableHostTransition) |
| 48 | { |
| 49 | EXPECT_EQ(scheduledHostTransition.scheduledTime(0), 0); |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame^] | 50 | EXPECT_FALSE(isTimerEnabled()); |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | TEST_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 Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame^] | 62 | TEST_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 | |
| 71 | TEST_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 Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 81 | } // namespace manager |
| 82 | } // namespace state |
| 83 | } // namespace phosphor |