xref: /openbmc/telemetry/tests/src/test_trigger.cpp (revision e8fc57514575f07565035db07cb522fe21988224)
1 #include "dbus_environment.hpp"
2 #include "helpers.hpp"
3 #include "mocks/trigger_manager_mock.hpp"
4 #include "params/trigger_params.hpp"
5 #include "trigger.hpp"
6 #include "utils/set_exception.hpp"
7 
8 using namespace testing;
9 using namespace std::literals::string_literals;
10 
11 class TestTrigger : public Test
12 {
13   public:
14     TriggerParams triggerParams;
15 
16     std::unique_ptr<TriggerManagerMock> triggerManagerMockPtr =
17         std::make_unique<NiceMock<TriggerManagerMock>>();
18     std::unique_ptr<Trigger> sut;
19 
20     void SetUp() override
21     {
22         sut = std::make_unique<Trigger>(
23             DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(),
24             triggerParams.name(), triggerParams.isDiscrete(),
25             triggerParams.logToJournal(), triggerParams.logToRedfish(),
26             triggerParams.updateReport(), triggerParams.sensors(),
27             triggerParams.reportNames(), triggerParams.thresholdParams(),
28             std::vector<std::shared_ptr<interfaces::Threshold>>{},
29             *triggerManagerMockPtr);
30     }
31 
32     template <class T>
33     static T getProperty(const std::string& path, const std::string& property)
34     {
35         std::promise<T> propertyPromise;
36         sdbusplus::asio::getProperty<T>(
37             *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path,
38             Trigger::triggerIfaceName, property,
39             [&propertyPromise](boost::system::error_code) {
40                 utils::setException(propertyPromise, "GetProperty failed");
41             },
42             [&propertyPromise](T t) { propertyPromise.set_value(t); });
43         return DbusEnvironment::waitForFuture(propertyPromise.get_future());
44     }
45 
46     boost::system::error_code deleteTrigger(const std::string& path)
47     {
48         std::promise<boost::system::error_code> methodPromise;
49         DbusEnvironment::getBus()->async_method_call(
50             [&methodPromise](boost::system::error_code ec) {
51                 methodPromise.set_value(ec);
52             },
53             DbusEnvironment::serviceName(), path, Trigger::deleteIfaceName,
54             "Delete");
55         return DbusEnvironment::waitForFuture(methodPromise.get_future());
56     }
57 };
58 
59 TEST_F(TestTrigger, checkIfPropertiesAreSet)
60 {
61     EXPECT_THAT(getProperty<bool>(sut->getPath(), "Discrete"),
62                 Eq(triggerParams.isDiscrete()));
63     EXPECT_THAT(getProperty<bool>(sut->getPath(), "LogToJournal"),
64                 Eq(triggerParams.logToJournal()));
65     EXPECT_THAT(getProperty<bool>(sut->getPath(), "LogToRedfish"),
66                 Eq(triggerParams.logToRedfish()));
67     EXPECT_THAT(getProperty<bool>(sut->getPath(), "UpdateReport"),
68                 Eq(triggerParams.updateReport()));
69     EXPECT_THAT((getProperty<std::vector<
70                      std::pair<sdbusplus::message::object_path, std::string>>>(
71                     sut->getPath(), "Sensors")),
72                 Eq(triggerParams.sensors()));
73     EXPECT_THAT(
74         getProperty<std::vector<std::string>>(sut->getPath(), "ReportNames"),
75         Eq(triggerParams.reportNames()));
76     EXPECT_THAT(
77         getProperty<TriggerThresholdParams>(sut->getPath(), "Thresholds"),
78         Eq(triggerParams.thresholdParams()));
79 }
80 
81 TEST_F(TestTrigger, deleteTrigger)
82 {
83     EXPECT_CALL(*triggerManagerMockPtr, removeTrigger(sut.get()));
84     auto ec = deleteTrigger(sut->getPath());
85     EXPECT_THAT(ec, Eq(boost::system::errc::success));
86 }
87 
88 TEST_F(TestTrigger, deletingNonExistingTriggerReturnInvalidRequestDescriptor)
89 {
90     auto ec = deleteTrigger(Trigger::triggerDir + "NonExisting"s);
91     EXPECT_THAT(ec.value(), Eq(EBADR));
92 }
93