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