blob: 89b599fa7bee70b023afa1d4792538b3650c8f32 [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
Tomáš Pecka3f811962023-04-14 10:54:32 +020021 auto sysfsTempCpu = std::make_shared<FakeHWMon>();
Tomáš Pecka3f811962023-04-14 10:54:32 +020022 auto sysfsPower = std::make_shared<FakeHWMon>();
Tomáš Pecka3f811962023-04-14 10:54:32 +020023
24 REQUIRE_CALL(*sysfsTempCpu, attribute("temp1_input")).RETURN(41800);
Tomáš Pecka3f811962023-04-14 10:54:32 +020025 REQUIRE_CALL(*sysfsPower, attribute("power1_input")).RETURN(14000000);
Tomáš Pecka3f811962023-04-14 10:54:32 +020026
Tomáš Pecka3f811962023-04-14 10:54:32 +020027 using velia::ietf_hardware::data_reader::SensorType;
28 using velia::ietf_hardware::data_reader::StaticData;
29 using velia::ietf_hardware::data_reader::SysfsValue;
Tomáš Peckae5366c62023-04-14 11:03:04 +020030
Tomáš Pecka3f811962023-04-14 10:54:32 +020031 // register components into hw state
32 ietfHardware->registerDataReader(StaticData("ne", std::nullopt, {{"class", "iana-hardware:chassis"}, {"mfg-name", "CESNET"s}}));
Tomáš Peckae5366c62023-04-14 11:03:04 +020033 ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:temperature-cpu", "ne", sysfsTempCpu, 1));
34 ietfHardware->registerDataReader(SysfsValue<SensorType::Power>("ne:power", "ne", sysfsPower, 1));
Tomáš Pecka3f811962023-04-14 10:54:32 +020035
36 auto ietfHardwareSysrepo = std::make_shared<velia::ietf_hardware::sysrepo::Sysrepo>(srSess, ietfHardware);
37
38 SECTION("test last-change")
39 {
40 // at least check that there is some timestamp
41 REQUIRE(dataFromSysrepo(client, modulePrefix, sysrepo::Datastore::Operational).count("/last-change") > 0);
42 }
43
44 SECTION("test components")
45 {
46 std::map<std::string, std::string> expected = {
47 {"[name='ne']", ""},
48 {"[name='ne']/name", "ne"},
49 {"[name='ne']/class", "iana-hardware:chassis"},
50 {"[name='ne']/mfg-name", "CESNET"},
51
Tomáš Peckae5366c62023-04-14 11:03:04 +020052 {"[name='ne:temperature-cpu']", ""},
53 {"[name='ne:temperature-cpu']/name", "ne:temperature-cpu"},
54 {"[name='ne:temperature-cpu']/class", "iana-hardware:sensor"},
55 {"[name='ne:temperature-cpu']/parent", "ne"},
56 {"[name='ne:temperature-cpu']/sensor-data", ""},
57 {"[name='ne:temperature-cpu']/sensor-data/oper-status", "ok"},
58 {"[name='ne:temperature-cpu']/sensor-data/value", "41800"},
59 {"[name='ne:temperature-cpu']/sensor-data/value-precision", "0"},
60 {"[name='ne:temperature-cpu']/sensor-data/value-scale", "milli"},
61 {"[name='ne:temperature-cpu']/sensor-data/value-type", "celsius"},
Tomáš Pecka3f811962023-04-14 10:54:32 +020062
Tomáš Peckae5366c62023-04-14 11:03:04 +020063 {"[name='ne:power']", ""},
64 {"[name='ne:power']/name", "ne:power"},
65 {"[name='ne:power']/class", "iana-hardware:sensor"},
66 {"[name='ne:power']/parent", "ne"},
67 {"[name='ne:power']/sensor-data", ""},
68 {"[name='ne:power']/sensor-data/oper-status", "ok"},
69 {"[name='ne:power']/sensor-data/value", "14000000"},
70 {"[name='ne:power']/sensor-data/value-precision", "0"},
71 {"[name='ne:power']/sensor-data/value-scale", "micro"},
72 {"[name='ne:power']/sensor-data/value-type", "watts"},
Tomáš Pecka3f811962023-04-14 10:54:32 +020073 };
74
75 REQUIRE(dataFromSysrepo(client, modulePrefix + "/component", sysrepo::Datastore::Operational) == expected);
Tomáš Peckacb089ac2023-04-21 14:54:26 +020076
77 // data changes
78 REQUIRE_CALL(*sysfsTempCpu, attribute("temp1_input")).RETURN(222);
79 REQUIRE_CALL(*sysfsPower, attribute("power1_input")).RETURN(333);
80
81 expected["[name='ne:temperature-cpu']/sensor-data/value"] = "222";
82 expected["[name='ne:power']/sensor-data/value"] = "333";
83
84 REQUIRE(dataFromSysrepo(client, modulePrefix + "/component", sysrepo::Datastore::Operational) == expected);
Tomáš Pecka3f811962023-04-14 10:54:32 +020085 }
86
87 SECTION("test leafnode query")
88 {
Tomáš Peckae5366c62023-04-14 11:03:04 +020089 const auto xpath = modulePrefix + "/component[name='ne:power']/class";
Tomáš Pecka3f811962023-04-14 10:54:32 +020090 client.switchDatastore(sysrepo::Datastore::Operational);
91 auto val = client.getData(xpath);
92 client.switchDatastore(sysrepo::Datastore::Running);
93 REQUIRE(val);
94 REQUIRE(val->findPath(xpath)->asTerm().valueStr() == "iana-hardware:sensor"s);
95 }
96}
97