blob: 0e8ae711e74003bc6185469353104561253be484 [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
7TEST(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
21TEST(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 Venture69c51062019-02-11 09:46:03 -080030 "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1",
31 "writePath": "/sys/devices/platform/ahb/ahb:apb/1e786000.pwm-tacho-controller/hwmon/**/pwm1",
Patrick Ventureeeeb8672019-02-08 11:47:42 -080032 "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 Venture69c51062019-02-11 09:46:03 -080041 EXPECT_EQ(output["fan1"].readPath,
Patrick Ventureeeeb8672019-02-08 11:47:42 -080042 "/xyz/openbmc_project/sensors/fan_tach/fan1");
Patrick Venture69c51062019-02-11 09:46:03 -080043 EXPECT_EQ(output["fan1"].writePath,
Patrick Ventureeeeb8672019-02-08 11:47:42 -080044 "/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));
50}
51
52TEST(SensorsFromJson, validateOptionalFields)
53{
Patrick Venture69c51062019-02-11 09:46:03 -080054 // The writePath, min, max, timeout fields are optional.
Patrick Ventureeeeb8672019-02-08 11:47:42 -080055
56 auto j2 = R"(
57 {
58 "sensors": [{
59 "name": "fan1",
60 "type": "fan",
Patrick Venture69c51062019-02-11 09:46:03 -080061 "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
Patrick Ventureeeeb8672019-02-08 11:47:42 -080062 }]
63 }
64 )"_json;
65
66 auto output = buildSensorsFromJson(j2);
67 EXPECT_EQ(1, output.size());
68 EXPECT_EQ(output["fan1"].type, "fan");
Patrick Venture69c51062019-02-11 09:46:03 -080069 EXPECT_EQ(output["fan1"].readPath,
Patrick Ventureeeeb8672019-02-08 11:47:42 -080070 "/xyz/openbmc_project/sensors/fan_tach/fan1");
Patrick Venture69c51062019-02-11 09:46:03 -080071 EXPECT_EQ(output["fan1"].writePath, "");
Patrick Ventureeeeb8672019-02-08 11:47:42 -080072 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}
77
78TEST(SensorsFromJson, twoSensors)
79{
80 // Same as one sensor, but two.
81 // If a configuration has two sensors with the same name the information
82 // last is the information used.
83
84 auto j2 = R"(
85 {
86 "sensors": [{
87 "name": "fan1",
88 "type": "fan",
Patrick Venture69c51062019-02-11 09:46:03 -080089 "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
Patrick Ventureeeeb8672019-02-08 11:47:42 -080090 }, {
91 "name": "fan2",
92 "type": "fan",
Patrick Venture69c51062019-02-11 09:46:03 -080093 "readPath": "/xyz/openbmc_project/sensors/fan_tach/fan1"
Patrick Ventureeeeb8672019-02-08 11:47:42 -080094 }]
95 }
96 )"_json;
97
98 auto output = buildSensorsFromJson(j2);
99 EXPECT_EQ(2, output.size());
100}