1 #include "dbus_environment.hpp" 2 #include "helpers.hpp" 3 #include "mocks/json_storage_mock.hpp" 4 #include "mocks/trigger_factory_mock.hpp" 5 #include "mocks/trigger_mock.hpp" 6 #include "params/trigger_params.hpp" 7 #include "trigger_manager.hpp" 8 9 using namespace testing; 10 11 class TestTriggerManager : public Test 12 { 13 public: 14 std::pair<boost::system::error_code, std::string> 15 addTrigger(const TriggerParams& params) 16 { 17 std::promise<std::pair<boost::system::error_code, std::string>> 18 addTriggerPromise; 19 DbusEnvironment::getBus()->async_method_call( 20 [&addTriggerPromise](boost::system::error_code ec, 21 const std::string& path) { 22 addTriggerPromise.set_value({ec, path}); 23 }, 24 DbusEnvironment::serviceName(), TriggerManager::triggerManagerPath, 25 TriggerManager::triggerManagerIfaceName, "AddTrigger", 26 params.name(), params.isDiscrete(), params.logToJournal(), 27 params.logToRedfish(), params.updateReport(), params.sensors(), 28 params.reportNames(), params.thresholdParams()); 29 return DbusEnvironment::waitForFuture(addTriggerPromise.get_future()); 30 } 31 32 TriggerParams triggerParams; 33 std::unique_ptr<StorageMock> storageMockPtr = 34 std::make_unique<NiceMock<StorageMock>>(); 35 StorageMock& storageMock = *storageMockPtr; 36 std::unique_ptr<TriggerFactoryMock> triggerFactoryMockPtr = 37 std::make_unique<NiceMock<TriggerFactoryMock>>(); 38 TriggerFactoryMock& triggerFactoryMock = *triggerFactoryMockPtr; 39 std::unique_ptr<TriggerMock> triggerMockPtr = 40 std::make_unique<NiceMock<TriggerMock>>(triggerParams.name()); 41 TriggerMock& triggerMock = *triggerMockPtr; 42 std::unique_ptr<TriggerManager> sut = std::make_unique<TriggerManager>( 43 std::move(triggerFactoryMockPtr), std::move(storageMockPtr), 44 std::move(DbusEnvironment::getObjServer())); 45 MockFunction<void(std::string)> checkPoint; 46 }; 47 48 TEST_F(TestTriggerManager, addTrigger) 49 { 50 triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock)) 51 .WillOnce(Return(ByMove(std::move(triggerMockPtr)))); 52 53 auto [ec, path] = addTrigger(triggerParams); 54 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); 55 EXPECT_THAT(path, Eq(triggerMock.getPath())); 56 } 57 58 TEST_F(TestTriggerManager, addTriggerWithDiscreteThresholds) 59 { 60 TriggerParams triggerParamsDiscrete; 61 auto thresholds = std::vector<discrete::ThresholdParam>{ 62 {"discrete_threshold1", 63 discrete::severityToString(discrete::Severity::ok), 10, 11.0}, 64 {"discrete_threshold2", 65 discrete::severityToString(discrete::Severity::warning), 10, 12.0}, 66 {"discrete_threshold3", 67 discrete::severityToString(discrete::Severity::critical), 10, 13.0}}; 68 69 triggerParamsDiscrete.thresholdParams(thresholds).isDiscrete(true); 70 71 auto [ec, path] = addTrigger(triggerParamsDiscrete); 72 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); 73 EXPECT_THAT(path, Eq(triggerMock.getPath())); 74 } 75 76 TEST_F(TestTriggerManager, addDiscreteTriggerWithoutThresholds) 77 { 78 TriggerParams triggerParamsDiscrete; 79 auto thresholds = std::vector<discrete::ThresholdParam>(); 80 81 triggerParamsDiscrete.thresholdParams(thresholds).isDiscrete(true); 82 83 auto [ec, path] = addTrigger(triggerParamsDiscrete); 84 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); 85 EXPECT_THAT(path, Eq(triggerMock.getPath())); 86 } 87 88 TEST_F(TestTriggerManager, DISABLED_failToAddTriggerTwice) 89 { 90 triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock)) 91 .WillOnce(Return(ByMove(std::move(triggerMockPtr)))); 92 93 addTrigger(triggerParams); 94 95 auto [ec, path] = addTrigger(triggerParams); 96 EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists)); 97 EXPECT_THAT(path, Eq(std::string())); 98 } 99 100 TEST_F(TestTriggerManager, DISABLED_failToAddTriggerWhenMaxTriggerIsReached) 101 { 102 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) 103 .Times(TriggerManager::maxTriggers); 104 105 for (size_t i = 0; i < TriggerManager::maxTriggers; i++) 106 { 107 triggerParams.name(triggerParams.name() + std::to_string(i)); 108 109 auto [ec, path] = addTrigger(triggerParams); 110 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); 111 } 112 113 triggerParams.name(triggerParams.name() + 114 std::to_string(TriggerManager::maxTriggers)); 115 auto [ec, path] = addTrigger(triggerParams); 116 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open)); 117 EXPECT_THAT(path, Eq(std::string())); 118 } 119 120 TEST_F(TestTriggerManager, removeTrigger) 121 { 122 { 123 InSequence seq; 124 triggerFactoryMock 125 .expectMake(triggerParams, Ref(*sut), Ref(storageMock)) 126 .WillOnce(Return(ByMove(std::move(triggerMockPtr)))); 127 EXPECT_CALL(triggerMock, Die()); 128 EXPECT_CALL(checkPoint, Call("end")); 129 } 130 131 addTrigger(triggerParams); 132 sut->removeTrigger(&triggerMock); 133 checkPoint.Call("end"); 134 } 135 136 TEST_F(TestTriggerManager, removingTriggerThatIsNotInContainerHasNoEffect) 137 { 138 { 139 InSequence seq; 140 EXPECT_CALL(checkPoint, Call("end")); 141 EXPECT_CALL(triggerMock, Die()); 142 } 143 144 sut->removeTrigger(&triggerMock); 145 checkPoint.Call("end"); 146 } 147 148 TEST_F(TestTriggerManager, removingSameTriggerTwiceHasNoSideEffect) 149 { 150 { 151 InSequence seq; 152 triggerFactoryMock 153 .expectMake(triggerParams, Ref(*sut), Ref(storageMock)) 154 .WillOnce(Return(ByMove(std::move(triggerMockPtr)))); 155 EXPECT_CALL(triggerMock, Die()); 156 EXPECT_CALL(checkPoint, Call("end")); 157 } 158 159 addTrigger(triggerParams); 160 sut->removeTrigger(&triggerMock); 161 sut->removeTrigger(&triggerMock); 162 checkPoint.Call("end"); 163 } 164