blob: 42488aa89115d5a22bf0e388f8f8a226cb2e0ce1 [file] [log] [blame]
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01001#include "dbus_environment.hpp"
Szymon Dompke1cdd7e42022-06-08 14:43:13 +02002#include "helpers.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01003#include "messages/update_report_ind.hpp"
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +01004#include "trigger_actions.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01005#include "utils/messanger.hpp"
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +01006
7#include <stdexcept>
8
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01009#include <gmock/gmock.h>
10
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010011using namespace testing;
12
13namespace action
14{
Szymon Dompkef763c9e2021-03-12 09:19:22 +010015namespace numeric
16{
17using LogParam = std::tuple<::numeric::Type, double, double>;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010018
Szymon Dompkef763c9e2021-03-12 09:19:22 +010019static auto getCorrectParams()
20{
21 return Values(std::make_tuple(::numeric::Type::upperCritical, 91.1, 90),
22 std::make_tuple(::numeric::Type::lowerCritical, 91.2, 90),
23 std::make_tuple(::numeric::Type::upperWarning, 88.5, 90),
24 std::make_tuple(::numeric::Type::lowerWarning, 88.6, 90));
25}
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010026
Szymon Dompkef763c9e2021-03-12 09:19:22 +010027static auto getIncorrectParams()
28{
29 return Values(
30 std::make_tuple(::numeric::Type::upperCritical, 90.0, 90),
31 std::make_tuple(static_cast<::numeric::Type>(-1), 88.0, 90),
32 std::make_tuple(static_cast<::numeric::Type>(123), 123.0, 90));
33}
34
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020035template <typename ActionType>
36class TestActionNumeric : public Test, public WithParamInterface<LogParam>
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010037{
38 public:
39 void SetUp() override
40 {
Szymon Dompkef763c9e2021-03-12 09:19:22 +010041 auto [type, threshold, value] = GetParam();
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020042 sut = std::make_unique<ActionType>(type, threshold);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010043 commmitValue = value;
44 }
45
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020046 void commit()
47 {
48 sut->commit("MyTrigger", std::nullopt, "MySensor",
49 Milliseconds{100'000}, commmitValue);
50 }
51
52 std::unique_ptr<ActionType> sut;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010053 double commmitValue;
54};
55
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020056class TestLogToJournalNumeric : public TestActionNumeric<LogToJournal>
57{};
58
Szymon Dompkef763c9e2021-03-12 09:19:22 +010059INSTANTIATE_TEST_SUITE_P(LogToJournalNumericParams, TestLogToJournalNumeric,
60 getCorrectParams());
61
62TEST_P(TestLogToJournalNumeric, commitAnActionDoesNotThrow)
63{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020064 EXPECT_NO_THROW(commit());
Szymon Dompkef763c9e2021-03-12 09:19:22 +010065}
66
67class TestLogToJournalNumericThrow : public TestLogToJournalNumeric
68{};
69
70INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalNumericThrow, getIncorrectParams());
71
72TEST_P(TestLogToJournalNumericThrow, commitAnActionExpectThrow)
73{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020074 EXPECT_ANY_THROW(commit());
Szymon Dompkef763c9e2021-03-12 09:19:22 +010075}
76
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020077class TestLogToRedfishEventLogNumeric :
78 public TestActionNumeric<LogToRedfishEventLog>
Szymon Dompkef763c9e2021-03-12 09:19:22 +010079{};
80
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020081INSTANTIATE_TEST_SUITE_P(LogToRedfishEventLogNumericParams,
82 TestLogToRedfishEventLogNumeric, getCorrectParams());
Szymon Dompkef763c9e2021-03-12 09:19:22 +010083
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020084TEST_P(TestLogToRedfishEventLogNumeric, commitExpectNoThrow)
Szymon Dompkef763c9e2021-03-12 09:19:22 +010085{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020086 EXPECT_NO_THROW(commit());
87}
88
89class TestLogToRedfishEventLogNumericThrow :
90 public TestLogToRedfishEventLogNumeric
91{};
92
93INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishEventLogNumericThrow,
94 getIncorrectParams());
95
96TEST_P(TestLogToRedfishEventLogNumericThrow, commitExpectToThrow)
97{
98 EXPECT_ANY_THROW(commit());
Szymon Dompkef763c9e2021-03-12 09:19:22 +010099}
100
101} // namespace numeric
102
103namespace discrete
104{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200105using LogParam = std::tuple<::discrete::Severity, TriggerValue>;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100106
107static auto getCorrectParams()
108{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200109 return Values(
110 std::make_tuple(::discrete::Severity::critical,
111 TriggerValue("DiscreteVal")),
112 std::make_tuple(::discrete::Severity::warning, TriggerValue("On")),
113 std::make_tuple(::discrete::Severity::ok, TriggerValue("Off")));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100114}
115
116static auto getIncorrectParams()
117{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200118 return Values(
119 std::make_tuple(static_cast<::discrete::Severity>(-1),
120 TriggerValue("DiscreteVal42")),
121 std::make_tuple(static_cast<::discrete::Severity>(42),
122 TriggerValue("On")),
123 std::make_tuple(::discrete::Severity::critical, TriggerValue(42.0)),
124 std::make_tuple(::discrete::Severity::warning, TriggerValue(0.0)),
125 std::make_tuple(::discrete::Severity::ok, TriggerValue(0.1)));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100126}
127
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200128template <typename ActionType>
129class TestActionDiscrete : public Test, public WithParamInterface<LogParam>
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100130{
131 public:
132 void SetUp() override
133 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200134 auto [severity, value] = GetParam();
135 sut = std::make_unique<ActionType>(severity);
136 commitValue = value;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100137 }
138
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200139 void commit()
140 {
141 std::string thresholdName = "MyThreshold";
142 sut->commit("MyTrigger", std::cref(thresholdName), "MySensor",
143 Milliseconds{100'000}, commitValue);
144 }
145
146 TriggerValue commitValue;
147 std::unique_ptr<ActionType> sut;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100148};
149
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200150class TestLogToJournalDiscrete : public TestActionDiscrete<LogToJournal>
151{};
152
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100153INSTANTIATE_TEST_SUITE_P(LogToJournalDiscreteParams, TestLogToJournalDiscrete,
154 getCorrectParams());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100155
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200156TEST_P(TestLogToJournalDiscrete, commitAnActionWIthDiscreteValueDoesNotThrow)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100157{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200158 EXPECT_NO_THROW(commit());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100159}
160
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100161class TestLogToJournalDiscreteThrow : public TestLogToJournalDiscrete
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100162{};
163
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100164INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalDiscreteThrow,
165 getIncorrectParams());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100166
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100167TEST_P(TestLogToJournalDiscreteThrow, commitAnActionExpectThrow)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100168{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200169 EXPECT_ANY_THROW(commit());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100170}
171
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200172class TestLogToRedfishEventLogDiscrete :
173 public TestActionDiscrete<LogToRedfishEventLog>
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100174{};
175
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200176INSTANTIATE_TEST_SUITE_P(LogToRedfishEventLogDiscreteParams,
177 TestLogToRedfishEventLogDiscrete, getCorrectParams());
178
179TEST_P(TestLogToRedfishEventLogDiscrete, commitExpectNoThrow)
180{
181 EXPECT_NO_THROW(commit());
182}
183
184class TestLogToRedfishEventLogDiscreteThrow :
185 public TestLogToRedfishEventLogDiscrete
186{};
187
188INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishEventLogDiscreteThrow,
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100189 getIncorrectParams());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100190
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200191TEST_P(TestLogToRedfishEventLogDiscreteThrow, commitExpectToThrow)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100192{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200193 EXPECT_ANY_THROW(commit());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100194}
195
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100196namespace onChange
197{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200198
199template <typename ActionType>
200class TestActionOnChange : public Test
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100201{
202 public:
203 void SetUp() override
204 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200205 sut = std::make_unique<ActionType>();
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100206 }
207
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200208 void commit(TriggerValue value)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100209 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200210 sut->commit("MyTrigger", std::nullopt, "MySensor",
211 Milliseconds{100'000}, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100212 }
213
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200214 std::unique_ptr<ActionType> sut;
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100215};
216
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200217class TestLogToJournalDiscreteOnChange : public TestActionOnChange<LogToJournal>
218{};
219
220TEST_F(TestLogToJournalDiscreteOnChange, commitNumericValueExpectNoThrow)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100221{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200222 EXPECT_NO_THROW(commit(90.0));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100223}
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200224
225TEST_F(TestLogToJournalDiscreteOnChange, commitDiscreteValueExpectNoThrow)
226{
227 EXPECT_NO_THROW(commit("Off"));
228}
229
230class TestLogToRedfishEventLogDiscreteOnChange :
231 public TestActionOnChange<LogToRedfishEventLog>
232{};
233
234TEST_F(TestLogToRedfishEventLogDiscreteOnChange,
235 commitNumericValueExpectNoThrow)
236{
237 EXPECT_NO_THROW(commit(90.0));
238}
239
240TEST_F(TestLogToRedfishEventLogDiscreteOnChange,
241 commitDiscreteValueExpectNoThrow)
242{
243 EXPECT_NO_THROW(commit("Off"));
244}
245
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100246} // namespace onChange
247} // namespace discrete
248
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100249class TestUpdateReport : public Test
250{
251 public:
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100252 TestUpdateReport() : messanger(DbusEnvironment::getIoc())
253 {}
254
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100255 void make(std::vector<std::string> names)
256 {
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100257 messanger.on_receive<messages::UpdateReportInd>(
258 [this](const auto& msg) { updateReport.Call(msg); });
259
Szymon Dompke94f71c52021-12-10 07:16:33 +0100260 sut = std::make_unique<UpdateReport>(
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100261 DbusEnvironment::getIoc(),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100262 std::make_shared<std::vector<std::string>>(std::move(names)));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100263 }
264
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200265 void commit(TriggerValue value)
266 {
267 sut->commit(triggerId, std::nullopt, "MySensor", Milliseconds{100'000},
268 value);
269 }
270
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100271 utils::Messanger messanger;
272 NiceMock<MockFunction<void(const messages::UpdateReportInd&)>> updateReport;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100273 std::unique_ptr<UpdateReport> sut;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200274 std::string triggerId = "MyTrigger";
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100275};
276
277TEST_F(TestUpdateReport, commitWhenReportNameIsEmptyExpectNoReportUpdate)
278{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100279 EXPECT_CALL(updateReport, Call(_)).Times(0);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100280
281 make({});
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200282 commit(90.0);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100283}
284
285TEST_F(TestUpdateReport, commitExpectReportUpdate)
286{
287 std::vector<std::string> names = {"Report1", "Report2", "Report3"};
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100288 EXPECT_CALL(updateReport,
289 Call(FieldsAre(UnorderedElementsAreArray(names))));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100290
291 make(names);
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200292 commit(90.0);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100293}
294
295} // namespace action