summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorPatrick Venture <venture@google.com>2019-02-08 11:47:42 -0800
committerPatrick Venture <venture@google.com>2019-02-08 14:40:52 -0800
commiteeeb867de1fac2d524cd2d0c94d11194bfbfcebb (patch)
tree816ec10f0e9ffbe4a77e50a06e7bc538fc172b5d /test
parent1f802f5e9086f3771a8b1d50c5af4a77be56cee0 (diff)
downloadphosphor-pid-control-eeeb867de1fac2d524cd2d0c94d11194bfbfcebb.tar.gz
phosphor-pid-control-eeeb867de1fac2d524cd2d0c94d11194bfbfcebb.zip
add support to build sensors from json
Add support to build sensors from a json configuration file. Change-Id: Ic5bcbcd01e085ab0d4efaed314af8dc7e82b0b9d Signed-off-by: Patrick Venture <venture@google.com>
Diffstat (limited to 'test')
-rw-r--r--test/Makefile.am6
-rw-r--r--test/sensors_json_unittest.cpp100
2 files changed, 105 insertions, 1 deletions
diff --git a/test/Makefile.am b/test/Makefile.am
index c46284f..fb1d58d 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -17,7 +17,8 @@ check_PROGRAMS = sensor_manager_unittest sensor_pluggable_unittest \
sensor_host_unittest util_unittest pid_zone_unittest \
pid_thermalcontroller_unittest pid_fancontroller_unittest \
pid_stepwisecontroller_unittest \
- dbus_passive_unittest dbus_active_unittest
+ dbus_passive_unittest dbus_active_unittest \
+ sensors_json_unittest
TESTS = $(check_PROGRAMS)
# Until libconfig is mocked out or replaced, include it.
@@ -58,3 +59,6 @@ dbus_passive_unittest_LDADD = $(top_builddir)/dbus/util.o \
dbus_active_unittest_SOURCES = dbus_active_unittest.cpp
dbus_active_unittest_LDADD = $(top_builddir)/dbus/dbusactiveread.o
+
+sensors_json_unittest_SOURCES = sensors_json_unittest.cpp
+sensors_json_unittest_LDADD = $(top_builddir)/sensors/buildjson.o
diff --git a/test/sensors_json_unittest.cpp b/test/sensors_json_unittest.cpp
new file mode 100644
index 0000000..99ab3db
--- /dev/null
+++ b/test/sensors_json_unittest.cpp
@@ -0,0 +1,100 @@
+#include "sensors/buildjson.hpp"
+#include "sensors/sensor.hpp"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+TEST(SensorsFromJson, emptyJsonNoSensors)
+{
+ // If the json has no sensors, the map is empty.
+
+ auto j2 = R"(
+ {
+ "sensors": []
+ }
+ )"_json;
+
+ auto output = buildSensorsFromJson(j2);
+ EXPECT_TRUE(output.empty());
+}
+
+TEST(SensorsFromJson, oneFanSensor)
+{
+ // If the json has one sensor, it's in the map.
+
+ auto j2 = R"(
+ {
+ "sensors": [{
+ "name": "fan1",
+ "type": "fan",
+ "readpath": "/xyz/openbmc_project/sensors/fan_tach/fan1",
+ "writepath": "/sys/devices/platform/ahb/ahb:apb/1e786000.pwm-tacho-controller/hwmon/**/pwm1",
+ "min": 0,
+ "max": 255
+ }]
+ }
+ )"_json;
+
+ auto output = buildSensorsFromJson(j2);
+ EXPECT_EQ(1, output.size());
+ EXPECT_EQ(output["fan1"].type, "fan");
+ EXPECT_EQ(output["fan1"].readpath,
+ "/xyz/openbmc_project/sensors/fan_tach/fan1");
+ EXPECT_EQ(output["fan1"].writepath,
+ "/sys/devices/platform/ahb/ahb:apb/1e786000.pwm-tacho-controller/"
+ "hwmon/**/pwm1");
+ EXPECT_EQ(output["fan1"].min, 0);
+ EXPECT_EQ(output["fan1"].max, 255);
+ EXPECT_EQ(output["fan1"].timeout,
+ Sensor::getDefaultTimeout(output["fan1"].type));
+}
+
+TEST(SensorsFromJson, validateOptionalFields)
+{
+ // The writepath, min, max, timeout fields are optional.
+
+ auto j2 = R"(
+ {
+ "sensors": [{
+ "name": "fan1",
+ "type": "fan",
+ "readpath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
+ }]
+ }
+ )"_json;
+
+ auto output = buildSensorsFromJson(j2);
+ EXPECT_EQ(1, output.size());
+ EXPECT_EQ(output["fan1"].type, "fan");
+ EXPECT_EQ(output["fan1"].readpath,
+ "/xyz/openbmc_project/sensors/fan_tach/fan1");
+ EXPECT_EQ(output["fan1"].writepath, "");
+ EXPECT_EQ(output["fan1"].min, 0);
+ EXPECT_EQ(output["fan1"].max, 0);
+ EXPECT_EQ(output["fan1"].timeout,
+ Sensor::getDefaultTimeout(output["fan1"].type));
+}
+
+TEST(SensorsFromJson, twoSensors)
+{
+ // Same as one sensor, but two.
+ // If a configuration has two sensors with the same name the information
+ // last is the information used.
+
+ auto j2 = R"(
+ {
+ "sensors": [{
+ "name": "fan1",
+ "type": "fan",
+ "readpath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
+ }, {
+ "name": "fan2",
+ "type": "fan",
+ "readpath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
+ }]
+ }
+ )"_json;
+
+ auto output = buildSensorsFromJson(j2);
+ EXPECT_EQ(2, output.size());
+}
OpenPOWER on IntegriCloud