Tomáš Pecka | af7b704 | 2021-04-20 20:14:13 +0200 | [diff] [blame] | 1 | #include "trompeloeil_doctest.h" |
| 2 | #include "dbus-helpers/dbus_rauc_server.h" |
Tomáš Pecka | 5be83e4 | 2021-04-21 17:26:40 +0200 | [diff] [blame] | 3 | #include "fs-helpers/utils.h" |
Tomáš Pecka | af7b704 | 2021-04-20 20:14:13 +0200 | [diff] [blame] | 4 | #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áš Pecka | 5be83e4 | 2021-04-21 17:26:40 +0200 | [diff] [blame] | 9 | #include "utils/io.h" |
Tomáš Pecka | af7b704 | 2021-04-20 20:14:13 +0200 | [diff] [blame] | 10 | |
| 11 | using namespace std::literals; |
| 12 | |
| 13 | TEST_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 | |
Jan Kundrát | af90572 | 2021-12-01 20:00:05 +0100 | [diff] [blame] | 21 | auto fakeSysfsDir = std::filesystem::path {CMAKE_CURRENT_BINARY_DIR + "/tests/sysrepo_system-leds/"s}; |
Tomáš Pecka | 5be83e4 | 2021-04-21 17:26:40 +0200 | [diff] [blame] | 22 | removeDirectoryTreeIfExists(fakeSysfsDir); |
| 23 | std::filesystem::copy(CMAKE_CURRENT_SOURCE_DIR + "/tests/sysfs/leds"s, fakeSysfsDir, std::filesystem::copy_options::recursive); |
| 24 | |
Tomáš Pecka | 63b2d65 | 2021-04-22 19:03:14 +0200 | [diff] [blame] | 25 | const auto WAIT = 125ms /* poll interval */ + 10ms /* just to be sure */; |
| 26 | |
Tomáš Pecka | af7b704 | 2021-04-20 20:14:13 +0200 | [diff] [blame] | 27 | velia::system::LED led(srConn, fakeSysfsDir); |
| 28 | |
| 29 | std::this_thread::sleep_for(10ms); |
| 30 | |
| 31 | REQUIRE(dataFromSysrepo(client, "/czechlight-system:leds", SR_DS_OPERATIONAL) == std::map<std::string, std::string> { |
| 32 | {"/led[name='line:green']", ""}, |
| 33 | {"/led[name='line:green']/brightness", "100"}, |
| 34 | {"/led[name='line:green']/name", "line:green"}, |
| 35 | {"/led[name='uid:blue']", ""}, |
| 36 | {"/led[name='uid:blue']/brightness", "0"}, |
| 37 | {"/led[name='uid:blue']/name", "uid:blue"}, |
| 38 | {"/led[name='uid:green']", ""}, |
| 39 | {"/led[name='uid:green']/brightness", "39"}, |
| 40 | {"/led[name='uid:green']/name", "uid:green"}, |
| 41 | {"/led[name='uid:red']", ""}, |
| 42 | {"/led[name='uid:red']/brightness", "100"}, |
| 43 | {"/led[name='uid:red']/name", "uid:red"}, |
| 44 | }); |
Tomáš Pecka | 5be83e4 | 2021-04-21 17:26:40 +0200 | [diff] [blame] | 45 | |
Tomáš Pecka | 63b2d65 | 2021-04-22 19:03:14 +0200 | [diff] [blame] | 46 | SECTION("Change uid:green brightness") |
Tomáš Pecka | 5be83e4 | 2021-04-21 17:26:40 +0200 | [diff] [blame] | 47 | { |
Tomáš Pecka | 63b2d65 | 2021-04-22 19:03:14 +0200 | [diff] [blame] | 48 | velia::utils::writeFile(fakeSysfsDir / "uid:green" / "brightness", "0"); |
| 49 | std::this_thread::sleep_for(WAIT); |
| 50 | |
| 51 | REQUIRE(dataFromSysrepo(client, "/czechlight-system:leds", SR_DS_OPERATIONAL) == std::map<std::string, std::string> { |
| 52 | {"/led[name='line:green']", ""}, |
| 53 | {"/led[name='line:green']/brightness", "100"}, |
| 54 | {"/led[name='line:green']/name", "line:green"}, |
| 55 | {"/led[name='uid:blue']", ""}, |
| 56 | {"/led[name='uid:blue']/brightness", "0"}, |
| 57 | {"/led[name='uid:blue']/name", "uid:blue"}, |
| 58 | {"/led[name='uid:green']", ""}, |
| 59 | {"/led[name='uid:green']/brightness", "0"}, |
| 60 | {"/led[name='uid:green']/name", "uid:green"}, |
| 61 | {"/led[name='uid:red']", ""}, |
| 62 | {"/led[name='uid:red']/brightness", "100"}, |
| 63 | {"/led[name='uid:red']/name", "uid:red"}, |
| 64 | }); |
Tomáš Pecka | 5be83e4 | 2021-04-21 17:26:40 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Tomáš Pecka | 63b2d65 | 2021-04-22 19:03:14 +0200 | [diff] [blame] | 67 | SECTION("Test UID RPC") |
Tomáš Pecka | 5be83e4 | 2021-04-21 17:26:40 +0200 | [diff] [blame] | 68 | { |
Tomáš Pecka | 63b2d65 | 2021-04-22 19:03:14 +0200 | [diff] [blame] | 69 | std::shared_ptr<sysrepo::Vals> rpcInput = std::make_shared<sysrepo::Vals>(1); |
| 70 | std::string state; |
| 71 | std::string expectedTrigger; |
| 72 | std::string expectedBrightness; |
Tomáš Pecka | 5be83e4 | 2021-04-21 17:26:40 +0200 | [diff] [blame] | 73 | |
Tomáš Pecka | 63b2d65 | 2021-04-22 19:03:14 +0200 | [diff] [blame] | 74 | /* 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). |
| 75 | * |
| 76 | * [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 |
| 77 | * |
| 78 | * 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. |
| 79 | * 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. |
| 80 | * Also, I'm not implementing the 'timer' trigger behaviour, so the value written to the brightness file is static. |
| 81 | */ |
| 82 | REQUIRE(velia::utils::readFileString(fakeSysfsDir / "uid:blue" / "trigger") == "none"); |
Tomáš Pecka | 5be83e4 | 2021-04-21 17:26:40 +0200 | [diff] [blame] | 83 | |
Tomáš Pecka | 63b2d65 | 2021-04-22 19:03:14 +0200 | [diff] [blame] | 84 | SECTION("UID led on") |
| 85 | { |
| 86 | state = "on"; |
| 87 | expectedTrigger = "none"; |
| 88 | expectedBrightness = "256"; |
| 89 | } |
| 90 | |
| 91 | SECTION("UID led off") |
| 92 | { |
| 93 | state = "off"; |
| 94 | expectedTrigger = "none"; |
| 95 | expectedBrightness = "0"; |
| 96 | } |
| 97 | |
| 98 | SECTION("UID led blinking") |
| 99 | { |
| 100 | state = "blinking"; |
| 101 | expectedTrigger = "timer"; |
| 102 | expectedBrightness = "256"; |
| 103 | } |
| 104 | |
| 105 | rpcInput->val(0)->set("/czechlight-system:leds/uid/state", state.c_str()); |
| 106 | auto res = client->rpc_send("/czechlight-system:leds/uid", rpcInput); |
| 107 | REQUIRE(res->val_cnt() == 0); |
| 108 | REQUIRE(velia::utils::readFileString(fakeSysfsDir / "uid:blue" / "trigger") == expectedTrigger); |
| 109 | REQUIRE(velia::utils::readFileString(fakeSysfsDir / "uid:blue" / "brightness") == expectedBrightness); |
| 110 | } |
Tomáš Pecka | af7b704 | 2021-04-20 20:14:13 +0200 | [diff] [blame] | 111 | } |