blob: 1bc4f1b113be46b7732e244d83fcdcf52d9e7b8b [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"
7#include "trigger_manager.hpp"
8
9using namespace testing;
10
11class TestTriggerManager : public Test
12{
13 public:
14 std::pair<boost::system::error_code, std::string>
15 addTrigger(const TriggerParams& params)
16 {
17 std::promise<std::pair<boost::system::error_code, std::string>>
18 addTriggerPromise;
19 DbusEnvironment::getBus()->async_method_call(
20 [&addTriggerPromise](boost::system::error_code ec,
21 const std::string& path) {
22 addTriggerPromise.set_value({ec, path});
23 },
24 DbusEnvironment::serviceName(), TriggerManager::triggerManagerPath,
25 TriggerManager::triggerManagerIfaceName, "AddTrigger",
26 params.name(), params.isDiscrete(), params.logToJournal(),
27 params.logToRedfish(), params.updateReport(), params.sensors(),
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010028 params.reportNames(), params.thresholdParams());
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010029 return DbusEnvironment::waitForFuture(addTriggerPromise.get_future());
30 }
31
32 TriggerParams triggerParams;
Cezary Zwolaka4e67612021-02-18 13:16:16 +010033 std::unique_ptr<StorageMock> storageMockPtr =
34 std::make_unique<NiceMock<StorageMock>>();
35 StorageMock& storageMock = *storageMockPtr;
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010036 std::unique_ptr<TriggerFactoryMock> triggerFactoryMockPtr =
37 std::make_unique<NiceMock<TriggerFactoryMock>>();
38 TriggerFactoryMock& triggerFactoryMock = *triggerFactoryMockPtr;
39 std::unique_ptr<TriggerMock> triggerMockPtr =
40 std::make_unique<NiceMock<TriggerMock>>(triggerParams.name());
41 TriggerMock& triggerMock = *triggerMockPtr;
42 std::unique_ptr<TriggerManager> sut = std::make_unique<TriggerManager>(
Cezary Zwolaka4e67612021-02-18 13:16:16 +010043 std::move(triggerFactoryMockPtr), std::move(storageMockPtr),
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010044 std::move(DbusEnvironment::getObjServer()));
45 MockFunction<void(std::string)> checkPoint;
46};
47
48TEST_F(TestTriggerManager, addTrigger)
49{
Cezary Zwolaka4e67612021-02-18 13:16:16 +010050 triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010051 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
52
53 auto [ec, path] = addTrigger(triggerParams);
54 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
55 EXPECT_THAT(path, Eq(triggerMock.getPath()));
56}
57
Szymon Dompkef763c9e2021-03-12 09:19:22 +010058TEST_F(TestTriggerManager, addTriggerWithDiscreteThresholds)
59{
60 TriggerParams triggerParamsDiscrete;
61 auto thresholds = std::vector<discrete::ThresholdParam>{
62 {"discrete_threshold1",
63 discrete::severityToString(discrete::Severity::ok), 10, 11.0},
64 {"discrete_threshold2",
65 discrete::severityToString(discrete::Severity::warning), 10, 12.0},
66 {"discrete_threshold3",
67 discrete::severityToString(discrete::Severity::critical), 10, 13.0}};
68
69 triggerParamsDiscrete.thresholdParams(thresholds).isDiscrete(true);
70
71 auto [ec, path] = addTrigger(triggerParamsDiscrete);
72 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
73 EXPECT_THAT(path, Eq(triggerMock.getPath()));
74}
75
76TEST_F(TestTriggerManager, addDiscreteTriggerWithoutThresholds)
77{
78 TriggerParams triggerParamsDiscrete;
79 auto thresholds = std::vector<discrete::ThresholdParam>();
80
81 triggerParamsDiscrete.thresholdParams(thresholds).isDiscrete(true);
82
83 auto [ec, path] = addTrigger(triggerParamsDiscrete);
84 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
85 EXPECT_THAT(path, Eq(triggerMock.getPath()));
86}
87
Wludzik, Jozef1477fe62021-01-02 11:56:10 +010088TEST_F(TestTriggerManager, DISABLED_failToAddTriggerTwice)
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010089{
Cezary Zwolaka4e67612021-02-18 13:16:16 +010090 triggerFactoryMock.expectMake(triggerParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +010091 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
92
93 addTrigger(triggerParams);
94
95 auto [ec, path] = addTrigger(triggerParams);
96 EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists));
97 EXPECT_THAT(path, Eq(std::string()));
98}
99
Wludzik, Jozef1477fe62021-01-02 11:56:10 +0100100TEST_F(TestTriggerManager, DISABLED_failToAddTriggerWhenMaxTriggerIsReached)
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100101{
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100102 triggerFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100103 .Times(TriggerManager::maxTriggers);
104
105 for (size_t i = 0; i < TriggerManager::maxTriggers; i++)
106 {
107 triggerParams.name(triggerParams.name() + std::to_string(i));
108
109 auto [ec, path] = addTrigger(triggerParams);
110 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
111 }
112
113 triggerParams.name(triggerParams.name() +
114 std::to_string(TriggerManager::maxTriggers));
115 auto [ec, path] = addTrigger(triggerParams);
116 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
117 EXPECT_THAT(path, Eq(std::string()));
118}
119
120TEST_F(TestTriggerManager, removeTrigger)
121{
122 {
123 InSequence seq;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100124 triggerFactoryMock
125 .expectMake(triggerParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100126 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
127 EXPECT_CALL(triggerMock, Die());
128 EXPECT_CALL(checkPoint, Call("end"));
129 }
130
131 addTrigger(triggerParams);
132 sut->removeTrigger(&triggerMock);
133 checkPoint.Call("end");
134}
135
136TEST_F(TestTriggerManager, removingTriggerThatIsNotInContainerHasNoEffect)
137{
138 {
139 InSequence seq;
140 EXPECT_CALL(checkPoint, Call("end"));
141 EXPECT_CALL(triggerMock, Die());
142 }
143
144 sut->removeTrigger(&triggerMock);
145 checkPoint.Call("end");
146}
147
148TEST_F(TestTriggerManager, removingSameTriggerTwiceHasNoSideEffect)
149{
150 {
151 InSequence seq;
Cezary Zwolaka4e67612021-02-18 13:16:16 +0100152 triggerFactoryMock
153 .expectMake(triggerParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef76833cb2020-12-21 14:42:41 +0100154 .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
155 EXPECT_CALL(triggerMock, Die());
156 EXPECT_CALL(checkPoint, Call("end"));
157 }
158
159 addTrigger(triggerParams);
160 sut->removeTrigger(&triggerMock);
161 sut->removeTrigger(&triggerMock);
162 checkPoint.Call("end");
163}