blob: 9db26327ef2ddc48d5cb30cda43bdc16161c213a [file] [log] [blame]
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01001#include "dbus_environment.hpp"
2#include "helpers.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01003#include "messages/collect_trigger_id.hpp"
4#include "messages/trigger_presence_changed_ind.hpp"
Cezary Zwolaka4e67612021-02-18 13:16:16 +01005#include "mocks/json_storage_mock.hpp"
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +01006#include "mocks/report_manager_mock.hpp"
Szymon Dompke94f71c52021-12-10 07:16:33 +01007#include "mocks/sensor_mock.hpp"
8#include "mocks/threshold_mock.hpp"
9#include "mocks/trigger_factory_mock.hpp"
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010010#include "mocks/trigger_manager_mock.hpp"
11#include "params/trigger_params.hpp"
12#include "trigger.hpp"
Szymon Dompkee28aa532021-10-27 12:33:12 +020013#include "trigger_manager.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +010014#include "utils/conversion_trigger.hpp"
Szymon Dompke32305f12022-07-05 15:37:21 +020015#include "utils/dbus_path_utils.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010016#include "utils/messanger.hpp"
Szymon Dompke32305f12022-07-05 15:37:21 +020017#include "utils/string_utils.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +010018#include "utils/transform.hpp"
19#include "utils/tstring.hpp"
20
21#include <boost/range/combine.hpp>
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010022
23using namespace testing;
24using namespace std::literals::string_literals;
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020025using sdbusplus::message::object_path;
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010026
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020027static constexpr size_t expectedTriggerVersion = 2;
Cezary Zwolaka4e67612021-02-18 13:16:16 +010028
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010029class TestTrigger : public Test
30{
31 public:
32 TriggerParams triggerParams;
Cezary Zwolak4416fce2021-03-17 03:21:06 +010033 TriggerParams triggerDiscreteParams =
34 TriggerParams()
Szymon Dompkee28aa532021-10-27 12:33:12 +020035 .id("DiscreteTrigger")
36 .name("My Discrete Trigger")
Cezary Zwolak4416fce2021-03-17 03:21:06 +010037 .thresholdParams(std::vector<discrete::LabeledThresholdParam>{
38 discrete::LabeledThresholdParam{
39 "userId", discrete::Severity::warning,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000040 Milliseconds(10).count(), "15.2"},
Cezary Zwolak4416fce2021-03-17 03:21:06 +010041 discrete::LabeledThresholdParam{
42 "userId_2", discrete::Severity::critical,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000043 Milliseconds(5).count(), "32.7"},
Cezary Zwolak4416fce2021-03-17 03:21:06 +010044 });
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010045
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010046 std::unique_ptr<ReportManagerMock> reportManagerMockPtr =
47 std::make_unique<NiceMock<ReportManagerMock>>();
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010048 std::unique_ptr<TriggerManagerMock> triggerManagerMockPtr =
49 std::make_unique<NiceMock<TriggerManagerMock>>();
Szymon Dompke94f71c52021-12-10 07:16:33 +010050 std::unique_ptr<TriggerFactoryMock> triggerFactoryMockPtr =
51 std::make_unique<NiceMock<TriggerFactoryMock>>();
Cezary Zwolaka4e67612021-02-18 13:16:16 +010052 testing::NiceMock<StorageMock> storageMock;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010053 NiceMock<MockFunction<void(const messages::TriggerPresenceChangedInd)>>
54 triggerPresenceChanged;
Szymon Dompke94f71c52021-12-10 07:16:33 +010055 std::vector<std::shared_ptr<interfaces::Threshold>> thresholdMocks;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010056 utils::Messanger messanger;
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010057 std::unique_ptr<Trigger> sut;
58
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010059 TestTrigger() : messanger(DbusEnvironment::getIoc())
60 {
61 messanger.on_receive<messages::TriggerPresenceChangedInd>(
62 [this](const auto& msg) { triggerPresenceChanged.Call(msg); });
63 }
64
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010065 void SetUp() override
66 {
Cezary Zwolaka4e67612021-02-18 13:16:16 +010067 sut = makeTrigger(triggerParams);
68 }
69
Cezary Zwolak4416fce2021-03-17 03:21:06 +010070 static std::vector<LabeledSensorInfo>
71 convertToLabeledSensor(const SensorsInfo& sensorsInfo)
72 {
73 return utils::transform(sensorsInfo, [](const auto& sensorInfo) {
74 const auto& [sensorPath, sensorMetadata] = sensorInfo;
75 return LabeledSensorInfo("service1", sensorPath, sensorMetadata);
76 });
77 }
78
Cezary Zwolaka4e67612021-02-18 13:16:16 +010079 std::unique_ptr<Trigger> makeTrigger(const TriggerParams& params)
80 {
Szymon Dompke94f71c52021-12-10 07:16:33 +010081 thresholdMocks =
82 ThresholdMock::makeThresholds(params.thresholdParams());
83
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020084 auto id = std::make_unique<const std::string>(params.id());
85
Cezary Zwolaka4e67612021-02-18 13:16:16 +010086 return std::make_unique<Trigger>(
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010087 DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(),
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020088 std::move(id), params.name(), params.triggerActions(),
Szymon Dompke94f71c52021-12-10 07:16:33 +010089 std::make_shared<std::vector<std::string>>(
90 params.reportIds().begin(), params.reportIds().end()),
91 std::vector<std::shared_ptr<interfaces::Threshold>>(thresholdMocks),
92 *triggerManagerMockPtr, storageMock, *triggerFactoryMockPtr,
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010093 SensorMock::makeSensorMocks(params.sensors()));
Cezary Zwolaka4e67612021-02-18 13:16:16 +010094 }
95
96 static interfaces::JsonStorage::FilePath to_file_path(std::string name)
97 {
98 return interfaces::JsonStorage::FilePath(
99 std::to_string(std::hash<std::string>{}(name)));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100100 }
101
102 template <class T>
103 static T getProperty(const std::string& path, const std::string& property)
104 {
Szymon Dompkee28aa532021-10-27 12:33:12 +0200105 return DbusEnvironment::getProperty<T>(path, Trigger::triggerIfaceName,
106 property);
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100107 }
108
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100109 template <class T>
110 static boost::system::error_code setProperty(const std::string& path,
111 const std::string& property,
112 const T& newValue)
113 {
Szymon Dompkee28aa532021-10-27 12:33:12 +0200114 return DbusEnvironment::setProperty<T>(path, Trigger::triggerIfaceName,
115 property, newValue);
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100116 }
117
Szymon Dompke32305f12022-07-05 15:37:21 +0200118 template <class T>
119 struct ChangePropertyParams
120 {
121 Matcher<T> valueBefore = _;
122 T newValue;
123 Matcher<boost::system::error_code> ec =
124 Eq(boost::system::errc::success);
125 Matcher<T> valueAfter = Eq(newValue);
126 };
127
128 template <class T>
129 static void changeProperty(const std::string& path,
130 const std::string& property,
131 ChangePropertyParams<T> p)
132 {
133 ASSERT_THAT(getProperty<T>(path, property), p.valueBefore);
134 ASSERT_THAT(setProperty<T>(path, property, p.newValue), p.ec);
135 EXPECT_THAT(getProperty<T>(path, property), p.valueAfter);
136 }
137
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100138 boost::system::error_code deleteTrigger(const std::string& path)
139 {
140 std::promise<boost::system::error_code> methodPromise;
141 DbusEnvironment::getBus()->async_method_call(
142 [&methodPromise](boost::system::error_code ec) {
143 methodPromise.set_value(ec);
144 },
145 DbusEnvironment::serviceName(), path, Trigger::deleteIfaceName,
146 "Delete");
147 return DbusEnvironment::waitForFuture(methodPromise.get_future());
148 }
149};
150
151TEST_F(TestTrigger, checkIfPropertiesAreSet)
152{
Szymon Dompkee28aa532021-10-27 12:33:12 +0200153 EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"),
154 Eq(triggerParams.name()));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100155 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"), Eq(true));
Szymon Dompke20013012021-07-23 09:54:20 +0200156 EXPECT_THAT(
157 getProperty<std::vector<std::string>>(sut->getPath(), "TriggerActions"),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100158 Eq(utils::transform(
159 triggerParams.triggerActions(),
160 [](const auto& action) { return actionToString(action); })));
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100161 EXPECT_THAT((getProperty<SensorsInfo>(sut->getPath(), "Sensors")),
162 Eq(utils::fromLabeledSensorsInfo(triggerParams.sensors())));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100163 EXPECT_THAT(
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200164 getProperty<std::vector<object_path>>(sut->getPath(), "Reports"),
165 Eq(triggerParams.reports()));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100166 EXPECT_THAT(
Szymon Dompke94f71c52021-12-10 07:16:33 +0100167 getProperty<bool>(sut->getPath(), "Discrete"),
168 Eq(isTriggerThresholdDiscrete(triggerParams.thresholdParams())));
169 EXPECT_THAT(
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100170 getProperty<TriggerThresholdParams>(sut->getPath(), "Thresholds"),
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100171 Eq(std::visit(utils::FromLabeledThresholdParamConversion(),
172 triggerParams.thresholdParams())));
173}
174
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100175TEST_F(TestTrigger, checkBasicGetters)
176{
177 EXPECT_THAT(sut->getId(), Eq(triggerParams.id()));
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200178 EXPECT_THAT(sut->getPath(),
179 Eq(utils::constants::triggerDirPath.str + triggerParams.id()));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100180}
181
Szymon Dompkee28aa532021-10-27 12:33:12 +0200182TEST_F(TestTrigger, setPropertyNameToCorrectValue)
183{
184 std::string name = "custom name 1234 %^#5";
185 EXPECT_THAT(setProperty(sut->getPath(), "Name", name),
186 Eq(boost::system::errc::success));
187 EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"), Eq(name));
188}
189
Szymon Dompke94f71c52021-12-10 07:16:33 +0100190TEST_F(TestTrigger, setPropertyReportNames)
191{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200192 std::vector<object_path> newNames = {
193 utils::constants::reportDirPath / "abc",
194 utils::constants::reportDirPath / "one",
195 utils::constants::reportDirPath / "prefix" / "two"};
196 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newNames),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100197 Eq(boost::system::errc::success));
198 EXPECT_THAT(
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200199 getProperty<std::vector<object_path>>(sut->getPath(), "Reports"),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100200 Eq(newNames));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100201}
202
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100203TEST_F(TestTrigger, sendsUpdateWhenReportNamesChanges)
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100204{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200205 std::vector<object_path> newPropertyVal = {
206 utils::constants::reportDirPath / "abc",
207 utils::constants::reportDirPath / "one",
208 utils::constants::reportDirPath / "two"};
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100209
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100210 EXPECT_CALL(triggerPresenceChanged,
211 Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200212 UnorderedElementsAre("abc", "one", "two"))));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100213
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200214 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100215 Eq(boost::system::errc::success));
216}
217
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100218TEST_F(TestTrigger, sendsUpdateWhenReportNamesChangesToSameValue)
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100219{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200220 const std::vector<object_path> newPropertyVal = triggerParams.reports();
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100221
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100222 EXPECT_CALL(
223 triggerPresenceChanged,
224 Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
225 UnorderedElementsAreArray(triggerParams.reportIds()))));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100226
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200227 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100228 Eq(boost::system::errc::success));
229}
230
231TEST_F(TestTrigger,
232 DISABLED_settingPropertyReportNamesThrowsExceptionWhenDuplicateReportIds)
233{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200234 std::vector<object_path> newPropertyVal{
235 utils::constants::reportDirPath / "report1",
236 utils::constants::reportDirPath / "report2",
237 utils::constants::reportDirPath / "report1"};
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100238
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100239 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100240
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200241 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
242 Eq(boost::system::errc::invalid_argument));
243}
244
245TEST_F(
246 TestTrigger,
247 DISABLED_settingPropertyReportNamesThrowsExceptionWhenReportWithTooManyPrefixes)
248{
249 std::vector<object_path> newPropertyVal{
250 object_path("/xyz/openbmc_project/Telemetry/Reports/P1/P2/MyReport")};
251
252 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
253
254 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
255 Eq(boost::system::errc::invalid_argument));
256}
257
Szymon Dompke32305f12022-07-05 15:37:21 +0200258TEST_F(
259 TestTrigger,
260 DISABLED_settingPropertyReportNamesThrowsExceptionWhenReportWithTooLongPrefix)
261{
262 std::vector<object_path> newPropertyVal{
263 object_path("/xyz/openbmc_project/Telemetry/Reports/" +
264 utils::string_utils::getTooLongPrefix() + "/MyReport")};
265
266 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
267
268 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
269 Eq(boost::system::errc::invalid_argument));
270}
271
272TEST_F(
273 TestTrigger,
274 DISABLED_settingPropertyReportNamesThrowsExceptionWhenReportWithTooLongId)
275{
276 std::vector<object_path> newPropertyVal{
277 object_path("/xyz/openbmc_project/Telemetry/Reports/Prefix/" +
278 utils::string_utils::getTooLongId())};
279
280 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
281
282 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
283 Eq(boost::system::errc::invalid_argument));
284}
285
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200286TEST_F(TestTrigger,
287 DISABLED_settingPropertyReportNamesThrowsExceptionWhenReportWithBadPath)
288{
289 std::vector<object_path> newPropertyVal{
290 object_path("/xyz/openbmc_project/Telemetry/NotReports/MyReport")};
291
292 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
293
294 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100295 Eq(boost::system::errc::invalid_argument));
Szymon Dompke94f71c52021-12-10 07:16:33 +0100296}
297
298TEST_F(TestTrigger, setPropertySensors)
299{
300 EXPECT_CALL(*triggerFactoryMockPtr, updateSensors(_, _));
301 for (const auto& threshold : thresholdMocks)
302 {
303 auto thresholdMockPtr =
304 std::dynamic_pointer_cast<NiceMock<ThresholdMock>>(threshold);
305 EXPECT_CALL(*thresholdMockPtr, updateSensors(_));
306 }
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200307 SensorsInfo newSensors(
308 {std::make_pair(object_path("/abc/def"), "metadata")});
Szymon Dompke94f71c52021-12-10 07:16:33 +0100309 EXPECT_THAT(setProperty(sut->getPath(), "Sensors", newSensors),
310 Eq(boost::system::errc::success));
311}
312
313TEST_F(TestTrigger, setPropertyThresholds)
314{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200315 EXPECT_CALL(*triggerFactoryMockPtr, updateThresholds(_, _, _, _, _, _));
Szymon Dompke94f71c52021-12-10 07:16:33 +0100316 TriggerThresholdParams newThresholds =
317 std::vector<discrete::ThresholdParam>(
318 {std::make_tuple("discrete threshold", "OK", 10, "12.3")});
319 EXPECT_THAT(setProperty(sut->getPath(), "Thresholds", newThresholds),
320 Eq(boost::system::errc::success));
321}
322
Szymon Dompke32305f12022-07-05 15:37:21 +0200323TEST_F(TestTrigger, setThresholdParamsWithTooLongDiscreteName)
324{
325 const TriggerThresholdParams currentValue =
326 std::visit(utils::FromLabeledThresholdParamConversion(),
327 triggerParams.thresholdParams());
328
329 TriggerThresholdParams newThresholds =
330 std::vector<discrete::ThresholdParam>({std::make_tuple(
331 utils::string_utils::getTooLongName(), "OK", 10, "12.3")});
332
333 changeProperty<TriggerThresholdParams>(
334 sut->getPath(), "Thresholds",
335 {.valueBefore = Eq(currentValue),
336 .newValue = newThresholds,
337 .ec = Eq(boost::system::errc::invalid_argument),
338 .valueAfter = Eq(currentValue)});
339}
340
341TEST_F(TestTrigger, setNameTooLong)
342{
343 std::string currentValue = TriggerParams().name();
344
345 changeProperty<std::string>(
346 sut->getPath(), "Name",
347 {.valueBefore = Eq(currentValue),
348 .newValue = utils::string_utils::getTooLongName(),
349 .ec = Eq(boost::system::errc::invalid_argument),
350 .valueAfter = Eq(currentValue)});
351}
352
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100353TEST_F(TestTrigger, checkIfNumericCoversionsAreGood)
354{
355 const auto& labeledParamsBase =
356 std::get<std::vector<numeric::LabeledThresholdParam>>(
357 triggerParams.thresholdParams());
358 const auto paramsToCheck =
359 std::visit(utils::FromLabeledThresholdParamConversion(),
360 triggerParams.thresholdParams());
361 const auto labeledParamsToCheck =
362 std::get<std::vector<numeric::LabeledThresholdParam>>(std::visit(
363 utils::ToLabeledThresholdParamConversion(), paramsToCheck));
364
365 for (const auto& [tocheck, base] :
366 boost::combine(labeledParamsToCheck, labeledParamsBase))
367 {
368 EXPECT_THAT(tocheck.at_label<utils::tstring::Type>(),
369 Eq(base.at_label<utils::tstring::Type>()));
370 EXPECT_THAT(tocheck.at_label<utils::tstring::Direction>(),
371 Eq(base.at_label<utils::tstring::Direction>()));
372 EXPECT_THAT(tocheck.at_label<utils::tstring::DwellTime>(),
373 Eq(base.at_label<utils::tstring::DwellTime>()));
374 EXPECT_THAT(tocheck.at_label<utils::tstring::ThresholdValue>(),
375 Eq(base.at_label<utils::tstring::ThresholdValue>()));
376 }
377}
378
379TEST_F(TestTrigger, checkIfDiscreteCoversionsAreGood)
380{
381 const auto& labeledParamsBase =
382 std::get<std::vector<discrete::LabeledThresholdParam>>(
383 triggerDiscreteParams.thresholdParams());
384 const auto paramsToCheck =
385 std::visit(utils::FromLabeledThresholdParamConversion(),
386 triggerDiscreteParams.thresholdParams());
387 const auto labeledParamsToCheck =
388 std::get<std::vector<discrete::LabeledThresholdParam>>(std::visit(
389 utils::ToLabeledThresholdParamConversion(), paramsToCheck));
390
391 for (const auto& [tocheck, base] :
392 boost::combine(labeledParamsToCheck, labeledParamsBase))
393 {
394 EXPECT_THAT(tocheck.at_label<utils::tstring::UserId>(),
395 Eq(base.at_label<utils::tstring::UserId>()));
396 EXPECT_THAT(tocheck.at_label<utils::tstring::Severity>(),
397 Eq(base.at_label<utils::tstring::Severity>()));
398 EXPECT_THAT(tocheck.at_label<utils::tstring::DwellTime>(),
399 Eq(base.at_label<utils::tstring::DwellTime>()));
400 EXPECT_THAT(tocheck.at_label<utils::tstring::ThresholdValue>(),
401 Eq(base.at_label<utils::tstring::ThresholdValue>()));
402 }
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100403}
404
405TEST_F(TestTrigger, deleteTrigger)
406{
Szymon Dompkee28aa532021-10-27 12:33:12 +0200407 EXPECT_CALL(storageMock, remove(to_file_path(sut->getId())));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100408 EXPECT_CALL(*triggerManagerMockPtr, removeTrigger(sut.get()));
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100409
410 auto ec = deleteTrigger(sut->getPath());
411 EXPECT_THAT(ec, Eq(boost::system::errc::success));
412}
413
414TEST_F(TestTrigger, sendUpdateWhenTriggerIsDeleted)
415{
416 EXPECT_CALL(triggerPresenceChanged,
417 Call(FieldsAre(messages::Presence::Removed, triggerParams.id(),
418 UnorderedElementsAre())));
419
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100420 auto ec = deleteTrigger(sut->getPath());
421 EXPECT_THAT(ec, Eq(boost::system::errc::success));
422}
423
424TEST_F(TestTrigger, deletingNonExistingTriggerReturnInvalidRequestDescriptor)
425{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200426 auto ec =
427 deleteTrigger(utils::constants::triggerDirPath.str + "NonExisting"s);
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100428 EXPECT_THAT(ec.value(), Eq(EBADR));
429}
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100430
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100431TEST_F(TestTrigger, settingPersistencyToFalseRemovesTriggerFromStorage)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100432{
Szymon Dompkee28aa532021-10-27 12:33:12 +0200433 EXPECT_CALL(storageMock, remove(to_file_path(sut->getId())));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100434
435 bool persistent = false;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100436 EXPECT_THAT(setProperty(sut->getPath(), "Persistent", persistent),
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100437 Eq(boost::system::errc::success));
438 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"),
439 Eq(persistent));
440}
441
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100442class TestTriggerInitialization : public TestTrigger
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100443{
444 public:
445 void SetUp() override
446 {}
447
448 nlohmann::json storedConfiguration;
449};
450
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100451TEST_F(TestTriggerInitialization,
452 exceptionDuringTriggerStoreDisablesPersistency)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100453{
454 EXPECT_CALL(storageMock, store(_, _))
455 .WillOnce(Throw(std::runtime_error("Generic error!")));
456
457 sut = makeTrigger(triggerParams);
458
459 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"), Eq(false));
460}
461
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100462TEST_F(TestTriggerInitialization, creatingTriggerThrowsExceptionWhenIdIsInvalid)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100463{
464 EXPECT_CALL(storageMock, store(_, _)).Times(0);
465
Szymon Dompkee28aa532021-10-27 12:33:12 +0200466 EXPECT_THROW(makeTrigger(triggerParams.id("inv?lidId")),
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100467 sdbusplus::exception::SdBusError);
468}
469
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100470TEST_F(TestTriggerInitialization, creatingTriggerUpdatesTriggersIdsInReports)
471{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100472 EXPECT_CALL(
473 triggerPresenceChanged,
474 Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
475 UnorderedElementsAreArray(triggerParams.reportIds()))));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100476
477 sut = makeTrigger(triggerParams);
478}
479
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100480class TestTriggerStore : public TestTrigger
481{
482 public:
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100483 nlohmann::json storedConfiguration;
484 nlohmann::json storedDiscreteConfiguration;
485 std::unique_ptr<Trigger> sutDiscrete;
486
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100487 void SetUp() override
488 {
489 ON_CALL(storageMock, store(_, _))
490 .WillByDefault(SaveArg<1>(&storedConfiguration));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100491 sut = makeTrigger(triggerParams);
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100492
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100493 ON_CALL(storageMock, store(_, _))
494 .WillByDefault(SaveArg<1>(&storedDiscreteConfiguration));
495 sutDiscrete = makeTrigger(triggerDiscreteParams);
496 }
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100497};
498
499TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerVersion)
500{
501 ASSERT_THAT(storedConfiguration.at("Version"), Eq(expectedTriggerVersion));
502}
503
Szymon Dompkee28aa532021-10-27 12:33:12 +0200504TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerId)
505{
506 ASSERT_THAT(storedConfiguration.at("Id"), Eq(triggerParams.id()));
507}
508
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100509TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerName)
510{
511 ASSERT_THAT(storedConfiguration.at("Name"), Eq(triggerParams.name()));
512}
513
Szymon Dompke20013012021-07-23 09:54:20 +0200514TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerTriggerActions)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100515{
Szymon Dompke20013012021-07-23 09:54:20 +0200516 ASSERT_THAT(storedConfiguration.at("TriggerActions"),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100517 Eq(utils::transform(triggerParams.triggerActions(),
518 [](const auto& action) {
519 return actionToString(action);
520 })));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100521}
522
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100523TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerReportIds)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100524{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100525 ASSERT_THAT(storedConfiguration.at("ReportIds"),
526 Eq(triggerParams.reportIds()));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100527}
528
529TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerSensors)
530{
531 nlohmann::json expectedItem;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100532 expectedItem["service"] = "service1";
Szymon Dompke94f71c52021-12-10 07:16:33 +0100533 expectedItem["path"] = "/xyz/openbmc_project/sensors/temperature/BMC_Temp";
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100534 expectedItem["metadata"] = "metadata1";
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100535
536 ASSERT_THAT(storedConfiguration.at("Sensors"), ElementsAre(expectedItem));
537}
538
539TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerThresholdParams)
540{
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100541 nlohmann::json expectedItem0;
542 expectedItem0["type"] = 0;
543 expectedItem0["dwellTime"] = 10;
544 expectedItem0["direction"] = 1;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100545 expectedItem0["thresholdValue"] = 0.5;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100546
547 nlohmann::json expectedItem1;
548 expectedItem1["type"] = 3;
549 expectedItem1["dwellTime"] = 10;
550 expectedItem1["direction"] = 2;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100551 expectedItem1["thresholdValue"] = 90.2;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100552
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100553 ASSERT_THAT(storedConfiguration.at("ThresholdParamsDiscriminator"), Eq(0));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100554 ASSERT_THAT(storedConfiguration.at("ThresholdParams"),
555 ElementsAre(expectedItem0, expectedItem1));
556}
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100557
558TEST_F(TestTriggerStore,
559 settingPersistencyToTrueStoresDiscreteTriggerThresholdParams)
560{
561 nlohmann::json expectedItem0;
562 expectedItem0["userId"] = "userId";
563 expectedItem0["severity"] = discrete::Severity::warning;
564 expectedItem0["dwellTime"] = 10;
Szymon Dompke9f346792021-07-14 21:07:11 +0200565 expectedItem0["thresholdValue"] = "15.2";
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100566
567 nlohmann::json expectedItem1;
568 expectedItem1["userId"] = "userId_2";
569 expectedItem1["severity"] = discrete::Severity::critical;
570 expectedItem1["dwellTime"] = 5;
Szymon Dompke9f346792021-07-14 21:07:11 +0200571 expectedItem1["thresholdValue"] = "32.7";
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100572
573 ASSERT_THAT(storedDiscreteConfiguration.at("ThresholdParamsDiscriminator"),
574 Eq(1));
575 ASSERT_THAT(storedDiscreteConfiguration.at("ThresholdParams"),
576 ElementsAre(expectedItem0, expectedItem1));
577}