blob: 7b3dbc3699ee1ad61364f1d78795f2b917d02c6a [file] [log] [blame]
Patrick Ventureeeeb8672019-02-08 11:47:42 -08001#include "sensors/buildjson.hpp"
2#include "sensors/sensor.hpp"
3
4#include <gmock/gmock.h>
5#include <gtest/gtest.h>
6
Patrick Venturea0764872020-08-08 07:48:43 -07007namespace pid_control
8{
9namespace
10{
11
Patrick Ventureeeeb8672019-02-08 11:47:42 -080012TEST(SensorsFromJson, emptyJsonNoSensors)
13{
14 // If the json has no sensors, the map is empty.
15
16 auto j2 = R"(
17 {
18 "sensors": []
19 }
20 )"_json;
21
22 auto output = buildSensorsFromJson(j2);
23 EXPECT_TRUE(output.empty());
24}
25
26TEST(SensorsFromJson, oneFanSensor)
27{
28 // If the json has one sensor, it's in the map.
29
30 auto j2 = R"(
31 {
32 "sensors": [{
33 "name": "fan1",
34 "type": "fan",
Patrick Venture69c51062019-02-11 09:46:03 -080035 "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1",
36 "writePath": "/sys/devices/platform/ahb/ahb:apb/1e786000.pwm-tacho-controller/hwmon/**/pwm1",
Patrick Ventureeeeb8672019-02-08 11:47:42 -080037 "min": 0,
38 "max": 255
39 }]
40 }
41 )"_json;
42
43 auto output = buildSensorsFromJson(j2);
44 EXPECT_EQ(1, output.size());
45 EXPECT_EQ(output["fan1"].type, "fan");
Patrick Venture69c51062019-02-11 09:46:03 -080046 EXPECT_EQ(output["fan1"].readPath,
Patrick Ventureeeeb8672019-02-08 11:47:42 -080047 "/xyz/openbmc_project/sensors/fan_tach/fan1");
Patrick Venture69c51062019-02-11 09:46:03 -080048 EXPECT_EQ(output["fan1"].writePath,
Patrick Ventureeeeb8672019-02-08 11:47:42 -080049 "/sys/devices/platform/ahb/ahb:apb/1e786000.pwm-tacho-controller/"
50 "hwmon/**/pwm1");
51 EXPECT_EQ(output["fan1"].min, 0);
52 EXPECT_EQ(output["fan1"].max, 255);
53 EXPECT_EQ(output["fan1"].timeout,
54 Sensor::getDefaultTimeout(output["fan1"].type));
Patrick Venture6b9f5992019-09-10 09:18:28 -070055 EXPECT_EQ(output["fan1"].ignoreDbusMinMax, false);
56}
57
58TEST(SensorsFromJson, IgnoreDbusSensor)
59{
60 auto j2 = R"(
61 {
62 "sensors": [{
63 "name": "fan1",
64 "type": "fan",
65 "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1",
66 "ignoreDbusMinMax": true
67 }]
68 }
69 )"_json;
70
71 auto output = buildSensorsFromJson(j2);
72 EXPECT_EQ(1, output.size());
73 EXPECT_EQ(output["fan1"].type, "fan");
74 EXPECT_EQ(output["fan1"].readPath,
75 "/xyz/openbmc_project/sensors/fan_tach/fan1");
76 EXPECT_EQ(output["fan1"].writePath, "");
77 EXPECT_EQ(output["fan1"].min, 0);
78 EXPECT_EQ(output["fan1"].max, 0);
79 EXPECT_EQ(output["fan1"].timeout,
80 Sensor::getDefaultTimeout(output["fan1"].type));
81 EXPECT_EQ(output["fan1"].ignoreDbusMinMax, true);
Patrick Ventureeeeb8672019-02-08 11:47:42 -080082}
83
84TEST(SensorsFromJson, validateOptionalFields)
85{
Patrick Venture6b9f5992019-09-10 09:18:28 -070086 // The writePath, min, max, timeout, and ignoreDbusMinMax fields are
87 // optional.
Patrick Ventureeeeb8672019-02-08 11:47:42 -080088
89 auto j2 = R"(
90 {
91 "sensors": [{
92 "name": "fan1",
93 "type": "fan",
Patrick Venture69c51062019-02-11 09:46:03 -080094 "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
Patrick Ventureeeeb8672019-02-08 11:47:42 -080095 }]
96 }
97 )"_json;
98
99 auto output = buildSensorsFromJson(j2);
100 EXPECT_EQ(1, output.size());
101 EXPECT_EQ(output["fan1"].type, "fan");
Patrick Venture69c51062019-02-11 09:46:03 -0800102 EXPECT_EQ(output["fan1"].readPath,
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800103 "/xyz/openbmc_project/sensors/fan_tach/fan1");
Patrick Venture69c51062019-02-11 09:46:03 -0800104 EXPECT_EQ(output["fan1"].writePath, "");
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800105 EXPECT_EQ(output["fan1"].min, 0);
106 EXPECT_EQ(output["fan1"].max, 0);
107 EXPECT_EQ(output["fan1"].timeout,
108 Sensor::getDefaultTimeout(output["fan1"].type));
Patrick Venture6b9f5992019-09-10 09:18:28 -0700109 EXPECT_EQ(output["fan1"].ignoreDbusMinMax, false);
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800110}
111
112TEST(SensorsFromJson, twoSensors)
113{
114 // Same as one sensor, but two.
115 // If a configuration has two sensors with the same name the information
116 // last is the information used.
117
118 auto j2 = R"(
119 {
120 "sensors": [{
121 "name": "fan1",
122 "type": "fan",
Patrick Venture69c51062019-02-11 09:46:03 -0800123 "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800124 }, {
125 "name": "fan2",
126 "type": "fan",
Patrick Venture69c51062019-02-11 09:46:03 -0800127 "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800128 }]
129 }
130 )"_json;
131
132 auto output = buildSensorsFromJson(j2);
133 EXPECT_EQ(2, output.size());
134}
Patrick Venturea0764872020-08-08 07:48:43 -0700135
136} // namespace
137} // namespace pid_control