| 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 |  | 
|  | 7 | TEST(SensorsFromJson, emptyJsonNoSensors) | 
|  | 8 | { | 
|  | 9 | // If the json has no sensors, the map is empty. | 
|  | 10 |  | 
|  | 11 | auto j2 = R"( | 
|  | 12 | { | 
|  | 13 | "sensors": [] | 
|  | 14 | } | 
|  | 15 | )"_json; | 
|  | 16 |  | 
|  | 17 | auto output = buildSensorsFromJson(j2); | 
|  | 18 | EXPECT_TRUE(output.empty()); | 
|  | 19 | } | 
|  | 20 |  | 
|  | 21 | TEST(SensorsFromJson, oneFanSensor) | 
|  | 22 | { | 
|  | 23 | // If the json has one sensor, it's in the map. | 
|  | 24 |  | 
|  | 25 | auto j2 = R"( | 
|  | 26 | { | 
|  | 27 | "sensors": [{ | 
|  | 28 | "name": "fan1", | 
|  | 29 | "type": "fan", | 
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 30 | "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1", | 
|  | 31 | "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] | 32 | "min": 0, | 
|  | 33 | "max": 255 | 
|  | 34 | }] | 
|  | 35 | } | 
|  | 36 | )"_json; | 
|  | 37 |  | 
|  | 38 | auto output = buildSensorsFromJson(j2); | 
|  | 39 | EXPECT_EQ(1, output.size()); | 
|  | 40 | EXPECT_EQ(output["fan1"].type, "fan"); | 
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 41 | EXPECT_EQ(output["fan1"].readPath, | 
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 42 | "/xyz/openbmc_project/sensors/fan_tach/fan1"); | 
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 43 | EXPECT_EQ(output["fan1"].writePath, | 
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 44 | "/sys/devices/platform/ahb/ahb:apb/1e786000.pwm-tacho-controller/" | 
|  | 45 | "hwmon/**/pwm1"); | 
|  | 46 | EXPECT_EQ(output["fan1"].min, 0); | 
|  | 47 | EXPECT_EQ(output["fan1"].max, 255); | 
|  | 48 | EXPECT_EQ(output["fan1"].timeout, | 
|  | 49 | Sensor::getDefaultTimeout(output["fan1"].type)); | 
| Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 50 | EXPECT_EQ(output["fan1"].ignoreDbusMinMax, false); | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | TEST(SensorsFromJson, IgnoreDbusSensor) | 
|  | 54 | { | 
|  | 55 | auto j2 = R"( | 
|  | 56 | { | 
|  | 57 | "sensors": [{ | 
|  | 58 | "name": "fan1", | 
|  | 59 | "type": "fan", | 
|  | 60 | "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1", | 
|  | 61 | "ignoreDbusMinMax": true | 
|  | 62 | }] | 
|  | 63 | } | 
|  | 64 | )"_json; | 
|  | 65 |  | 
|  | 66 | auto output = buildSensorsFromJson(j2); | 
|  | 67 | EXPECT_EQ(1, output.size()); | 
|  | 68 | EXPECT_EQ(output["fan1"].type, "fan"); | 
|  | 69 | EXPECT_EQ(output["fan1"].readPath, | 
|  | 70 | "/xyz/openbmc_project/sensors/fan_tach/fan1"); | 
|  | 71 | EXPECT_EQ(output["fan1"].writePath, ""); | 
|  | 72 | EXPECT_EQ(output["fan1"].min, 0); | 
|  | 73 | EXPECT_EQ(output["fan1"].max, 0); | 
|  | 74 | EXPECT_EQ(output["fan1"].timeout, | 
|  | 75 | Sensor::getDefaultTimeout(output["fan1"].type)); | 
|  | 76 | EXPECT_EQ(output["fan1"].ignoreDbusMinMax, true); | 
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
|  | 79 | TEST(SensorsFromJson, validateOptionalFields) | 
|  | 80 | { | 
| Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 81 | // The writePath, min, max, timeout, and ignoreDbusMinMax fields are | 
|  | 82 | // optional. | 
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 83 |  | 
|  | 84 | auto j2 = R"( | 
|  | 85 | { | 
|  | 86 | "sensors": [{ | 
|  | 87 | "name": "fan1", | 
|  | 88 | "type": "fan", | 
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 89 | "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1" | 
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 90 | }] | 
|  | 91 | } | 
|  | 92 | )"_json; | 
|  | 93 |  | 
|  | 94 | auto output = buildSensorsFromJson(j2); | 
|  | 95 | EXPECT_EQ(1, output.size()); | 
|  | 96 | EXPECT_EQ(output["fan1"].type, "fan"); | 
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 97 | EXPECT_EQ(output["fan1"].readPath, | 
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 98 | "/xyz/openbmc_project/sensors/fan_tach/fan1"); | 
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 99 | EXPECT_EQ(output["fan1"].writePath, ""); | 
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 100 | EXPECT_EQ(output["fan1"].min, 0); | 
|  | 101 | EXPECT_EQ(output["fan1"].max, 0); | 
|  | 102 | EXPECT_EQ(output["fan1"].timeout, | 
|  | 103 | Sensor::getDefaultTimeout(output["fan1"].type)); | 
| Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 104 | EXPECT_EQ(output["fan1"].ignoreDbusMinMax, false); | 
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
|  | 107 | TEST(SensorsFromJson, twoSensors) | 
|  | 108 | { | 
|  | 109 | // Same as one sensor, but two. | 
|  | 110 | // If a configuration has two sensors with the same name the information | 
|  | 111 | // last is the information used. | 
|  | 112 |  | 
|  | 113 | auto j2 = R"( | 
|  | 114 | { | 
|  | 115 | "sensors": [{ | 
|  | 116 | "name": "fan1", | 
|  | 117 | "type": "fan", | 
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 118 | "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1" | 
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 119 | }, { | 
|  | 120 | "name": "fan2", | 
|  | 121 | "type": "fan", | 
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 122 | "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1" | 
| Patrick Venture | eeeb867 | 2019-02-08 11:47:42 -0800 | [diff] [blame] | 123 | }] | 
|  | 124 | } | 
|  | 125 | )"_json; | 
|  | 126 |  | 
|  | 127 | auto output = buildSensorsFromJson(j2); | 
|  | 128 | EXPECT_EQ(2, output.size()); | 
|  | 129 | } |