Add TriggerManager and Trigger objects
Implemented initial version of Trigger and TriggerManager DBus
interfaces. Now DBus user is able to add new Trigger and delete it.
There is no support for other functionality added.
Tested:
- Built using yocto dependencies with success
- Unit tests passed
- Verified manually if Trigger and TriggerManager works as
expected
Change-Id: Ie68463526ecccc3be67cc7bfaaf9a9aa7326dee6
Signed-off-by: Wludzik, Jozef <jozef.wludzik@intel.com>
diff --git a/tests/src/mocks/report_mock.hpp b/tests/src/mocks/report_mock.hpp
index 2f6ffac..f3eafba 100644
--- a/tests/src/mocks/report_mock.hpp
+++ b/tests/src/mocks/report_mock.hpp
@@ -7,17 +7,12 @@
class ReportMock : public interfaces::Report
{
public:
- ReportMock(std::string reportName)
+ ReportMock(std::string name)
{
using namespace testing;
- ON_CALL(*this, getName).WillByDefault([reportName] {
- return reportName;
- });
- ON_CALL(*this, getPath).WillByDefault([reportName] {
- return "/" + reportName;
- });
-
+ ON_CALL(*this, getName).WillByDefault([name] { return name; });
+ ON_CALL(*this, getPath).WillByDefault([name] { return "/" + name; });
EXPECT_CALL(*this, Die).Times(AnyNumber());
}
diff --git a/tests/src/mocks/trigger_factory_mock.hpp b/tests/src/mocks/trigger_factory_mock.hpp
new file mode 100644
index 0000000..b714a44
--- /dev/null
+++ b/tests/src/mocks/trigger_factory_mock.hpp
@@ -0,0 +1,51 @@
+#pragma once
+
+#include "interfaces/trigger_factory.hpp"
+#include "mocks/trigger_mock.hpp"
+#include "params/trigger_params.hpp"
+
+#include <gmock/gmock.h>
+
+class TriggerFactoryMock : public interfaces::TriggerFactory
+{
+ public:
+ TriggerFactoryMock()
+ {
+ using namespace testing;
+
+ ON_CALL(*this, make(_, _, _, _, _, _, _, _, _))
+ .WillByDefault(WithArgs<0>(Invoke([](const std::string& name) {
+ return std::make_unique<NiceMock<TriggerMock>>(name);
+ })));
+ }
+
+ MOCK_METHOD(std::unique_ptr<interfaces::Trigger>, make,
+ (const std::string& name, bool isDiscrete, bool logToJournal,
+ bool logToRedfish, bool updateReport,
+ (const std::vector<std::pair<sdbusplus::message::object_path,
+ std::string>>& sensors),
+ const std::vector<std::string>& reportNames,
+ const TriggerThresholdParams& thresholds,
+ interfaces::TriggerManager& triggerManager),
+ (const, override));
+
+ auto& expectMake(
+ std::optional<std::reference_wrapper<const TriggerParams>> paramsOpt,
+ const testing::Matcher<interfaces::TriggerManager&>& tm)
+ {
+ if (paramsOpt)
+ {
+ const TriggerParams& params = *paramsOpt;
+ return EXPECT_CALL(
+ *this, make(params.name(), params.isDiscrete(),
+ params.logToJournal(), params.logToRedfish(),
+ params.updateReport(), params.sensors(),
+ params.reportNames(), params.thresholds(), tm));
+ }
+ else
+ {
+ using testing::_;
+ return EXPECT_CALL(*this, make(_, _, _, _, _, _, _, _, tm));
+ }
+ }
+};
diff --git a/tests/src/mocks/trigger_manager_mock.hpp b/tests/src/mocks/trigger_manager_mock.hpp
new file mode 100644
index 0000000..9377d2a
--- /dev/null
+++ b/tests/src/mocks/trigger_manager_mock.hpp
@@ -0,0 +1,12 @@
+#pragma once
+
+#include "interfaces/trigger_manager.hpp"
+
+#include <gmock/gmock.h>
+
+class TriggerManagerMock : public interfaces::TriggerManager
+{
+ public:
+ MOCK_METHOD(void, removeTrigger, (const interfaces::Trigger* trigger),
+ (override));
+};
diff --git a/tests/src/mocks/trigger_mock.hpp b/tests/src/mocks/trigger_mock.hpp
new file mode 100644
index 0000000..d8eddd2
--- /dev/null
+++ b/tests/src/mocks/trigger_mock.hpp
@@ -0,0 +1,27 @@
+#pragma once
+
+#include "interfaces/trigger.hpp"
+
+#include <gmock/gmock.h>
+
+class TriggerMock : public interfaces::Trigger
+{
+ public:
+ TriggerMock(std::string name)
+ {
+ using namespace testing;
+
+ ON_CALL(*this, getName).WillByDefault([name] { return name; });
+ ON_CALL(*this, getPath).WillByDefault([name] { return "/" + name; });
+ EXPECT_CALL(*this, Die).Times(AnyNumber());
+ }
+
+ virtual ~TriggerMock()
+ {
+ Die();
+ }
+
+ MOCK_METHOD(std::string, getName, (), (const, override));
+ MOCK_METHOD(std::string, getPath, (), (const, override));
+ MOCK_METHOD(void, Die, ());
+};
diff --git a/tests/src/params/trigger_params.hpp b/tests/src/params/trigger_params.hpp
new file mode 100644
index 0000000..340f9b3
--- /dev/null
+++ b/tests/src/params/trigger_params.hpp
@@ -0,0 +1,67 @@
+#pragma once
+
+#include "interfaces/trigger_types.hpp"
+
+#include <utility>
+
+class TriggerParams
+{
+ public:
+ TriggerParams& name(std::string val)
+ {
+ nameProperty = std::move(val);
+ return *this;
+ }
+
+ const std::string& name() const
+ {
+ return nameProperty;
+ }
+
+ bool isDiscrete() const
+ {
+ return discreteProperty;
+ }
+
+ bool logToJournal() const
+ {
+ return logToJournalProperty;
+ }
+
+ bool logToRedfish() const
+ {
+ return logToRedfishProperty;
+ }
+
+ bool updateReport() const
+ {
+ return updateReportProperty;
+ }
+
+ const std::vector<std::pair<sdbusplus::message::object_path, std::string>>&
+ sensors() const
+ {
+ return sensorsProperty;
+ }
+
+ const std::vector<std::string>& reportNames() const
+ {
+ return reportNamesProperty;
+ }
+
+ const TriggerThresholdParams& thresholds() const
+ {
+ return thresholdsProperty;
+ }
+
+ private:
+ std::string nameProperty = "Trigger1";
+ bool discreteProperty = false;
+ bool logToJournalProperty = false;
+ bool logToRedfishProperty = false;
+ bool updateReportProperty = false;
+ std::vector<std::pair<sdbusplus::message::object_path, std::string>>
+ sensorsProperty = {};
+ std::vector<std::string> reportNamesProperty = {};
+ TriggerThresholdParams thresholdsProperty = {};
+};
diff --git a/tests/src/test_trigger.cpp b/tests/src/test_trigger.cpp
new file mode 100644
index 0000000..0f2d752
--- /dev/null
+++ b/tests/src/test_trigger.cpp
@@ -0,0 +1,91 @@
+#include "dbus_environment.hpp"
+#include "helpers.hpp"
+#include "mocks/trigger_manager_mock.hpp"
+#include "params/trigger_params.hpp"
+#include "trigger.hpp"
+#include "utils/set_exception.hpp"
+
+using namespace testing;
+using namespace std::literals::string_literals;
+
+class TestTrigger : public Test
+{
+ public:
+ TriggerParams triggerParams;
+
+ std::unique_ptr<TriggerManagerMock> triggerManagerMockPtr =
+ std::make_unique<NiceMock<TriggerManagerMock>>();
+ std::unique_ptr<Trigger> sut;
+
+ void SetUp() override
+ {
+ sut = std::make_unique<Trigger>(
+ DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(),
+ triggerParams.name(), triggerParams.isDiscrete(),
+ triggerParams.logToJournal(), triggerParams.logToRedfish(),
+ triggerParams.updateReport(), triggerParams.sensors(),
+ triggerParams.reportNames(), triggerParams.thresholds(),
+ *triggerManagerMockPtr);
+ }
+
+ template <class T>
+ static T getProperty(const std::string& path, const std::string& property)
+ {
+ std::promise<T> propertyPromise;
+ sdbusplus::asio::getProperty<T>(
+ *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path,
+ Trigger::triggerIfaceName, property,
+ [&propertyPromise](boost::system::error_code) {
+ utils::setException(propertyPromise, "GetProperty failed");
+ },
+ [&propertyPromise](T t) { propertyPromise.set_value(t); });
+ return DbusEnvironment::waitForFuture(propertyPromise.get_future());
+ }
+
+ boost::system::error_code deleteTrigger(const std::string& path)
+ {
+ std::promise<boost::system::error_code> methodPromise;
+ DbusEnvironment::getBus()->async_method_call(
+ [&methodPromise](boost::system::error_code ec) {
+ methodPromise.set_value(ec);
+ },
+ DbusEnvironment::serviceName(), path, Trigger::deleteIfaceName,
+ "Delete");
+ return DbusEnvironment::waitForFuture(methodPromise.get_future());
+ }
+};
+
+TEST_F(TestTrigger, checkIfPropertiesAreSet)
+{
+ EXPECT_THAT(getProperty<bool>(sut->getPath(), "Discrete"),
+ Eq(triggerParams.isDiscrete()));
+ EXPECT_THAT(getProperty<bool>(sut->getPath(), "LogToJournal"),
+ Eq(triggerParams.logToJournal()));
+ EXPECT_THAT(getProperty<bool>(sut->getPath(), "LogToRedfish"),
+ Eq(triggerParams.logToRedfish()));
+ EXPECT_THAT(getProperty<bool>(sut->getPath(), "UpdateReport"),
+ Eq(triggerParams.updateReport()));
+ EXPECT_THAT((getProperty<std::vector<
+ std::pair<sdbusplus::message::object_path, std::string>>>(
+ sut->getPath(), "Sensors")),
+ Eq(triggerParams.sensors()));
+ EXPECT_THAT(
+ getProperty<std::vector<std::string>>(sut->getPath(), "ReportNames"),
+ Eq(triggerParams.reportNames()));
+ EXPECT_THAT(
+ getProperty<TriggerThresholdParams>(sut->getPath(), "Thresholds"),
+ Eq(triggerParams.thresholds()));
+}
+
+TEST_F(TestTrigger, deleteTrigger)
+{
+ EXPECT_CALL(*triggerManagerMockPtr, removeTrigger(sut.get()));
+ auto ec = deleteTrigger(sut->getPath());
+ EXPECT_THAT(ec, Eq(boost::system::errc::success));
+}
+
+TEST_F(TestTrigger, deletingNonExistingTriggerReturnInvalidRequestDescriptor)
+{
+ auto ec = deleteTrigger(Trigger::triggerDir + "NonExisting"s);
+ EXPECT_THAT(ec.value(), Eq(EBADR));
+}
diff --git a/tests/src/test_trigger_manager.cpp b/tests/src/test_trigger_manager.cpp
new file mode 100644
index 0000000..71314fa
--- /dev/null
+++ b/tests/src/test_trigger_manager.cpp
@@ -0,0 +1,127 @@
+#include "dbus_environment.hpp"
+#include "helpers.hpp"
+#include "mocks/trigger_factory_mock.hpp"
+#include "mocks/trigger_mock.hpp"
+#include "params/trigger_params.hpp"
+#include "trigger_manager.hpp"
+
+using namespace testing;
+
+class TestTriggerManager : public Test
+{
+ public:
+ std::pair<boost::system::error_code, std::string>
+ addTrigger(const TriggerParams& params)
+ {
+ std::promise<std::pair<boost::system::error_code, std::string>>
+ addTriggerPromise;
+ DbusEnvironment::getBus()->async_method_call(
+ [&addTriggerPromise](boost::system::error_code ec,
+ const std::string& path) {
+ addTriggerPromise.set_value({ec, path});
+ },
+ DbusEnvironment::serviceName(), TriggerManager::triggerManagerPath,
+ TriggerManager::triggerManagerIfaceName, "AddTrigger",
+ params.name(), params.isDiscrete(), params.logToJournal(),
+ params.logToRedfish(), params.updateReport(), params.sensors(),
+ params.reportNames(), params.thresholds());
+ return DbusEnvironment::waitForFuture(addTriggerPromise.get_future());
+ }
+
+ TriggerParams triggerParams;
+ std::unique_ptr<TriggerFactoryMock> triggerFactoryMockPtr =
+ std::make_unique<NiceMock<TriggerFactoryMock>>();
+ TriggerFactoryMock& triggerFactoryMock = *triggerFactoryMockPtr;
+ std::unique_ptr<TriggerMock> triggerMockPtr =
+ std::make_unique<NiceMock<TriggerMock>>(triggerParams.name());
+ TriggerMock& triggerMock = *triggerMockPtr;
+ std::unique_ptr<TriggerManager> sut = std::make_unique<TriggerManager>(
+ std::move(triggerFactoryMockPtr),
+ std::move(DbusEnvironment::getObjServer()));
+ MockFunction<void(std::string)> checkPoint;
+};
+
+TEST_F(TestTriggerManager, addTrigger)
+{
+ triggerFactoryMock.expectMake(triggerParams, Ref(*sut))
+ .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
+
+ auto [ec, path] = addTrigger(triggerParams);
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
+ EXPECT_THAT(path, Eq(triggerMock.getPath()));
+}
+
+TEST_F(TestTriggerManager, failToAddTriggerTwice)
+{
+ triggerFactoryMock.expectMake(triggerParams, Ref(*sut))
+ .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
+
+ addTrigger(triggerParams);
+
+ auto [ec, path] = addTrigger(triggerParams);
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists));
+ EXPECT_THAT(path, Eq(std::string()));
+}
+
+TEST_F(TestTriggerManager, failToAddTriggerWhenMaxTriggerIsReached)
+{
+ triggerFactoryMock.expectMake(std::nullopt, Ref(*sut))
+ .Times(TriggerManager::maxTriggers);
+
+ for (size_t i = 0; i < TriggerManager::maxTriggers; i++)
+ {
+ triggerParams.name(triggerParams.name() + std::to_string(i));
+
+ auto [ec, path] = addTrigger(triggerParams);
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
+ }
+
+ triggerParams.name(triggerParams.name() +
+ std::to_string(TriggerManager::maxTriggers));
+ auto [ec, path] = addTrigger(triggerParams);
+ EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
+ EXPECT_THAT(path, Eq(std::string()));
+}
+
+TEST_F(TestTriggerManager, removeTrigger)
+{
+ {
+ InSequence seq;
+ triggerFactoryMock.expectMake(triggerParams, Ref(*sut))
+ .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
+ EXPECT_CALL(triggerMock, Die());
+ EXPECT_CALL(checkPoint, Call("end"));
+ }
+
+ addTrigger(triggerParams);
+ sut->removeTrigger(&triggerMock);
+ checkPoint.Call("end");
+}
+
+TEST_F(TestTriggerManager, removingTriggerThatIsNotInContainerHasNoEffect)
+{
+ {
+ InSequence seq;
+ EXPECT_CALL(checkPoint, Call("end"));
+ EXPECT_CALL(triggerMock, Die());
+ }
+
+ sut->removeTrigger(&triggerMock);
+ checkPoint.Call("end");
+}
+
+TEST_F(TestTriggerManager, removingSameTriggerTwiceHasNoSideEffect)
+{
+ {
+ InSequence seq;
+ triggerFactoryMock.expectMake(triggerParams, Ref(*sut))
+ .WillOnce(Return(ByMove(std::move(triggerMockPtr))));
+ EXPECT_CALL(triggerMock, Die());
+ EXPECT_CALL(checkPoint, Call("end"));
+ }
+
+ addTrigger(triggerParams);
+ sut->removeTrigger(&triggerMock);
+ sut->removeTrigger(&triggerMock);
+ checkPoint.Call("end");
+}