blob: 911197fae2cd6caecb140fdf544f98c14151c11d [file] [log] [blame]
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01001#include "dbus_environment.hpp"
2#include "messages/update_report_ind.hpp"
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +01003#include "trigger_actions.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01004#include "utils/messanger.hpp"
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +01005
6#include <stdexcept>
7
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01008#include <gmock/gmock.h>
9
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010010using namespace testing;
11
12namespace action
13{
Szymon Dompkef763c9e2021-03-12 09:19:22 +010014namespace numeric
15{
16using LogParam = std::tuple<::numeric::Type, double, double>;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010017
Szymon Dompkef763c9e2021-03-12 09:19:22 +010018static auto getCorrectParams()
19{
20 return Values(std::make_tuple(::numeric::Type::upperCritical, 91.1, 90),
21 std::make_tuple(::numeric::Type::lowerCritical, 91.2, 90),
22 std::make_tuple(::numeric::Type::upperWarning, 88.5, 90),
23 std::make_tuple(::numeric::Type::lowerWarning, 88.6, 90));
24}
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010025
Szymon Dompkef763c9e2021-03-12 09:19:22 +010026static auto getIncorrectParams()
27{
28 return Values(
29 std::make_tuple(::numeric::Type::upperCritical, 90.0, 90),
30 std::make_tuple(static_cast<::numeric::Type>(-1), 88.0, 90),
31 std::make_tuple(static_cast<::numeric::Type>(123), 123.0, 90));
32}
33
34class TestLogToJournalNumeric : public Test, public WithParamInterface<LogParam>
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010035{
36 public:
37 void SetUp() override
38 {
Szymon Dompkef763c9e2021-03-12 09:19:22 +010039 auto [type, threshold, value] = GetParam();
40 sut = std::make_unique<numeric::LogToJournal>(type, threshold);
41 commmitValue = value;
42 }
43
44 std::unique_ptr<numeric::LogToJournal> sut;
45 double commmitValue;
46};
47
48INSTANTIATE_TEST_SUITE_P(LogToJournalNumericParams, TestLogToJournalNumeric,
49 getCorrectParams());
50
51TEST_P(TestLogToJournalNumeric, commitAnActionDoesNotThrow)
52{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010053 EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010054}
55
56class TestLogToJournalNumericThrow : public TestLogToJournalNumeric
57{};
58
59INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalNumericThrow, getIncorrectParams());
60
61TEST_P(TestLogToJournalNumericThrow, commitAnActionExpectThrow)
62{
Szymon Dompkef670b022022-03-16 19:21:11 +010063 EXPECT_ANY_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010064}
65
66class TestLogToRedfishNumeric : public Test, public WithParamInterface<LogParam>
67{
68 public:
69 void SetUp() override
70 {
71 auto [type, threshold, value] = GetParam();
72 sut = std::make_unique<LogToRedfish>(type, threshold);
73 commmitValue = value;
74 }
75
76 std::unique_ptr<LogToRedfish> sut;
77 double commmitValue;
78};
79
80INSTANTIATE_TEST_SUITE_P(LogToRedfishNumericParams, TestLogToRedfishNumeric,
81 getCorrectParams());
82
83TEST_P(TestLogToRedfishNumeric, commitExpectNoThrow)
84{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010085 EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010086}
87
88class TestLogToRedfishNumericThrow : public TestLogToRedfishNumeric
89{};
90
91INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishNumericThrow, getIncorrectParams());
92
93TEST_P(TestLogToRedfishNumericThrow, commitExpectToThrow)
94{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010095 EXPECT_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue),
Szymon Dompkef763c9e2021-03-12 09:19:22 +010096 std::runtime_error);
97}
98
99} // namespace numeric
100
101namespace discrete
102{
103using LogParam = ::discrete::Severity;
104
105static auto getCorrectParams()
106{
107 return Values(::discrete::Severity::critical, ::discrete::Severity::warning,
108 ::discrete::Severity::ok);
109}
110
111static auto getIncorrectParams()
112{
113 return Values(static_cast<::discrete::Severity>(-1),
114 static_cast<::discrete::Severity>(42));
115}
116
117class TestLogToJournalDiscrete :
118 public Test,
119 public WithParamInterface<LogParam>
120{
121 public:
122 void SetUp() override
123 {
124 auto severity = GetParam();
125 sut = std::make_unique<LogToJournal>(severity);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100126 }
127
128 std::unique_ptr<LogToJournal> sut;
129};
130
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100131INSTANTIATE_TEST_SUITE_P(LogToJournalDiscreteParams, TestLogToJournalDiscrete,
132 getCorrectParams());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100133
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100134TEST_P(TestLogToJournalDiscrete, commitAnActionDoesNotThrow)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100135{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100136 EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100137}
138
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100139class TestLogToJournalDiscreteThrow : public TestLogToJournalDiscrete
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100140{};
141
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100142INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalDiscreteThrow,
143 getIncorrectParams());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100144
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100145TEST_P(TestLogToJournalDiscreteThrow, commitAnActionExpectThrow)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100146{
Szymon Dompkef670b022022-03-16 19:21:11 +0100147 EXPECT_ANY_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100148}
149
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100150class TestLogToRedfishDiscrete :
151 public Test,
152 public WithParamInterface<LogParam>
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100153{
154 public:
155 void SetUp() override
156 {
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100157 auto severity = GetParam();
158 sut = std::make_unique<LogToRedfish>(severity);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100159 }
160
161 std::unique_ptr<LogToRedfish> sut;
162};
163
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100164INSTANTIATE_TEST_SUITE_P(LogToRedfishDiscreteParams, TestLogToRedfishDiscrete,
165 getCorrectParams());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100166
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100167TEST_P(TestLogToRedfishDiscrete, commitExpectNoThrow)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100168{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100169 EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100170}
171
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100172class TestLogToRedfishDiscreteThrow : public TestLogToRedfishDiscrete
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100173{};
174
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100175INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishDiscreteThrow,
176 getIncorrectParams());
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100177
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100178TEST_P(TestLogToRedfishDiscreteThrow, commitExpectToThrow)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100179{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100180 EXPECT_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0),
181 std::runtime_error);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100182}
183
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100184namespace onChange
185{
186class TestLogToJournalDiscreteOnChange : public Test
187{
188 public:
189 void SetUp() override
190 {
191 sut = std::make_unique<LogToJournal>();
192 }
193
194 std::unique_ptr<LogToJournal> sut;
195};
196
197TEST_F(TestLogToJournalDiscreteOnChange, commitExpectNoThrow)
198{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100199 EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100200}
201
202class TestLogToRedfishDiscreteOnChange : public Test
203{
204 public:
205 void SetUp() override
206 {
207 sut = std::make_unique<LogToRedfish>();
208 }
209
210 std::unique_ptr<LogToRedfish> sut;
211};
212
213TEST_F(TestLogToRedfishDiscreteOnChange, commitExpectNoThrow)
214{
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100215 EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100216}
217} // namespace onChange
218} // namespace discrete
219
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100220class TestUpdateReport : public Test
221{
222 public:
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100223 TestUpdateReport() : messanger(DbusEnvironment::getIoc())
224 {}
225
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100226 void make(std::vector<std::string> names)
227 {
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100228 messanger.on_receive<messages::UpdateReportInd>(
229 [this](const auto& msg) { updateReport.Call(msg); });
230
Szymon Dompke94f71c52021-12-10 07:16:33 +0100231 sut = std::make_unique<UpdateReport>(
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100232 DbusEnvironment::getIoc(),
Szymon Dompke94f71c52021-12-10 07:16:33 +0100233 std::make_shared<std::vector<std::string>>(std::move(names)));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100234 }
235
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100236 utils::Messanger messanger;
237 NiceMock<MockFunction<void(const messages::UpdateReportInd&)>> updateReport;
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100238 std::unique_ptr<UpdateReport> sut;
239};
240
241TEST_F(TestUpdateReport, commitWhenReportNameIsEmptyExpectNoReportUpdate)
242{
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100243 EXPECT_CALL(updateReport, Call(_)).Times(0);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100244
245 make({});
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100246 sut->commit("Test", Milliseconds{100'000}, 90.0);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100247}
248
249TEST_F(TestUpdateReport, commitExpectReportUpdate)
250{
251 std::vector<std::string> names = {"Report1", "Report2", "Report3"};
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100252 EXPECT_CALL(updateReport,
253 Call(FieldsAre(UnorderedElementsAreArray(names))));
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100254
255 make(names);
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100256 sut->commit("Test", Milliseconds{100'000}, 90.0);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100257}
258
259} // namespace action