blob: 800311564e3e9b91933f3587cb3b0ef781d16cd1 [file] [log] [blame]
Tomáš Pecka3f811962023-04-14 10:54:32 +02001#include "trompeloeil_doctest.h"
2#include "ietf-hardware/IETFHardware.h"
3#include "ietf-hardware/sysrepo/Sysrepo.h"
4#include "mock/ietf_hardware.h"
5#include "pretty_printers.h"
6#include "test_log_setup.h"
7#include "test_sysrepo_helpers.h"
8
9using namespace std::literals;
10
11TEST_CASE("IETF Hardware with sysrepo")
12{
13 TEST_SYSREPO_INIT_LOGS;
14 TEST_SYSREPO_INIT;
15 TEST_SYSREPO_INIT_CLIENT;
16 static const auto modulePrefix = "/ietf-hardware:hardware"s;
17
18 trompeloeil::sequence seq1;
19 auto ietfHardware = std::make_shared<velia::ietf_hardware::IETFHardware>();
20
21 auto fans = std::make_shared<FakeHWMon>();
22 auto sysfsTempCpu = std::make_shared<FakeHWMon>();
23 auto sysfsVoltageAc = std::make_shared<FakeHWMon>();
24 auto sysfsVoltageDc = std::make_shared<FakeHWMon>();
25 auto sysfsPower = std::make_shared<FakeHWMon>();
26 auto sysfsCurrent = std::make_shared<FakeHWMon>();
27 auto emmc = std::make_shared<FakeEMMC>();
28
29 std::map<std::string, std::string> attributesEMMC;
30 std::map<std::string, int64_t> attributesHWMon;
31
32 // initialize all mocks
33 attributesEMMC = {
34 // FIXME passing initializer_list to macro is hell
35 {"date"s, "02/2017"s},
36 {"serial"s, "0x00a8808d"s},
37 {"name"s, "8GME4R"s},
38 };
39 FAKE_EMMC(emmc, attributesEMMC);
40
41 REQUIRE_CALL(*fans, attribute("fan1_input"s)).RETURN(253);
42 REQUIRE_CALL(*fans, attribute("fan2_input"s)).RETURN(0);
43 REQUIRE_CALL(*fans, attribute("fan3_input"s)).RETURN(1280);
44 REQUIRE_CALL(*fans, attribute("fan4_input"s)).RETURN(666);
45
46 REQUIRE_CALL(*sysfsTempCpu, attribute("temp1_input")).RETURN(41800);
47
48 REQUIRE_CALL(*sysfsVoltageAc, attribute("in1_input")).RETURN(220000);
49 REQUIRE_CALL(*sysfsVoltageDc, attribute("in1_input")).RETURN(12000);
50 REQUIRE_CALL(*sysfsPower, attribute("power1_input")).RETURN(14000000);
51 REQUIRE_CALL(*sysfsCurrent, attribute("curr1_input")).RETURN(200);
52
53 attributesEMMC = {{"life_time"s, "40"s}};
54 FAKE_EMMC(emmc, attributesEMMC);
55
56 using velia::ietf_hardware::data_reader::EMMC;
57 using velia::ietf_hardware::data_reader::Fans;
58 using velia::ietf_hardware::data_reader::SensorType;
59 using velia::ietf_hardware::data_reader::StaticData;
60 using velia::ietf_hardware::data_reader::SysfsValue;
61 // register components into hw state
62 ietfHardware->registerDataReader(StaticData("ne", std::nullopt, {{"class", "iana-hardware:chassis"}, {"mfg-name", "CESNET"s}}));
63 ietfHardware->registerDataReader(StaticData("ne:ctrl", "ne", {{"class", "iana-hardware:module"}}));
64 ietfHardware->registerDataReader(Fans("ne:fans", "ne", fans, 4));
65 ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:ctrl:temperature-cpu", "ne:ctrl", sysfsTempCpu, 1));
66 ietfHardware->registerDataReader(SysfsValue<SensorType::VoltageAC>("ne:ctrl:voltage-in", "ne:ctrl", sysfsVoltageAc, 1));
67 ietfHardware->registerDataReader(SysfsValue<SensorType::VoltageDC>("ne:ctrl:voltage-out", "ne:ctrl", sysfsVoltageDc, 1));
68 ietfHardware->registerDataReader(SysfsValue<SensorType::Power>("ne:ctrl:power", "ne:ctrl", sysfsPower, 1));
69 ietfHardware->registerDataReader(SysfsValue<SensorType::Current>("ne:ctrl:current", "ne:ctrl", sysfsCurrent, 1));
70 ietfHardware->registerDataReader(EMMC("ne:ctrl:emmc", "ne:ctrl", emmc));
71
72 auto ietfHardwareSysrepo = std::make_shared<velia::ietf_hardware::sysrepo::Sysrepo>(srSess, ietfHardware);
73
74 SECTION("test last-change")
75 {
76 // at least check that there is some timestamp
77 REQUIRE(dataFromSysrepo(client, modulePrefix, sysrepo::Datastore::Operational).count("/last-change") > 0);
78 }
79
80 SECTION("test components")
81 {
82 std::map<std::string, std::string> expected = {
83 {"[name='ne']", ""},
84 {"[name='ne']/name", "ne"},
85 {"[name='ne']/class", "iana-hardware:chassis"},
86 {"[name='ne']/mfg-name", "CESNET"},
87
88 {"[name='ne:fans']", ""},
89 {"[name='ne:fans']/class", "iana-hardware:module"},
90 {"[name='ne:fans']/name", "ne:fans"},
91 {"[name='ne:fans']/parent", "ne"},
92 {"[name='ne:fans:fan1']", ""},
93 {"[name='ne:fans:fan1']/class", "iana-hardware:fan"},
94 {"[name='ne:fans:fan1']/name", "ne:fans:fan1"},
95 {"[name='ne:fans:fan1']/parent", "ne:fans"},
96 {"[name='ne:fans:fan1:rpm']", ""},
97 {"[name='ne:fans:fan1:rpm']/class", "iana-hardware:sensor"},
98 {"[name='ne:fans:fan1:rpm']/name", "ne:fans:fan1:rpm"},
99 {"[name='ne:fans:fan1:rpm']/parent", "ne:fans:fan1"},
100 {"[name='ne:fans:fan1:rpm']/sensor-data", ""},
101 {"[name='ne:fans:fan1:rpm']/sensor-data/oper-status", "ok"},
102 {"[name='ne:fans:fan1:rpm']/sensor-data/value", "253"},
103 {"[name='ne:fans:fan1:rpm']/sensor-data/value-precision", "0"},
104 {"[name='ne:fans:fan1:rpm']/sensor-data/value-scale", "units"},
105 {"[name='ne:fans:fan1:rpm']/sensor-data/value-type", "rpm"},
106 {"[name='ne:fans:fan2']", ""},
107 {"[name='ne:fans:fan2']/class", "iana-hardware:fan"},
108 {"[name='ne:fans:fan2']/name", "ne:fans:fan2"},
109 {"[name='ne:fans:fan2']/parent", "ne:fans"},
110 {"[name='ne:fans:fan2:rpm']", ""},
111 {"[name='ne:fans:fan2:rpm']/class", "iana-hardware:sensor"},
112 {"[name='ne:fans:fan2:rpm']/name", "ne:fans:fan2:rpm"},
113 {"[name='ne:fans:fan2:rpm']/parent", "ne:fans:fan2"},
114 {"[name='ne:fans:fan2:rpm']/sensor-data", ""},
115 {"[name='ne:fans:fan2:rpm']/sensor-data/oper-status", "ok"},
116 {"[name='ne:fans:fan2:rpm']/sensor-data/value", "0"},
117 {"[name='ne:fans:fan2:rpm']/sensor-data/value-precision", "0"},
118 {"[name='ne:fans:fan2:rpm']/sensor-data/value-scale", "units"},
119 {"[name='ne:fans:fan2:rpm']/sensor-data/value-type", "rpm"},
120 {"[name='ne:fans:fan3']", ""},
121 {"[name='ne:fans:fan3']/class", "iana-hardware:fan"},
122 {"[name='ne:fans:fan3']/name", "ne:fans:fan3"},
123 {"[name='ne:fans:fan3']/parent", "ne:fans"},
124 {"[name='ne:fans:fan3:rpm']", ""},
125 {"[name='ne:fans:fan3:rpm']/class", "iana-hardware:sensor"},
126 {"[name='ne:fans:fan3:rpm']/name", "ne:fans:fan3:rpm"},
127 {"[name='ne:fans:fan3:rpm']/parent", "ne:fans:fan3"},
128 {"[name='ne:fans:fan3:rpm']/sensor-data", ""},
129 {"[name='ne:fans:fan3:rpm']/sensor-data/oper-status", "ok"},
130 {"[name='ne:fans:fan3:rpm']/sensor-data/value", "1280"},
131 {"[name='ne:fans:fan3:rpm']/sensor-data/value-precision", "0"},
132 {"[name='ne:fans:fan3:rpm']/sensor-data/value-scale", "units"},
133 {"[name='ne:fans:fan3:rpm']/sensor-data/value-type", "rpm"},
134 {"[name='ne:fans:fan4']", ""},
135 {"[name='ne:fans:fan4']/class", "iana-hardware:fan"},
136 {"[name='ne:fans:fan4']/name", "ne:fans:fan4"},
137 {"[name='ne:fans:fan4']/parent", "ne:fans"},
138 {"[name='ne:fans:fan4:rpm']", ""},
139 {"[name='ne:fans:fan4:rpm']/class", "iana-hardware:sensor"},
140 {"[name='ne:fans:fan4:rpm']/name", "ne:fans:fan4:rpm"},
141 {"[name='ne:fans:fan4:rpm']/parent", "ne:fans:fan4"},
142 {"[name='ne:fans:fan4:rpm']/sensor-data", ""},
143 {"[name='ne:fans:fan4:rpm']/sensor-data/oper-status", "ok"},
144 {"[name='ne:fans:fan4:rpm']/sensor-data/value", "666"},
145 {"[name='ne:fans:fan4:rpm']/sensor-data/value-precision", "0"},
146 {"[name='ne:fans:fan4:rpm']/sensor-data/value-scale", "units"},
147 {"[name='ne:fans:fan4:rpm']/sensor-data/value-type", "rpm"},
148
149 {"[name='ne:ctrl']", ""},
150 {"[name='ne:ctrl']/name", "ne:ctrl"},
151 {"[name='ne:ctrl']/parent", "ne"},
152 {"[name='ne:ctrl']/class", "iana-hardware:module"},
153
154 {"[name='ne:ctrl:temperature-cpu']", ""},
155 {"[name='ne:ctrl:temperature-cpu']/name", "ne:ctrl:temperature-cpu"},
156 {"[name='ne:ctrl:temperature-cpu']/class", "iana-hardware:sensor"},
157 {"[name='ne:ctrl:temperature-cpu']/parent", "ne:ctrl"},
158 {"[name='ne:ctrl:temperature-cpu']/sensor-data", ""},
159 {"[name='ne:ctrl:temperature-cpu']/sensor-data/oper-status", "ok"},
160 {"[name='ne:ctrl:temperature-cpu']/sensor-data/value", "41800"},
161 {"[name='ne:ctrl:temperature-cpu']/sensor-data/value-precision", "0"},
162 {"[name='ne:ctrl:temperature-cpu']/sensor-data/value-scale", "milli"},
163 {"[name='ne:ctrl:temperature-cpu']/sensor-data/value-type", "celsius"},
164
165 {"[name='ne:ctrl:power']", ""},
166 {"[name='ne:ctrl:power']/name", "ne:ctrl:power"},
167 {"[name='ne:ctrl:power']/class", "iana-hardware:sensor"},
168 {"[name='ne:ctrl:power']/parent", "ne:ctrl"},
169 {"[name='ne:ctrl:power']/sensor-data", ""},
170 {"[name='ne:ctrl:power']/sensor-data/oper-status", "ok"},
171 {"[name='ne:ctrl:power']/sensor-data/value", "14000000"},
172 {"[name='ne:ctrl:power']/sensor-data/value-precision", "0"},
173 {"[name='ne:ctrl:power']/sensor-data/value-scale", "micro"},
174 {"[name='ne:ctrl:power']/sensor-data/value-type", "watts"},
175
176 {"[name='ne:ctrl:voltage-in']", ""},
177 {"[name='ne:ctrl:voltage-in']/name", "ne:ctrl:voltage-in"},
178 {"[name='ne:ctrl:voltage-in']/class", "iana-hardware:sensor"},
179 {"[name='ne:ctrl:voltage-in']/parent", "ne:ctrl"},
180 {"[name='ne:ctrl:voltage-in']/sensor-data", ""},
181 {"[name='ne:ctrl:voltage-in']/sensor-data/oper-status", "ok"},
182 {"[name='ne:ctrl:voltage-in']/sensor-data/value", "220000"},
183 {"[name='ne:ctrl:voltage-in']/sensor-data/value-precision", "0"},
184 {"[name='ne:ctrl:voltage-in']/sensor-data/value-scale", "milli"},
185 {"[name='ne:ctrl:voltage-in']/sensor-data/value-type", "volts-AC"},
186 {"[name='ne:ctrl:voltage-out']", ""},
187 {"[name='ne:ctrl:voltage-out']/name", "ne:ctrl:voltage-out"},
188 {"[name='ne:ctrl:voltage-out']/class", "iana-hardware:sensor"},
189 {"[name='ne:ctrl:voltage-out']/parent", "ne:ctrl"},
190 {"[name='ne:ctrl:voltage-out']/sensor-data", ""},
191 {"[name='ne:ctrl:voltage-out']/sensor-data/oper-status", "ok"},
192 {"[name='ne:ctrl:voltage-out']/sensor-data/value", "12000"},
193 {"[name='ne:ctrl:voltage-out']/sensor-data/value-precision", "0"},
194 {"[name='ne:ctrl:voltage-out']/sensor-data/value-scale", "milli"},
195 {"[name='ne:ctrl:voltage-out']/sensor-data/value-type", "volts-DC"},
196
197 {"[name='ne:ctrl:current']", ""},
198 {"[name='ne:ctrl:current']/name", "ne:ctrl:current"},
199 {"[name='ne:ctrl:current']/class", "iana-hardware:sensor"},
200 {"[name='ne:ctrl:current']/parent", "ne:ctrl"},
201 {"[name='ne:ctrl:current']/sensor-data", ""},
202 {"[name='ne:ctrl:current']/sensor-data/oper-status", "ok"},
203 {"[name='ne:ctrl:current']/sensor-data/value", "200"},
204 {"[name='ne:ctrl:current']/sensor-data/value-precision", "0"},
205 {"[name='ne:ctrl:current']/sensor-data/value-scale", "milli"},
206 {"[name='ne:ctrl:current']/sensor-data/value-type", "amperes"},
207
208 {"[name='ne:ctrl:emmc']", ""},
209 {"[name='ne:ctrl:emmc']/name", "ne:ctrl:emmc"},
210 {"[name='ne:ctrl:emmc']/parent", "ne:ctrl"},
211 {"[name='ne:ctrl:emmc']/class", "iana-hardware:module"},
212 {"[name='ne:ctrl:emmc']/serial-num", "0x00a8808d"},
213 {"[name='ne:ctrl:emmc']/mfg-date", "2017-02-01T00:00:00-00:00"},
214 {"[name='ne:ctrl:emmc']/model-name", "8GME4R"},
215 {"[name='ne:ctrl:emmc:lifetime']", ""},
216 {"[name='ne:ctrl:emmc:lifetime']/name", "ne:ctrl:emmc:lifetime"},
217 {"[name='ne:ctrl:emmc:lifetime']/class", "iana-hardware:sensor"},
218 {"[name='ne:ctrl:emmc:lifetime']/parent", "ne:ctrl:emmc"},
219 {"[name='ne:ctrl:emmc:lifetime']/sensor-data", ""},
220 {"[name='ne:ctrl:emmc:lifetime']/sensor-data/oper-status", "ok"},
221 {"[name='ne:ctrl:emmc:lifetime']/sensor-data/value", "40"},
222 {"[name='ne:ctrl:emmc:lifetime']/sensor-data/value-precision", "0"},
223 {"[name='ne:ctrl:emmc:lifetime']/sensor-data/value-scale", "units"},
224 {"[name='ne:ctrl:emmc:lifetime']/sensor-data/value-type", "other"},
225 {"[name='ne:ctrl:emmc:lifetime']/sensor-data/units-display", "percent"},
226 };
227
228 REQUIRE(dataFromSysrepo(client, modulePrefix + "/component", sysrepo::Datastore::Operational) == expected);
229 }
230
231 SECTION("test leafnode query")
232 {
233 const auto xpath = modulePrefix + "/component[name='ne:ctrl:emmc:lifetime']/class";
234 client.switchDatastore(sysrepo::Datastore::Operational);
235 auto val = client.getData(xpath);
236 client.switchDatastore(sysrepo::Datastore::Running);
237 REQUIRE(val);
238 REQUIRE(val->findPath(xpath)->asTerm().valueStr() == "iana-hardware:sensor"s);
239 }
240}
241