blob: db648a4ee52736fdb964d1a77a0fdb6a4a49bce3 [file] [log] [blame]
Tomáš Peckaaf7b7042021-04-20 20:14:13 +02001#include "trompeloeil_doctest.h"
2#include "dbus-helpers/dbus_rauc_server.h"
Tomáš Pecka5be83e42021-04-21 17:26:40 +02003#include "fs-helpers/utils.h"
Tomáš Peckaaf7b7042021-04-20 20:14:13 +02004#include "pretty_printers.h"
5#include "system/LED.h"
6#include "test_log_setup.h"
7#include "test_sysrepo_helpers.h"
8#include "tests/configure.cmake.h"
Tomáš Pecka5be83e42021-04-21 17:26:40 +02009#include "utils/io.h"
Tomáš Peckaaf7b7042021-04-20 20:14:13 +020010
11using namespace std::literals;
12
13TEST_CASE("Sysrepo reports system LEDs")
14{
15 trompeloeil::sequence seq1;
16
17 TEST_SYSREPO_INIT_LOGS;
18 TEST_SYSREPO_INIT;
19 TEST_SYSREPO_INIT_CLIENT;
20
Tomáš Pecka5be83e42021-04-21 17:26:40 +020021 auto fakeSysfsDir = std::filesystem::path {CMAKE_CURRENT_BINARY_DIR + "/tests/leds/"s};
22 removeDirectoryTreeIfExists(fakeSysfsDir);
23 std::filesystem::copy(CMAKE_CURRENT_SOURCE_DIR + "/tests/sysfs/leds"s, fakeSysfsDir, std::filesystem::copy_options::recursive);
24
Tomáš Peckaaf7b7042021-04-20 20:14:13 +020025 velia::system::LED led(srConn, fakeSysfsDir);
26
27 std::this_thread::sleep_for(10ms);
28
29 REQUIRE(dataFromSysrepo(client, "/czechlight-system:leds", SR_DS_OPERATIONAL) == std::map<std::string, std::string> {
30 {"/led[name='line:green']", ""},
31 {"/led[name='line:green']/brightness", "100"},
32 {"/led[name='line:green']/name", "line:green"},
33 {"/led[name='uid:blue']", ""},
34 {"/led[name='uid:blue']/brightness", "0"},
35 {"/led[name='uid:blue']/name", "uid:blue"},
36 {"/led[name='uid:green']", ""},
37 {"/led[name='uid:green']/brightness", "39"},
38 {"/led[name='uid:green']/name", "uid:green"},
39 {"/led[name='uid:red']", ""},
40 {"/led[name='uid:red']/brightness", "100"},
41 {"/led[name='uid:red']/name", "uid:red"},
42 });
Tomáš Pecka5be83e42021-04-21 17:26:40 +020043
44 std::shared_ptr<sysrepo::Vals> rpcInput = std::make_shared<sysrepo::Vals>(1);
45 std::string state;
46 std::string expectedTrigger;
47 std::string expectedBrightness;
48
49 /* This isn't what actually happens in real-life. The contents of the trigger file is usually something like this (i.e., list of available triggers).
50 *
51 * [none] kbd-scrolllock kbd-numlock kbd-capslock kbd-kanalock kbd-shiftlock kbd-altgrlock kbd-ctrllock kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ctrlllock kbd-ctrlrlock mmc0 timer oneshot heartbeat gpio default-on transient panic netdev f1072004.mdio-mii:01:link f1072004.mdio-mii:01:1Gbps f1072004.mdio-mii:01:100Mbps f1072004.mdio-mii:01:10Mbps f1072004.mdio-mii:00:link f1072004.mdio-mii:00:1Gbps f1072004.mdio-mii:00:100Mbps f1072004.mdio-mii:00:10Mbps
52 *
53 * The value enclosed in brackets is the current active trigger. You can change it by writing a value corresponding to a trigger to the trigger file.
54 * I'm not going to simulate sysfs led behaviour here, so just test that the original contents was "none" and the value written by the RPC is the expected value.
55 * Also, I'm not implementing the 'timer' trigger behaviour, so the value written to the brightness file is static.
56 */
57 REQUIRE(velia::utils::readFileString(fakeSysfsDir / "uid:blue" / "trigger") == "none");
58
59 SECTION("UID led on")
60 {
61 state = "on";
62 expectedTrigger = "none";
63 expectedBrightness = "256";
64 }
65
66 SECTION("UID led off")
67 {
68 state = "off";
69 expectedTrigger = "none";
70 expectedBrightness = "0";
71 }
72
73 SECTION("UID led blinking")
74 {
75 state = "blinking";
76 expectedTrigger = "timer";
77 expectedBrightness = "256";
78 }
79
80 rpcInput->val(0)->set("/czechlight-system:leds/uid/state", state.c_str());
81 auto res = client->rpc_send("/czechlight-system:leds/uid", rpcInput);
82 REQUIRE(res->val_cnt() == 0);
83 REQUIRE(velia::utils::readFileString(fakeSysfsDir / "uid:blue" / "trigger") == expectedTrigger);
84 REQUIRE(velia::utils::readFileString(fakeSysfsDir / "uid:blue" / "brightness") == expectedBrightness);
Tomáš Peckaaf7b7042021-04-20 20:14:13 +020085}