enable unit-tests: enable for SensorManager
Enabled unit-tests in general for the project, and more
specifically started with a benign construction test for
the SensorManager object.
Tested: Verified continues to build and link, and unit-test
passes.
Tested: Ran on quanta-q71l board and it behaved as expected.
Change-Id: I4ad9a0c57efd0b9ccc37d26faa0cc1b82026b8d7
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/main.cpp b/main.cpp
index 6dbd7d6..a5758fe 100644
--- a/main.cpp
+++ b/main.cpp
@@ -85,7 +85,7 @@
}
auto ModeControlBus = sdbusplus::bus::new_default();
- std::shared_ptr<SensorManager> mgmr;
+ SensorManager mgmr;
std::map<int64_t, std::shared_ptr<PIDZone>> zones;
// Create a manager for the ModeBus because we own it.
@@ -126,8 +126,8 @@
* it.
*/
- auto& HostSensorBus = mgmr->getHostBus();
- auto& PassiveListeningBus = mgmr->getPassiveBus();
+ auto& HostSensorBus = mgmr.getHostBus();
+ auto& PassiveListeningBus = mgmr.getPassiveBus();
std::cerr << "Starting threads\n";
diff --git a/pid/zone.cpp b/pid/zone.cpp
index 393f6e6..ed8cd0f 100644
--- a/pid/zone.cpp
+++ b/pid/zone.cpp
@@ -211,7 +211,7 @@
for (auto& f : _fanInputs)
{
- auto& sensor = _mgr->getSensor(f);
+ auto& sensor = _mgr.getSensor(f);
ReadReturn r = sensor->read();
_cachedValuesByName[f] = r.value;
@@ -243,7 +243,7 @@
for (auto& t : _thermalInputs)
{
- auto& sensor = _mgr->getSensor(t);
+ auto& sensor = _mgr.getSensor(t);
ReadReturn r = sensor->read();
int64_t timeout = sensor->GetTimeout();
@@ -319,9 +319,9 @@
}
}
-std::unique_ptr<Sensor>& PIDZone::getSensor(std::string name)
+const std::unique_ptr<Sensor>& PIDZone::getSensor(std::string name)
{
- return _mgr->getSensor(name);
+ return _mgr.getSensor(name);
}
bool PIDZone::manual(bool value)
@@ -344,7 +344,7 @@
std::map<int64_t, std::shared_ptr<PIDZone>> BuildZones(
std::map<int64_t, PIDConf>& ZonePids,
std::map<int64_t, struct zone>& ZoneConfigs,
- std::shared_ptr<SensorManager> mgr,
+ SensorManager& mgr,
sdbusplus::bus::bus& ModeControlBus)
{
std::map<int64_t, std::shared_ptr<PIDZone>> zones;
@@ -446,7 +446,7 @@
std::map<int64_t, std::shared_ptr<PIDZone>> BuildZonesFromConfig(
std::string& path,
- std::shared_ptr<SensorManager> mgr,
+ SensorManager& mgr,
sdbusplus::bus::bus& ModeControlBus)
{
using namespace libconfig;
diff --git a/pid/zone.hpp b/pid/zone.hpp
index c7b52c7..1cf517c 100644
--- a/pid/zone.hpp
+++ b/pid/zone.hpp
@@ -34,7 +34,7 @@
PIDZone(int64_t zone,
float minThermalRpm,
float failSafePercent,
- std::shared_ptr<SensorManager> mgr,
+ const SensorManager& mgr,
sdbusplus::bus::bus& bus,
const char* objPath,
bool defer)
@@ -64,7 +64,7 @@
float getFailSafePercent(void) const;
float getMinThermalRpmSetPt(void) const;
- std::unique_ptr<Sensor>& getSensor(std::string name);
+ const std::unique_ptr<Sensor>& getSensor(std::string name);
void determineMaxRPMRequest(void);
void updateFanTelemetry(void);
void updateSensors(void);
@@ -106,7 +106,7 @@
std::vector<std::string> _fanInputs;
std::vector<std::string> _thermalInputs;
std::map<std::string, double> _cachedValuesByName;
- std::shared_ptr<SensorManager> _mgr;
+ const SensorManager& _mgr;
std::vector<std::unique_ptr<PIDController>> _fans;
std::vector<std::unique_ptr<PIDController>> _thermals;
@@ -115,10 +115,10 @@
std::map<int64_t, std::shared_ptr<PIDZone>> BuildZones(
std::map<int64_t, PIDConf>& ZonePids,
std::map<int64_t, struct zone>& ZoneConfigs,
- std::shared_ptr<SensorManager> mgmr,
+ SensorManager& mgmr,
sdbusplus::bus::bus& ModeControlBus);
std::map<int64_t, std::shared_ptr<PIDZone>> BuildZonesFromConfig(
std::string& path,
- std::shared_ptr<SensorManager> mgmr,
+ SensorManager& mgmr,
sdbusplus::bus::bus& ModeControlBus);
diff --git a/sensors/builder.cpp b/sensors/builder.cpp
index 2786141..4654e51 100644
--- a/sensors/builder.cpp
+++ b/sensors/builder.cpp
@@ -15,6 +15,8 @@
*/
#include <iostream>
+#include <map>
+#include <string>
/* Configuration. */
#include "conf.hpp"
@@ -33,12 +35,12 @@
static constexpr bool deferSignals = true;
-std::shared_ptr<SensorManager> BuildSensors(
+SensorManager BuildSensors(
const std::map<std::string, struct sensor>& config)
{
- auto mgmr = std::make_shared<SensorManager>();
- auto& HostSensorBus = mgmr->getHostBus();
- auto& PassiveListeningBus = mgmr->getPassiveBus();
+ SensorManager mgmr;
+ auto& HostSensorBus = mgmr.getHostBus();
+ auto& PassiveListeningBus = mgmr.getPassiveBus();
for (auto& it : config)
{
@@ -110,7 +112,7 @@
info->timeout,
std::move(ri),
std::move(wi));
- mgmr->addSensor(info->type, name, std::move(sensor));
+ mgmr.addSensor(info->type, name, std::move(sensor));
}
else if (info->type == "temp" || info->type == "margin")
{
@@ -133,7 +135,7 @@
HostSensorBus,
info->readpath.c_str(),
deferSignals);
- mgmr->addSensor(info->type, name, std::move(sensor));
+ mgmr.addSensor(info->type, name, std::move(sensor));
}
else
{
@@ -143,7 +145,7 @@
info->timeout,
std::move(ri),
std::move(wi));
- mgmr->addSensor(info->type, name, std::move(sensor));
+ mgmr.addSensor(info->type, name, std::move(sensor));
}
}
}
diff --git a/sensors/builder.hpp b/sensors/builder.hpp
index 2849c71..edb30c3 100644
--- a/sensors/builder.hpp
+++ b/sensors/builder.hpp
@@ -1,7 +1,6 @@
#pragma once
#include <map>
-#include <memory>
#include <string>
#include "sensors/manager.hpp"
@@ -10,6 +9,6 @@
/**
* Build the sensors and associate them with a SensorManager.
*/
-std::shared_ptr<SensorManager> BuildSensors(
+SensorManager BuildSensors(
const std::map<std::string, struct sensor>& config);
diff --git a/sensors/builderconfig.cpp b/sensors/builderconfig.cpp
index b3507ae..f1d95a0 100644
--- a/sensors/builderconfig.cpp
+++ b/sensors/builderconfig.cpp
@@ -16,6 +16,8 @@
#include <iostream>
#include <libconfig.h++>
+#include <string>
+#include <unordered_map>
/* Configuration. */
#include "conf.hpp"
@@ -28,7 +30,7 @@
* parsing. I should just ditch the compile-time version to reduce the
* probability of sync bugs.
*/
-std::shared_ptr<SensorManager> BuildSensorsFromConfig(const std::string& path)
+SensorManager BuildSensorsFromConfig(const std::string& path)
{
using namespace libconfig;
diff --git a/sensors/builderconfig.hpp b/sensors/builderconfig.hpp
index 078f484..0948c69 100644
--- a/sensors/builderconfig.hpp
+++ b/sensors/builderconfig.hpp
@@ -1,6 +1,5 @@
#pragma once
-#include <memory>
#include <string>
#include "sensors/manager.hpp"
@@ -9,4 +8,4 @@
* Given a configuration file, parsable by libconfig++, parse it and then pass
* the information onto BuildSensors.
*/
-std::shared_ptr<SensorManager> BuildSensorsFromConfig(const std::string& path);
+SensorManager BuildSensorsFromConfig(const std::string& path);
diff --git a/sensors/manager.hpp b/sensors/manager.hpp
index a3e7420..d37787a 100644
--- a/sensors/manager.hpp
+++ b/sensors/manager.hpp
@@ -17,15 +17,26 @@
class SensorManager
{
public:
- SensorManager()
- : _passiveListeningBus(std::move(sdbusplus::bus::new_default())),
- _hostSensorBus(std::move(sdbusplus::bus::new_default()))
+ SensorManager(sdbusplus::bus::bus&& pass, sdbusplus::bus::bus&& host)
+ : _passiveListeningBus(std::move(pass)),
+ _hostSensorBus(std::move(host))
{
- // Create a manager for the sensor root because we own it.
- static constexpr auto SensorRoot = "/xyz/openbmc_project/extsensors";
+ // manager gets its interface from the bus. :D
sdbusplus::server::manager::manager(_hostSensorBus, SensorRoot);
}
+ SensorManager()
+ : SensorManager(std::move(sdbusplus::bus::new_default()),
+ std::move(sdbusplus::bus::new_default()))
+ {
+ }
+
+ ~SensorManager() = default;
+ SensorManager(const SensorManager&) = delete;
+ SensorManager& operator=(const SensorManager&) = delete;
+ SensorManager(SensorManager&&) = default;
+ SensorManager& operator=(SensorManager&&) = default;
+
/*
* Add a Sensor to the Manager.
*/
@@ -35,7 +46,7 @@
std::unique_ptr<Sensor> sensor);
// TODO(venture): Should implement read/write by name.
- std::unique_ptr<Sensor>& getSensor(std::string name)
+ const std::unique_ptr<Sensor>& getSensor(const std::string& name) const
{
return _sensorMap.at(name);
}
@@ -56,5 +67,7 @@
sdbusplus::bus::bus _passiveListeningBus;
sdbusplus::bus::bus _hostSensorBus;
+
+ static constexpr auto SensorRoot = "/xyz/openbmc_project/extsensors";
};
diff --git a/test/Makefile.am b/test/Makefile.am
index ea93a96..738ab91 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -12,5 +12,9 @@
$(PHOSPHOR_DBUS_INTERFACES_LIBS)
# Run all 'check' test programs
-check_PROGRAMS =
+check_PROGRAMS = sensor_manager_unittest
TESTS = $(check_PROGRAMS)
+
+# Until libconfig is mocked out or replaced, include it.
+sensor_manager_unittest_SOURCES = sensor_manager_unittest.cpp
+sensor_manager_unittest_LDADD = $(top_builddir)/sensors/manager.o
diff --git a/test/sensor_manager_unittest.cpp b/test/sensor_manager_unittest.cpp
new file mode 100644
index 0000000..17ccffa
--- /dev/null
+++ b/test/sensor_manager_unittest.cpp
@@ -0,0 +1,28 @@
+#include "sensors/manager.hpp"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <sdbusplus/test/sdbus_mock.hpp>
+
+using ::testing::_;
+using ::testing::IsNull;
+using ::testing::Return;
+using ::testing::StrEq;
+
+TEST(SensorManagerTest, BoringConstructorTest) {
+ // Build a boring SensorManager.
+
+ 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));
+ // Success
+}