Tomáš Pecka | 3f81196 | 2023-04-14 10:54:32 +0200 | [diff] [blame] | 1 | #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 | |
| 9 | using namespace std::literals; |
| 10 | |
| 11 | TEST_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áš Pecka | 3f81196 | 2023-04-14 10:54:32 +0200 | [diff] [blame] | 21 | auto sysfsTempCpu = std::make_shared<FakeHWMon>(); |
Tomáš Pecka | 3f81196 | 2023-04-14 10:54:32 +0200 | [diff] [blame] | 22 | auto sysfsPower = std::make_shared<FakeHWMon>(); |
Tomáš Pecka | 3f81196 | 2023-04-14 10:54:32 +0200 | [diff] [blame] | 23 | |
| 24 | REQUIRE_CALL(*sysfsTempCpu, attribute("temp1_input")).RETURN(41800); |
Tomáš Pecka | 3f81196 | 2023-04-14 10:54:32 +0200 | [diff] [blame] | 25 | REQUIRE_CALL(*sysfsPower, attribute("power1_input")).RETURN(14000000); |
Tomáš Pecka | 3f81196 | 2023-04-14 10:54:32 +0200 | [diff] [blame] | 26 | |
Tomáš Pecka | 3f81196 | 2023-04-14 10:54:32 +0200 | [diff] [blame] | 27 | using velia::ietf_hardware::data_reader::SensorType; |
| 28 | using velia::ietf_hardware::data_reader::StaticData; |
| 29 | using velia::ietf_hardware::data_reader::SysfsValue; |
Tomáš Pecka | e5366c6 | 2023-04-14 11:03:04 +0200 | [diff] [blame] | 30 | |
Tomáš Pecka | 3f81196 | 2023-04-14 10:54:32 +0200 | [diff] [blame] | 31 | // register components into hw state |
| 32 | ietfHardware->registerDataReader(StaticData("ne", std::nullopt, {{"class", "iana-hardware:chassis"}, {"mfg-name", "CESNET"s}})); |
Tomáš Pecka | e5366c6 | 2023-04-14 11:03:04 +0200 | [diff] [blame] | 33 | ietfHardware->registerDataReader(SysfsValue<SensorType::Temperature>("ne:temperature-cpu", "ne", sysfsTempCpu, 1)); |
| 34 | ietfHardware->registerDataReader(SysfsValue<SensorType::Power>("ne:power", "ne", sysfsPower, 1)); |
Tomáš Pecka | 3f81196 | 2023-04-14 10:54:32 +0200 | [diff] [blame] | 35 | |
| 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áš Pecka | e5366c6 | 2023-04-14 11:03:04 +0200 | [diff] [blame] | 52 | {"[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áš Pecka | 3f81196 | 2023-04-14 10:54:32 +0200 | [diff] [blame] | 62 | |
Tomáš Pecka | e5366c6 | 2023-04-14 11:03:04 +0200 | [diff] [blame] | 63 | {"[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áš Pecka | 3f81196 | 2023-04-14 10:54:32 +0200 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | REQUIRE(dataFromSysrepo(client, modulePrefix + "/component", sysrepo::Datastore::Operational) == expected); |
Tomáš Pecka | cb089ac | 2023-04-21 14:54:26 +0200 | [diff] [blame^] | 76 | |
| 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áš Pecka | 3f81196 | 2023-04-14 10:54:32 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | SECTION("test leafnode query") |
| 88 | { |
Tomáš Pecka | e5366c6 | 2023-04-14 11:03:04 +0200 | [diff] [blame] | 89 | const auto xpath = modulePrefix + "/component[name='ne:power']/class"; |
Tomáš Pecka | 3f81196 | 2023-04-14 10:54:32 +0200 | [diff] [blame] | 90 | 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 | |