Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 1 | #include "sensors/buildjson.hpp" |
| 2 | #include "sensors/sensor.hpp" |
| 3 | |
| 4 | #include <gmock/gmock.h> |
| 5 | #include <gtest/gtest.h> |
| 6 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 7 | namespace pid_control |
| 8 | { |
| 9 | namespace |
| 10 | { |
| 11 | |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 12 | TEST(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 | |
| 26 | TEST(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 Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 35 | "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1", |
| 36 | "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] | 37 | "min": 0, |
| 38 | "max": 255 |
| 39 | }] |
| 40 | } |
| 41 | )"_json; |
| 42 | |
| 43 | auto output = buildSensorsFromJson(j2); |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 44 | EXPECT_EQ(static_cast<u_int64_t>(1), output.size()); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 45 | EXPECT_EQ(output["fan1"].type, "fan"); |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 46 | EXPECT_EQ(output["fan1"].readPath, |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 47 | "/xyz/openbmc_project/sensors/fan_tach/fan1"); |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 48 | EXPECT_EQ(output["fan1"].writePath, |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 49 | "/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 Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 55 | EXPECT_EQ(output["fan1"].ignoreDbusMinMax, false); |
| 56 | } |
| 57 | |
| 58 | TEST(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); |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 72 | EXPECT_EQ(static_cast<u_int64_t>(1), output.size()); |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 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 Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 84 | TEST(SensorsFromJson, TempDbusSensor) |
| 85 | { |
| 86 | auto j2 = R"( |
| 87 | { |
| 88 | "sensors": [{ |
| 89 | "name": "CPU_DTS", |
| 90 | "type": "temp", |
| 91 | "readPath": "/xyz/openbmc_project/sensors/temperature/CPU_DTS", |
| 92 | "unavailableAsFailed": false |
| 93 | }] |
| 94 | } |
| 95 | )"_json; |
| 96 | |
| 97 | auto output = buildSensorsFromJson(j2); |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 98 | EXPECT_EQ(static_cast<u_int64_t>(1), output.size()); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 99 | EXPECT_EQ(output["CPU_DTS"].type, "temp"); |
| 100 | EXPECT_EQ(output["CPU_DTS"].readPath, |
| 101 | "/xyz/openbmc_project/sensors/temperature/CPU_DTS"); |
| 102 | EXPECT_EQ(output["CPU_DTS"].writePath, ""); |
| 103 | EXPECT_EQ(output["CPU_DTS"].min, 0); |
| 104 | EXPECT_EQ(output["CPU_DTS"].max, 0); |
| 105 | EXPECT_EQ(output["CPU_DTS"].timeout, |
| 106 | Sensor::getDefaultTimeout(output["CPU_DTS"].type)); |
| 107 | EXPECT_EQ(output["CPU_DTS"].unavailableAsFailed, false); |
| 108 | } |
| 109 | |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 110 | TEST(SensorsFromJson, validateOptionalFields) |
| 111 | { |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 112 | // The writePath, min, max, timeout, and ignoreDbusMinMax fields are |
| 113 | // optional. |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 114 | |
| 115 | auto j2 = R"( |
| 116 | { |
| 117 | "sensors": [{ |
| 118 | "name": "fan1", |
| 119 | "type": "fan", |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 120 | "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1" |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 121 | }] |
| 122 | } |
| 123 | )"_json; |
| 124 | |
| 125 | auto output = buildSensorsFromJson(j2); |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 126 | EXPECT_EQ(static_cast<u_int64_t>(1), output.size()); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 127 | EXPECT_EQ(output["fan1"].type, "fan"); |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 128 | EXPECT_EQ(output["fan1"].readPath, |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 129 | "/xyz/openbmc_project/sensors/fan_tach/fan1"); |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 130 | EXPECT_EQ(output["fan1"].writePath, ""); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 131 | EXPECT_EQ(output["fan1"].min, 0); |
| 132 | EXPECT_EQ(output["fan1"].max, 0); |
| 133 | EXPECT_EQ(output["fan1"].timeout, |
| 134 | Sensor::getDefaultTimeout(output["fan1"].type)); |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 135 | EXPECT_EQ(output["fan1"].ignoreDbusMinMax, false); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 136 | EXPECT_EQ(output["fan1"].unavailableAsFailed, true); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | TEST(SensorsFromJson, twoSensors) |
| 140 | { |
| 141 | // Same as one sensor, but two. |
| 142 | // If a configuration has two sensors with the same name the information |
| 143 | // last is the information used. |
| 144 | |
| 145 | auto j2 = R"( |
| 146 | { |
| 147 | "sensors": [{ |
| 148 | "name": "fan1", |
| 149 | "type": "fan", |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 150 | "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1" |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 151 | }, { |
| 152 | "name": "fan2", |
| 153 | "type": "fan", |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 154 | "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1" |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 155 | }] |
| 156 | } |
| 157 | )"_json; |
| 158 | |
| 159 | auto output = buildSensorsFromJson(j2); |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 160 | EXPECT_EQ(static_cast<u_int64_t>(2), output.size()); |
Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 161 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 162 | |
| 163 | } // namespace |
| 164 | } // namespace pid_control |