xref: /openbmc/telemetry/tests/src/test_trigger_actions.cpp (revision aa572361124d4797b25674297bbc4cc2682030fa)
1 #include "dbus_environment.hpp"
2 #include "messages/update_report_ind.hpp"
3 #include "trigger_actions.hpp"
4 #include "utils/messanger.hpp"
5 
6 #include <stdexcept>
7 
8 #include <gmock/gmock.h>
9 
10 using namespace testing;
11 
12 namespace action
13 {
14 namespace numeric
15 {
16 using LogParam = std::tuple<::numeric::Type, double, double>;
17 
18 static 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 }
25 
26 static 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 
34 class TestLogToJournalNumeric : public Test, public WithParamInterface<LogParam>
35 {
36   public:
37     void SetUp() override
38     {
39         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 
48 INSTANTIATE_TEST_SUITE_P(LogToJournalNumericParams, TestLogToJournalNumeric,
49                          getCorrectParams());
50 
51 TEST_P(TestLogToJournalNumeric, commitAnActionDoesNotThrow)
52 {
53     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue));
54 }
55 
56 class TestLogToJournalNumericThrow : public TestLogToJournalNumeric
57 {};
58 
59 INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalNumericThrow, getIncorrectParams());
60 
61 TEST_P(TestLogToJournalNumericThrow, commitAnActionExpectThrow)
62 {
63     EXPECT_ANY_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue));
64 }
65 
66 class 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 
80 INSTANTIATE_TEST_SUITE_P(LogToRedfishNumericParams, TestLogToRedfishNumeric,
81                          getCorrectParams());
82 
83 TEST_P(TestLogToRedfishNumeric, commitExpectNoThrow)
84 {
85     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue));
86 }
87 
88 class TestLogToRedfishNumericThrow : public TestLogToRedfishNumeric
89 {};
90 
91 INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishNumericThrow, getIncorrectParams());
92 
93 TEST_P(TestLogToRedfishNumericThrow, commitExpectToThrow)
94 {
95     EXPECT_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue),
96                  std::runtime_error);
97 }
98 
99 } // namespace numeric
100 
101 namespace discrete
102 {
103 using LogParam = ::discrete::Severity;
104 
105 static auto getCorrectParams()
106 {
107     return Values(::discrete::Severity::critical, ::discrete::Severity::warning,
108                   ::discrete::Severity::ok);
109 }
110 
111 static auto getIncorrectParams()
112 {
113     return Values(static_cast<::discrete::Severity>(-1),
114                   static_cast<::discrete::Severity>(42));
115 }
116 
117 class 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);
126     }
127 
128     std::unique_ptr<LogToJournal> sut;
129 };
130 
131 INSTANTIATE_TEST_SUITE_P(LogToJournalDiscreteParams, TestLogToJournalDiscrete,
132                          getCorrectParams());
133 
134 TEST_P(TestLogToJournalDiscrete, commitAnActionDoesNotThrow)
135 {
136     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
137 }
138 
139 class TestLogToJournalDiscreteThrow : public TestLogToJournalDiscrete
140 {};
141 
142 INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalDiscreteThrow,
143                          getIncorrectParams());
144 
145 TEST_P(TestLogToJournalDiscreteThrow, commitAnActionExpectThrow)
146 {
147     EXPECT_ANY_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
148 }
149 
150 class TestLogToRedfishDiscrete :
151     public Test,
152     public WithParamInterface<LogParam>
153 {
154   public:
155     void SetUp() override
156     {
157         auto severity = GetParam();
158         sut = std::make_unique<LogToRedfish>(severity);
159     }
160 
161     std::unique_ptr<LogToRedfish> sut;
162 };
163 
164 INSTANTIATE_TEST_SUITE_P(LogToRedfishDiscreteParams, TestLogToRedfishDiscrete,
165                          getCorrectParams());
166 
167 TEST_P(TestLogToRedfishDiscrete, commitExpectNoThrow)
168 {
169     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
170 }
171 
172 class TestLogToRedfishDiscreteThrow : public TestLogToRedfishDiscrete
173 {};
174 
175 INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishDiscreteThrow,
176                          getIncorrectParams());
177 
178 TEST_P(TestLogToRedfishDiscreteThrow, commitExpectToThrow)
179 {
180     EXPECT_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0),
181                  std::runtime_error);
182 }
183 
184 namespace onChange
185 {
186 class 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 
197 TEST_F(TestLogToJournalDiscreteOnChange, commitExpectNoThrow)
198 {
199     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
200 }
201 
202 class 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 
213 TEST_F(TestLogToRedfishDiscreteOnChange, commitExpectNoThrow)
214 {
215     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
216 }
217 } // namespace onChange
218 } // namespace discrete
219 
220 class TestUpdateReport : public Test
221 {
222   public:
223     TestUpdateReport() : messanger(DbusEnvironment::getIoc())
224     {}
225 
226     void make(std::vector<std::string> names)
227     {
228         messanger.on_receive<messages::UpdateReportInd>(
229             [this](const auto& msg) { updateReport.Call(msg); });
230 
231         sut = std::make_unique<UpdateReport>(
232             DbusEnvironment::getIoc(),
233             std::make_shared<std::vector<std::string>>(std::move(names)));
234     }
235 
236     utils::Messanger messanger;
237     NiceMock<MockFunction<void(const messages::UpdateReportInd&)>> updateReport;
238     std::unique_ptr<UpdateReport> sut;
239 };
240 
241 TEST_F(TestUpdateReport, commitWhenReportNameIsEmptyExpectNoReportUpdate)
242 {
243     EXPECT_CALL(updateReport, Call(_)).Times(0);
244 
245     make({});
246     sut->commit("Test", Milliseconds{100'000}, 90.0);
247 }
248 
249 TEST_F(TestUpdateReport, commitExpectReportUpdate)
250 {
251     std::vector<std::string> names = {"Report1", "Report2", "Report3"};
252     EXPECT_CALL(updateReport,
253                 Call(FieldsAre(UnorderedElementsAreArray(names))));
254 
255     make(names);
256     sut->commit("Test", Milliseconds{100'000}, 90.0);
257 }
258 
259 } // namespace action
260