blob: 611c72411798f45d7ce3d6aba6b8b3169913f29d [file] [log] [blame]
Patrick Ventureeeeb8672019-02-08 11:47:42 -08001#include "sensors/buildjson.hpp"
2#include "sensors/sensor.hpp"
3
Ed Tanousf8b6e552025-06-27 13:27:50 -07004#include <sys/types.h>
5
Patrick Ventureeeeb8672019-02-08 11:47:42 -08006#include <gtest/gtest.h>
7
Patrick Venturea0764872020-08-08 07:48:43 -07008namespace pid_control
9{
10namespace
11{
12
Patrick Ventureeeeb8672019-02-08 11:47:42 -080013TEST(SensorsFromJson, emptyJsonNoSensors)
14{
15 // If the json has no sensors, the map is empty.
16
17 auto j2 = R"(
18 {
19 "sensors": []
20 }
21 )"_json;
22
23 auto output = buildSensorsFromJson(j2);
24 EXPECT_TRUE(output.empty());
25}
26
27TEST(SensorsFromJson, oneFanSensor)
28{
29 // If the json has one sensor, it's in the map.
30
31 auto j2 = R"(
32 {
33 "sensors": [{
34 "name": "fan1",
35 "type": "fan",
Patrick Venture69c51062019-02-11 09:46:03 -080036 "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1",
37 "writePath": "/sys/devices/platform/ahb/ahb:apb/1e786000.pwm-tacho-controller/hwmon/**/pwm1",
Patrick Ventureeeeb8672019-02-08 11:47:42 -080038 "min": 0,
39 "max": 255
40 }]
41 }
42 )"_json;
43
44 auto output = buildSensorsFromJson(j2);
Harvey.Wua1ae4fa2022-10-28 17:38:35 +080045 EXPECT_EQ(static_cast<u_int64_t>(1), output.size());
Patrick Ventureeeeb8672019-02-08 11:47:42 -080046 EXPECT_EQ(output["fan1"].type, "fan");
Patrick Venture69c51062019-02-11 09:46:03 -080047 EXPECT_EQ(output["fan1"].readPath,
Patrick Ventureeeeb8672019-02-08 11:47:42 -080048 "/xyz/openbmc_project/sensors/fan_tach/fan1");
Patrick Venture69c51062019-02-11 09:46:03 -080049 EXPECT_EQ(output["fan1"].writePath,
Patrick Ventureeeeb8672019-02-08 11:47:42 -080050 "/sys/devices/platform/ahb/ahb:apb/1e786000.pwm-tacho-controller/"
51 "hwmon/**/pwm1");
52 EXPECT_EQ(output["fan1"].min, 0);
53 EXPECT_EQ(output["fan1"].max, 255);
54 EXPECT_EQ(output["fan1"].timeout,
55 Sensor::getDefaultTimeout(output["fan1"].type));
Patrick Venture6b9f5992019-09-10 09:18:28 -070056 EXPECT_EQ(output["fan1"].ignoreDbusMinMax, false);
57}
58
59TEST(SensorsFromJson, IgnoreDbusSensor)
60{
61 auto j2 = R"(
62 {
63 "sensors": [{
64 "name": "fan1",
65 "type": "fan",
66 "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1",
67 "ignoreDbusMinMax": true
68 }]
69 }
70 )"_json;
71
72 auto output = buildSensorsFromJson(j2);
Harvey.Wua1ae4fa2022-10-28 17:38:35 +080073 EXPECT_EQ(static_cast<u_int64_t>(1), output.size());
Patrick Venture6b9f5992019-09-10 09:18:28 -070074 EXPECT_EQ(output["fan1"].type, "fan");
75 EXPECT_EQ(output["fan1"].readPath,
76 "/xyz/openbmc_project/sensors/fan_tach/fan1");
77 EXPECT_EQ(output["fan1"].writePath, "");
78 EXPECT_EQ(output["fan1"].min, 0);
79 EXPECT_EQ(output["fan1"].max, 0);
80 EXPECT_EQ(output["fan1"].timeout,
81 Sensor::getDefaultTimeout(output["fan1"].type));
82 EXPECT_EQ(output["fan1"].ignoreDbusMinMax, true);
Patrick Ventureeeeb8672019-02-08 11:47:42 -080083}
84
Alex.Song8f73ad72021-10-07 00:18:27 +080085TEST(SensorsFromJson, TempDbusSensor)
86{
87 auto j2 = R"(
88 {
89 "sensors": [{
90 "name": "CPU_DTS",
91 "type": "temp",
92 "readPath": "/xyz/openbmc_project/sensors/temperature/CPU_DTS",
93 "unavailableAsFailed": false
94 }]
95 }
96 )"_json;
97
98 auto output = buildSensorsFromJson(j2);
Harvey.Wua1ae4fa2022-10-28 17:38:35 +080099 EXPECT_EQ(static_cast<u_int64_t>(1), output.size());
Alex.Song8f73ad72021-10-07 00:18:27 +0800100 EXPECT_EQ(output["CPU_DTS"].type, "temp");
101 EXPECT_EQ(output["CPU_DTS"].readPath,
102 "/xyz/openbmc_project/sensors/temperature/CPU_DTS");
103 EXPECT_EQ(output["CPU_DTS"].writePath, "");
104 EXPECT_EQ(output["CPU_DTS"].min, 0);
105 EXPECT_EQ(output["CPU_DTS"].max, 0);
106 EXPECT_EQ(output["CPU_DTS"].timeout,
107 Sensor::getDefaultTimeout(output["CPU_DTS"].type));
108 EXPECT_EQ(output["CPU_DTS"].unavailableAsFailed, false);
109}
110
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800111TEST(SensorsFromJson, validateOptionalFields)
112{
Patrick Venture6b9f5992019-09-10 09:18:28 -0700113 // The writePath, min, max, timeout, and ignoreDbusMinMax fields are
114 // optional.
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800115
116 auto j2 = R"(
117 {
118 "sensors": [{
119 "name": "fan1",
120 "type": "fan",
Patrick Venture69c51062019-02-11 09:46:03 -0800121 "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800122 }]
123 }
124 )"_json;
125
126 auto output = buildSensorsFromJson(j2);
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800127 EXPECT_EQ(static_cast<u_int64_t>(1), output.size());
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800128 EXPECT_EQ(output["fan1"].type, "fan");
Patrick Venture69c51062019-02-11 09:46:03 -0800129 EXPECT_EQ(output["fan1"].readPath,
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800130 "/xyz/openbmc_project/sensors/fan_tach/fan1");
Patrick Venture69c51062019-02-11 09:46:03 -0800131 EXPECT_EQ(output["fan1"].writePath, "");
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800132 EXPECT_EQ(output["fan1"].min, 0);
133 EXPECT_EQ(output["fan1"].max, 0);
134 EXPECT_EQ(output["fan1"].timeout,
135 Sensor::getDefaultTimeout(output["fan1"].type));
Patrick Venture6b9f5992019-09-10 09:18:28 -0700136 EXPECT_EQ(output["fan1"].ignoreDbusMinMax, false);
Alex.Song8f73ad72021-10-07 00:18:27 +0800137 EXPECT_EQ(output["fan1"].unavailableAsFailed, true);
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800138}
139
140TEST(SensorsFromJson, twoSensors)
141{
142 // Same as one sensor, but two.
143 // If a configuration has two sensors with the same name the information
144 // last is the information used.
145
146 auto j2 = R"(
147 {
148 "sensors": [{
149 "name": "fan1",
150 "type": "fan",
Patrick Venture69c51062019-02-11 09:46:03 -0800151 "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800152 }, {
153 "name": "fan2",
154 "type": "fan",
Patrick Venture69c51062019-02-11 09:46:03 -0800155 "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800156 }]
157 }
158 )"_json;
159
160 auto output = buildSensorsFromJson(j2);
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800161 EXPECT_EQ(static_cast<u_int64_t>(2), output.size());
Patrick Ventureeeeb8672019-02-08 11:47:42 -0800162}
Patrick Venturea0764872020-08-08 07:48:43 -0700163
164} // namespace
165} // namespace pid_control