regulators: Create services interface
Create an interface to system services like error logging and the
journal.
This interface is a container for a set of system services. It can be
passed as a single parameter to the rest of the application. It also
simplies automated testing.
Class hierarchy:
* Services: Abstract base class that defines the interface
* BMCServices: Implementation that uses standard BMC services
* MockServices: Implementation that uses mock services
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I4ac8696e2a9ef6521b369df532ea00bc483708c1
diff --git a/phosphor-regulators/src/services.hpp b/phosphor-regulators/src/services.hpp
new file mode 100644
index 0000000..1e38e7c
--- /dev/null
+++ b/phosphor-regulators/src/services.hpp
@@ -0,0 +1,129 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "error_logging.hpp"
+#include "journal.hpp"
+
+#include <sdbusplus/bus.hpp>
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * @class Services
+ *
+ * Abstract base class that provides an interface to system services like error
+ * logging and the journal.
+ *
+ * This interface is a container for a set of system services. It can be passed
+ * as a single parameter to the rest of the application.
+ */
+class Services
+{
+ public:
+ // Specify which compiler-generated methods we want
+ Services() = default;
+ Services(const Services&) = delete;
+ Services(Services&&) = delete;
+ Services& operator=(const Services&) = delete;
+ Services& operator=(Services&&) = delete;
+ virtual ~Services() = default;
+
+ /**
+ * Returns the D-Bus bus object.
+ *
+ * @return D-Bus bus
+ */
+ virtual sdbusplus::bus::bus& getBus() = 0;
+
+ /**
+ * Returns the error logging interface.
+ *
+ * @return error logging interface
+ */
+ virtual ErrorLogging& getErrorLogging() = 0;
+
+ /**
+ * Returns the journal interface.
+ *
+ * @return journal interface
+ */
+ virtual Journal& getJournal() = 0;
+};
+
+/**
+ * @class BMCServices
+ *
+ * Implementation of the Services interface using standard BMC system services.
+ */
+class BMCServices : public Services
+{
+ public:
+ // Specify which compiler-generated methods we want
+ BMCServices() = delete;
+ BMCServices(const BMCServices&) = delete;
+ BMCServices(BMCServices&&) = delete;
+ BMCServices& operator=(const BMCServices&) = delete;
+ BMCServices& operator=(BMCServices&&) = delete;
+ virtual ~BMCServices() = default;
+
+ /**
+ * Constructor.
+ *
+ * @param bus D-Bus bus object
+ */
+ explicit BMCServices(sdbusplus::bus::bus& bus) : bus{bus}, errorLogging{bus}
+ {
+ }
+
+ /** @copydoc Services::getBus() */
+ virtual sdbusplus::bus::bus& getBus() override
+ {
+ return bus;
+ }
+
+ /** @copydoc Services::getErrorLogging() */
+ virtual ErrorLogging& getErrorLogging() override
+ {
+ return errorLogging;
+ }
+
+ /** @copydoc Services::getJournal() */
+ virtual Journal& getJournal() override
+ {
+ return journal;
+ }
+
+ private:
+ /**
+ * D-Bus bus object.
+ */
+ sdbusplus::bus::bus& bus;
+
+ /**
+ * Implementation of the ErrorLogging interface using D-Bus method calls.
+ */
+ DBusErrorLogging errorLogging;
+
+ /**
+ * Implementation of the Journal interface that writes to the systemd
+ * journal.
+ */
+ SystemdJournal journal{};
+};
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index 8a1d059..8bf6a5c 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -56,6 +56,7 @@
dependencies: [
gmock,
gtest,
+ sdbusplus
],
link_args: dynamic_linker,
build_rpath: get_option('oe-sdk').enabled() ? rpath : '',
diff --git a/phosphor-regulators/test/mock_services.hpp b/phosphor-regulators/test/mock_services.hpp
new file mode 100644
index 0000000..58c565e
--- /dev/null
+++ b/phosphor-regulators/test/mock_services.hpp
@@ -0,0 +1,104 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "error_logging.hpp"
+#include "journal.hpp"
+#include "mock_error_logging.hpp"
+#include "mock_journal.hpp"
+
+#include <sdbusplus/bus.hpp>
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * @class MockServices
+ *
+ * Implementation of the Services interface using mock system services.
+ */
+class MockServices : public Services
+{
+ public:
+ // Specify which compiler-generated methods we want
+ MockServices() = default;
+ MockServices(const MockServices&) = delete;
+ MockServices(MockServices&&) = delete;
+ MockServices& operator=(const MockServices&) = delete;
+ MockServices& operator=(MockServices&&) = delete;
+ virtual ~MockServices() = default;
+
+ /** @copydoc Services::getBus() */
+ virtual sdbusplus::bus::bus& getBus() override
+ {
+ return bus;
+ }
+
+ /** @copydoc Services::getErrorLogging() */
+ virtual ErrorLogging& getErrorLogging() override
+ {
+ return errorLogging;
+ }
+
+ /** @copydoc Services::getJournal() */
+ virtual Journal& getJournal() override
+ {
+ return journal;
+ }
+
+ /**
+ * Returns the MockErrorLogging object that implements the ErrorLogging
+ * interface.
+ *
+ * This allows test cases to use the object in EXPECT_CALL() statements.
+ *
+ * @return mock error logging object
+ */
+ virtual MockErrorLogging& getMockErrorLogging() override
+ {
+ return errorLogging;
+ }
+
+ /**
+ * Returns the MockJournal object that implements the Journal interface.
+ *
+ * This allows test cases to use the object in EXPECT_CALL() statements.
+ *
+ * @return mock journal object
+ */
+ virtual MockJournal& getMockJournal() override
+ {
+ return journal;
+ }
+
+ private:
+ /**
+ * D-Bus bus object.
+ */
+ sdbusplus::bus::bus bus{sdbusplus::bus::new_default()};
+
+ /**
+ * Mock implementation of the ErrorLogging interface.
+ */
+ MockErrorLogging errorLogging{};
+
+ /**
+ * Mock implementation of the Journal interface.
+ */
+ MockJournal journal{};
+};
+
+} // namespace phosphor::power::regulators