xref: /openbmc/telemetry/tests/src/test_trigger_actions.cpp (revision e6d4887453f0b23b46c012da7cb26f2beb38ef0e)
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_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue),
64                  std::runtime_error);
65 }
66 
67 class TestLogToRedfishNumeric : public Test, public WithParamInterface<LogParam>
68 {
69   public:
70     void SetUp() override
71     {
72         auto [type, threshold, value] = GetParam();
73         sut = std::make_unique<LogToRedfish>(type, threshold);
74         commmitValue = value;
75     }
76 
77     std::unique_ptr<LogToRedfish> sut;
78     double commmitValue;
79 };
80 
81 INSTANTIATE_TEST_SUITE_P(LogToRedfishNumericParams, TestLogToRedfishNumeric,
82                          getCorrectParams());
83 
84 TEST_P(TestLogToRedfishNumeric, commitExpectNoThrow)
85 {
86     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue));
87 }
88 
89 class TestLogToRedfishNumericThrow : public TestLogToRedfishNumeric
90 {};
91 
92 INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishNumericThrow, getIncorrectParams());
93 
94 TEST_P(TestLogToRedfishNumericThrow, commitExpectToThrow)
95 {
96     EXPECT_THROW(sut->commit("Test", Milliseconds{100'000}, commmitValue),
97                  std::runtime_error);
98 }
99 
100 } // namespace numeric
101 
102 namespace discrete
103 {
104 using LogParam = ::discrete::Severity;
105 
106 static auto getCorrectParams()
107 {
108     return Values(::discrete::Severity::critical, ::discrete::Severity::warning,
109                   ::discrete::Severity::ok);
110 }
111 
112 static auto getIncorrectParams()
113 {
114     return Values(static_cast<::discrete::Severity>(-1),
115                   static_cast<::discrete::Severity>(42));
116 }
117 
118 class TestLogToJournalDiscrete :
119     public Test,
120     public WithParamInterface<LogParam>
121 {
122   public:
123     void SetUp() override
124     {
125         auto severity = GetParam();
126         sut = std::make_unique<LogToJournal>(severity);
127     }
128 
129     std::unique_ptr<LogToJournal> sut;
130 };
131 
132 INSTANTIATE_TEST_SUITE_P(LogToJournalDiscreteParams, TestLogToJournalDiscrete,
133                          getCorrectParams());
134 
135 TEST_P(TestLogToJournalDiscrete, commitAnActionDoesNotThrow)
136 {
137     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
138 }
139 
140 class TestLogToJournalDiscreteThrow : public TestLogToJournalDiscrete
141 {};
142 
143 INSTANTIATE_TEST_SUITE_P(_, TestLogToJournalDiscreteThrow,
144                          getIncorrectParams());
145 
146 TEST_P(TestLogToJournalDiscreteThrow, commitAnActionExpectThrow)
147 {
148     EXPECT_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0),
149                  std::runtime_error);
150 }
151 
152 class TestLogToRedfishDiscrete :
153     public Test,
154     public WithParamInterface<LogParam>
155 {
156   public:
157     void SetUp() override
158     {
159         auto severity = GetParam();
160         sut = std::make_unique<LogToRedfish>(severity);
161     }
162 
163     std::unique_ptr<LogToRedfish> sut;
164 };
165 
166 INSTANTIATE_TEST_SUITE_P(LogToRedfishDiscreteParams, TestLogToRedfishDiscrete,
167                          getCorrectParams());
168 
169 TEST_P(TestLogToRedfishDiscrete, commitExpectNoThrow)
170 {
171     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
172 }
173 
174 class TestLogToRedfishDiscreteThrow : public TestLogToRedfishDiscrete
175 {};
176 
177 INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishDiscreteThrow,
178                          getIncorrectParams());
179 
180 TEST_P(TestLogToRedfishDiscreteThrow, commitExpectToThrow)
181 {
182     EXPECT_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0),
183                  std::runtime_error);
184 }
185 
186 namespace onChange
187 {
188 class TestLogToJournalDiscreteOnChange : public Test
189 {
190   public:
191     void SetUp() override
192     {
193         sut = std::make_unique<LogToJournal>();
194     }
195 
196     std::unique_ptr<LogToJournal> sut;
197 };
198 
199 TEST_F(TestLogToJournalDiscreteOnChange, commitExpectNoThrow)
200 {
201     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
202 }
203 
204 class TestLogToRedfishDiscreteOnChange : public Test
205 {
206   public:
207     void SetUp() override
208     {
209         sut = std::make_unique<LogToRedfish>();
210     }
211 
212     std::unique_ptr<LogToRedfish> sut;
213 };
214 
215 TEST_F(TestLogToRedfishDiscreteOnChange, commitExpectNoThrow)
216 {
217     EXPECT_NO_THROW(sut->commit("Test", Milliseconds{100'000}, 90.0));
218 }
219 } // namespace onChange
220 } // namespace discrete
221 
222 class TestUpdateReport : public Test
223 {
224   public:
225     TestUpdateReport() : messanger(DbusEnvironment::getIoc())
226     {}
227 
228     void make(std::vector<std::string> names)
229     {
230         messanger.on_receive<messages::UpdateReportInd>(
231             [this](const auto& msg) { updateReport.Call(msg); });
232 
233         sut = std::make_unique<UpdateReport>(
234             DbusEnvironment::getIoc(),
235             std::make_shared<std::vector<std::string>>(std::move(names)));
236     }
237 
238     utils::Messanger messanger;
239     NiceMock<MockFunction<void(const messages::UpdateReportInd&)>> updateReport;
240     std::unique_ptr<UpdateReport> sut;
241 };
242 
243 TEST_F(TestUpdateReport, commitWhenReportNameIsEmptyExpectNoReportUpdate)
244 {
245     EXPECT_CALL(updateReport, Call(_)).Times(0);
246 
247     make({});
248     sut->commit("Test", Milliseconds{100'000}, 90.0);
249 }
250 
251 TEST_F(TestUpdateReport, commitExpectReportUpdate)
252 {
253     std::vector<std::string> names = {"Report1", "Report2", "Report3"};
254     EXPECT_CALL(updateReport,
255                 Call(FieldsAre(UnorderedElementsAreArray(names))));
256 
257     make(names);
258     sut->commit("Test", Milliseconds{100'000}, 90.0);
259 }
260 
261 } // namespace action
262