Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 1 | #include "sensors/buildjson.hpp" |
| 2 | #include "sensors/sensor.hpp" |
| 3 | |
Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame^] | 4 | #include <sys/types.h> |
| 5 | |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 6 | #include <gtest/gtest.h> |
| 7 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 8 | namespace pid_control |
| 9 | { |
| 10 | namespace |
| 11 | { |
| 12 | |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 13 | TEST(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 | |
| 27 | TEST(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 Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 36 | "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1", |
| 37 | "writePath": "/sys/devices/platform/ahb/ahb:apb/1e786000.pwm-tacho-controller/hwmon/**/pwm1", |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 38 | "min": 0, |
| 39 | "max": 255 |
| 40 | }] |
| 41 | } |
| 42 | )"_json; |
| 43 | |
| 44 | auto output = buildSensorsFromJson(j2); |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 45 | EXPECT_EQ(static_cast<u_int64_t>(1), output.size()); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 46 | EXPECT_EQ(output["fan1"].type, "fan"); |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 47 | EXPECT_EQ(output["fan1"].readPath, |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 48 | "/xyz/openbmc_project/sensors/fan_tach/fan1"); |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 49 | EXPECT_EQ(output["fan1"].writePath, |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 50 | "/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 Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 56 | EXPECT_EQ(output["fan1"].ignoreDbusMinMax, false); |
| 57 | } |
| 58 | |
| 59 | TEST(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.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 73 | EXPECT_EQ(static_cast<u_int64_t>(1), output.size()); |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 74 | 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 Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 85 | TEST(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.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 99 | EXPECT_EQ(static_cast<u_int64_t>(1), output.size()); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 100 | 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 Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 111 | TEST(SensorsFromJson, validateOptionalFields) |
| 112 | { |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 113 | // The writePath, min, max, timeout, and ignoreDbusMinMax fields are |
| 114 | // optional. |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 115 | |
| 116 | auto j2 = R"( |
| 117 | { |
| 118 | "sensors": [{ |
| 119 | "name": "fan1", |
| 120 | "type": "fan", |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 121 | "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1" |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 122 | }] |
| 123 | } |
| 124 | )"_json; |
| 125 | |
| 126 | auto output = buildSensorsFromJson(j2); |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 127 | EXPECT_EQ(static_cast<u_int64_t>(1), output.size()); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 128 | EXPECT_EQ(output["fan1"].type, "fan"); |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 129 | EXPECT_EQ(output["fan1"].readPath, |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 130 | "/xyz/openbmc_project/sensors/fan_tach/fan1"); |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 131 | EXPECT_EQ(output["fan1"].writePath, ""); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 132 | 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 Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 136 | EXPECT_EQ(output["fan1"].ignoreDbusMinMax, false); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 137 | EXPECT_EQ(output["fan1"].unavailableAsFailed, true); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | TEST(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 Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 151 | "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1" |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 152 | }, { |
| 153 | "name": "fan2", |
| 154 | "type": "fan", |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 155 | "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1" |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 156 | }] |
| 157 | } |
| 158 | )"_json; |
| 159 | |
| 160 | auto output = buildSensorsFromJson(j2); |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 161 | EXPECT_EQ(static_cast<u_int64_t>(2), output.size()); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 162 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 163 | |
| 164 | } // namespace |
| 165 | } // namespace pid_control |