blob: 6cccb4a72a87d8bf9589796c7436ac44d9dfd697 [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) {
Patrick Williams3a1c2972023-05-10 07:51:04 -0500143 methodPromise.set_value(ec);
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100144 },
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"),
Patrick Williams3a1c2972023-05-10 07:51:04 -0500158 Eq(utils::transform(triggerParams.triggerActions(),
159 [](const auto& action) {
160 return actionToString(action);
161 })));
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100162 EXPECT_THAT((getProperty<SensorsInfo>(sut->getPath(), "Sensors")),
163 Eq(utils::fromLabeledSensorsInfo(triggerParams.sensors())));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100164 EXPECT_THAT(
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200165 getProperty<std::vector<object_path>>(sut->getPath(), "Reports"),
166 Eq(triggerParams.reports()));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100167 EXPECT_THAT(
Szymon Dompke94f71c52021-12-10 07:16:33 +0100168 getProperty<bool>(sut->getPath(), "Discrete"),
169 Eq(isTriggerThresholdDiscrete(triggerParams.thresholdParams())));
170 EXPECT_THAT(
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100171 getProperty<TriggerThresholdParams>(sut->getPath(), "Thresholds"),
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100172 Eq(std::visit(utils::FromLabeledThresholdParamConversion(),
173 triggerParams.thresholdParams())));
174}
175
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100176TEST_F(TestTrigger, checkBasicGetters)
177{
178 EXPECT_THAT(sut->getId(), Eq(triggerParams.id()));
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200179 EXPECT_THAT(sut->getPath(),
180 Eq(utils::constants::triggerDirPath.str + triggerParams.id()));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100181}
182
Szymon Dompkee28aa532021-10-27 12:33:12 +0200183TEST_F(TestTrigger, setPropertyNameToCorrectValue)
184{
185 std::string name = "custom name 1234 %^#5";
186 EXPECT_THAT(setProperty(sut->getPath(), "Name", name),
187 Eq(boost::system::errc::success));
188 EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"), Eq(name));
189}
190
Szymon Dompke94f71c52021-12-10 07:16:33 +0100191TEST_F(TestTrigger, setPropertyReportNames)
192{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200193 std::vector<object_path> newNames = {
194 utils::constants::reportDirPath / "abc",
195 utils::constants::reportDirPath / "one",
196 utils::constants::reportDirPath / "prefix" / "two"};
197 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newNames),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100198 Eq(boost::system::errc::success));
199 EXPECT_THAT(
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200200 getProperty<std::vector<object_path>>(sut->getPath(), "Reports"),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100201 Eq(newNames));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100202}
203
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100204TEST_F(TestTrigger, sendsUpdateWhenReportNamesChanges)
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100205{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200206 std::vector<object_path> newPropertyVal = {
207 utils::constants::reportDirPath / "abc",
208 utils::constants::reportDirPath / "one",
209 utils::constants::reportDirPath / "two"};
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100210
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100211 EXPECT_CALL(triggerPresenceChanged,
212 Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200213 UnorderedElementsAre("abc", "one", "two"))));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100214
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200215 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100216 Eq(boost::system::errc::success));
217}
218
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100219TEST_F(TestTrigger, sendsUpdateWhenReportNamesChangesToSameValue)
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100220{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200221 const std::vector<object_path> newPropertyVal = triggerParams.reports();
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100222
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100223 EXPECT_CALL(
224 triggerPresenceChanged,
225 Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
226 UnorderedElementsAreArray(triggerParams.reportIds()))));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100227
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200228 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100229 Eq(boost::system::errc::success));
230}
231
232TEST_F(TestTrigger,
233 DISABLED_settingPropertyReportNamesThrowsExceptionWhenDuplicateReportIds)
234{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200235 std::vector<object_path> newPropertyVal{
236 utils::constants::reportDirPath / "report1",
237 utils::constants::reportDirPath / "report2",
238 utils::constants::reportDirPath / "report1"};
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100239
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100240 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100241
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200242 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
243 Eq(boost::system::errc::invalid_argument));
244}
245
246TEST_F(
247 TestTrigger,
248 DISABLED_settingPropertyReportNamesThrowsExceptionWhenReportWithTooManyPrefixes)
249{
250 std::vector<object_path> newPropertyVal{
251 object_path("/xyz/openbmc_project/Telemetry/Reports/P1/P2/MyReport")};
252
253 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
254
255 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
256 Eq(boost::system::errc::invalid_argument));
257}
258
Szymon Dompke32305f12022-07-05 15:37:21 +0200259TEST_F(
260 TestTrigger,
261 DISABLED_settingPropertyReportNamesThrowsExceptionWhenReportWithTooLongPrefix)
262{
263 std::vector<object_path> newPropertyVal{
264 object_path("/xyz/openbmc_project/Telemetry/Reports/" +
265 utils::string_utils::getTooLongPrefix() + "/MyReport")};
266
267 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
268
269 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
270 Eq(boost::system::errc::invalid_argument));
271}
272
273TEST_F(
274 TestTrigger,
275 DISABLED_settingPropertyReportNamesThrowsExceptionWhenReportWithTooLongId)
276{
277 std::vector<object_path> newPropertyVal{
278 object_path("/xyz/openbmc_project/Telemetry/Reports/Prefix/" +
279 utils::string_utils::getTooLongId())};
280
281 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
282
283 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
284 Eq(boost::system::errc::invalid_argument));
285}
286
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200287TEST_F(TestTrigger,
288 DISABLED_settingPropertyReportNamesThrowsExceptionWhenReportWithBadPath)
289{
290 std::vector<object_path> newPropertyVal{
291 object_path("/xyz/openbmc_project/Telemetry/NotReports/MyReport")};
292
293 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
294
295 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100296 Eq(boost::system::errc::invalid_argument));
Szymon Dompke94f71c52021-12-10 07:16:33 +0100297}
298
299TEST_F(TestTrigger, setPropertySensors)
300{
301 EXPECT_CALL(*triggerFactoryMockPtr, updateSensors(_, _));
302 for (const auto& threshold : thresholdMocks)
303 {
304 auto thresholdMockPtr =
305 std::dynamic_pointer_cast<NiceMock<ThresholdMock>>(threshold);
306 EXPECT_CALL(*thresholdMockPtr, updateSensors(_));
307 }
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200308 SensorsInfo newSensors(
309 {std::make_pair(object_path("/abc/def"), "metadata")});
Szymon Dompke94f71c52021-12-10 07:16:33 +0100310 EXPECT_THAT(setProperty(sut->getPath(), "Sensors", newSensors),
311 Eq(boost::system::errc::success));
312}
313
314TEST_F(TestTrigger, setPropertyThresholds)
315{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200316 EXPECT_CALL(*triggerFactoryMockPtr, updateThresholds(_, _, _, _, _, _));
Szymon Dompke94f71c52021-12-10 07:16:33 +0100317 TriggerThresholdParams newThresholds =
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000318 std::vector<discrete::ThresholdParam>({std::make_tuple(
319 "discrete threshold", utils::enumToString(discrete::Severity::ok),
320 10, "12.3")});
Szymon Dompke94f71c52021-12-10 07:16:33 +0100321 EXPECT_THAT(setProperty(sut->getPath(), "Thresholds", newThresholds),
322 Eq(boost::system::errc::success));
323}
324
Szymon Dompke32305f12022-07-05 15:37:21 +0200325TEST_F(TestTrigger, setThresholdParamsWithTooLongDiscreteName)
326{
327 const TriggerThresholdParams currentValue =
328 std::visit(utils::FromLabeledThresholdParamConversion(),
329 triggerParams.thresholdParams());
330
331 TriggerThresholdParams newThresholds =
332 std::vector<discrete::ThresholdParam>({std::make_tuple(
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000333 utils::string_utils::getTooLongName(),
334 utils::enumToString(discrete::Severity::ok), 10, "12.3")});
Szymon Dompke32305f12022-07-05 15:37:21 +0200335
336 changeProperty<TriggerThresholdParams>(
337 sut->getPath(), "Thresholds",
338 {.valueBefore = Eq(currentValue),
339 .newValue = newThresholds,
340 .ec = Eq(boost::system::errc::invalid_argument),
341 .valueAfter = Eq(currentValue)});
342}
343
344TEST_F(TestTrigger, setNameTooLong)
345{
346 std::string currentValue = TriggerParams().name();
347
348 changeProperty<std::string>(
349 sut->getPath(), "Name",
350 {.valueBefore = Eq(currentValue),
351 .newValue = utils::string_utils::getTooLongName(),
352 .ec = Eq(boost::system::errc::invalid_argument),
353 .valueAfter = Eq(currentValue)});
354}
355
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100356TEST_F(TestTrigger, checkIfNumericCoversionsAreGood)
357{
358 const auto& labeledParamsBase =
359 std::get<std::vector<numeric::LabeledThresholdParam>>(
360 triggerParams.thresholdParams());
361 const auto paramsToCheck =
362 std::visit(utils::FromLabeledThresholdParamConversion(),
363 triggerParams.thresholdParams());
364 const auto labeledParamsToCheck =
365 std::get<std::vector<numeric::LabeledThresholdParam>>(std::visit(
366 utils::ToLabeledThresholdParamConversion(), paramsToCheck));
367
368 for (const auto& [tocheck, base] :
369 boost::combine(labeledParamsToCheck, labeledParamsBase))
370 {
371 EXPECT_THAT(tocheck.at_label<utils::tstring::Type>(),
372 Eq(base.at_label<utils::tstring::Type>()));
373 EXPECT_THAT(tocheck.at_label<utils::tstring::Direction>(),
374 Eq(base.at_label<utils::tstring::Direction>()));
375 EXPECT_THAT(tocheck.at_label<utils::tstring::DwellTime>(),
376 Eq(base.at_label<utils::tstring::DwellTime>()));
377 EXPECT_THAT(tocheck.at_label<utils::tstring::ThresholdValue>(),
378 Eq(base.at_label<utils::tstring::ThresholdValue>()));
379 }
380}
381
382TEST_F(TestTrigger, checkIfDiscreteCoversionsAreGood)
383{
384 const auto& labeledParamsBase =
385 std::get<std::vector<discrete::LabeledThresholdParam>>(
386 triggerDiscreteParams.thresholdParams());
387 const auto paramsToCheck =
388 std::visit(utils::FromLabeledThresholdParamConversion(),
389 triggerDiscreteParams.thresholdParams());
390 const auto labeledParamsToCheck =
391 std::get<std::vector<discrete::LabeledThresholdParam>>(std::visit(
392 utils::ToLabeledThresholdParamConversion(), paramsToCheck));
393
394 for (const auto& [tocheck, base] :
395 boost::combine(labeledParamsToCheck, labeledParamsBase))
396 {
397 EXPECT_THAT(tocheck.at_label<utils::tstring::UserId>(),
398 Eq(base.at_label<utils::tstring::UserId>()));
399 EXPECT_THAT(tocheck.at_label<utils::tstring::Severity>(),
400 Eq(base.at_label<utils::tstring::Severity>()));
401 EXPECT_THAT(tocheck.at_label<utils::tstring::DwellTime>(),
402 Eq(base.at_label<utils::tstring::DwellTime>()));
403 EXPECT_THAT(tocheck.at_label<utils::tstring::ThresholdValue>(),
404 Eq(base.at_label<utils::tstring::ThresholdValue>()));
405 }
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100406}
407
408TEST_F(TestTrigger, deleteTrigger)
409{
Szymon Dompkee28aa532021-10-27 12:33:12 +0200410 EXPECT_CALL(storageMock, remove(to_file_path(sut->getId())));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100411 EXPECT_CALL(*triggerManagerMockPtr, removeTrigger(sut.get()));
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100412
413 auto ec = deleteTrigger(sut->getPath());
414 EXPECT_THAT(ec, Eq(boost::system::errc::success));
415}
416
417TEST_F(TestTrigger, sendUpdateWhenTriggerIsDeleted)
418{
419 EXPECT_CALL(triggerPresenceChanged,
420 Call(FieldsAre(messages::Presence::Removed, triggerParams.id(),
421 UnorderedElementsAre())));
422
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100423 auto ec = deleteTrigger(sut->getPath());
424 EXPECT_THAT(ec, Eq(boost::system::errc::success));
425}
426
427TEST_F(TestTrigger, deletingNonExistingTriggerReturnInvalidRequestDescriptor)
428{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200429 auto ec =
430 deleteTrigger(utils::constants::triggerDirPath.str + "NonExisting"s);
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100431 EXPECT_THAT(ec.value(), Eq(EBADR));
432}
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100433
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100434TEST_F(TestTrigger, settingPersistencyToFalseRemovesTriggerFromStorage)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100435{
Szymon Dompkee28aa532021-10-27 12:33:12 +0200436 EXPECT_CALL(storageMock, remove(to_file_path(sut->getId())));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100437
438 bool persistent = false;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100439 EXPECT_THAT(setProperty(sut->getPath(), "Persistent", persistent),
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100440 Eq(boost::system::errc::success));
441 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"),
442 Eq(persistent));
443}
444
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100445class TestTriggerInitialization : public TestTrigger
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100446{
447 public:
Patrick Williams3a1c2972023-05-10 07:51:04 -0500448 void SetUp() override {}
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100449
450 nlohmann::json storedConfiguration;
451};
452
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100453TEST_F(TestTriggerInitialization,
454 exceptionDuringTriggerStoreDisablesPersistency)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100455{
456 EXPECT_CALL(storageMock, store(_, _))
457 .WillOnce(Throw(std::runtime_error("Generic error!")));
458
459 sut = makeTrigger(triggerParams);
460
461 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"), Eq(false));
462}
463
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100464TEST_F(TestTriggerInitialization, creatingTriggerThrowsExceptionWhenIdIsInvalid)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100465{
466 EXPECT_CALL(storageMock, store(_, _)).Times(0);
467
Szymon Dompkee28aa532021-10-27 12:33:12 +0200468 EXPECT_THROW(makeTrigger(triggerParams.id("inv?lidId")),
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100469 sdbusplus::exception::SdBusError);
470}
471
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100472TEST_F(TestTriggerInitialization, creatingTriggerUpdatesTriggersIdsInReports)
473{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100474 EXPECT_CALL(
475 triggerPresenceChanged,
476 Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
477 UnorderedElementsAreArray(triggerParams.reportIds()))));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100478
479 sut = makeTrigger(triggerParams);
480}
481
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100482class TestTriggerStore : public TestTrigger
483{
484 public:
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100485 nlohmann::json storedConfiguration;
486 nlohmann::json storedDiscreteConfiguration;
487 std::unique_ptr<Trigger> sutDiscrete;
488
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100489 void SetUp() override
490 {
491 ON_CALL(storageMock, store(_, _))
492 .WillByDefault(SaveArg<1>(&storedConfiguration));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100493 sut = makeTrigger(triggerParams);
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100494
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100495 ON_CALL(storageMock, store(_, _))
496 .WillByDefault(SaveArg<1>(&storedDiscreteConfiguration));
497 sutDiscrete = makeTrigger(triggerDiscreteParams);
498 }
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100499};
500
501TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerVersion)
502{
503 ASSERT_THAT(storedConfiguration.at("Version"), Eq(expectedTriggerVersion));
504}
505
Szymon Dompkee28aa532021-10-27 12:33:12 +0200506TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerId)
507{
508 ASSERT_THAT(storedConfiguration.at("Id"), Eq(triggerParams.id()));
509}
510
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100511TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerName)
512{
513 ASSERT_THAT(storedConfiguration.at("Name"), Eq(triggerParams.name()));
514}
515
Szymon Dompke20013012021-07-23 09:54:20 +0200516TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerTriggerActions)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100517{
Szymon Dompke20013012021-07-23 09:54:20 +0200518 ASSERT_THAT(storedConfiguration.at("TriggerActions"),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100519 Eq(utils::transform(triggerParams.triggerActions(),
520 [](const auto& action) {
Patrick Williams3a1c2972023-05-10 07:51:04 -0500521 return actionToString(action);
522 })));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100523}
524
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100525TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerReportIds)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100526{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100527 ASSERT_THAT(storedConfiguration.at("ReportIds"),
528 Eq(triggerParams.reportIds()));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100529}
530
531TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerSensors)
532{
533 nlohmann::json expectedItem;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100534 expectedItem["service"] = "service1";
Szymon Dompke94f71c52021-12-10 07:16:33 +0100535 expectedItem["path"] = "/xyz/openbmc_project/sensors/temperature/BMC_Temp";
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100536 expectedItem["metadata"] = "metadata1";
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100537
538 ASSERT_THAT(storedConfiguration.at("Sensors"), ElementsAre(expectedItem));
539}
540
541TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerThresholdParams)
542{
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100543 nlohmann::json expectedItem0;
544 expectedItem0["type"] = 0;
545 expectedItem0["dwellTime"] = 10;
546 expectedItem0["direction"] = 1;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100547 expectedItem0["thresholdValue"] = 0.5;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100548
549 nlohmann::json expectedItem1;
550 expectedItem1["type"] = 3;
551 expectedItem1["dwellTime"] = 10;
552 expectedItem1["direction"] = 2;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100553 expectedItem1["thresholdValue"] = 90.2;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100554
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100555 ASSERT_THAT(storedConfiguration.at("ThresholdParamsDiscriminator"), Eq(0));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100556 ASSERT_THAT(storedConfiguration.at("ThresholdParams"),
557 ElementsAre(expectedItem0, expectedItem1));
558}
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100559
560TEST_F(TestTriggerStore,
561 settingPersistencyToTrueStoresDiscreteTriggerThresholdParams)
562{
563 nlohmann::json expectedItem0;
564 expectedItem0["userId"] = "userId";
565 expectedItem0["severity"] = discrete::Severity::warning;
566 expectedItem0["dwellTime"] = 10;
Szymon Dompke9f346792021-07-14 21:07:11 +0200567 expectedItem0["thresholdValue"] = "15.2";
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100568
569 nlohmann::json expectedItem1;
570 expectedItem1["userId"] = "userId_2";
571 expectedItem1["severity"] = discrete::Severity::critical;
572 expectedItem1["dwellTime"] = 5;
Szymon Dompke9f346792021-07-14 21:07:11 +0200573 expectedItem1["thresholdValue"] = "32.7";
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100574
575 ASSERT_THAT(storedDiscreteConfiguration.at("ThresholdParamsDiscriminator"),
576 Eq(1));
577 ASSERT_THAT(storedDiscreteConfiguration.at("ThresholdParams"),
578 ElementsAre(expectedItem0, expectedItem1));
579}