blob: 773b809a797f667da8e76e1f205c777a4c4c8dc4 [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",
32 params.name(), params.isDiscrete(), params.logToJournal(),
Cezary Zwolak4416fce2021-03-17 03:21:06 +010033 params.logToRedfish(), params.updateReport(), sensorInfos,
34 params.reportNames(),
35 std::visit(utils::FromLabeledThresholdParamConversion(),
36 params.thresholdParams()));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010037 return DbusEnvironment::waitForFuture(addTriggerPromise.get_future());
38 }
39
Cezary Zwolak4416fce2021-03-17 03:21:06 +010040 std::unique_ptr<TriggerManager> makeTriggerManager()
41 {
42 return std::make_unique<TriggerManager>(
43 std::move(triggerFactoryMockPtr), std::move(storageMockPtr),
44 DbusEnvironment::getObjServer());
45 }
46
47 void SetUp() override
48 {
49 sut = makeTriggerManager();
50 }
51
Cezary Zwolaka4e67612021-02-18 13:16:16 +010052 std::unique_ptr<StorageMock> storageMockPtr =
53 std::make_unique<NiceMock<StorageMock>>();
54 StorageMock& storageMock = *storageMockPtr;
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010055 std::unique_ptr<TriggerFactoryMock> triggerFactoryMockPtr =
56 std::make_unique<NiceMock<TriggerFactoryMock>>();
57 TriggerFactoryMock& triggerFactoryMock = *triggerFactoryMockPtr;
58 std::unique_ptr<TriggerMock> triggerMockPtr =
Cezary Zwolak4416fce2021-03-17 03:21:06 +010059 std::make_unique<NiceMock<TriggerMock>>(TriggerParams().name());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010060 TriggerMock& triggerMock = *triggerMockPtr;
Cezary Zwolak4416fce2021-03-17 03:21:06 +010061 std::unique_ptr<TriggerManager> sut;
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010062 MockFunction<void(std::string)> checkPoint;
63};
64
65TEST_F(TestTriggerManager, addTrigger)
66{
Cezary Zwolak4416fce2021-03-17 03:21:06 +010067 triggerFactoryMock.expectMake(TriggerParams(), Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010068 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
69
Cezary Zwolak4416fce2021-03-17 03:21:06 +010070 auto [ec, path] = addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010071 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
72 EXPECT_THAT(path, Eq(triggerMock.getPath()));
73}
74
Szymon Dompkef763c9e2021-03-12 09:19:22 +010075TEST_F(TestTriggerManager, addTriggerWithDiscreteThresholds)
76{
77 TriggerParams triggerParamsDiscrete;
Cezary Zwolak4416fce2021-03-17 03:21:06 +010078 auto thresholds = std::vector<discrete::LabeledThresholdParam>{
79 {"discrete_threshold1", discrete::Severity::ok, 10, 11.0},
80 {"discrete_threshold2", discrete::Severity::warning, 10, 12.0},
81 {"discrete_threshold3", discrete::Severity::critical, 10, 13.0}};
Szymon Dompkef763c9e2021-03-12 09:19:22 +010082
83 triggerParamsDiscrete.thresholdParams(thresholds).isDiscrete(true);
84
85 auto [ec, path] = addTrigger(triggerParamsDiscrete);
86 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
87 EXPECT_THAT(path, Eq(triggerMock.getPath()));
88}
89
90TEST_F(TestTriggerManager, addDiscreteTriggerWithoutThresholds)
91{
92 TriggerParams triggerParamsDiscrete;
Cezary Zwolak4416fce2021-03-17 03:21:06 +010093 auto thresholds = std::vector<discrete::LabeledThresholdParam>();
Szymon Dompkef763c9e2021-03-12 09:19:22 +010094
95 triggerParamsDiscrete.thresholdParams(thresholds).isDiscrete(true);
96
97 auto [ec, path] = addTrigger(triggerParamsDiscrete);
98 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
99 EXPECT_THAT(path, Eq(triggerMock.getPath()));
100}
101
Wludzik, Jozef1477fe62021-01-02 11:56:10 +0100102TEST_F(TestTriggerManager, DISABLED_failToAddTriggerTwice)
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100103{
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100104 triggerFactoryMock.expectMake(TriggerParams(), Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100105 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
106
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100107 addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100108
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100109 auto [ec, path] = addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100110 EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists));
111 EXPECT_THAT(path, Eq(std::string()));
112}
113
Wludzik, Jozef1477fe62021-01-02 11:56:10 +0100114TEST_F(TestTriggerManager, DISABLED_failToAddTriggerWhenMaxTriggerIsReached)
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100115{
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100116 auto triggerParams = TriggerParams();
117
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100118 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100119 .Times(TriggerManager::maxTriggers);
120
121 for (size_t i = 0; i < TriggerManager::maxTriggers; i++)
122 {
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100123 triggerParams.name(TriggerParams().name() + std::to_string(i));
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100124
125 auto [ec, path] = addTrigger(triggerParams);
126 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
127 }
128
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100129 triggerParams.name(TriggerParams().name() +
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100130 std::to_string(TriggerManager::maxTriggers));
131 auto [ec, path] = addTrigger(triggerParams);
132 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
133 EXPECT_THAT(path, Eq(std::string()));
134}
135
136TEST_F(TestTriggerManager, removeTrigger)
137{
138 {
139 InSequence seq;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100140 triggerFactoryMock
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100141 .expectMake(TriggerParams(), Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100142 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
143 EXPECT_CALL(triggerMock, Die());
144 EXPECT_CALL(checkPoint, Call("end"));
145 }
146
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100147 addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100148 sut->removeTrigger(&triggerMock);
149 checkPoint.Call("end");
150}
151
152TEST_F(TestTriggerManager, removingTriggerThatIsNotInContainerHasNoEffect)
153{
154 {
155 InSequence seq;
156 EXPECT_CALL(checkPoint, Call("end"));
157 EXPECT_CALL(triggerMock, Die());
158 }
159
160 sut->removeTrigger(&triggerMock);
161 checkPoint.Call("end");
162}
163
164TEST_F(TestTriggerManager, removingSameTriggerTwiceHasNoSideEffect)
165{
166 {
167 InSequence seq;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100168 triggerFactoryMock
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100169 .expectMake(TriggerParams(), Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100170 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
171 EXPECT_CALL(triggerMock, Die());
172 EXPECT_CALL(checkPoint, Call("end"));
173 }
174
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100175 addTrigger(TriggerParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100176 sut->removeTrigger(&triggerMock);
177 sut->removeTrigger(&triggerMock);
178 checkPoint.Call("end");
179}
Cezary Zwolak4416fce2021-03-17 03:21:06 +0100180
181class TestTriggerManagerStorage : public TestTriggerManager
182{
183 public:
184 using FilePath = interfaces::JsonStorage::FilePath;
185 using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
186
187 void SetUp() override
188 {
189 ON_CALL(storageMock, list())
190 .WillByDefault(Return(std::vector<FilePath>{
191 {FilePath("trigger1")}, {FilePath("trigger2")}}));
192
193 ON_CALL(storageMock, load(FilePath("trigger1")))
194 .WillByDefault(InvokeWithoutArgs([this] { return data1; }));
195
196 data2["Name"] = "Trigger2";
197 ON_CALL(storageMock, load(FilePath("trigger2")))
198 .WillByDefault(InvokeWithoutArgs([this] { return data2; }));
199 }
200
201 nlohmann::json data1 = nlohmann::json{
202 {"Version", Trigger::triggerVersion},
203 {"Name", TriggerParams().name()},
204 {"ThresholdParamsDiscriminator",
205 TriggerParams().thresholdParams().index()},
206 {"IsDiscrete", TriggerParams().isDiscrete()},
207 {"LogToJournal", TriggerParams().logToJournal()},
208 {"LogToRedfish", TriggerParams().logToRedfish()},
209 {"UpdateReport", TriggerParams().updateReport()},
210 {"ThresholdParams", utils::labeledThresholdParamsToJson(
211 TriggerParams().thresholdParams())},
212 {"ReportNames", TriggerParams().reportNames()},
213 {"Sensors", TriggerParams().sensors()}};
214
215 nlohmann::json data2 = data1;
216};
217
218TEST_F(TestTriggerManagerStorage, triggerManagerCtorAddTriggerFromStorage)
219{
220 triggerFactoryMock.expectMake(TriggerParams(), _, Ref(storageMock));
221 triggerFactoryMock.expectMake(TriggerParams().name("Trigger2"), _,
222 Ref(storageMock));
223 EXPECT_CALL(storageMock, remove(_)).Times(0);
224
225 sut = makeTriggerManager();
226}
227
228TEST_F(TestTriggerManagerStorage,
229 triggerManagerCtorRemoveDiscreteTriggerFromStorage)
230{
231 LabeledTriggerThresholdParams thresholdParams =
232 std::vector<discrete::LabeledThresholdParam>{
233 {"userId1", discrete::Severity::warning, 15, 10.0},
234 {"userId2", discrete::Severity::critical, 5, 20.0}};
235
236 data1["ThresholdParamsDiscriminator"] = thresholdParams.index();
237
238 data1["ThresholdParams"] =
239 utils::labeledThresholdParamsToJson(thresholdParams);
240
241 EXPECT_CALL(storageMock, remove(FilePath("trigger1"))).Times(0);
242
243 sut = makeTriggerManager();
244}
245
246TEST_F(TestTriggerManagerStorage,
247 triggerManagerCtorRemoveDiscreteTriggerFromStorage2)
248{
249 data1["IsDiscrete"] = true;
250
251 EXPECT_CALL(storageMock, remove(FilePath("trigger1"))).Times(0);
252
253 sut = makeTriggerManager();
254}
255
256TEST_F(TestTriggerManagerStorage,
257 triggerManagerCtorAddProperRemoveInvalidTriggerFromStorage)
258{
259 data1["Version"] = Trigger::triggerVersion - 1;
260
261 triggerFactoryMock.expectMake(TriggerParams().name("Trigger2"), _,
262 Ref(storageMock));
263 EXPECT_CALL(storageMock, remove(FilePath("trigger1")));
264
265 sut = makeTriggerManager();
266}