xref: /openbmc/phosphor-modbus/tests/test_events.cpp (revision 7184805ae4ede906935133e3e0f8ee2468bc781b)
1*7184805aSJagpal Singh Gill #include "common/events.hpp"
2*7184805aSJagpal Singh Gill 
3*7184805aSJagpal Singh Gill #include <xyz/openbmc_project/Logging/Create/aserver.hpp>
4*7184805aSJagpal Singh Gill #include <xyz/openbmc_project/Logging/Entry/aserver.hpp>
5*7184805aSJagpal Singh Gill #include <xyz/openbmc_project/Sensor/Threshold/event.hpp>
6*7184805aSJagpal Singh Gill #include <xyz/openbmc_project/Sensor/event.hpp>
7*7184805aSJagpal Singh Gill #include <xyz/openbmc_project/State/Leak/Detector/event.hpp>
8*7184805aSJagpal Singh Gill #include <xyz/openbmc_project/State/Power/event.hpp>
9*7184805aSJagpal Singh Gill #include <xyz/openbmc_project/State/SMC/event.hpp>
10*7184805aSJagpal Singh Gill 
11*7184805aSJagpal Singh Gill #include <gtest/gtest.h>
12*7184805aSJagpal Singh Gill 
13*7184805aSJagpal Singh Gill class TestEventServer;
14*7184805aSJagpal Singh Gill class TestEventEntry;
15*7184805aSJagpal Singh Gill 
16*7184805aSJagpal Singh Gill using namespace std::literals;
17*7184805aSJagpal Singh Gill namespace EventIntf = phosphor::modbus::events;
18*7184805aSJagpal Singh Gill using EventServerIntf =
19*7184805aSJagpal Singh Gill     sdbusplus::aserver::xyz::openbmc_project::logging::Create<TestEventServer>;
20*7184805aSJagpal Singh Gill using EventEntryIntf =
21*7184805aSJagpal Singh Gill     sdbusplus::aserver::xyz::openbmc_project::logging::Entry<TestEventEntry>;
22*7184805aSJagpal Singh Gill 
23*7184805aSJagpal Singh Gill namespace SensorThresholdErrorIntf =
24*7184805aSJagpal Singh Gill     sdbusplus::error::xyz::openbmc_project::sensor::Threshold;
25*7184805aSJagpal Singh Gill namespace SensorThresholdEventIntf =
26*7184805aSJagpal Singh Gill     sdbusplus::event::xyz::openbmc_project::sensor::Threshold;
27*7184805aSJagpal Singh Gill namespace SensorErrorIntf = sdbusplus::error::xyz::openbmc_project::Sensor;
28*7184805aSJagpal Singh Gill namespace SensorEventIntf = sdbusplus::event::xyz::openbmc_project::Sensor;
29*7184805aSJagpal Singh Gill namespace ControllerErrorIntf =
30*7184805aSJagpal Singh Gill     sdbusplus::error::xyz::openbmc_project::state::SMC;
31*7184805aSJagpal Singh Gill namespace ControllerEventIntf =
32*7184805aSJagpal Singh Gill     sdbusplus::event::xyz::openbmc_project::state::SMC;
33*7184805aSJagpal Singh Gill namespace PowerErrorIntf = sdbusplus::error::xyz::openbmc_project::state::Power;
34*7184805aSJagpal Singh Gill namespace PowerEventIntf = sdbusplus::event::xyz::openbmc_project::state::Power;
35*7184805aSJagpal Singh Gill namespace LeakErrorIntf =
36*7184805aSJagpal Singh Gill     sdbusplus::error::xyz::openbmc_project::state::leak::Detector;
37*7184805aSJagpal Singh Gill namespace LeakEventIntf =
38*7184805aSJagpal Singh Gill     sdbusplus::event::xyz::openbmc_project::state::leak::Detector;
39*7184805aSJagpal Singh Gill 
40*7184805aSJagpal Singh Gill // Test Event Class to mock the EventEntry
41*7184805aSJagpal Singh Gill class TestEventEntry : public EventEntryIntf
42*7184805aSJagpal Singh Gill {
43*7184805aSJagpal Singh Gill   public:
TestEventEntry(sdbusplus::async::context & ctx,const char * path)44*7184805aSJagpal Singh Gill     TestEventEntry(sdbusplus::async::context& ctx, const char* path) :
45*7184805aSJagpal Singh Gill         EventEntryIntf(ctx, path)
46*7184805aSJagpal Singh Gill     {}
47*7184805aSJagpal Singh Gill 
method_call(get_entry_t)48*7184805aSJagpal Singh Gill     auto method_call(get_entry_t)
49*7184805aSJagpal Singh Gill         -> sdbusplus::async::task<get_entry_t::return_type>
50*7184805aSJagpal Singh Gill     {
51*7184805aSJagpal Singh Gill         get_entry_t::return_type fd1 = 0;
52*7184805aSJagpal Singh Gill         co_return fd1;
53*7184805aSJagpal Singh Gill     }
54*7184805aSJagpal Singh Gill };
55*7184805aSJagpal Singh Gill 
56*7184805aSJagpal Singh Gill // Test Event Server Class to mock the EventServer
57*7184805aSJagpal Singh Gill class TestEventServer : public EventServerIntf
58*7184805aSJagpal Singh Gill {
59*7184805aSJagpal Singh Gill   public:
TestEventServer(sdbusplus::async::context & ctx,const char * path)60*7184805aSJagpal Singh Gill     TestEventServer(sdbusplus::async::context& ctx, const char* path) :
61*7184805aSJagpal Singh Gill         EventServerIntf(ctx, path), ctx(ctx)
62*7184805aSJagpal Singh Gill     {}
63*7184805aSJagpal Singh Gill 
method_call(create_t,auto message,auto,auto)64*7184805aSJagpal Singh Gill     auto method_call(create_t, auto message, auto, auto)
65*7184805aSJagpal Singh Gill         -> sdbusplus::async::task<create_t::return_type>
66*7184805aSJagpal Singh Gill 
67*7184805aSJagpal Singh Gill     {
68*7184805aSJagpal Singh Gill         static int cnt = 1;
69*7184805aSJagpal Singh Gill         cnt++;
70*7184805aSJagpal Singh Gill 
71*7184805aSJagpal Singh Gill         // Append the count to the object path to make it unique for each event
72*7184805aSJagpal Singh Gill         std::string objectPath =
73*7184805aSJagpal Singh Gill             "/xyz/openbmc_project/logging/entry/TestEvent1" +
74*7184805aSJagpal Singh Gill             std::to_string(cnt);
75*7184805aSJagpal Singh Gill         EXPECT_EQ(message, expectedEvent) << "Event name mismatch";
76*7184805aSJagpal Singh Gill 
77*7184805aSJagpal Singh Gill         eventEntries.emplace_back(
78*7184805aSJagpal Singh Gill             std::make_unique<TestEventEntry>(ctx, objectPath.c_str()));
79*7184805aSJagpal Singh Gill 
80*7184805aSJagpal Singh Gill         co_return sdbusplus::message::object_path(objectPath);
81*7184805aSJagpal Singh Gill     }
82*7184805aSJagpal Singh Gill 
method_call(create_with_ffdc_files_t,auto,auto,auto,auto)83*7184805aSJagpal Singh Gill     auto method_call(create_with_ffdc_files_t, auto, auto, auto, auto)
84*7184805aSJagpal Singh Gill         -> sdbusplus::async::task<create_with_ffdc_files_t::return_type>
85*7184805aSJagpal Singh Gill 
86*7184805aSJagpal Singh Gill     {
87*7184805aSJagpal Singh Gill         co_return;
88*7184805aSJagpal Singh Gill     }
89*7184805aSJagpal Singh Gill 
90*7184805aSJagpal Singh Gill     std::string expectedEvent = "";
91*7184805aSJagpal Singh Gill 
92*7184805aSJagpal Singh Gill   private:
93*7184805aSJagpal Singh Gill     sdbusplus::async::context& ctx;
94*7184805aSJagpal Singh Gill     std::vector<std::unique_ptr<TestEventEntry>> eventEntries;
95*7184805aSJagpal Singh Gill };
96*7184805aSJagpal Singh Gill 
97*7184805aSJagpal Singh Gill class EventsTest : public ::testing::Test
98*7184805aSJagpal Singh Gill {
99*7184805aSJagpal Singh Gill   public:
100*7184805aSJagpal Singh Gill     enum class EventTestType
101*7184805aSJagpal Singh Gill     {
102*7184805aSJagpal Singh Gill         sensorWarningEvent,
103*7184805aSJagpal Singh Gill         sensorCriticalEvent,
104*7184805aSJagpal Singh Gill         sensorFailureEvent,
105*7184805aSJagpal Singh Gill         controllerFailureEvent,
106*7184805aSJagpal Singh Gill         powerFailureEvent,
107*7184805aSJagpal Singh Gill         leakWarningEvent,
108*7184805aSJagpal Singh Gill         leakCriticalEvent,
109*7184805aSJagpal Singh Gill     };
110*7184805aSJagpal Singh Gill 
111*7184805aSJagpal Singh Gill     static constexpr auto sensorObjectPath =
112*7184805aSJagpal Singh Gill         "/xyz/openbmc_project/sensors/OutletTemperature";
113*7184805aSJagpal Singh Gill     static constexpr auto serviceName = "xyz.openbmc_project.Logging";
114*7184805aSJagpal Singh Gill     static constexpr auto assert = true;
115*7184805aSJagpal Singh Gill     static constexpr auto deassert = false;
116*7184805aSJagpal Singh Gill     const char* objectPath = "/xyz/openbmc_project/logging";
117*7184805aSJagpal Singh Gill     sdbusplus::async::context ctx;
118*7184805aSJagpal Singh Gill     EventIntf::Events events;
119*7184805aSJagpal Singh Gill     TestEventServer eventServer;
120*7184805aSJagpal Singh Gill     sdbusplus::server::manager_t manager;
121*7184805aSJagpal Singh Gill 
EventsTest()122*7184805aSJagpal Singh Gill     EventsTest() :
123*7184805aSJagpal Singh Gill         events(ctx), eventServer(ctx, objectPath), manager(ctx, objectPath)
124*7184805aSJagpal Singh Gill     {
125*7184805aSJagpal Singh Gill         ctx.request_name(serviceName);
126*7184805aSJagpal Singh Gill     }
127*7184805aSJagpal Singh Gill 
~EventsTest()128*7184805aSJagpal Singh Gill     ~EventsTest() noexcept override {}
129*7184805aSJagpal Singh Gill 
testAssertEvent(EventTestType eventType)130*7184805aSJagpal Singh Gill     auto testAssertEvent(EventTestType eventType)
131*7184805aSJagpal Singh Gill         -> sdbusplus::async::task<void>
132*7184805aSJagpal Singh Gill     {
133*7184805aSJagpal Singh Gill         switch (eventType)
134*7184805aSJagpal Singh Gill         {
135*7184805aSJagpal Singh Gill             case EventTestType::sensorWarningEvent:
136*7184805aSJagpal Singh Gill                 eventServer.expectedEvent =
137*7184805aSJagpal Singh Gill                     SensorThresholdErrorIntf::ReadingWarning::errName;
138*7184805aSJagpal Singh Gill                 co_await events.generateSensorReadingEvent(
139*7184805aSJagpal Singh Gill                     sdbusplus::message::object_path(sensorObjectPath),
140*7184805aSJagpal Singh Gill                     EventIntf::EventLevel::warning, 60,
141*7184805aSJagpal Singh Gill                     EventIntf::SensorValueIntf::Unit::DegreesC, assert);
142*7184805aSJagpal Singh Gill                 break;
143*7184805aSJagpal Singh Gill             case EventTestType::sensorCriticalEvent:
144*7184805aSJagpal Singh Gill                 eventServer.expectedEvent =
145*7184805aSJagpal Singh Gill                     SensorThresholdErrorIntf::ReadingCritical::errName;
146*7184805aSJagpal Singh Gill                 co_await events.generateSensorReadingEvent(
147*7184805aSJagpal Singh Gill                     sdbusplus::message::object_path(sensorObjectPath),
148*7184805aSJagpal Singh Gill                     EventIntf::EventLevel::critical, 80,
149*7184805aSJagpal Singh Gill                     EventIntf::SensorValueIntf::Unit::DegreesC, assert);
150*7184805aSJagpal Singh Gill                 break;
151*7184805aSJagpal Singh Gill             case EventTestType::sensorFailureEvent:
152*7184805aSJagpal Singh Gill                 eventServer.expectedEvent =
153*7184805aSJagpal Singh Gill                     SensorErrorIntf::SensorFailure::errName;
154*7184805aSJagpal Singh Gill                 co_await events.generateSensorFailureEvent(
155*7184805aSJagpal Singh Gill                     sdbusplus::message::object_path(sensorObjectPath), assert);
156*7184805aSJagpal Singh Gill                 break;
157*7184805aSJagpal Singh Gill             case EventTestType::controllerFailureEvent:
158*7184805aSJagpal Singh Gill                 eventServer.expectedEvent =
159*7184805aSJagpal Singh Gill                     ControllerErrorIntf::SMCFailed::errName;
160*7184805aSJagpal Singh Gill                 co_await events.generateControllerFailureEvent(
161*7184805aSJagpal Singh Gill                     sdbusplus::message::object_path(sensorObjectPath), "",
162*7184805aSJagpal Singh Gill                     assert);
163*7184805aSJagpal Singh Gill                 break;
164*7184805aSJagpal Singh Gill             case EventTestType::powerFailureEvent:
165*7184805aSJagpal Singh Gill                 eventServer.expectedEvent =
166*7184805aSJagpal Singh Gill                     PowerErrorIntf::PowerRailFault::errName;
167*7184805aSJagpal Singh Gill                 co_await events.generatePowerFaultEvent(
168*7184805aSJagpal Singh Gill                     sdbusplus::message::object_path(sensorObjectPath), "",
169*7184805aSJagpal Singh Gill                     assert);
170*7184805aSJagpal Singh Gill                 break;
171*7184805aSJagpal Singh Gill             case EventTestType::leakWarningEvent:
172*7184805aSJagpal Singh Gill                 eventServer.expectedEvent =
173*7184805aSJagpal Singh Gill                     LeakErrorIntf::LeakDetectedWarning::errName;
174*7184805aSJagpal Singh Gill                 co_await events.generateLeakDetectedEvent(
175*7184805aSJagpal Singh Gill                     sdbusplus::message::object_path(sensorObjectPath),
176*7184805aSJagpal Singh Gill                     EventIntf::EventLevel::warning, assert);
177*7184805aSJagpal Singh Gill                 break;
178*7184805aSJagpal Singh Gill             case EventTestType::leakCriticalEvent:
179*7184805aSJagpal Singh Gill                 eventServer.expectedEvent =
180*7184805aSJagpal Singh Gill                     LeakErrorIntf::LeakDetectedCritical::errName;
181*7184805aSJagpal Singh Gill                 co_await events.generateLeakDetectedEvent(
182*7184805aSJagpal Singh Gill                     sdbusplus::message::object_path(sensorObjectPath),
183*7184805aSJagpal Singh Gill                     EventIntf::EventLevel::critical, assert);
184*7184805aSJagpal Singh Gill                 break;
185*7184805aSJagpal Singh Gill         }
186*7184805aSJagpal Singh Gill     }
187*7184805aSJagpal Singh Gill 
testDeassertEvent(EventTestType eventType)188*7184805aSJagpal Singh Gill     auto testDeassertEvent(EventTestType eventType)
189*7184805aSJagpal Singh Gill         -> sdbusplus::async::task<void>
190*7184805aSJagpal Singh Gill     {
191*7184805aSJagpal Singh Gill         switch (eventType)
192*7184805aSJagpal Singh Gill         {
193*7184805aSJagpal Singh Gill             case EventTestType::sensorWarningEvent:
194*7184805aSJagpal Singh Gill                 eventServer.expectedEvent =
195*7184805aSJagpal Singh Gill                     SensorThresholdEventIntf::SensorReadingNormalRange::errName;
196*7184805aSJagpal Singh Gill                 co_await events.generateSensorReadingEvent(
197*7184805aSJagpal Singh Gill                     sdbusplus::message::object_path(sensorObjectPath),
198*7184805aSJagpal Singh Gill                     EventIntf::EventLevel::warning, 40,
199*7184805aSJagpal Singh Gill                     EventIntf::SensorValueIntf::Unit::DegreesC, deassert);
200*7184805aSJagpal Singh Gill                 break;
201*7184805aSJagpal Singh Gill             case EventTestType::sensorCriticalEvent:
202*7184805aSJagpal Singh Gill                 eventServer.expectedEvent =
203*7184805aSJagpal Singh Gill                     SensorThresholdEventIntf::SensorReadingNormalRange::errName;
204*7184805aSJagpal Singh Gill                 co_await events.generateSensorReadingEvent(
205*7184805aSJagpal Singh Gill                     sdbusplus::message::object_path(sensorObjectPath),
206*7184805aSJagpal Singh Gill                     EventIntf::EventLevel::critical, 40,
207*7184805aSJagpal Singh Gill                     EventIntf::SensorValueIntf::Unit::DegreesC, deassert);
208*7184805aSJagpal Singh Gill                 break;
209*7184805aSJagpal Singh Gill             case EventTestType::sensorFailureEvent:
210*7184805aSJagpal Singh Gill                 eventServer.expectedEvent =
211*7184805aSJagpal Singh Gill                     SensorEventIntf::SensorRestored::errName;
212*7184805aSJagpal Singh Gill                 co_await events.generateSensorFailureEvent(
213*7184805aSJagpal Singh Gill                     sdbusplus::message::object_path(sensorObjectPath),
214*7184805aSJagpal Singh Gill                     deassert);
215*7184805aSJagpal Singh Gill                 break;
216*7184805aSJagpal Singh Gill             case EventTestType::controllerFailureEvent:
217*7184805aSJagpal Singh Gill                 eventServer.expectedEvent =
218*7184805aSJagpal Singh Gill                     ControllerEventIntf::SMCRestored::errName;
219*7184805aSJagpal Singh Gill                 co_await events.generateControllerFailureEvent(
220*7184805aSJagpal Singh Gill                     sdbusplus::message::object_path(sensorObjectPath), "",
221*7184805aSJagpal Singh Gill                     deassert);
222*7184805aSJagpal Singh Gill                 break;
223*7184805aSJagpal Singh Gill             case EventTestType::powerFailureEvent:
224*7184805aSJagpal Singh Gill                 eventServer.expectedEvent =
225*7184805aSJagpal Singh Gill                     PowerEventIntf::PowerRailFaultRecovered::errName;
226*7184805aSJagpal Singh Gill                 co_await events.generatePowerFaultEvent(
227*7184805aSJagpal Singh Gill                     sdbusplus::message::object_path(sensorObjectPath), "",
228*7184805aSJagpal Singh Gill                     deassert);
229*7184805aSJagpal Singh Gill                 break;
230*7184805aSJagpal Singh Gill             case EventTestType::leakWarningEvent:
231*7184805aSJagpal Singh Gill                 eventServer.expectedEvent =
232*7184805aSJagpal Singh Gill                     LeakEventIntf::LeakDetectedNormal::errName;
233*7184805aSJagpal Singh Gill                 co_await events.generateLeakDetectedEvent(
234*7184805aSJagpal Singh Gill                     sdbusplus::message::object_path(sensorObjectPath),
235*7184805aSJagpal Singh Gill                     EventIntf::EventLevel::warning, deassert);
236*7184805aSJagpal Singh Gill                 break;
237*7184805aSJagpal Singh Gill             case EventTestType::leakCriticalEvent:
238*7184805aSJagpal Singh Gill                 eventServer.expectedEvent =
239*7184805aSJagpal Singh Gill                     LeakEventIntf::LeakDetectedNormal::errName;
240*7184805aSJagpal Singh Gill                 co_await events.generateLeakDetectedEvent(
241*7184805aSJagpal Singh Gill                     sdbusplus::message::object_path(sensorObjectPath),
242*7184805aSJagpal Singh Gill                     EventIntf::EventLevel::critical, deassert);
243*7184805aSJagpal Singh Gill                 break;
244*7184805aSJagpal Singh Gill         }
245*7184805aSJagpal Singh Gill     }
246*7184805aSJagpal Singh Gill 
testEvents(EventTestType eventType)247*7184805aSJagpal Singh Gill     auto testEvents(EventTestType eventType) -> sdbusplus::async::task<void>
248*7184805aSJagpal Singh Gill     {
249*7184805aSJagpal Singh Gill         co_await testAssertEvent(eventType);
250*7184805aSJagpal Singh Gill 
251*7184805aSJagpal Singh Gill         co_await sdbusplus::async::sleep_for(ctx, 1s);
252*7184805aSJagpal Singh Gill 
253*7184805aSJagpal Singh Gill         co_await testDeassertEvent(eventType);
254*7184805aSJagpal Singh Gill 
255*7184805aSJagpal Singh Gill         ctx.request_stop();
256*7184805aSJagpal Singh Gill     }
257*7184805aSJagpal Singh Gill };
258*7184805aSJagpal Singh Gill 
TEST_F(EventsTest,TestEventsSensorWarning)259*7184805aSJagpal Singh Gill TEST_F(EventsTest, TestEventsSensorWarning)
260*7184805aSJagpal Singh Gill {
261*7184805aSJagpal Singh Gill     ctx.spawn(testEvents(EventTestType::sensorWarningEvent));
262*7184805aSJagpal Singh Gill     ctx.run();
263*7184805aSJagpal Singh Gill }
264*7184805aSJagpal Singh Gill 
TEST_F(EventsTest,TestEventsSensorCritical)265*7184805aSJagpal Singh Gill TEST_F(EventsTest, TestEventsSensorCritical)
266*7184805aSJagpal Singh Gill {
267*7184805aSJagpal Singh Gill     ctx.spawn(testEvents(EventTestType::sensorCriticalEvent));
268*7184805aSJagpal Singh Gill     ctx.run();
269*7184805aSJagpal Singh Gill }
270*7184805aSJagpal Singh Gill 
TEST_F(EventsTest,TestEventsSensorFailure)271*7184805aSJagpal Singh Gill TEST_F(EventsTest, TestEventsSensorFailure)
272*7184805aSJagpal Singh Gill {
273*7184805aSJagpal Singh Gill     ctx.spawn(testEvents(EventTestType::sensorFailureEvent));
274*7184805aSJagpal Singh Gill     ctx.run();
275*7184805aSJagpal Singh Gill }
276*7184805aSJagpal Singh Gill 
TEST_F(EventsTest,TestEventsControllerFailure)277*7184805aSJagpal Singh Gill TEST_F(EventsTest, TestEventsControllerFailure)
278*7184805aSJagpal Singh Gill {
279*7184805aSJagpal Singh Gill     ctx.spawn(testEvents(EventTestType::controllerFailureEvent));
280*7184805aSJagpal Singh Gill     ctx.run();
281*7184805aSJagpal Singh Gill }
282*7184805aSJagpal Singh Gill 
TEST_F(EventsTest,TestEventsPowerFailure)283*7184805aSJagpal Singh Gill TEST_F(EventsTest, TestEventsPowerFailure)
284*7184805aSJagpal Singh Gill {
285*7184805aSJagpal Singh Gill     ctx.spawn(testEvents(EventTestType::powerFailureEvent));
286*7184805aSJagpal Singh Gill     ctx.run();
287*7184805aSJagpal Singh Gill }
288*7184805aSJagpal Singh Gill 
TEST_F(EventsTest,TestEventsLeakWarning)289*7184805aSJagpal Singh Gill TEST_F(EventsTest, TestEventsLeakWarning)
290*7184805aSJagpal Singh Gill {
291*7184805aSJagpal Singh Gill     ctx.spawn(testEvents(EventTestType::leakWarningEvent));
292*7184805aSJagpal Singh Gill     ctx.run();
293*7184805aSJagpal Singh Gill }
294*7184805aSJagpal Singh Gill 
TEST_F(EventsTest,TestEventsLeakCritical)295*7184805aSJagpal Singh Gill TEST_F(EventsTest, TestEventsLeakCritical)
296*7184805aSJagpal Singh Gill {
297*7184805aSJagpal Singh Gill     ctx.spawn(testEvents(EventTestType::leakCriticalEvent));
298*7184805aSJagpal Singh Gill     ctx.run();
299*7184805aSJagpal Singh Gill }
300