blob: 58087edbc49000dd158c208d7526c63b0dc5e4c7 [file] [log] [blame]
Tomáš Pecka339bc672020-11-11 15:59:03 +01001#include "trompeloeil_doctest.h"
Tomáš Pecka339bc672020-11-11 15:59:03 +01002#include "ietf-hardware/IETFHardware.h"
3#include "mock/ietf_hardware.h"
4#include "pretty_printers.h"
5#include "test_log_setup.h"
Tomáš Pecka339bc672020-11-11 15:59:03 +01006
7using namespace std::literals;
8
9TEST_CASE("HardwareState")
10{
11 TEST_INIT_LOGS;
Tomáš Pecka83b62e12020-12-16 14:50:49 +010012 static const auto modulePrefix = "/ietf-hardware:hardware"s;
Tomáš Pecka339bc672020-11-11 15:59:03 +010013
14 trompeloeil::sequence seq1;
Tomáš Pecka83b62e12020-12-16 14:50:49 +010015 auto ietfHardware = std::make_shared<velia::ietf_hardware::IETFHardware>();
Tomáš Pecka339bc672020-11-11 15:59:03 +010016
17 auto fans = std::make_shared<FakeHWMon>();
18 auto sysfsTempCpu = std::make_shared<FakeHWMon>();
Václav Kubernát97e5ea12021-03-24 00:36:57 +010019 auto sysfsVoltageAc = std::make_shared<FakeHWMon>();
20 auto sysfsVoltageDc = std::make_shared<FakeHWMon>();
21 auto sysfsPower = std::make_shared<FakeHWMon>();
22 auto sysfsCurrent = std::make_shared<FakeHWMon>();
Tomáš Pecka339bc672020-11-11 15:59:03 +010023 auto emmc = std::make_shared<FakeEMMC>();
24
25 std::map<std::string, std::string> attributesEMMC;
26 std::map<std::string, int64_t> attributesHWMon;
27
28 // initialize all mocks
29 attributesEMMC = {
30 // FIXME passing initializer_list to macro is hell
31 {"date"s, "02/2017"s},
32 {"serial"s, "0x00a8808d"s},
33 {"name"s, "8GME4R"s},
34 };
35 FAKE_EMMC(emmc, attributesEMMC);
36
Tomáš Pecka63c3c3c2023-04-13 18:20:22 +020037 REQUIRE_CALL(*fans, attribute("fan1_input"s)).RETURN(253);
38 REQUIRE_CALL(*fans, attribute("fan2_input"s)).RETURN(0);
39 REQUIRE_CALL(*fans, attribute("fan3_input"s)).RETURN(1280);
40 REQUIRE_CALL(*fans, attribute("fan4_input"s)).RETURN(666);
Tomáš Pecka339bc672020-11-11 15:59:03 +010041
Václav Kubernát0dee6b92021-04-13 09:14:04 +020042 REQUIRE_CALL(*sysfsTempCpu, attribute("temp1_input")).RETURN(41800);
Tomáš Pecka339bc672020-11-11 15:59:03 +010043
Václav Kubernát97e5ea12021-03-24 00:36:57 +010044 REQUIRE_CALL(*sysfsVoltageAc, attribute("in1_input")).RETURN(220000);
45 REQUIRE_CALL(*sysfsVoltageDc, attribute("in1_input")).RETURN(12000);
46 REQUIRE_CALL(*sysfsPower, attribute("power1_input")).RETURN(14000000);
47 REQUIRE_CALL(*sysfsCurrent, attribute("curr1_input")).RETURN(200);
48
Tomáš Pecka339bc672020-11-11 15:59:03 +010049 attributesEMMC = {{"life_time"s, "40"s}};
50 FAKE_EMMC(emmc, attributesEMMC);
51
Tomáš Pecka63c3c3c2023-04-13 18:20:22 +020052 using velia::ietf_hardware::data_reader::EMMC;
53 using velia::ietf_hardware::data_reader::Fans;
Václav Kubernát6c17d0a2021-03-29 04:55:31 +020054 using velia::ietf_hardware::data_reader::SensorType;
55 using velia::ietf_hardware::data_reader::StaticData;
Václav Kubernát6c17d0a2021-03-29 04:55:31 +020056 using velia::ietf_hardware::data_reader::SysfsValue;
Tomáš Pecka339bc672020-11-11 15:59:03 +010057 // register components into hw state
Václav Kubernát6c17d0a2021-03-29 04:55:31 +020058 ietfHardware->registerDataReader(StaticData("ne", std::nullopt, {{"class", "iana-hardware:chassis"}, {"mfg-name", "CESNET"s}}));
59 ietfHardware->registerDataReader(StaticData("ne:ctrl", "ne", {{"class", "iana-hardware:module"}}));
60 ietfHardware->registerDataReader(Fans("ne:fans", "ne", fans, 4));
Václav Kubernát6c17d0a2021-03-29 04:55:31 +020061 ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-cpu", "ne:ctrl", sysfsTempCpu, 1));
Václav Kubernát97e5ea12021-03-24 00:36:57 +010062 ietfHardware->registerDataReader(SysfsValue<SensorType::VoltageAC>("ne:ctrl:voltage-in", "ne:ctrl", sysfsVoltageAc, 1));
63 ietfHardware->registerDataReader(SysfsValue<SensorType::VoltageDC>("ne:ctrl:voltage-out", "ne:ctrl", sysfsVoltageDc, 1));
64 ietfHardware->registerDataReader(SysfsValue<SensorType::Power>("ne:ctrl:power", "ne:ctrl", sysfsPower, 1));
65 ietfHardware->registerDataReader(SysfsValue<SensorType::Current>("ne:ctrl:current", "ne:ctrl", sysfsCurrent, 1));
Václav Kubernát6c17d0a2021-03-29 04:55:31 +020066 ietfHardware->registerDataReader(EMMC("ne:ctrl:emmc", "ne:ctrl", emmc));
Tomáš Pecka339bc672020-11-11 15:59:03 +010067
Tomáš Pecka3f811962023-04-14 10:54:32 +020068 std::map<std::string, std::string> expected = {
69 {"/ietf-hardware:hardware/component[name='ne']/class", "iana-hardware:chassis"},
70 {"/ietf-hardware:hardware/component[name='ne']/mfg-name", "CESNET"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +020071 {"/ietf-hardware:hardware/component[name='ne']/state/oper-state", "enabled"},
Tomáš Pecka339bc672020-11-11 15:59:03 +010072
Tomáš Pecka3f811962023-04-14 10:54:32 +020073 {"/ietf-hardware:hardware/component[name='ne:fans']/class", "iana-hardware:module"},
74 {"/ietf-hardware:hardware/component[name='ne:fans']/parent", "ne"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +020075 {"/ietf-hardware:hardware/component[name='ne:fans']/state/oper-state", "enabled"},
Tomáš Pecka3f811962023-04-14 10:54:32 +020076 {"/ietf-hardware:hardware/component[name='ne:fans:fan1']/class", "iana-hardware:fan"},
77 {"/ietf-hardware:hardware/component[name='ne:fans:fan1']/parent", "ne:fans"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +020078 {"/ietf-hardware:hardware/component[name='ne:fans:fan1']/state/oper-state", "enabled"},
Tomáš Pecka3f811962023-04-14 10:54:32 +020079 {"/ietf-hardware:hardware/component[name='ne:fans:fan1:rpm']/class", "iana-hardware:sensor"},
80 {"/ietf-hardware:hardware/component[name='ne:fans:fan1:rpm']/parent", "ne:fans:fan1"},
81 {"/ietf-hardware:hardware/component[name='ne:fans:fan1:rpm']/sensor-data/oper-status", "ok"},
82 {"/ietf-hardware:hardware/component[name='ne:fans:fan1:rpm']/sensor-data/value", "253"},
83 {"/ietf-hardware:hardware/component[name='ne:fans:fan1:rpm']/sensor-data/value-precision", "0"},
84 {"/ietf-hardware:hardware/component[name='ne:fans:fan1:rpm']/sensor-data/value-scale", "units"},
85 {"/ietf-hardware:hardware/component[name='ne:fans:fan1:rpm']/sensor-data/value-type", "rpm"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +020086 {"/ietf-hardware:hardware/component[name='ne:fans:fan1:rpm']/state/oper-state", "enabled"},
Tomáš Pecka3f811962023-04-14 10:54:32 +020087 {"/ietf-hardware:hardware/component[name='ne:fans:fan2']/class", "iana-hardware:fan"},
88 {"/ietf-hardware:hardware/component[name='ne:fans:fan2']/parent", "ne:fans"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +020089 {"/ietf-hardware:hardware/component[name='ne:fans:fan2']/state/oper-state", "enabled"},
Tomáš Pecka3f811962023-04-14 10:54:32 +020090 {"/ietf-hardware:hardware/component[name='ne:fans:fan2:rpm']/class", "iana-hardware:sensor"},
91 {"/ietf-hardware:hardware/component[name='ne:fans:fan2:rpm']/parent", "ne:fans:fan2"},
92 {"/ietf-hardware:hardware/component[name='ne:fans:fan2:rpm']/sensor-data/oper-status", "ok"},
93 {"/ietf-hardware:hardware/component[name='ne:fans:fan2:rpm']/sensor-data/value", "0"},
94 {"/ietf-hardware:hardware/component[name='ne:fans:fan2:rpm']/sensor-data/value-precision", "0"},
95 {"/ietf-hardware:hardware/component[name='ne:fans:fan2:rpm']/sensor-data/value-scale", "units"},
96 {"/ietf-hardware:hardware/component[name='ne:fans:fan2:rpm']/sensor-data/value-type", "rpm"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +020097 {"/ietf-hardware:hardware/component[name='ne:fans:fan2:rpm']/state/oper-state", "enabled"},
Tomáš Pecka3f811962023-04-14 10:54:32 +020098 {"/ietf-hardware:hardware/component[name='ne:fans:fan3']/class", "iana-hardware:fan"},
99 {"/ietf-hardware:hardware/component[name='ne:fans:fan3']/parent", "ne:fans"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +0200100 {"/ietf-hardware:hardware/component[name='ne:fans:fan3']/state/oper-state", "enabled"},
Tomáš Pecka3f811962023-04-14 10:54:32 +0200101 {"/ietf-hardware:hardware/component[name='ne:fans:fan3:rpm']/class", "iana-hardware:sensor"},
102 {"/ietf-hardware:hardware/component[name='ne:fans:fan3:rpm']/parent", "ne:fans:fan3"},
103 {"/ietf-hardware:hardware/component[name='ne:fans:fan3:rpm']/sensor-data/oper-status", "ok"},
104 {"/ietf-hardware:hardware/component[name='ne:fans:fan3:rpm']/sensor-data/value", "1280"},
105 {"/ietf-hardware:hardware/component[name='ne:fans:fan3:rpm']/sensor-data/value-precision", "0"},
106 {"/ietf-hardware:hardware/component[name='ne:fans:fan3:rpm']/sensor-data/value-scale", "units"},
107 {"/ietf-hardware:hardware/component[name='ne:fans:fan3:rpm']/sensor-data/value-type", "rpm"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +0200108 {"/ietf-hardware:hardware/component[name='ne:fans:fan3:rpm']/state/oper-state", "enabled"},
Tomáš Pecka3f811962023-04-14 10:54:32 +0200109 {"/ietf-hardware:hardware/component[name='ne:fans:fan4']/class", "iana-hardware:fan"},
110 {"/ietf-hardware:hardware/component[name='ne:fans:fan4']/parent", "ne:fans"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +0200111 {"/ietf-hardware:hardware/component[name='ne:fans:fan4']/state/oper-state", "enabled"},
Tomáš Pecka3f811962023-04-14 10:54:32 +0200112 {"/ietf-hardware:hardware/component[name='ne:fans:fan4:rpm']/class", "iana-hardware:sensor"},
113 {"/ietf-hardware:hardware/component[name='ne:fans:fan4:rpm']/parent", "ne:fans:fan4"},
114 {"/ietf-hardware:hardware/component[name='ne:fans:fan4:rpm']/sensor-data/oper-status", "ok"},
115 {"/ietf-hardware:hardware/component[name='ne:fans:fan4:rpm']/sensor-data/value", "666"},
116 {"/ietf-hardware:hardware/component[name='ne:fans:fan4:rpm']/sensor-data/value-precision", "0"},
117 {"/ietf-hardware:hardware/component[name='ne:fans:fan4:rpm']/sensor-data/value-scale", "units"},
118 {"/ietf-hardware:hardware/component[name='ne:fans:fan4:rpm']/sensor-data/value-type", "rpm"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +0200119 {"/ietf-hardware:hardware/component[name='ne:fans:fan4:rpm']/state/oper-state", "enabled"},
Tomáš Pecka339bc672020-11-11 15:59:03 +0100120
Tomáš Pecka3f811962023-04-14 10:54:32 +0200121 {"/ietf-hardware:hardware/component[name='ne:ctrl']/parent", "ne"},
122 {"/ietf-hardware:hardware/component[name='ne:ctrl']/class", "iana-hardware:module"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +0200123 {"/ietf-hardware:hardware/component[name='ne:ctrl']/state/oper-state", "enabled"},
Tomáš Pecka339bc672020-11-11 15:59:03 +0100124
Tomáš Pecka3f811962023-04-14 10:54:32 +0200125 {"/ietf-hardware:hardware/component[name='ne:ctrl:temperature-cpu']/class", "iana-hardware:sensor"},
126 {"/ietf-hardware:hardware/component[name='ne:ctrl:temperature-cpu']/parent", "ne:ctrl"},
127 {"/ietf-hardware:hardware/component[name='ne:ctrl:temperature-cpu']/sensor-data/oper-status", "ok"},
128 {"/ietf-hardware:hardware/component[name='ne:ctrl:temperature-cpu']/sensor-data/value", "41800"},
129 {"/ietf-hardware:hardware/component[name='ne:ctrl:temperature-cpu']/sensor-data/value-precision", "0"},
130 {"/ietf-hardware:hardware/component[name='ne:ctrl:temperature-cpu']/sensor-data/value-scale", "milli"},
131 {"/ietf-hardware:hardware/component[name='ne:ctrl:temperature-cpu']/sensor-data/value-type", "celsius"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +0200132 {"/ietf-hardware:hardware/component[name='ne:ctrl:temperature-cpu']/state/oper-state", "enabled"},
Tomáš Pecka339bc672020-11-11 15:59:03 +0100133
Tomáš Pecka3f811962023-04-14 10:54:32 +0200134 {"/ietf-hardware:hardware/component[name='ne:ctrl:power']/class", "iana-hardware:sensor"},
135 {"/ietf-hardware:hardware/component[name='ne:ctrl:power']/parent", "ne:ctrl"},
136 {"/ietf-hardware:hardware/component[name='ne:ctrl:power']/sensor-data/oper-status", "ok"},
137 {"/ietf-hardware:hardware/component[name='ne:ctrl:power']/sensor-data/value", "14000000"},
138 {"/ietf-hardware:hardware/component[name='ne:ctrl:power']/sensor-data/value-precision", "0"},
139 {"/ietf-hardware:hardware/component[name='ne:ctrl:power']/sensor-data/value-scale", "micro"},
140 {"/ietf-hardware:hardware/component[name='ne:ctrl:power']/sensor-data/value-type", "watts"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +0200141 {"/ietf-hardware:hardware/component[name='ne:ctrl:power']/state/oper-state", "enabled"},
Václav Kubernát97e5ea12021-03-24 00:36:57 +0100142
Tomáš Pecka3f811962023-04-14 10:54:32 +0200143 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-in']/class", "iana-hardware:sensor"},
144 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-in']/parent", "ne:ctrl"},
145 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-in']/sensor-data/oper-status", "ok"},
146 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-in']/sensor-data/value", "220000"},
147 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-in']/sensor-data/value-precision", "0"},
148 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-in']/sensor-data/value-scale", "milli"},
149 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-in']/sensor-data/value-type", "volts-AC"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +0200150 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-in']/state/oper-state", "enabled"},
Tomáš Pecka3f811962023-04-14 10:54:32 +0200151 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-out']/class", "iana-hardware:sensor"},
152 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-out']/parent", "ne:ctrl"},
153 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-out']/sensor-data/oper-status", "ok"},
154 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-out']/sensor-data/value", "12000"},
155 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-out']/sensor-data/value-precision", "0"},
156 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-out']/sensor-data/value-scale", "milli"},
157 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-out']/sensor-data/value-type", "volts-DC"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +0200158 {"/ietf-hardware:hardware/component[name='ne:ctrl:voltage-out']/state/oper-state", "enabled"},
Václav Kubernát97e5ea12021-03-24 00:36:57 +0100159
Tomáš Pecka3f811962023-04-14 10:54:32 +0200160 {"/ietf-hardware:hardware/component[name='ne:ctrl:current']/class", "iana-hardware:sensor"},
161 {"/ietf-hardware:hardware/component[name='ne:ctrl:current']/parent", "ne:ctrl"},
162 {"/ietf-hardware:hardware/component[name='ne:ctrl:current']/sensor-data/oper-status", "ok"},
163 {"/ietf-hardware:hardware/component[name='ne:ctrl:current']/sensor-data/value", "200"},
164 {"/ietf-hardware:hardware/component[name='ne:ctrl:current']/sensor-data/value-precision", "0"},
165 {"/ietf-hardware:hardware/component[name='ne:ctrl:current']/sensor-data/value-scale", "milli"},
166 {"/ietf-hardware:hardware/component[name='ne:ctrl:current']/sensor-data/value-type", "amperes"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +0200167 {"/ietf-hardware:hardware/component[name='ne:ctrl:current']/state/oper-state", "enabled"},
Václav Kubernát97e5ea12021-03-24 00:36:57 +0100168
Tomáš Pecka3f811962023-04-14 10:54:32 +0200169 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc']/parent", "ne:ctrl"},
170 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc']/class", "iana-hardware:module"},
171 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc']/serial-num", "0x00a8808d"},
172 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc']/mfg-date", "2017-02-01T00:00:00-00:00"},
173 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc']/model-name", "8GME4R"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +0200174 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc']/state/oper-state", "enabled"},
Tomáš Pecka3f811962023-04-14 10:54:32 +0200175 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc:lifetime']/class", "iana-hardware:sensor"},
176 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc:lifetime']/parent", "ne:ctrl:emmc"},
177 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc:lifetime']/sensor-data/oper-status", "ok"},
178 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc:lifetime']/sensor-data/value", "40"},
179 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc:lifetime']/sensor-data/value-precision", "0"},
180 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc:lifetime']/sensor-data/value-scale", "units"},
181 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc:lifetime']/sensor-data/value-type", "other"},
182 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc:lifetime']/sensor-data/units-display", "percent"},
Tomáš Pecka7eb0c422023-04-21 15:36:33 +0200183 {"/ietf-hardware:hardware/component[name='ne:ctrl:emmc:lifetime']/state/oper-state", "enabled"},
Tomáš Pecka3f811962023-04-14 10:54:32 +0200184 };
Tomáš Pecka339bc672020-11-11 15:59:03 +0100185
Tomáš Pecka3f811962023-04-14 10:54:32 +0200186 // exclude last-change node
187 auto result = ietfHardware->process();
188 result.erase(modulePrefix + "/last-change");
189 REQUIRE(result == expected);
Tomáš Pecka339bc672020-11-11 15:59:03 +0100190}