xref: /openbmc/telemetry/tests/src/test_trigger_actions.cpp (revision 66db900fa95b28f8e5f4a313196026cc4d04cfed)
1 #include "dbus_environment.hpp"
2 #include "helpers.hpp"
3 #include "messages/update_report_ind.hpp"
4 #include "trigger_actions.hpp"
5 #include "utils/messanger.hpp"
6 
7 #include <stdexcept>
8 
9 #include <gmock/gmock.h>
10 
11 using namespace testing;
12 
13 namespace action
14 {
15 namespace numeric
16 {
17 using LogParam = std::tuple<::numeric::Type, double, TriggerValue>;
18 
getCorrectParams()19 static auto getCorrectParams()
20 {
21     return Values(
22         std::make_tuple(::numeric::Type::upperCritical, 91.1,
23                         TriggerValue(90.0)),
24         std::make_tuple(::numeric::Type::upperCritical, 90, TriggerValue(91.1)),
25         std::make_tuple(::numeric::Type::lowerCritical, 91.2,
26                         TriggerValue(90.0)),
27         std::make_tuple(::numeric::Type::lowerCritical, 90, TriggerValue(91.2)),
28         std::make_tuple(::numeric::Type::upperWarning, 88.5,
29                         TriggerValue(90.0)),
30         std::make_tuple(::numeric::Type::upperWarning, 90, TriggerValue(88.5)),
31         std::make_tuple(::numeric::Type::lowerWarning, 88.6,
32                         TriggerValue(90.0)),
33         std::make_tuple(::numeric::Type::lowerWarning, 90, TriggerValue(88.6)));
34 }
35 
getIncorrectParams()36 static auto getIncorrectParams()
37 {
38     return Values(std::make_tuple(::numeric::Type::upperCritical, 90.0,
39                                   TriggerValue(90.0)),
40                   std::make_tuple(static_cast<::numeric::Type>(-1), 88.0,
41                                   TriggerValue(90.0)),
42                   std::make_tuple(static_cast<::numeric::Type>(123), 123.0,
43                                   TriggerValue(90.0)),
44                   std::make_tuple(::numeric::Type::upperCritical, 90.0,
45                                   TriggerValue("numeric")));
46 }
47 
48 template <typename ActionType>
49 class TestActionNumeric : public Test, public WithParamInterface<LogParam>
50 {
51   public:
SetUp()52     void SetUp() override
53     {
54         auto [type, threshold, value] = GetParam();
55         sut = std::make_unique<ActionType>(type, threshold);
56         commmitValue = value;
57     }
58 
commit()59     void commit()
60     {
61         sut->commit("MyTrigger", std::nullopt, "MySensor",
62                     Milliseconds{100'000}, commmitValue);
63     }
64 
65     std::unique_ptr<ActionType> sut;
66     TriggerValue commmitValue;
67 };
68 
69 class TestLogToRedfishEventLogNumeric :
70     public TestActionNumeric<LogToRedfishEventLog>
71 {};
72 
73 INSTANTIATE_TEST_SUITE_P(LogToRedfishEventLogNumericParams,
74                          TestLogToRedfishEventLogNumeric, getCorrectParams());
75 
TEST_P(TestLogToRedfishEventLogNumeric,commitExpectNoThrow)76 TEST_P(TestLogToRedfishEventLogNumeric, commitExpectNoThrow)
77 {
78     EXPECT_NO_THROW(commit());
79 }
80 
81 class TestLogToRedfishEventLogNumericThrow :
82     public TestLogToRedfishEventLogNumeric
83 {};
84 
85 INSTANTIATE_TEST_SUITE_P(_, TestLogToRedfishEventLogNumericThrow,
86                          getIncorrectParams());
87 
TEST_P(TestLogToRedfishEventLogNumericThrow,commitExpectToThrow)88 TEST_P(TestLogToRedfishEventLogNumericThrow, commitExpectToThrow)
89 {
90     EXPECT_ANY_THROW(commit());
91 }
92 
93 } // namespace numeric
94 
95 namespace discrete
96 {
97 
98 class TestLogToRedfishEventLogDiscrete : public Test
99 {
100   public:
SetUp()101     void SetUp()
102     {
103         sut = std::make_unique<LogToRedfishEventLog>();
104     }
105 
commit(TriggerValue value) const106     void commit(TriggerValue value) const
107     {
108         std::string thresholdName = "MyThreshold";
109         sut->commit("MyTrigger", std::cref(thresholdName), "MySensor",
110                     Milliseconds{100'000}, value);
111     }
112 
113     std::unique_ptr<LogToRedfishEventLog> sut;
114 };
115 
TEST_F(TestLogToRedfishEventLogDiscrete,commitDiscreteValueExpectNoThrow)116 TEST_F(TestLogToRedfishEventLogDiscrete, commitDiscreteValueExpectNoThrow)
117 {
118     EXPECT_NO_THROW(commit("DiscreteVal"));
119 }
120 
TEST_F(TestLogToRedfishEventLogDiscrete,commitNumericValueExpectToThrow)121 TEST_F(TestLogToRedfishEventLogDiscrete, commitNumericValueExpectToThrow)
122 {
123     EXPECT_ANY_THROW(commit(42.0));
124 }
125 
126 namespace onChange
127 {
128 
129 template <typename ActionType>
130 class TestActionOnChange : public Test
131 {
132   public:
SetUp()133     void SetUp() override
134     {
135         sut = std::make_unique<ActionType>();
136     }
137 
commit(TriggerValue value)138     void commit(TriggerValue value)
139     {
140         sut->commit("MyTrigger", std::nullopt, "MySensor",
141                     Milliseconds{100'000}, value);
142     }
143 
144     std::unique_ptr<ActionType> sut;
145 };
146 
147 class TestLogToRedfishEventLogDiscreteOnChange :
148     public TestActionOnChange<LogToRedfishEventLog>
149 {};
150 
TEST_F(TestLogToRedfishEventLogDiscreteOnChange,commitNumericValueExpectNoThrow)151 TEST_F(TestLogToRedfishEventLogDiscreteOnChange,
152        commitNumericValueExpectNoThrow)
153 {
154     EXPECT_NO_THROW(commit(90.0));
155 }
156 
TEST_F(TestLogToRedfishEventLogDiscreteOnChange,commitDiscreteValueExpectNoThrow)157 TEST_F(TestLogToRedfishEventLogDiscreteOnChange,
158        commitDiscreteValueExpectNoThrow)
159 {
160     EXPECT_NO_THROW(commit("Off"));
161 }
162 
163 } // namespace onChange
164 } // namespace discrete
165 
166 class TestUpdateReport : public Test
167 {
168   public:
TestUpdateReport()169     TestUpdateReport() : messanger(DbusEnvironment::getIoc()) {}
170 
make(std::vector<std::string> names)171     void make(std::vector<std::string> names)
172     {
173         messanger.on_receive<messages::UpdateReportInd>(
174             [this](const auto& msg) { updateReport.Call(msg); });
175 
176         sut = std::make_unique<UpdateReport>(
177             DbusEnvironment::getIoc(),
178             std::make_shared<std::vector<std::string>>(std::move(names)));
179     }
180 
commit(TriggerValue value) const181     void commit(TriggerValue value) const
182     {
183         sut->commit(triggerId, std::nullopt, "MySensor", Milliseconds{100'000},
184                     value);
185     }
186 
187     utils::Messanger messanger;
188     NiceMock<MockFunction<void(const messages::UpdateReportInd&)>> updateReport;
189     std::unique_ptr<UpdateReport> sut;
190     std::string triggerId = "MyTrigger";
191 };
192 
TEST_F(TestUpdateReport,commitWhenReportNameIsEmptyExpectNoReportUpdate)193 TEST_F(TestUpdateReport, commitWhenReportNameIsEmptyExpectNoReportUpdate)
194 {
195     EXPECT_CALL(updateReport, Call(_)).Times(0);
196 
197     make({});
198     commit(90.0);
199 }
200 
TEST_F(TestUpdateReport,commitExpectReportUpdate)201 TEST_F(TestUpdateReport, commitExpectReportUpdate)
202 {
203     std::vector<std::string> names = {"Report1", "Report2", "Report3"};
204     EXPECT_CALL(updateReport,
205                 Call(FieldsAre(UnorderedElementsAreArray(names))));
206 
207     make(names);
208     commit(90.0);
209 }
210 
211 } // namespace action
212