blob: 540bcbcc2871af0ab6eeb61637d61183e9950cd3 [file] [log] [blame]
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01001#include "dbus_environment.hpp"
2#include "helpers.hpp"
Cezary Zwolaka4e67612021-02-18 13:16:16 +01003#include "mocks/json_storage_mock.hpp"
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01004#include "mocks/trigger_factory_mock.hpp"
5#include "mocks/trigger_mock.hpp"
6#include "params/trigger_params.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +01007#include "trigger.hpp"
Wludzik, Jozef76833cb2020-12-21 14:42:41 +01008#include "trigger_manager.hpp"
Cezary Zwolak4416fce2021-03-17 03:21:06 +01009#include "utils/conversion_trigger.hpp"
10#include "utils/transform.hpp"
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010011
12using namespace testing;
13
14class TestTriggerManager : public Test
15{
16 public:
17 std::pair<boost::system::error_code, std::string>
18 addTrigger(const TriggerParams& params)
19 {
Cezary Zwolak4416fce2021-03-17 03:21:06 +010020 const auto sensorInfos =
21 utils::fromLabeledSensorsInfo(params.sensors());
22
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010023 std::promise<std::pair<boost::system::error_code, std::string>>
24 addTriggerPromise;
25 DbusEnvironment::getBus()->async_method_call(
26 [&addTriggerPromise](boost::system::error_code ec,
27 const std::string& path) {
28 addTriggerPromise.set_value({ec, path});
29 },
30 DbusEnvironment::serviceName(), TriggerManager::triggerManagerPath,
31 TriggerManager::triggerManagerIfaceName, "AddTrigger",
Szymon Dompke20013012021-07-23 09:54:20 +020032 params.name(), params.triggerActions(), sensorInfos,
Cezary Zwolak4416fce2021-03-17 03:21:06 +010033 params.reportNames(),
34 std::visit(utils::FromLabeledThresholdParamConversion(),
35 params.thresholdParams()));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010036 return DbusEnvironment::waitForFuture(addTriggerPromise.get_future());
37 }
38
Cezary Zwolak4416fce2021-03-17 03:21:06 +010039 std::unique_ptr<TriggerManager> makeTriggerManager()
40 {
41 return std::make_unique<TriggerManager>(
42 std::move(triggerFactoryMockPtr), std::move(storageMockPtr),
43 DbusEnvironment::getObjServer());
44 }
45
46 void SetUp() override
47 {
48 sut = makeTriggerManager();
49 }
50
Cezary Zwolaka4e67612021-02-18 13:16:16 +010051 std::unique_ptr<StorageMock> storageMockPtr =
52 std::make_unique<NiceMock<StorageMock>>();
53 StorageMock& storageMock = *storageMockPtr;
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010054 std::unique_ptr<TriggerFactoryMock> triggerFactoryMockPtr =
55 std::make_unique<NiceMock<TriggerFactoryMock>>();
56 TriggerFactoryMock& triggerFactoryMock = *triggerFactoryMockPtr;
57 std::unique_ptr<TriggerMock> triggerMockPtr =
Cezary Zwolak4416fce2021-03-17 03:21:06 +010058 std::make_unique<NiceMock<TriggerMock>>(TriggerParams().name());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010059 TriggerMock& triggerMock = *triggerMockPtr;
Cezary Zwolak4416fce2021-03-17 03:21:06 +010060 std::unique_ptr<TriggerManager> sut;
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010061 MockFunction<void(std::string)> checkPoint;
62};
63
64TEST_F(TestTriggerManager, addTrigger)
65{
Cezary Zwolak4416fce2021-03-17 03:21:06 +010066 triggerFactoryMock.expectMake(TriggerParams(), Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010067 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
68
Cezary Zwolak4416fce2021-03-17 03:21:06 +010069 auto [ec, path] = addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010070 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
71 EXPECT_THAT(path, Eq(triggerMock.getPath()));
72}
73
Szymon Dompkef763c9e2021-03-12 09:19:22 +010074TEST_F(TestTriggerManager, addTriggerWithDiscreteThresholds)
75{
76 TriggerParams triggerParamsDiscrete;
Cezary Zwolak4416fce2021-03-17 03:21:06 +010077 auto thresholds = std::vector<discrete::LabeledThresholdParam>{
Szymon Dompke9f346792021-07-14 21:07:11 +020078 {"discrete_threshold1", discrete::Severity::ok, 10, "11.0"},
79 {"discrete_threshold2", discrete::Severity::warning, 10, "12.0"},
80 {"discrete_threshold3", discrete::Severity::critical, 10, "13.0"}};
Szymon Dompkef763c9e2021-03-12 09:19:22 +010081
Szymon Dompke20013012021-07-23 09:54:20 +020082 triggerParamsDiscrete.thresholdParams(thresholds);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010083
84 auto [ec, path] = addTrigger(triggerParamsDiscrete);
85 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
86 EXPECT_THAT(path, Eq(triggerMock.getPath()));
87}
88
89TEST_F(TestTriggerManager, addDiscreteTriggerWithoutThresholds)
90{
91 TriggerParams triggerParamsDiscrete;
Cezary Zwolak4416fce2021-03-17 03:21:06 +010092 auto thresholds = std::vector<discrete::LabeledThresholdParam>();
Szymon Dompkef763c9e2021-03-12 09:19:22 +010093
Szymon Dompke20013012021-07-23 09:54:20 +020094 triggerParamsDiscrete.thresholdParams(thresholds);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010095
96 auto [ec, path] = addTrigger(triggerParamsDiscrete);
97 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
98 EXPECT_THAT(path, Eq(triggerMock.getPath()));
99}
100
Wludzik, Jozef1477fe62021-01-02 11:56:10 +0100101TEST_F(TestTriggerManager, DISABLED_failToAddTriggerTwice)
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100102{
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100103 triggerFactoryMock.expectMake(TriggerParams(), Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100104 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
105
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100106 addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100107
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100108 auto [ec, path] = addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100109 EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists));
110 EXPECT_THAT(path, Eq(std::string()));
111}
112
Wludzik, Jozef1477fe62021-01-02 11:56:10 +0100113TEST_F(TestTriggerManager, DISABLED_failToAddTriggerWhenMaxTriggerIsReached)
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100114{
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100115 auto triggerParams = TriggerParams();
116
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100117 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100118 .Times(TriggerManager::maxTriggers);
119
120 for (size_t i = 0; i < TriggerManager::maxTriggers; i++)
121 {
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100122 triggerParams.name(TriggerParams().name() + std::to_string(i));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100123
124 auto [ec, path] = addTrigger(triggerParams);
125 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
126 }
127
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100128 triggerParams.name(TriggerParams().name() +
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100129 std::to_string(TriggerManager::maxTriggers));
130 auto [ec, path] = addTrigger(triggerParams);
131 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
132 EXPECT_THAT(path, Eq(std::string()));
133}
134
135TEST_F(TestTriggerManager, removeTrigger)
136{
137 {
138 InSequence seq;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100139 triggerFactoryMock
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100140 .expectMake(TriggerParams(), Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100141 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
142 EXPECT_CALL(triggerMock, Die());
143 EXPECT_CALL(checkPoint, Call("end"));
144 }
145
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100146 addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100147 sut->removeTrigger(&triggerMock);
148 checkPoint.Call("end");
149}
150
151TEST_F(TestTriggerManager, removingTriggerThatIsNotInContainerHasNoEffect)
152{
153 {
154 InSequence seq;
155 EXPECT_CALL(checkPoint, Call("end"));
156 EXPECT_CALL(triggerMock, Die());
157 }
158
159 sut->removeTrigger(&triggerMock);
160 checkPoint.Call("end");
161}
162
163TEST_F(TestTriggerManager, removingSameTriggerTwiceHasNoSideEffect)
164{
165 {
166 InSequence seq;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100167 triggerFactoryMock
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100168 .expectMake(TriggerParams(), Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100169 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
170 EXPECT_CALL(triggerMock, Die());
171 EXPECT_CALL(checkPoint, Call("end"));
172 }
173
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100174 addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100175 sut->removeTrigger(&triggerMock);
176 sut->removeTrigger(&triggerMock);
177 checkPoint.Call("end");
178}
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100179
180class TestTriggerManagerStorage : public TestTriggerManager
181{
182 public:
183 using FilePath = interfaces::JsonStorage::FilePath;
184 using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
185
186 void SetUp() override
187 {
188 ON_CALL(storageMock, list())
189 .WillByDefault(Return(std::vector<FilePath>{
190 {FilePath("trigger1")}, {FilePath("trigger2")}}));
191
192 ON_CALL(storageMock, load(FilePath("trigger1")))
193 .WillByDefault(InvokeWithoutArgs([this] { return data1; }));
194
195 data2["Name"] = "Trigger2";
196 ON_CALL(storageMock, load(FilePath("trigger2")))
197 .WillByDefault(InvokeWithoutArgs([this] { return data2; }));
198 }
199
200 nlohmann::json data1 = nlohmann::json{
201 {"Version", Trigger::triggerVersion},
202 {"Name", TriggerParams().name()},
203 {"ThresholdParamsDiscriminator",
204 TriggerParams().thresholdParams().index()},
Szymon Dompke20013012021-07-23 09:54:20 +0200205 {"TriggerActions", TriggerParams().triggerActions()},
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100206 {"ThresholdParams", utils::labeledThresholdParamsToJson(
207 TriggerParams().thresholdParams())},
208 {"ReportNames", TriggerParams().reportNames()},
209 {"Sensors", TriggerParams().sensors()}};
210
211 nlohmann::json data2 = data1;
212};
213
214TEST_F(TestTriggerManagerStorage, triggerManagerCtorAddTriggerFromStorage)
215{
216 triggerFactoryMock.expectMake(TriggerParams(), _, Ref(storageMock));
217 triggerFactoryMock.expectMake(TriggerParams().name("Trigger2"), _,
218 Ref(storageMock));
219 EXPECT_CALL(storageMock, remove(_)).Times(0);
220
221 sut = makeTriggerManager();
222}
223
224TEST_F(TestTriggerManagerStorage,
225 triggerManagerCtorRemoveDiscreteTriggerFromStorage)
226{
227 LabeledTriggerThresholdParams thresholdParams =
228 std::vector<discrete::LabeledThresholdParam>{
Szymon Dompke9f346792021-07-14 21:07:11 +0200229 {"userId1", discrete::Severity::warning, 15, "10.0"},
230 {"userId2", discrete::Severity::critical, 5, "20.0"}};
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100231
232 data1["ThresholdParamsDiscriminator"] = thresholdParams.index();
233
234 data1["ThresholdParams"] =
235 utils::labeledThresholdParamsToJson(thresholdParams);
236
237 EXPECT_CALL(storageMock, remove(FilePath("trigger1"))).Times(0);
238
239 sut = makeTriggerManager();
240}
241
242TEST_F(TestTriggerManagerStorage,
243 triggerManagerCtorRemoveDiscreteTriggerFromStorage2)
244{
245 data1["IsDiscrete"] = true;
246
247 EXPECT_CALL(storageMock, remove(FilePath("trigger1"))).Times(0);
248
249 sut = makeTriggerManager();
250}
251
252TEST_F(TestTriggerManagerStorage,
253 triggerManagerCtorAddProperRemoveInvalidTriggerFromStorage)
254{
255 data1["Version"] = Trigger::triggerVersion - 1;
256
257 triggerFactoryMock.expectMake(TriggerParams().name("Trigger2"), _,
258 Ref(storageMock));
259 EXPECT_CALL(storageMock, remove(FilePath("trigger1")));
260
261 sut = makeTriggerManager();
262}