test: sensors: host
Tests for sensors/host
Change-Id: I760825c666c711d6f9c394ceefe35f8151383785
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index c6b3375..4d5ca63 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -12,7 +12,8 @@
$(PHOSPHOR_DBUS_INTERFACES_LIBS)
# Run all 'check' test programs
-check_PROGRAMS = sensor_manager_unittest sensor_pluggable_unittest
+check_PROGRAMS = sensor_manager_unittest sensor_pluggable_unittest \
+ sensor_host_unittest
TESTS = $(check_PROGRAMS)
# Until libconfig is mocked out or replaced, include it.
@@ -21,3 +22,6 @@
sensor_pluggable_unittest_SOURCES = sensor_pluggable_unittest.cpp
sensor_pluggable_unittest_LDADD = $(top_builddir)/sensors/pluggable.o
+
+sensor_host_unittest_SOURCES = sensor_host_unittest.cpp
+sensor_host_unittest_LDADD = $(top_builddir)/sensors/host.o
diff --git a/test/helpers.hpp b/test/helpers.hpp
new file mode 100644
index 0000000..931a39e
--- /dev/null
+++ b/test/helpers.hpp
@@ -0,0 +1,77 @@
+// THIS EXISTS AS A COPY OF SDBUSPLUS/TEST/HELPERS.HPP until that is merged.
+#pragma once
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <sdbusplus/test/sdbus_mock.hpp>
+#include <string>
+#include <vector>
+
+using ::testing::Invoke;
+using ::testing::IsNull;
+using ::testing::NotNull;
+using ::testing::Return;
+using ::testing::StrEq;
+using ::testing::_;
+
+/** @brief Setup the expectations for sdbus-based object creation.
+ *
+ * Objects created that inherit a composition from sdbusplus will all
+ * require at least these expectations.
+ *
+ * If you have future sd_bus_emit_properties_changed_strv calls expected,
+ * you'll need to add those calls into your test. This only captures the
+ * property updates you tell it to expect initially.
+ *
+ * TODO: Make it support more cases, as I'm sure there are more.
+ *
+ * @param[in] sdbus_mock - Pointer to your sdbus mock interface used with
+ * the sdbusplus::bus::bus you created.
+ * @param[in] defer - Whether object announcement is deferred.
+ * @param[in] path - the dbus path passed to the object
+ * @param[in] intf - the dbus interface
+ * @param[in] properties - an ordered list of expected property updates.
+ * @param[in] index - a pointer to a valid integer in a surviving scope.
+ */
+void SetupDbusObject(sdbusplus::SdBusMock *sdbus_mock, bool defer,
+ const std::string &path, const std::string &intf,
+ const std::vector<std::string> &properties, int *index)
+{
+ EXPECT_CALL(*sdbus_mock,
+ sd_bus_add_object_vtable(IsNull(), NotNull(), StrEq(path),
+ StrEq(intf), NotNull(), NotNull()))
+ .WillOnce(Return(0));
+
+ if (!defer)
+ {
+ EXPECT_CALL(*sdbus_mock,
+ sd_bus_emit_object_added(IsNull(), StrEq(path)))
+ .WillOnce(Return(0));
+ }
+
+ if (properties.empty())
+ {
+ // We always expect one, but in this case we're not concerned with the
+ // output. If there is more than one property update, we should care
+ // and the test writer can add the code to trigger the below case.
+ EXPECT_CALL(*sdbus_mock,
+ sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
+ StrEq(intf), NotNull()))
+ .WillOnce(Return(0));
+ }
+ else
+ {
+ (*index) = 0;
+ EXPECT_CALL(*sdbus_mock,
+ sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
+ StrEq(intf), NotNull()))
+ .Times(properties.size())
+ .WillRepeatedly(Invoke([=](sd_bus *bus, const char *path,
+ const char *interface, char **names) {
+ EXPECT_STREQ(properties[(*index)++].c_str(), names[0]);
+ return 0;
+ }));
+ }
+
+ return;
+}
diff --git a/test/sensor_host_unittest.cpp b/test/sensor_host_unittest.cpp
new file mode 100644
index 0000000..5e8af4b
--- /dev/null
+++ b/test/sensor_host_unittest.cpp
@@ -0,0 +1,129 @@
+#include "sensors/host.hpp"
+
+#include <chrono>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <memory>
+#include <sdbusplus/test/sdbus_mock.hpp>
+#include <string>
+#include <vector>
+
+#include "test/helpers.hpp"
+
+using ::testing::IsNull;
+using ::testing::Return;
+using ::testing::StrEq;
+
+TEST(HostSensorTest, BoringConstructorTest) {
+ // WARN: The host sensor is not presently meant to be created this way,
+ // TODO: Can I move the constructor into private?
+}
+
+TEST(HostSensorTest, CreateHostTempSensorTest) {
+ // The normal case for this sensor is to be a temperature sensor, where
+ // the value is treated as a margin sensor.
+
+ sdbusplus::SdBusMock sdbus_mock;
+ auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
+ std::string name = "fleeting0";
+ int64_t timeout = 1;
+ const char *objPath = "/asdf/asdf0";
+ bool defer = false;
+ std::string interface = "xyz.openbmc_project.Sensor.Value";
+
+ // Scale is the only property we change in the code. Also,
+ // emit_object_added isn't called twice.
+ // Temperature is the default for type, and everything, so those aren't
+ // updated.
+ std::vector<std::string> properties = {"Scale"};
+ int i;
+
+ // The CreateTemp updates all the properties, however, only Scale is set
+ // to non-default.
+ SetupDbusObject(
+ &sdbus_mock,
+ defer,
+ objPath,
+ interface,
+ properties,
+ &i);
+
+ // This is called during object destruction.
+ EXPECT_CALL(sdbus_mock,
+ sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
+ .WillOnce(Return(0));
+
+ std::unique_ptr<Sensor> s = HostSensor::CreateTemp(
+ name, timeout, bus_mock, objPath, defer);
+}
+
+TEST(HostSensorTest, VerifyWriteThenReadMatches) {
+ // Verify that when value is updated, the information matches
+ // what we expect when read back.
+
+ sdbusplus::SdBusMock sdbus_mock;
+ auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
+ std::string name = "fleeting0";
+ int64_t timeout = 1;
+ const char *objPath = "/asdf/asdf0";
+ bool defer = false;
+ std::string interface = "xyz.openbmc_project.Sensor.Value";
+
+ // Scale is the only property we change in the code. Also,
+ // emit_object_added isn't called twice.
+ // Temperature is the default for type, and everything, so those aren't
+ // updated.
+ std::vector<std::string> properties = {"Scale"};
+ int i;
+
+ SetupDbusObject(
+ &sdbus_mock,
+ defer,
+ objPath,
+ interface,
+ properties,
+ &i);
+
+ EXPECT_CALL(sdbus_mock,
+ sd_bus_emit_object_removed(IsNull(), StrEq(objPath)))
+ .WillOnce(Return(0));
+
+ std::unique_ptr<Sensor> s = HostSensor::CreateTemp(
+ name, timeout, bus_mock, objPath, defer);
+
+ // Value is updated from dbus calls only (normally).
+ HostSensor *hs = static_cast<HostSensor *>(s.get());
+ int64_t new_value = 2;
+
+ ReadReturn r = hs->read();
+ EXPECT_EQ(r.value, 0);
+
+ EXPECT_CALL(sdbus_mock,
+ sd_bus_emit_properties_changed_strv(
+ IsNull(),
+ StrEq(objPath),
+ StrEq(interface),
+ NotNull()))
+ .WillOnce(
+ Invoke([=](sd_bus *bus,
+ const char *path,
+ const char *interface,
+ char **names) {
+ EXPECT_STREQ("Value", names[0]);
+ return 0;
+ })
+ );
+
+ std::chrono::high_resolution_clock::time_point t1 =
+ std::chrono::high_resolution_clock::now();
+
+ hs->value(new_value);
+ r = hs->read();
+ EXPECT_EQ(r.value, new_value * 0.001);
+
+ auto duration = std::chrono::duration_cast<std::chrono::seconds>(
+ t1 - r.updated).count();
+
+ // Verify it was updated within the last second.
+ EXPECT_TRUE(duration < 1);
+}