test: sensor_manager: add test and sensor_mock
Adds a mock sensor for use in future tests, as well as
adds a unit-test to sensor_manager_unittest to check that the
type of a sensor added is not validated against anything.
Change-Id: I684ec71c02f427cfa7a414a6a4162beaba2269da
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/sensor_manager_unittest.cpp b/test/sensor_manager_unittest.cpp
index 17ccffa..b873886 100644
--- a/test/sensor_manager_unittest.cpp
+++ b/test/sensor_manager_unittest.cpp
@@ -4,6 +4,8 @@
#include <gtest/gtest.h>
#include <sdbusplus/test/sdbus_mock.hpp>
+#include "test/sensor_mock.hpp"
+
using ::testing::_;
using ::testing::IsNull;
using ::testing::Return;
@@ -26,3 +28,32 @@
SensorManager s(std::move(bus_mock_passive), std::move(bus_mock_host));
// Success
}
+
+TEST(SensorManagerTest, AddSensorInvalidTypeTest) {
+ // AddSensor doesn't validate the type of sensor you're adding, because
+ // ultimately it doesn't care -- but if we decide to change that this
+ // test will start failing :D
+
+ sdbusplus::SdBusMock sdbus_mock_passive, sdbus_mock_host;
+ auto bus_mock_passive = sdbusplus::get_mocked_new(&sdbus_mock_passive);
+ auto bus_mock_host = sdbusplus::get_mocked_new(&sdbus_mock_host);
+
+ EXPECT_CALL(sdbus_mock_host,
+ sd_bus_add_object_manager(
+ IsNull(),
+ _,
+ StrEq("/xyz/openbmc_project/extsensors")))
+ .WillOnce(Return(0));
+
+ SensorManager s(std::move(bus_mock_passive), std::move(bus_mock_host));
+
+ std::string name = "name";
+ std::string type = "invalid";
+ int64_t timeout = 1;
+ std::unique_ptr<Sensor> sensor =
+ std::make_unique<SensorMock>(name, timeout);
+ Sensor *sensor_ptr = sensor.get();
+
+ s.addSensor(type, name, std::move(sensor));
+ EXPECT_EQ(s.getSensor(name).get(), sensor_ptr);
+}
diff --git a/test/sensor_mock.hpp b/test/sensor_mock.hpp
new file mode 100644
index 0000000..2b63d02
--- /dev/null
+++ b/test/sensor_mock.hpp
@@ -0,0 +1,18 @@
+#pragma once
+
+#include <gmock/gmock.h>
+
+#include "interfaces.hpp"
+#include "sensors/sensor.hpp"
+
+class SensorMock : public Sensor
+{
+ public:
+ virtual ~SensorMock() = default;
+
+ SensorMock(const std::string& name, int64_t timeout)
+ : Sensor(name, timeout) {}
+
+ MOCK_METHOD0(read, ReadReturn());
+ MOCK_METHOD1(write, void(double));
+};