blob: 9c68d941c1cb5f0fc847b75c092eeda6ff1d5ad9 [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);
Patrick Williamsf535cad2024-08-16 15:21:20 -0400144 }, DbusEnvironment::serviceName(), path, Trigger::deleteIfaceName,
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100145 "Delete");
146 return DbusEnvironment::waitForFuture(methodPromise.get_future());
147 }
148};
149
150TEST_F(TestTrigger, checkIfPropertiesAreSet)
151{
Szymon Dompkee28aa532021-10-27 12:33:12 +0200152 EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"),
153 Eq(triggerParams.name()));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100154 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"), Eq(true));
Szymon Dompke20013012021-07-23 09:54:20 +0200155 EXPECT_THAT(
156 getProperty<std::vector<std::string>>(sut->getPath(), "TriggerActions"),
Patrick Williamsc7935fa2023-10-20 11:19:30 -0500157 Eq(utils::transform(
158 triggerParams.triggerActions(),
159 [](const auto& action) { return actionToString(action); })));
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100160 EXPECT_THAT((getProperty<SensorsInfo>(sut->getPath(), "Sensors")),
161 Eq(utils::fromLabeledSensorsInfo(triggerParams.sensors())));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100162 EXPECT_THAT(
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200163 getProperty<std::vector<object_path>>(sut->getPath(), "Reports"),
164 Eq(triggerParams.reports()));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100165 EXPECT_THAT(
Szymon Dompke94f71c52021-12-10 07:16:33 +0100166 getProperty<bool>(sut->getPath(), "Discrete"),
167 Eq(isTriggerThresholdDiscrete(triggerParams.thresholdParams())));
168 EXPECT_THAT(
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100169 getProperty<TriggerThresholdParams>(sut->getPath(), "Thresholds"),
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100170 Eq(std::visit(utils::FromLabeledThresholdParamConversion(),
171 triggerParams.thresholdParams())));
172}
173
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100174TEST_F(TestTrigger, checkBasicGetters)
175{
176 EXPECT_THAT(sut->getId(), Eq(triggerParams.id()));
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200177 EXPECT_THAT(sut->getPath(),
178 Eq(utils::constants::triggerDirPath.str + triggerParams.id()));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100179}
180
Szymon Dompkee28aa532021-10-27 12:33:12 +0200181TEST_F(TestTrigger, setPropertyNameToCorrectValue)
182{
183 std::string name = "custom name 1234 %^#5";
184 EXPECT_THAT(setProperty(sut->getPath(), "Name", name),
185 Eq(boost::system::errc::success));
186 EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"), Eq(name));
187}
188
Szymon Dompke94f71c52021-12-10 07:16:33 +0100189TEST_F(TestTrigger, setPropertyReportNames)
190{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200191 std::vector<object_path> newNames = {
192 utils::constants::reportDirPath / "abc",
193 utils::constants::reportDirPath / "one",
194 utils::constants::reportDirPath / "prefix" / "two"};
195 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newNames),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100196 Eq(boost::system::errc::success));
197 EXPECT_THAT(
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200198 getProperty<std::vector<object_path>>(sut->getPath(), "Reports"),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100199 Eq(newNames));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100200}
201
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100202TEST_F(TestTrigger, sendsUpdateWhenReportNamesChanges)
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100203{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200204 std::vector<object_path> newPropertyVal = {
205 utils::constants::reportDirPath / "abc",
206 utils::constants::reportDirPath / "one",
207 utils::constants::reportDirPath / "two"};
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100208
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100209 EXPECT_CALL(triggerPresenceChanged,
210 Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200211 UnorderedElementsAre("abc", "one", "two"))));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100212
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200213 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100214 Eq(boost::system::errc::success));
215}
216
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100217TEST_F(TestTrigger, sendsUpdateWhenReportNamesChangesToSameValue)
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100218{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200219 const std::vector<object_path> newPropertyVal = triggerParams.reports();
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100220
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100221 EXPECT_CALL(
222 triggerPresenceChanged,
223 Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
224 UnorderedElementsAreArray(triggerParams.reportIds()))));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100225
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200226 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100227 Eq(boost::system::errc::success));
228}
229
230TEST_F(TestTrigger,
Michal Orzelb47b7db2024-09-16 10:02:29 +0200231 settingPropertyReportNamesThrowsExceptionWhenDuplicateReportIds)
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100232{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200233 std::vector<object_path> newPropertyVal{
234 utils::constants::reportDirPath / "report1",
235 utils::constants::reportDirPath / "report2",
236 utils::constants::reportDirPath / "report1"};
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100237
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100238 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100239
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200240 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
241 Eq(boost::system::errc::invalid_argument));
242}
243
Michal Orzelb47b7db2024-09-16 10:02:29 +0200244TEST_F(TestTrigger,
245 settingPropertyReportNamesThrowsExceptionWhenReportWithTooManyPrefixes)
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200246{
247 std::vector<object_path> newPropertyVal{
248 object_path("/xyz/openbmc_project/Telemetry/Reports/P1/P2/MyReport")};
249
250 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
251
252 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
253 Eq(boost::system::errc::invalid_argument));
254}
255
Michal Orzelb47b7db2024-09-16 10:02:29 +0200256TEST_F(TestTrigger,
257 settingPropertyReportNamesThrowsExceptionWhenReportWithTooLongPrefix)
Szymon Dompke32305f12022-07-05 15:37:21 +0200258{
259 std::vector<object_path> newPropertyVal{
260 object_path("/xyz/openbmc_project/Telemetry/Reports/" +
261 utils::string_utils::getTooLongPrefix() + "/MyReport")};
262
263 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
264
265 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
266 Eq(boost::system::errc::invalid_argument));
267}
268
Michal Orzelb47b7db2024-09-16 10:02:29 +0200269TEST_F(TestTrigger,
270 settingPropertyReportNamesThrowsExceptionWhenReportWithTooLongId)
Szymon Dompke32305f12022-07-05 15:37:21 +0200271{
272 std::vector<object_path> newPropertyVal{
273 object_path("/xyz/openbmc_project/Telemetry/Reports/Prefix/" +
274 utils::string_utils::getTooLongId())};
275
276 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
277
278 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
279 Eq(boost::system::errc::invalid_argument));
280}
281
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200282TEST_F(TestTrigger,
Michal Orzelb47b7db2024-09-16 10:02:29 +0200283 settingPropertyReportNamesThrowsExceptionWhenReportWithBadPath)
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200284{
285 std::vector<object_path> newPropertyVal{
286 object_path("/xyz/openbmc_project/Telemetry/NotReports/MyReport")};
287
288 EXPECT_CALL(triggerPresenceChanged, Call(_)).Times(0);
289
290 EXPECT_THAT(setProperty(sut->getPath(), "Reports", newPropertyVal),
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100291 Eq(boost::system::errc::invalid_argument));
Szymon Dompke94f71c52021-12-10 07:16:33 +0100292}
293
294TEST_F(TestTrigger, setPropertySensors)
295{
296 EXPECT_CALL(*triggerFactoryMockPtr, updateSensors(_, _));
297 for (const auto& threshold : thresholdMocks)
298 {
299 auto thresholdMockPtr =
300 std::dynamic_pointer_cast<NiceMock<ThresholdMock>>(threshold);
301 EXPECT_CALL(*thresholdMockPtr, updateSensors(_));
302 }
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200303 SensorsInfo newSensors(
304 {std::make_pair(object_path("/abc/def"), "metadata")});
Szymon Dompke94f71c52021-12-10 07:16:33 +0100305 EXPECT_THAT(setProperty(sut->getPath(), "Sensors", newSensors),
306 Eq(boost::system::errc::success));
307}
308
309TEST_F(TestTrigger, setPropertyThresholds)
310{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200311 EXPECT_CALL(*triggerFactoryMockPtr, updateThresholds(_, _, _, _, _, _));
Szymon Dompke94f71c52021-12-10 07:16:33 +0100312 TriggerThresholdParams newThresholds =
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000313 std::vector<discrete::ThresholdParam>({std::make_tuple(
314 "discrete threshold", utils::enumToString(discrete::Severity::ok),
315 10, "12.3")});
Szymon Dompke94f71c52021-12-10 07:16:33 +0100316 EXPECT_THAT(setProperty(sut->getPath(), "Thresholds", newThresholds),
317 Eq(boost::system::errc::success));
318}
319
Szymon Dompke32305f12022-07-05 15:37:21 +0200320TEST_F(TestTrigger, setThresholdParamsWithTooLongDiscreteName)
321{
322 const TriggerThresholdParams currentValue =
323 std::visit(utils::FromLabeledThresholdParamConversion(),
324 triggerParams.thresholdParams());
325
326 TriggerThresholdParams newThresholds =
327 std::vector<discrete::ThresholdParam>({std::make_tuple(
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000328 utils::string_utils::getTooLongName(),
329 utils::enumToString(discrete::Severity::ok), 10, "12.3")});
Szymon Dompke32305f12022-07-05 15:37:21 +0200330
331 changeProperty<TriggerThresholdParams>(
332 sut->getPath(), "Thresholds",
333 {.valueBefore = Eq(currentValue),
334 .newValue = newThresholds,
335 .ec = Eq(boost::system::errc::invalid_argument),
336 .valueAfter = Eq(currentValue)});
337}
338
339TEST_F(TestTrigger, setNameTooLong)
340{
341 std::string currentValue = TriggerParams().name();
342
343 changeProperty<std::string>(
344 sut->getPath(), "Name",
345 {.valueBefore = Eq(currentValue),
346 .newValue = utils::string_utils::getTooLongName(),
347 .ec = Eq(boost::system::errc::invalid_argument),
348 .valueAfter = Eq(currentValue)});
349}
350
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100351TEST_F(TestTrigger, checkIfNumericCoversionsAreGood)
352{
353 const auto& labeledParamsBase =
354 std::get<std::vector<numeric::LabeledThresholdParam>>(
355 triggerParams.thresholdParams());
356 const auto paramsToCheck =
357 std::visit(utils::FromLabeledThresholdParamConversion(),
358 triggerParams.thresholdParams());
359 const auto labeledParamsToCheck =
360 std::get<std::vector<numeric::LabeledThresholdParam>>(std::visit(
361 utils::ToLabeledThresholdParamConversion(), paramsToCheck));
362
363 for (const auto& [tocheck, base] :
364 boost::combine(labeledParamsToCheck, labeledParamsBase))
365 {
366 EXPECT_THAT(tocheck.at_label<utils::tstring::Type>(),
367 Eq(base.at_label<utils::tstring::Type>()));
368 EXPECT_THAT(tocheck.at_label<utils::tstring::Direction>(),
369 Eq(base.at_label<utils::tstring::Direction>()));
370 EXPECT_THAT(tocheck.at_label<utils::tstring::DwellTime>(),
371 Eq(base.at_label<utils::tstring::DwellTime>()));
372 EXPECT_THAT(tocheck.at_label<utils::tstring::ThresholdValue>(),
373 Eq(base.at_label<utils::tstring::ThresholdValue>()));
374 }
375}
376
377TEST_F(TestTrigger, checkIfDiscreteCoversionsAreGood)
378{
379 const auto& labeledParamsBase =
380 std::get<std::vector<discrete::LabeledThresholdParam>>(
381 triggerDiscreteParams.thresholdParams());
382 const auto paramsToCheck =
383 std::visit(utils::FromLabeledThresholdParamConversion(),
384 triggerDiscreteParams.thresholdParams());
385 const auto labeledParamsToCheck =
386 std::get<std::vector<discrete::LabeledThresholdParam>>(std::visit(
387 utils::ToLabeledThresholdParamConversion(), paramsToCheck));
388
389 for (const auto& [tocheck, base] :
390 boost::combine(labeledParamsToCheck, labeledParamsBase))
391 {
392 EXPECT_THAT(tocheck.at_label<utils::tstring::UserId>(),
393 Eq(base.at_label<utils::tstring::UserId>()));
394 EXPECT_THAT(tocheck.at_label<utils::tstring::Severity>(),
395 Eq(base.at_label<utils::tstring::Severity>()));
396 EXPECT_THAT(tocheck.at_label<utils::tstring::DwellTime>(),
397 Eq(base.at_label<utils::tstring::DwellTime>()));
398 EXPECT_THAT(tocheck.at_label<utils::tstring::ThresholdValue>(),
399 Eq(base.at_label<utils::tstring::ThresholdValue>()));
400 }
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100401}
402
403TEST_F(TestTrigger, deleteTrigger)
404{
Szymon Dompkee28aa532021-10-27 12:33:12 +0200405 EXPECT_CALL(storageMock, remove(to_file_path(sut->getId())));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100406 EXPECT_CALL(*triggerManagerMockPtr, removeTrigger(sut.get()));
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100407
408 auto ec = deleteTrigger(sut->getPath());
409 EXPECT_THAT(ec, Eq(boost::system::errc::success));
410}
411
412TEST_F(TestTrigger, sendUpdateWhenTriggerIsDeleted)
413{
414 EXPECT_CALL(triggerPresenceChanged,
415 Call(FieldsAre(messages::Presence::Removed, triggerParams.id(),
416 UnorderedElementsAre())));
417
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100418 auto ec = deleteTrigger(sut->getPath());
419 EXPECT_THAT(ec, Eq(boost::system::errc::success));
420}
421
422TEST_F(TestTrigger, deletingNonExistingTriggerReturnInvalidRequestDescriptor)
423{
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200424 auto ec =
425 deleteTrigger(utils::constants::triggerDirPath.str + "NonExisting"s);
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100426 EXPECT_THAT(ec.value(), Eq(EBADR));
427}
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100428
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100429TEST_F(TestTrigger, settingPersistencyToFalseRemovesTriggerFromStorage)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100430{
Szymon Dompkee28aa532021-10-27 12:33:12 +0200431 EXPECT_CALL(storageMock, remove(to_file_path(sut->getId())));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100432
433 bool persistent = false;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100434 EXPECT_THAT(setProperty(sut->getPath(), "Persistent", persistent),
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100435 Eq(boost::system::errc::success));
436 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"),
437 Eq(persistent));
438}
439
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100440class TestTriggerInitialization : public TestTrigger
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100441{
442 public:
Patrick Williams3a1c2972023-05-10 07:51:04 -0500443 void SetUp() override {}
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100444
445 nlohmann::json storedConfiguration;
446};
447
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100448TEST_F(TestTriggerInitialization,
449 exceptionDuringTriggerStoreDisablesPersistency)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100450{
451 EXPECT_CALL(storageMock, store(_, _))
452 .WillOnce(Throw(std::runtime_error("Generic error!")));
453
454 sut = makeTrigger(triggerParams);
455
456 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistent"), Eq(false));
457}
458
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100459TEST_F(TestTriggerInitialization, creatingTriggerThrowsExceptionWhenIdIsInvalid)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100460{
461 EXPECT_CALL(storageMock, store(_, _)).Times(0);
462
Szymon Dompkee28aa532021-10-27 12:33:12 +0200463 EXPECT_THROW(makeTrigger(triggerParams.id("inv?lidId")),
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100464 sdbusplus::exception::SdBusError);
465}
466
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100467TEST_F(TestTriggerInitialization, creatingTriggerUpdatesTriggersIdsInReports)
468{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100469 EXPECT_CALL(
470 triggerPresenceChanged,
471 Call(FieldsAre(messages::Presence::Exist, triggerParams.id(),
472 UnorderedElementsAreArray(triggerParams.reportIds()))));
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100473
474 sut = makeTrigger(triggerParams);
475}
476
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100477class TestTriggerStore : public TestTrigger
478{
479 public:
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100480 nlohmann::json storedConfiguration;
481 nlohmann::json storedDiscreteConfiguration;
482 std::unique_ptr<Trigger> sutDiscrete;
483
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100484 void SetUp() override
485 {
486 ON_CALL(storageMock, store(_, _))
487 .WillByDefault(SaveArg<1>(&storedConfiguration));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100488 sut = makeTrigger(triggerParams);
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100489
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100490 ON_CALL(storageMock, store(_, _))
491 .WillByDefault(SaveArg<1>(&storedDiscreteConfiguration));
492 sutDiscrete = makeTrigger(triggerDiscreteParams);
493 }
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100494};
495
496TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerVersion)
497{
498 ASSERT_THAT(storedConfiguration.at("Version"), Eq(expectedTriggerVersion));
499}
500
Szymon Dompkee28aa532021-10-27 12:33:12 +0200501TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerId)
502{
503 ASSERT_THAT(storedConfiguration.at("Id"), Eq(triggerParams.id()));
504}
505
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100506TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerName)
507{
508 ASSERT_THAT(storedConfiguration.at("Name"), Eq(triggerParams.name()));
509}
510
Szymon Dompke20013012021-07-23 09:54:20 +0200511TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerTriggerActions)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100512{
Szymon Dompke20013012021-07-23 09:54:20 +0200513 ASSERT_THAT(storedConfiguration.at("TriggerActions"),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100514 Eq(utils::transform(triggerParams.triggerActions(),
515 [](const auto& action) {
Patrick Williams3a1c2972023-05-10 07:51:04 -0500516 return actionToString(action);
Patrick Williamsc7935fa2023-10-20 11:19:30 -0500517 })));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100518}
519
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100520TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerReportIds)
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100521{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100522 ASSERT_THAT(storedConfiguration.at("ReportIds"),
523 Eq(triggerParams.reportIds()));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100524}
525
526TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerSensors)
527{
528 nlohmann::json expectedItem;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100529 expectedItem["service"] = "service1";
Szymon Dompke94f71c52021-12-10 07:16:33 +0100530 expectedItem["path"] = "/xyz/openbmc_project/sensors/temperature/BMC_Temp";
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100531 expectedItem["metadata"] = "metadata1";
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100532
533 ASSERT_THAT(storedConfiguration.at("Sensors"), ElementsAre(expectedItem));
534}
535
536TEST_F(TestTriggerStore, settingPersistencyToTrueStoresTriggerThresholdParams)
537{
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100538 nlohmann::json expectedItem0;
539 expectedItem0["type"] = 0;
540 expectedItem0["dwellTime"] = 10;
541 expectedItem0["direction"] = 1;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100542 expectedItem0["thresholdValue"] = 0.5;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100543
544 nlohmann::json expectedItem1;
545 expectedItem1["type"] = 3;
546 expectedItem1["dwellTime"] = 10;
547 expectedItem1["direction"] = 2;
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100548 expectedItem1["thresholdValue"] = 90.2;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100549
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100550 ASSERT_THAT(storedConfiguration.at("ThresholdParamsDiscriminator"), Eq(0));
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100551 ASSERT_THAT(storedConfiguration.at("ThresholdParams"),
552 ElementsAre(expectedItem0, expectedItem1));
553}
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100554
555TEST_F(TestTriggerStore,
556 settingPersistencyToTrueStoresDiscreteTriggerThresholdParams)
557{
558 nlohmann::json expectedItem0;
559 expectedItem0["userId"] = "userId";
560 expectedItem0["severity"] = discrete::Severity::warning;
561 expectedItem0["dwellTime"] = 10;
Szymon Dompke9f346792021-07-14 21:07:11 +0200562 expectedItem0["thresholdValue"] = "15.2";
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100563
564 nlohmann::json expectedItem1;
565 expectedItem1["userId"] = "userId_2";
566 expectedItem1["severity"] = discrete::Severity::critical;
567 expectedItem1["dwellTime"] = 5;
Szymon Dompke9f346792021-07-14 21:07:11 +0200568 expectedItem1["thresholdValue"] = "32.7";
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100569
570 ASSERT_THAT(storedDiscreteConfiguration.at("ThresholdParamsDiscriminator"),
571 Eq(1));
572 ASSERT_THAT(storedDiscreteConfiguration.at("ThresholdParams"),
573 ElementsAre(expectedItem0, expectedItem1));
574}