Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 1 | #include "dbus_environment.hpp" |
| 2 | #include "helpers.hpp" |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 3 | #include "mocks/json_storage_mock.hpp" |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 4 | #include "mocks/trigger_factory_mock.hpp" |
| 5 | #include "mocks/trigger_mock.hpp" |
| 6 | #include "params/trigger_params.hpp" |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 7 | #include "trigger.hpp" |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 8 | #include "trigger_manager.hpp" |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 9 | #include "utils/conversion_trigger.hpp" |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 10 | #include "utils/dbus_path_utils.hpp" |
| 11 | #include "utils/string_utils.hpp" |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 12 | #include "utils/transform.hpp" |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 13 | |
| 14 | using namespace testing; |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 15 | using sdbusplus::message::object_path; |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 16 | using namespace std::literals::string_literals; |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 17 | |
| 18 | class TestTriggerManager : public Test |
| 19 | { |
| 20 | public: |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 21 | TriggerParams triggerParams; |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 22 | std::pair<boost::system::error_code, std::string> |
| 23 | addTrigger(const TriggerParams& params) |
| 24 | { |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 25 | const auto sensorInfos = |
| 26 | utils::fromLabeledSensorsInfo(params.sensors()); |
Ed Tanous | 2efa95d | 2024-10-19 11:36:53 -0700 | [diff] [blame] | 27 | const std::vector<numeric::LabeledThresholdParam> numThresh = |
| 28 | params.numericThresholdParams(); |
| 29 | const std::vector<discrete::LabeledThresholdParam> discThresh = |
| 30 | params.discreteThresholdParams(); |
| 31 | const std::vector<numeric::ThresholdParam> numThresh2 = std::get<0>( |
| 32 | utils::FromLabeledThresholdParamConversion()(numThresh)); |
| 33 | const auto discThresh2 = std::get<1>( |
| 34 | utils::FromLabeledThresholdParamConversion()(discThresh)); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 35 | |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 36 | std::promise<std::pair<boost::system::error_code, std::string>> |
| 37 | addTriggerPromise; |
| 38 | DbusEnvironment::getBus()->async_method_call( |
| 39 | [&addTriggerPromise](boost::system::error_code ec, |
| 40 | const std::string& path) { |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 41 | addTriggerPromise.set_value({ec, path}); |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 42 | }, |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 43 | DbusEnvironment::serviceName(), TriggerManager::triggerManagerPath, |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 44 | TriggerManager::triggerManagerIfaceName, "AddTrigger", params.id(), |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 45 | params.name(), |
| 46 | utils::transform( |
| 47 | params.triggerActions(), |
| 48 | [](const auto& action) { return actionToString(action); }), |
Ed Tanous | 2efa95d | 2024-10-19 11:36:53 -0700 | [diff] [blame] | 49 | sensorInfos, params.reports(), numThresh2, discThresh2); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 50 | return DbusEnvironment::waitForFuture(addTriggerPromise.get_future()); |
| 51 | } |
| 52 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 53 | std::unique_ptr<TriggerManager> makeTriggerManager() |
| 54 | { |
| 55 | return std::make_unique<TriggerManager>( |
| 56 | std::move(triggerFactoryMockPtr), std::move(storageMockPtr), |
| 57 | DbusEnvironment::getObjServer()); |
| 58 | } |
| 59 | |
| 60 | void SetUp() override |
| 61 | { |
| 62 | sut = makeTriggerManager(); |
| 63 | } |
| 64 | |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 65 | std::unique_ptr<StorageMock> storageMockPtr = |
| 66 | std::make_unique<NiceMock<StorageMock>>(); |
| 67 | StorageMock& storageMock = *storageMockPtr; |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 68 | std::unique_ptr<TriggerFactoryMock> triggerFactoryMockPtr = |
| 69 | std::make_unique<NiceMock<TriggerFactoryMock>>(); |
| 70 | TriggerFactoryMock& triggerFactoryMock = *triggerFactoryMockPtr; |
| 71 | std::unique_ptr<TriggerMock> triggerMockPtr = |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 72 | std::make_unique<NiceMock<TriggerMock>>(TriggerParams().id()); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 73 | TriggerMock& triggerMock = *triggerMockPtr; |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 74 | std::unique_ptr<TriggerManager> sut; |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 75 | MockFunction<void(std::string)> checkPoint; |
| 76 | }; |
| 77 | |
| 78 | TEST_F(TestTriggerManager, addTrigger) |
| 79 | { |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 80 | triggerFactoryMock.expectMake(TriggerParams(), Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 81 | .WillOnce(Return(ByMove(std::move(triggerMockPtr)))); |
| 82 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 83 | auto [ec, path] = addTrigger(TriggerParams()); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 84 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 85 | EXPECT_THAT(path, Eq(triggerMock.getPath())); |
| 86 | } |
| 87 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 88 | TEST_F(TestTriggerManager, addTriggerWithDiscreteThresholds) |
| 89 | { |
| 90 | TriggerParams triggerParamsDiscrete; |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 91 | auto thresholds = std::vector<discrete::LabeledThresholdParam>{ |
Szymon Dompke | 9f34679 | 2021-07-14 21:07:11 +0200 | [diff] [blame] | 92 | {"discrete_threshold1", discrete::Severity::ok, 10, "11.0"}, |
| 93 | {"discrete_threshold2", discrete::Severity::warning, 10, "12.0"}, |
| 94 | {"discrete_threshold3", discrete::Severity::critical, 10, "13.0"}}; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 95 | |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 96 | triggerParamsDiscrete.thresholdParams(thresholds); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 97 | |
| 98 | auto [ec, path] = addTrigger(triggerParamsDiscrete); |
| 99 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 100 | EXPECT_THAT(path, Eq(triggerMock.getPath())); |
| 101 | } |
| 102 | |
| 103 | TEST_F(TestTriggerManager, addDiscreteTriggerWithoutThresholds) |
| 104 | { |
| 105 | TriggerParams triggerParamsDiscrete; |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 106 | auto thresholds = std::vector<discrete::LabeledThresholdParam>(); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 107 | |
Szymon Dompke | 2001301 | 2021-07-23 09:54:20 +0200 | [diff] [blame] | 108 | triggerParamsDiscrete.thresholdParams(thresholds); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 109 | |
| 110 | auto [ec, path] = addTrigger(triggerParamsDiscrete); |
| 111 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 112 | EXPECT_THAT(path, Eq(triggerMock.getPath())); |
| 113 | } |
| 114 | |
Michal Orzel | b47b7db | 2024-09-16 10:02:29 +0200 | [diff] [blame] | 115 | TEST_F(TestTriggerManager, failToAddTriggerTwice) |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 116 | { |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 117 | triggerFactoryMock.expectMake(TriggerParams(), Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 118 | .WillOnce(Return(ByMove(std::move(triggerMockPtr)))); |
| 119 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 120 | addTrigger(TriggerParams()); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 121 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 122 | auto [ec, path] = addTrigger(TriggerParams()); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 123 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists)); |
| 124 | EXPECT_THAT(path, Eq(std::string())); |
| 125 | } |
| 126 | |
Michal Orzel | b47b7db | 2024-09-16 10:02:29 +0200 | [diff] [blame] | 127 | TEST_F(TestTriggerManager, failToAddTriggerWithInvalidId) |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 128 | { |
| 129 | triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
| 130 | .Times(0); |
| 131 | |
| 132 | auto [ec, path] = addTrigger(TriggerParams().id("not valid?")); |
| 133 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 134 | EXPECT_THAT(path, Eq(std::string())); |
| 135 | } |
| 136 | |
Michal Orzel | b47b7db | 2024-09-16 10:02:29 +0200 | [diff] [blame] | 137 | TEST_F(TestTriggerManager, failToAddTriggerWithDuplicatesInReportsIds) |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 138 | { |
| 139 | triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
| 140 | .Times(0); |
| 141 | |
| 142 | auto [ec, path] = addTrigger( |
| 143 | TriggerParams().reportIds({"trigger1", "trigger2", "trigger1"})); |
| 144 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 145 | EXPECT_THAT(path, Eq(std::string())); |
| 146 | } |
| 147 | |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 148 | TEST_F(TestTriggerManager, addTriggerWithProperReportPaths) |
| 149 | { |
| 150 | auto [ec, path] = addTrigger(TriggerParams().reports( |
| 151 | {object_path("/xyz/openbmc_project/Telemetry/Reports/MyReport"), |
| 152 | object_path( |
| 153 | "/xyz/openbmc_project/Telemetry/Reports/MyPrefix/MyReport")})); |
| 154 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 155 | EXPECT_THAT(path, Eq(triggerMock.getPath())); |
| 156 | } |
| 157 | |
Michal Orzel | b47b7db | 2024-09-16 10:02:29 +0200 | [diff] [blame] | 158 | TEST_F(TestTriggerManager, failToAddTriggerWithBadReportsPath) |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 159 | { |
| 160 | triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
| 161 | .Times(0); |
| 162 | |
| 163 | auto [ec, path] = addTrigger(TriggerParams().reports( |
| 164 | {object_path("/xyz/openbmc_project/Telemetry/NotReports/MyReport")})); |
| 165 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 166 | EXPECT_THAT(path, Eq(std::string())); |
| 167 | } |
| 168 | |
Michal Orzel | b47b7db | 2024-09-16 10:02:29 +0200 | [diff] [blame] | 169 | TEST_F(TestTriggerManager, failToAddTriggerWithTooManyReportPrefixes) |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 170 | { |
| 171 | triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
| 172 | .Times(0); |
| 173 | |
| 174 | auto [ec, path] = addTrigger(TriggerParams().reports({object_path( |
| 175 | "/xyz/openbmc_project/Telemetry/Reports/P1/P2/MyReport")})); |
| 176 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 177 | EXPECT_THAT(path, Eq(std::string())); |
| 178 | } |
| 179 | |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 180 | TEST_F(TestTriggerManager, addTriggerWithoutIdAndName) |
| 181 | { |
| 182 | triggerFactoryMock |
| 183 | .expectMake(TriggerParams() |
| 184 | .id(TriggerManager::triggerNameDefault) |
| 185 | .name(TriggerManager::triggerNameDefault), |
| 186 | Ref(*sut), Ref(storageMock)) |
| 187 | .WillOnce(Return(ByMove(std::move(triggerMockPtr)))); |
| 188 | |
| 189 | auto [ec, path] = addTrigger(TriggerParams().id("").name("")); |
| 190 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 191 | EXPECT_THAT(path, Not(Eq(""))); |
| 192 | } |
| 193 | |
| 194 | TEST_F(TestTriggerManager, addTriggerWithPrefixId) |
| 195 | { |
| 196 | triggerFactoryMock |
| 197 | .expectMake(TriggerParams() |
| 198 | .id("TelemetryService/HackyName") |
| 199 | .name("Hacky/Name!@#$"), |
| 200 | Ref(*sut), Ref(storageMock)) |
| 201 | .WillOnce(Return(ByMove(std::move(triggerMockPtr)))); |
| 202 | |
| 203 | auto [ec, path] = addTrigger( |
| 204 | TriggerParams().id("TelemetryService/").name("Hacky/Name!@#$")); |
| 205 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 206 | EXPECT_THAT(path, Not(Eq(""))); |
| 207 | } |
| 208 | |
| 209 | TEST_F(TestTriggerManager, addTriggerWithoutIdTwice) |
| 210 | { |
| 211 | addTrigger(TriggerParams().id("")); |
| 212 | |
| 213 | auto [ec, path] = addTrigger(TriggerParams().id("")); |
| 214 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 215 | EXPECT_THAT(path, Not(Eq(""))); |
| 216 | } |
| 217 | |
| 218 | TEST_F(TestTriggerManager, addTriggerWithoutIdAndWithLongNameTwice) |
| 219 | { |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 220 | std::string longName = utils::string_utils::getMaxName(); |
| 221 | addTrigger(TriggerParams().id("").name(longName)); |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 222 | |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 223 | auto [ec, path] = addTrigger(TriggerParams().id("").name(longName)); |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 224 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 225 | EXPECT_THAT(path, Not(Eq(""))); |
| 226 | } |
| 227 | |
| 228 | TEST_F(TestTriggerManager, addTriggerWithMaxLengthId) |
| 229 | { |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 230 | std::string reportId = utils::string_utils::getMaxId(); |
| 231 | triggerParams.id(reportId); |
| 232 | triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock)); |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 233 | |
| 234 | auto [ec, path] = addTrigger(triggerParams); |
| 235 | |
| 236 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 237 | EXPECT_THAT(path, Eq("/"s + reportId)); |
| 238 | } |
| 239 | |
| 240 | TEST_F(TestTriggerManager, addTriggerWithMaxLengthPrefix) |
| 241 | { |
| 242 | std::string reportId = utils::string_utils::getMaxPrefix() + "/MyId"; |
| 243 | triggerParams.id(reportId); |
| 244 | triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock)); |
| 245 | |
| 246 | auto [ec, path] = addTrigger(triggerParams); |
| 247 | |
| 248 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 249 | EXPECT_THAT(path, Eq("/"s + reportId)); |
| 250 | } |
| 251 | |
| 252 | TEST_F(TestTriggerManager, addTriggerWithMaxLengthName) |
| 253 | { |
| 254 | triggerParams.name(utils::string_utils::getMaxName()); |
| 255 | triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock)); |
| 256 | |
| 257 | auto [ec, path] = addTrigger(triggerParams); |
| 258 | |
| 259 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 260 | EXPECT_THAT(path, Eq("/"s + triggerParams.id())); |
| 261 | } |
| 262 | |
| 263 | TEST_F(TestTriggerManager, addTriggerWithMaxLengthDiscreteThresholdName) |
| 264 | { |
| 265 | namespace ts = utils::tstring; |
| 266 | |
| 267 | triggerParams = |
| 268 | TriggerParams() |
| 269 | .id("DiscreteTrigger") |
| 270 | .name("My Discrete Trigger") |
| 271 | .thresholdParams(std::vector<discrete::LabeledThresholdParam>{ |
| 272 | discrete::LabeledThresholdParam{ |
| 273 | utils::string_utils::getMaxName(), |
| 274 | discrete::Severity::warning, Milliseconds(10).count(), |
| 275 | "15.2"}}); |
| 276 | |
| 277 | triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock)); |
| 278 | |
| 279 | auto [ec, path] = addTrigger(triggerParams); |
| 280 | |
| 281 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 282 | EXPECT_THAT(path, Eq("/"s + triggerParams.id())); |
| 283 | } |
| 284 | |
Michal Orzel | b47b7db | 2024-09-16 10:02:29 +0200 | [diff] [blame] | 285 | TEST_F(TestTriggerManager, failToAddTriggerWithTooLongFullId) |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 286 | { |
| 287 | triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
| 288 | .Times(0); |
| 289 | |
| 290 | triggerParams.id( |
| 291 | std::string(utils::constants::maxReportFullIdLength + 1, 'z')); |
| 292 | |
| 293 | auto [ec, path] = addTrigger(triggerParams); |
| 294 | |
| 295 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 296 | EXPECT_THAT(path, Eq(std::string())); |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Michal Orzel | b47b7db | 2024-09-16 10:02:29 +0200 | [diff] [blame] | 299 | TEST_F(TestTriggerManager, failToAddTriggerWithTooLongId) |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 300 | { |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 301 | triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
| 302 | .Times(0); |
| 303 | |
| 304 | triggerParams.id(utils::string_utils::getTooLongId()); |
| 305 | |
| 306 | auto [ec, path] = addTrigger(triggerParams); |
| 307 | |
| 308 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 309 | EXPECT_THAT(path, Eq(std::string())); |
| 310 | } |
| 311 | |
Michal Orzel | b47b7db | 2024-09-16 10:02:29 +0200 | [diff] [blame] | 312 | TEST_F(TestTriggerManager, failToAddTriggerWithTooLongPrefix) |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 313 | { |
| 314 | triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
| 315 | .Times(0); |
| 316 | |
| 317 | triggerParams.id(utils::string_utils::getTooLongPrefix() + "/MyId"); |
| 318 | |
| 319 | auto [ec, path] = addTrigger(triggerParams); |
| 320 | |
| 321 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 322 | EXPECT_THAT(path, Eq(std::string())); |
| 323 | } |
| 324 | |
Michal Orzel | b47b7db | 2024-09-16 10:02:29 +0200 | [diff] [blame] | 325 | TEST_F(TestTriggerManager, failToAddTriggerWithTooManyPrefixes) |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 326 | { |
| 327 | triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
| 328 | .Times(0); |
| 329 | |
| 330 | std::string reportId; |
| 331 | for (size_t i = 0; i < utils::constants::maxPrefixesInId + 1; i++) |
| 332 | { |
| 333 | reportId += "prefix/"; |
| 334 | } |
| 335 | reportId += "MyId"; |
| 336 | |
| 337 | triggerParams.id(reportId); |
| 338 | |
| 339 | auto [ec, path] = addTrigger(triggerParams); |
| 340 | |
| 341 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 342 | EXPECT_THAT(path, Eq(std::string())); |
| 343 | } |
| 344 | |
Michal Orzel | b47b7db | 2024-09-16 10:02:29 +0200 | [diff] [blame] | 345 | TEST_F(TestTriggerManager, failToAddTriggerWithTooLongName) |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 346 | { |
| 347 | triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
| 348 | .Times(0); |
| 349 | |
| 350 | triggerParams.name(utils::string_utils::getTooLongName()); |
| 351 | |
| 352 | auto [ec, path] = addTrigger(triggerParams); |
| 353 | |
| 354 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 355 | EXPECT_THAT(path, Eq(std::string())); |
| 356 | } |
| 357 | |
Michal Orzel | b47b7db | 2024-09-16 10:02:29 +0200 | [diff] [blame] | 358 | TEST_F(TestTriggerManager, failToAddTriggerWithTooLongMetricId) |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 359 | { |
| 360 | namespace ts = utils::tstring; |
| 361 | |
| 362 | triggerParams = |
| 363 | TriggerParams() |
| 364 | .id("DiscreteTrigger") |
| 365 | .name("My Discrete Trigger") |
| 366 | .thresholdParams(std::vector<discrete::LabeledThresholdParam>{ |
| 367 | discrete::LabeledThresholdParam{ |
| 368 | utils::string_utils::getTooLongName(), |
| 369 | discrete::Severity::warning, Milliseconds(10).count(), |
| 370 | "15.2"}}); |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 371 | |
| 372 | triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
| 373 | .Times(0); |
| 374 | |
| 375 | auto [ec, path] = addTrigger(triggerParams); |
| 376 | |
| 377 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 378 | EXPECT_THAT(path, Eq(std::string())); |
| 379 | } |
| 380 | |
Michal Orzel | b47b7db | 2024-09-16 10:02:29 +0200 | [diff] [blame] | 381 | TEST_F(TestTriggerManager, failToAddTriggerWhenMaxTriggerIsReached) |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 382 | { |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 383 | auto triggerParams = TriggerParams(); |
| 384 | |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 385 | triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 386 | .Times(TriggerManager::maxTriggers); |
| 387 | |
| 388 | for (size_t i = 0; i < TriggerManager::maxTriggers; i++) |
| 389 | { |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 390 | triggerParams.id(TriggerParams().id() + std::to_string(i)); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 391 | |
| 392 | auto [ec, path] = addTrigger(triggerParams); |
| 393 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 394 | } |
| 395 | |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 396 | triggerParams.id(TriggerParams().id() + |
| 397 | std::to_string(TriggerManager::maxTriggers)); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 398 | auto [ec, path] = addTrigger(triggerParams); |
| 399 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open)); |
| 400 | EXPECT_THAT(path, Eq(std::string())); |
| 401 | } |
| 402 | |
| 403 | TEST_F(TestTriggerManager, removeTrigger) |
| 404 | { |
| 405 | { |
| 406 | InSequence seq; |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 407 | triggerFactoryMock |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 408 | .expectMake(TriggerParams(), Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 409 | .WillOnce(Return(ByMove(std::move(triggerMockPtr)))); |
| 410 | EXPECT_CALL(triggerMock, Die()); |
| 411 | EXPECT_CALL(checkPoint, Call("end")); |
| 412 | } |
| 413 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 414 | addTrigger(TriggerParams()); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 415 | sut->removeTrigger(&triggerMock); |
| 416 | checkPoint.Call("end"); |
| 417 | } |
| 418 | |
| 419 | TEST_F(TestTriggerManager, removingTriggerThatIsNotInContainerHasNoEffect) |
| 420 | { |
| 421 | { |
| 422 | InSequence seq; |
| 423 | EXPECT_CALL(checkPoint, Call("end")); |
| 424 | EXPECT_CALL(triggerMock, Die()); |
| 425 | } |
| 426 | |
| 427 | sut->removeTrigger(&triggerMock); |
| 428 | checkPoint.Call("end"); |
| 429 | } |
| 430 | |
| 431 | TEST_F(TestTriggerManager, removingSameTriggerTwiceHasNoSideEffect) |
| 432 | { |
| 433 | { |
| 434 | InSequence seq; |
Cezary Zwolak | a4e6761 | 2021-02-18 13:16:16 +0100 | [diff] [blame] | 435 | triggerFactoryMock |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 436 | .expectMake(TriggerParams(), Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 437 | .WillOnce(Return(ByMove(std::move(triggerMockPtr)))); |
| 438 | EXPECT_CALL(triggerMock, Die()); |
| 439 | EXPECT_CALL(checkPoint, Call("end")); |
| 440 | } |
| 441 | |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 442 | addTrigger(TriggerParams()); |
Wludzik, Jozef | 76833cb | 2020-12-21 14:42:41 +0100 | [diff] [blame] | 443 | sut->removeTrigger(&triggerMock); |
| 444 | sut->removeTrigger(&triggerMock); |
| 445 | checkPoint.Call("end"); |
| 446 | } |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 447 | class TestTriggerManagerStorage : public TestTriggerManager |
| 448 | { |
| 449 | public: |
| 450 | using FilePath = interfaces::JsonStorage::FilePath; |
| 451 | using DirectoryPath = interfaces::JsonStorage::DirectoryPath; |
| 452 | |
| 453 | void SetUp() override |
| 454 | { |
| 455 | ON_CALL(storageMock, list()) |
| 456 | .WillByDefault(Return(std::vector<FilePath>{ |
| 457 | {FilePath("trigger1")}, {FilePath("trigger2")}})); |
| 458 | |
| 459 | ON_CALL(storageMock, load(FilePath("trigger1"))) |
| 460 | .WillByDefault(InvokeWithoutArgs([this] { return data1; })); |
| 461 | |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 462 | data2["Id"] = "Trigger2"; |
| 463 | data2["Name"] = "Second Trigger"; |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 464 | ON_CALL(storageMock, load(FilePath("trigger2"))) |
| 465 | .WillByDefault(InvokeWithoutArgs([this] { return data2; })); |
| 466 | } |
| 467 | |
| 468 | nlohmann::json data1 = nlohmann::json{ |
| 469 | {"Version", Trigger::triggerVersion}, |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 470 | {"Id", TriggerParams().id()}, |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 471 | {"Name", TriggerParams().name()}, |
| 472 | {"ThresholdParamsDiscriminator", |
| 473 | TriggerParams().thresholdParams().index()}, |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 474 | {"TriggerActions", utils::transform(TriggerParams().triggerActions(), |
| 475 | [](const auto& action) { |
| 476 | return actionToString(action); |
| 477 | })}, |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 478 | {"ThresholdParams", utils::labeledThresholdParamsToJson( |
| 479 | TriggerParams().thresholdParams())}, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 480 | {"ReportIds", TriggerParams().reportIds()}, |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 481 | {"Sensors", TriggerParams().sensors()}}; |
| 482 | |
| 483 | nlohmann::json data2 = data1; |
| 484 | }; |
| 485 | |
| 486 | TEST_F(TestTriggerManagerStorage, triggerManagerCtorAddTriggerFromStorage) |
| 487 | { |
| 488 | triggerFactoryMock.expectMake(TriggerParams(), _, Ref(storageMock)); |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 489 | triggerFactoryMock.expectMake( |
| 490 | TriggerParams().id("Trigger2").name("Second Trigger"), _, |
| 491 | Ref(storageMock)); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 492 | EXPECT_CALL(storageMock, remove(_)).Times(0); |
| 493 | |
| 494 | sut = makeTriggerManager(); |
| 495 | } |
| 496 | |
| 497 | TEST_F(TestTriggerManagerStorage, |
| 498 | triggerManagerCtorRemoveDiscreteTriggerFromStorage) |
| 499 | { |
| 500 | LabeledTriggerThresholdParams thresholdParams = |
| 501 | std::vector<discrete::LabeledThresholdParam>{ |
Szymon Dompke | 9f34679 | 2021-07-14 21:07:11 +0200 | [diff] [blame] | 502 | {"userId1", discrete::Severity::warning, 15, "10.0"}, |
| 503 | {"userId2", discrete::Severity::critical, 5, "20.0"}}; |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 504 | |
| 505 | data1["ThresholdParamsDiscriminator"] = thresholdParams.index(); |
| 506 | |
| 507 | data1["ThresholdParams"] = |
| 508 | utils::labeledThresholdParamsToJson(thresholdParams); |
| 509 | |
| 510 | EXPECT_CALL(storageMock, remove(FilePath("trigger1"))).Times(0); |
| 511 | |
| 512 | sut = makeTriggerManager(); |
| 513 | } |
| 514 | |
| 515 | TEST_F(TestTriggerManagerStorage, |
| 516 | triggerManagerCtorRemoveDiscreteTriggerFromStorage2) |
| 517 | { |
| 518 | data1["IsDiscrete"] = true; |
| 519 | |
| 520 | EXPECT_CALL(storageMock, remove(FilePath("trigger1"))).Times(0); |
| 521 | |
| 522 | sut = makeTriggerManager(); |
| 523 | } |
| 524 | |
| 525 | TEST_F(TestTriggerManagerStorage, |
| 526 | triggerManagerCtorAddProperRemoveInvalidTriggerFromStorage) |
| 527 | { |
| 528 | data1["Version"] = Trigger::triggerVersion - 1; |
| 529 | |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 530 | triggerFactoryMock.expectMake( |
| 531 | TriggerParams().id("Trigger2").name("Second Trigger"), _, |
| 532 | Ref(storageMock)); |
Cezary Zwolak | 4416fce | 2021-03-17 03:21:06 +0100 | [diff] [blame] | 533 | EXPECT_CALL(storageMock, remove(FilePath("trigger1"))); |
| 534 | |
| 535 | sut = makeTriggerManager(); |
| 536 | } |