| #include "boot_manager.hpp" |
| #include "mockups.hpp" |
| #include "utils.hpp" |
| |
| #include <string> |
| #include <string_view> |
| #include <tuple> |
| |
| #include <gtest/gtest.h> |
| |
| namespace boot_time_monitor |
| { |
| namespace |
| { |
| |
| using ::testing::_; |
| using ::testing::Return; |
| |
| class BootManagerTest : public ::testing::Test |
| { |
| public: |
| // Wall time (second param) equals zero means BMC will get wall time by |
| // itself. |
| const std::vector<std::tuple<std::string_view, int64_t, int64_t>> inCP = { |
| {"reboot_start", 1000, 0}, |
| {"Shutdown1", 0 /*will be 2000*/, 1000}, |
| {"thisIs_2_shutdown", 0 /*will be 3000*/, 50}, |
| {"000S0", 0 /*will be 4000*/, 0}, |
| {"_PowerBack_", 0 /*will be 5000*/, 100}, |
| {"start1", 0 /*will be 6000*/, 500}, |
| {"start2", 0 /*will be 7000*/, 100}, |
| {"reboot_end", 8000, 0}, |
| }; |
| |
| const std::vector<std::tuple<std::string_view, int64_t>> inDur = { |
| {"nic1", 0}, |
| {"A_nic", 1000}, |
| {"_nextNIC", 4}, |
| {"99NNN", 99}, |
| }; |
| |
| BootManagerTest() : |
| util(std::make_shared<MockUtil>()), |
| cpFile(std::make_shared<MockFileUtil>()), |
| durFile(std::make_shared<MockFileUtil>()) |
| { |
| utilPtr = util.get(); |
| cpFilePtr = cpFile.get(); |
| durFilePtr = durFile.get(); |
| |
| EXPECT_CALL(*cpFile, loadCheckpoints(false)) |
| .WillOnce(Return(std::make_unique<std::vector<Checkpoint>>())); |
| EXPECT_CALL(*durFile, loadDurations(false)) |
| .WillOnce(Return(std::make_unique<std::vector<Duration>>())); |
| EXPECT_CALL(*cpFile, loadCheckpoints(true)) |
| .WillOnce(Return(std::make_unique<std::vector<Checkpoint>>())); |
| EXPECT_CALL(*durFile, loadDurations(true)) |
| .WillOnce(Return(std::make_unique<std::vector<Duration>>())); |
| |
| stateMachine = std::make_unique<BootManager>( |
| std::move(util), std::move(cpFile), std::move(durFile)); |
| } |
| |
| protected: |
| std::shared_ptr<MockUtil> util; |
| MockUtil* utilPtr; |
| std::shared_ptr<MockFileUtil> cpFile; |
| MockFileUtil* cpFilePtr; |
| std::shared_ptr<MockFileUtil> durFile; |
| MockFileUtil* durFilePtr; |
| |
| std::unique_ptr<BootManager> stateMachine; |
| }; |
| |
| TEST_F(BootManagerTest, AddCheckpointTest) |
| { |
| EXPECT_CALL(*utilPtr, getUpTimeInMs()).WillOnce(Return(10000)); |
| EXPECT_CALL(*utilPtr, isValidName(std::get<0>(inCP[0]))) |
| .WillOnce(Return(true)); |
| EXPECT_CALL(*cpFilePtr, addCheckpoint(std::get<0>(inCP[0]), _, _)); |
| stateMachine->setCheckpoint(std::get<0>(inCP[0]), std::get<1>(inCP[0]), |
| std::get<2>(inCP[0])); |
| |
| for (uint32_t i = 1; i <= 6; i++) |
| { |
| EXPECT_CALL(*utilPtr, getWallTimeInMs()) |
| .WillOnce(Return(1000 + 1000 * i)); |
| EXPECT_CALL(*utilPtr, getUpTimeInMs()) |
| .WillOnce(Return(10000 + 1000 * i)); |
| EXPECT_CALL(*utilPtr, isValidName(std::get<0>(inCP[i]))) |
| .WillOnce(Return(true)); |
| // Won't enter `addCheckpoint` twice if duration is 0. |
| if (std::get<2>(inCP[i]) != 0) |
| { |
| EXPECT_CALL( |
| *cpFilePtr, |
| addCheckpoint(std::get<0>(inCP[i]).data() + |
| std::string{BootManager::kBeginStageSuffix}, |
| _, _)); |
| } |
| EXPECT_CALL(*cpFilePtr, addCheckpoint(std::get<0>(inCP[i]), _, _)); |
| stateMachine->setCheckpoint(std::get<0>(inCP[i]), std::get<1>(inCP[i]), |
| std::get<2>(inCP[i])); |
| } |
| |
| EXPECT_CALL(*utilPtr, getUpTimeInMs()).WillOnce(Return(17000)); |
| EXPECT_CALL(*utilPtr, isValidName(std::get<0>(inCP[7]))) |
| .WillOnce(Return(true)); |
| EXPECT_CALL(*cpFilePtr, addCheckpoint(std::get<0>(inCP[7]), _, _)); |
| stateMachine->setCheckpoint(std::get<0>(inCP[7]), std::get<1>(inCP[7]), |
| std::get<2>(inCP[7])); |
| |
| auto cps = stateMachine->getCheckpoints(); |
| int32_t count = 0; |
| for (uint32_t i = 0; i < inCP.size(); i++) |
| { |
| auto name = std::get<0>(inCP[i]); |
| auto duration = std::get<2>(inCP[i]); |
| |
| if (i != 0 && duration != 0) |
| { |
| EXPECT_EQ(cps[count].name, |
| name.data() + |
| std::string{BootManager::kBeginStageSuffix}); |
| EXPECT_EQ(cps[count].wallTime, 1000 * (i + 1) - duration); |
| EXPECT_EQ(cps[count].monoTime, 10000 + 1000 * i - duration); |
| count++; |
| } |
| EXPECT_EQ(cps[count].name, name); |
| EXPECT_EQ(cps[count].wallTime, 1000 * (i + 1)); |
| EXPECT_EQ(cps[count].monoTime, 10000 + 1000 * i); |
| count++; |
| } |
| EXPECT_EQ(count, cps.size()); |
| } |
| |
| TEST_F(BootManagerTest, AddDurationTest) |
| { |
| for (auto dur : inDur) |
| { |
| EXPECT_CALL(*durFilePtr, addDuration(_, _)); |
| EXPECT_CALL(*utilPtr, isValidName(std::get<0>(dur))) |
| .WillOnce(Return(true)); |
| stateMachine->setDuration(std::get<0>(dur), std::get<1>(dur)); |
| } |
| |
| auto durs = stateMachine->getDurations(); |
| EXPECT_EQ(inDur.size(), durs.size()); |
| |
| for (uint32_t i = 0; i < inDur.size(); i++) |
| { |
| auto name = std::get<0>(inDur[i]); |
| auto duration = std::get<1>(inDur[i]); |
| |
| EXPECT_EQ(durs[i].name, name); |
| EXPECT_EQ(durs[i].duration, duration); |
| } |
| } |
| |
| TEST_F(BootManagerTest, CompleteTest) |
| { |
| EXPECT_CALL(*utilPtr, getUpTimeInMs()).WillOnce(Return(10000)); |
| EXPECT_CALL(*utilPtr, isValidName(std::get<0>(inCP[0]))) |
| .WillOnce(Return(true)); |
| EXPECT_CALL(*cpFilePtr, addCheckpoint(std::get<0>(inCP[0]), _, _)); |
| stateMachine->setCheckpoint(std::get<0>(inCP[0]), std::get<1>(inCP[0]), |
| std::get<2>(inCP[0])); |
| EXPECT_CALL(*durFilePtr, addDuration(std::get<0>(inDur[0]), _)); |
| EXPECT_CALL(*utilPtr, isValidName(std::get<0>(inDur[0]))) |
| .WillOnce(Return(true)); |
| stateMachine->setDuration(std::get<0>(inDur[0]), std::get<1>(inDur[0])); |
| |
| // Originally these 2 vectors should be empty. |
| EXPECT_TRUE(stateMachine->getPreCheckpoints().empty()); |
| EXPECT_TRUE(stateMachine->getPreDurations().empty()); |
| |
| EXPECT_CALL(*cpFilePtr, completeCurrent()); |
| EXPECT_CALL(*durFilePtr, completeCurrent()); |
| stateMachine->notifyComplete(); |
| |
| auto cps = stateMachine->getPreCheckpoints(); |
| auto durs = stateMachine->getPreDurations(); |
| EXPECT_EQ(cps.size(), 1); |
| EXPECT_EQ(durs.size(), 1); |
| EXPECT_TRUE(stateMachine->getCheckpoints().empty()); |
| EXPECT_TRUE(stateMachine->getDurations().empty()); |
| |
| EXPECT_EQ(cps[0].name, std::get<0>(inCP[0])); |
| EXPECT_EQ(cps[0].wallTime, std::get<1>(inCP[0])); |
| EXPECT_EQ(cps[0].monoTime, 10000); |
| |
| EXPECT_EQ(durs[0].name, std::get<0>(inDur[0])); |
| EXPECT_EQ(durs[0].duration, std::get<1>(inDur[0])); |
| } |
| |
| } // namespace |
| } // namespace boot_time_monitor |