Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 1 | #include "trompeloeil_doctest.h" |
| 2 | #include "dbus-helpers/dbus_rauc_server.h" |
| 3 | #include "pretty_printers.h" |
Tomáš Pecka | 594a676 | 2021-01-29 11:06:08 +0100 | [diff] [blame^] | 4 | #include "system/Firmware.h" |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 5 | #include "test_log_setup.h" |
| 6 | #include "test_sysrepo_helpers.h" |
| 7 | #include "tests/configure.cmake.h" |
Tomáš Pecka | 9a6e15b | 2021-01-25 18:57:48 +0100 | [diff] [blame] | 8 | #include "tests/mock/sysrepo/events.h" |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 9 | |
| 10 | using namespace std::literals; |
| 11 | |
Tomáš Pecka | 594a676 | 2021-01-29 11:06:08 +0100 | [diff] [blame^] | 12 | TEST_CASE("Firmware in czechlight-system") |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 13 | { |
| 14 | trompeloeil::sequence seq1; |
| 15 | |
| 16 | TEST_SYSREPO_INIT_LOGS; |
| 17 | TEST_SYSREPO_INIT; |
| 18 | TEST_SYSREPO_INIT_CLIENT; |
| 19 | |
| 20 | auto dbusServerConnection = sdbus::createSessionBusConnection("de.pengutronix.rauc"); |
| 21 | auto dbusClientConnection = sdbus::createSessionBusConnection(); |
| 22 | dbusClientConnection->enterEventLoopAsync(); |
| 23 | dbusServerConnection->enterEventLoopAsync(); |
| 24 | |
| 25 | std::map<std::string, velia::system::RAUC::SlotProperties> dbusRaucStatus = { |
| 26 | {"rootfs.1", { |
| 27 | {"activated.count", uint32_t {39}}, |
| 28 | {"activated.timestamp", "2021-01-13T17:20:18Z"}, |
| 29 | {"bootname", "B"}, |
| 30 | {"boot-status", "good"}, |
| 31 | {"bundle.compatible", "czechlight-clearfog"}, |
| 32 | {"bundle.version", "v4-103-g34d2f48"}, |
| 33 | {"class", "rootfs"}, |
| 34 | {"device", "/dev/mmcblk0p3"}, |
| 35 | {"installed.count", uint32_t {39}}, |
| 36 | {"installed.timestamp", "2021-01-13T17:20:15Z"}, |
| 37 | {"mountpoint", "/"}, |
| 38 | {"sha256", "07b30d065c7aad64d2006ce99fd339c929d3ca97b666fca4584b9ef726469fc4"}, |
| 39 | {"size", uint64_t {45601892}}, |
| 40 | {"state", "booted"}, |
| 41 | {"status", "ok"}, |
| 42 | {"type", "ext4"}, |
| 43 | }}, |
| 44 | {"rootfs.0", { |
| 45 | {"activated.count", uint32_t {41}}, |
| 46 | {"activated.timestamp", "2021-01-13T17:15:54Z"}, |
| 47 | {"bootname", "A"}, |
| 48 | {"boot-status", "bad"}, |
| 49 | {"bundle.compatible", "czechlight-clearfog"}, |
| 50 | {"bundle.version", "v4-104-ge80fcd4"}, |
| 51 | {"class", "rootfs"}, |
| 52 | {"device", "/dev/mmcblk0p1"}, |
| 53 | {"installed.count", uint32_t {41}}, |
| 54 | {"installed.timestamp", "2021-01-13T17:15:50Z"}, |
| 55 | {"sha256", "6d81e8f341edd17c127811f7347c7e23d18c2fc25c0bdc29ac56999cc9c25629"}, |
| 56 | {"size", uint64_t {45647664}}, |
| 57 | {"state", "inactive"}, |
| 58 | {"status", "ok"}, |
| 59 | {"type", "ext4"}, |
| 60 | }}, |
| 61 | {"cfg.1", { |
| 62 | {"bundle.compatible", "czechlight-clearfog"}, |
| 63 | {"bundle.version", "v4-103-g34d2f48"}, |
| 64 | {"class", "cfg"}, |
| 65 | {"device", "/dev/mmcblk0p4"}, |
| 66 | {"installed.count", uint32_t {39}}, |
| 67 | {"installed.timestamp", "2021-01-13T17:20:18Z"}, |
| 68 | {"mountpoint", "/cfg"}, |
| 69 | {"parent", "rootfs.1"}, |
| 70 | {"sha256", "5ca1b6c461fc194055d52b181f57c63dc1d34c19d041f6395e6f6abc039692bb"}, |
| 71 | {"size", uint64_t {108}}, |
| 72 | {"state", "active"}, |
| 73 | {"status", "ok"}, |
| 74 | {"type", "ext4"}, |
| 75 | }}, |
| 76 | {"cfg.0", { |
| 77 | {"bundle.compatible", "czechlight-clearfog"}, |
| 78 | {"bundle.version", "v4-104-ge80fcd4"}, |
| 79 | {"class", "cfg"}, |
| 80 | {"device", "/dev/mmcblk0p2"}, |
| 81 | {"installed.count", uint32_t {41}}, |
| 82 | {"installed.timestamp", "2021-01-13T17:15:54Z"}, |
| 83 | {"parent", "rootfs.0"}, |
| 84 | {"sha256", "5ca1b6c461fc194055d52b181f57c63dc1d34c19d041f6395e6f6abc039692bb"}, |
| 85 | {"size", uint64_t {108}}, |
| 86 | {"state", "inactive"}, |
| 87 | {"status", "ok"}, |
| 88 | {"type", "ext4"}, |
| 89 | }}, |
| 90 | }; |
| 91 | auto raucServer = DBusRAUCServer(*dbusServerConnection, "rootfs.1", dbusRaucStatus); |
Tomáš Pecka | 594a676 | 2021-01-29 11:06:08 +0100 | [diff] [blame^] | 92 | auto sysrepo = std::make_shared<velia::system::Firmware>(srConn, *dbusClientConnection); |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 93 | |
| 94 | REQUIRE(dataFromSysrepo(client, "/czechlight-system:firmware", SR_DS_OPERATIONAL) == std::map<std::string, std::string>{ |
| 95 | {"/installation", ""}, |
| 96 | {"/installation/message", ""}, |
| 97 | {"/installation/status", "none"}, |
| 98 | }); |
| 99 | |
| 100 | SECTION("Firmware install RPC") |
| 101 | { |
| 102 | auto rpcInput = std::make_shared<sysrepo::Vals>(1); |
| 103 | rpcInput->val(0)->set("/czechlight-system:firmware/installation/install/url", "/path/to/bundle/update.raucb"); |
| 104 | |
| 105 | SECTION("Installation runs") |
| 106 | { |
| 107 | DBusRAUCServer::InstallBehaviour installType; |
| 108 | std::map<std::string, std::string> expectedFinished, expectedInProgress = { |
| 109 | {"/installation", ""}, |
| 110 | {"/installation/message", ""}, |
| 111 | {"/installation/status", "in-progress"}, |
| 112 | }; |
Tomáš Pecka | 9a6e15b | 2021-01-25 18:57:48 +0100 | [diff] [blame] | 113 | size_t expectedNotificationsCount; |
| 114 | std::string expectedLastNotificationMsg; |
| 115 | |
| 116 | // subscribe to notifications |
| 117 | EventWatcher events; |
| 118 | subscription->event_notif_subscribe("czechlight-system", events, "/czechlight-system:firmware/installation/update"); |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 119 | |
| 120 | SECTION("Successfull install") |
| 121 | { |
| 122 | installType = DBusRAUCServer::InstallBehaviour::OK; |
| 123 | expectedFinished = { |
| 124 | {"/installation", ""}, |
| 125 | {"/installation/message", ""}, |
| 126 | {"/installation/status", "succeeded"}, |
| 127 | }; |
Tomáš Pecka | 9a6e15b | 2021-01-25 18:57:48 +0100 | [diff] [blame] | 128 | expectedNotificationsCount = 22; |
| 129 | expectedLastNotificationMsg = "Installing done."; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | SECTION("Unsuccessfull install") |
| 133 | { |
| 134 | installType = DBusRAUCServer::InstallBehaviour::FAILURE; |
| 135 | expectedFinished = { |
| 136 | {"/installation", ""}, |
| 137 | {"/installation/message", "Failed to download bundle https://10.88.3.11:8000/update.raucb: Transfer failed: error:1408F10B:SSL routines:ssl3_get_record:wrong version number"}, |
| 138 | {"/installation/status", "failed"}, |
| 139 | }; |
Tomáš Pecka | 9a6e15b | 2021-01-25 18:57:48 +0100 | [diff] [blame] | 140 | expectedNotificationsCount = 6; |
| 141 | expectedLastNotificationMsg = "Installing failed."; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | raucServer.installBundleBehaviour(installType); |
| 145 | auto res = client->rpc_send("/czechlight-system:firmware/installation/install", rpcInput); |
| 146 | REQUIRE(res->val_cnt() == 0); |
| 147 | |
| 148 | std::this_thread::sleep_for(10ms); // lets wait a while, so the RAUC's callback for operation changed takes place |
| 149 | REQUIRE(dataFromSysrepo(client, "/czechlight-system:firmware", SR_DS_OPERATIONAL) == expectedInProgress); |
| 150 | |
| 151 | std::this_thread::sleep_for(2s); // lets wait a while, so the installation can finish |
| 152 | REQUIRE(dataFromSysrepo(client, "/czechlight-system:firmware", SR_DS_OPERATIONAL) == expectedFinished); |
Tomáš Pecka | 9a6e15b | 2021-01-25 18:57:48 +0100 | [diff] [blame] | 153 | |
| 154 | // check updates notification count and that at least some of them are reasonable |
| 155 | REQUIRE(events.count() == expectedNotificationsCount); |
| 156 | REQUIRE(events.peek(0).data["/czechlight-system:firmware/installation/update/message"] == "Installing"); |
| 157 | REQUIRE(events.peek(0).data["/czechlight-system:firmware/installation/update/progress"] == "0"); |
| 158 | REQUIRE(events.peek(events.count() - 1).data["/czechlight-system:firmware/installation/update/message"] == expectedLastNotificationMsg); |
| 159 | REQUIRE(events.peek(events.count() - 1).data["/czechlight-system:firmware/installation/update/progress"] == "100"); |
| 160 | |
| 161 | // check updates notification progress is an increasing sequence |
| 162 | for (size_t i = 1; i < events.count(); i++) { |
| 163 | auto prevProgress = std::stoi(events.peek(i - 1).data["/czechlight-system:firmware/installation/update/progress"]); |
| 164 | auto currProgress = std::stoi(events.peek(i).data["/czechlight-system:firmware/installation/update/progress"]); |
| 165 | REQUIRE(prevProgress <= currProgress); |
| 166 | } |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | SECTION("Invoke another installation before the first finishes") |
| 170 | { |
| 171 | client->rpc_send("/czechlight-system:firmware/installation/install", rpcInput); |
| 172 | std::this_thread::sleep_for(10ms); |
| 173 | REQUIRE_THROWS_WITH_AS(client->rpc_send("/czechlight-system:firmware/installation/install", rpcInput), "User callback failed", sysrepo::sysrepo_exception); |
| 174 | } |
| 175 | } |
| 176 | } |