blob: 67765189ade7e30943d9a3e1d33a29cb12251786 [file] [log] [blame]
Tomáš Peckacb7a5f82021-01-20 15:12:00 +01001#include "trompeloeil_doctest.h"
2#include "dbus-helpers/dbus_rauc_server.h"
3#include "pretty_printers.h"
Tomáš Pecka594a6762021-01-29 11:06:08 +01004#include "system/Firmware.h"
Tomáš Peckacb7a5f82021-01-20 15:12:00 +01005#include "test_log_setup.h"
6#include "test_sysrepo_helpers.h"
7#include "tests/configure.cmake.h"
Tomáš Pecka9a6e15b2021-01-25 18:57:48 +01008#include "tests/mock/sysrepo/events.h"
Tomáš Peckacb7a5f82021-01-20 15:12:00 +01009
10using namespace std::literals;
11
Tomáš Pecka594a6762021-01-29 11:06:08 +010012TEST_CASE("Firmware in czechlight-system")
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010013{
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");
Tomáš Peckad51d4cb2021-02-03 14:15:49 +010021 auto dbusClientConnectionSignals = sdbus::createSessionBusConnection();
22 auto dbusClientConnectionMethods = sdbus::createSessionBusConnection();
23 dbusClientConnectionSignals->enterEventLoopAsync();
24 dbusClientConnectionMethods->enterEventLoopAsync();
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010025 dbusServerConnection->enterEventLoopAsync();
26
27 std::map<std::string, velia::system::RAUC::SlotProperties> dbusRaucStatus = {
28 {"rootfs.1", {
29 {"activated.count", uint32_t {39}},
30 {"activated.timestamp", "2021-01-13T17:20:18Z"},
31 {"bootname", "B"},
32 {"boot-status", "good"},
33 {"bundle.compatible", "czechlight-clearfog"},
34 {"bundle.version", "v4-103-g34d2f48"},
35 {"class", "rootfs"},
36 {"device", "/dev/mmcblk0p3"},
37 {"installed.count", uint32_t {39}},
38 {"installed.timestamp", "2021-01-13T17:20:15Z"},
39 {"mountpoint", "/"},
40 {"sha256", "07b30d065c7aad64d2006ce99fd339c929d3ca97b666fca4584b9ef726469fc4"},
41 {"size", uint64_t {45601892}},
42 {"state", "booted"},
43 {"status", "ok"},
44 {"type", "ext4"},
45 }},
46 {"rootfs.0", {
47 {"activated.count", uint32_t {41}},
48 {"activated.timestamp", "2021-01-13T17:15:54Z"},
49 {"bootname", "A"},
50 {"boot-status", "bad"},
51 {"bundle.compatible", "czechlight-clearfog"},
52 {"bundle.version", "v4-104-ge80fcd4"},
53 {"class", "rootfs"},
54 {"device", "/dev/mmcblk0p1"},
55 {"installed.count", uint32_t {41}},
56 {"installed.timestamp", "2021-01-13T17:15:50Z"},
57 {"sha256", "6d81e8f341edd17c127811f7347c7e23d18c2fc25c0bdc29ac56999cc9c25629"},
58 {"size", uint64_t {45647664}},
59 {"state", "inactive"},
60 {"status", "ok"},
61 {"type", "ext4"},
62 }},
63 {"cfg.1", {
64 {"bundle.compatible", "czechlight-clearfog"},
65 {"bundle.version", "v4-103-g34d2f48"},
66 {"class", "cfg"},
67 {"device", "/dev/mmcblk0p4"},
68 {"installed.count", uint32_t {39}},
69 {"installed.timestamp", "2021-01-13T17:20:18Z"},
70 {"mountpoint", "/cfg"},
71 {"parent", "rootfs.1"},
72 {"sha256", "5ca1b6c461fc194055d52b181f57c63dc1d34c19d041f6395e6f6abc039692bb"},
73 {"size", uint64_t {108}},
74 {"state", "active"},
75 {"status", "ok"},
76 {"type", "ext4"},
77 }},
78 {"cfg.0", {
79 {"bundle.compatible", "czechlight-clearfog"},
80 {"bundle.version", "v4-104-ge80fcd4"},
81 {"class", "cfg"},
82 {"device", "/dev/mmcblk0p2"},
83 {"installed.count", uint32_t {41}},
84 {"installed.timestamp", "2021-01-13T17:15:54Z"},
85 {"parent", "rootfs.0"},
86 {"sha256", "5ca1b6c461fc194055d52b181f57c63dc1d34c19d041f6395e6f6abc039692bb"},
87 {"size", uint64_t {108}},
88 {"state", "inactive"},
89 {"status", "ok"},
90 {"type", "ext4"},
91 }},
92 };
93 auto raucServer = DBusRAUCServer(*dbusServerConnection, "rootfs.1", dbusRaucStatus);
Tomáš Peckad51d4cb2021-02-03 14:15:49 +010094 auto sysrepo = std::make_shared<velia::system::Firmware>(srConn, *dbusClientConnectionSignals, *dbusClientConnectionMethods);
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010095
Tomáš Pecka92b14e42021-01-27 17:33:11 +010096 REQUIRE(dataFromSysrepo(client, "/czechlight-system:firmware", SR_DS_OPERATIONAL) == std::map<std::string, std::string> {
97 {"/firmware-slot[name='A']", ""},
98 {"/firmware-slot[name='A']/boot-status", "bad"},
99 {"/firmware-slot[name='A']/installed", "2021-01-13T17:15:50Z"},
100 {"/firmware-slot[name='A']/name", "A"},
101 {"/firmware-slot[name='A']/state", "inactive"},
102 {"/firmware-slot[name='A']/version", "v4-104-ge80fcd4"},
103 {"/firmware-slot[name='B']", ""},
104 {"/firmware-slot[name='B']/boot-status", "good"},
105 {"/firmware-slot[name='B']/installed", "2021-01-13T17:20:15Z"},
106 {"/firmware-slot[name='B']/name", "B"},
107 {"/firmware-slot[name='B']/state", "booted"},
108 {"/firmware-slot[name='B']/version", "v4-103-g34d2f48"},
109 {"/installation", ""},
110 {"/installation/message", ""},
111 {"/installation/status", "none"},
112 });
Tomáš Peckacb7a5f82021-01-20 15:12:00 +0100113
114 SECTION("Firmware install RPC")
115 {
116 auto rpcInput = std::make_shared<sysrepo::Vals>(1);
117 rpcInput->val(0)->set("/czechlight-system:firmware/installation/install/url", "/path/to/bundle/update.raucb");
118
119 SECTION("Installation runs")
120 {
121 DBusRAUCServer::InstallBehaviour installType;
122 std::map<std::string, std::string> expectedFinished, expectedInProgress = {
Tomáš Pecka92b14e42021-01-27 17:33:11 +0100123 {"/message", ""},
124 {"/status", "in-progress"},
Tomáš Peckacb7a5f82021-01-20 15:12:00 +0100125 };
Tomáš Pecka9a6e15b2021-01-25 18:57:48 +0100126 size_t expectedNotificationsCount;
127 std::string expectedLastNotificationMsg;
128
129 // subscribe to notifications
130 EventWatcher events;
131 subscription->event_notif_subscribe("czechlight-system", events, "/czechlight-system:firmware/installation/update");
Tomáš Peckacb7a5f82021-01-20 15:12:00 +0100132
133 SECTION("Successfull install")
134 {
135 installType = DBusRAUCServer::InstallBehaviour::OK;
136 expectedFinished = {
Tomáš Pecka92b14e42021-01-27 17:33:11 +0100137 {"/message", ""},
138 {"/status", "succeeded"},
Tomáš Peckacb7a5f82021-01-20 15:12:00 +0100139 };
Tomáš Pecka9a6e15b2021-01-25 18:57:48 +0100140 expectedNotificationsCount = 22;
141 expectedLastNotificationMsg = "Installing done.";
Tomáš Peckacb7a5f82021-01-20 15:12:00 +0100142 }
143
144 SECTION("Unsuccessfull install")
145 {
146 installType = DBusRAUCServer::InstallBehaviour::FAILURE;
147 expectedFinished = {
Tomáš Pecka92b14e42021-01-27 17:33:11 +0100148 {"/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"},
149 {"/status", "failed"},
Tomáš Peckacb7a5f82021-01-20 15:12:00 +0100150 };
Tomáš Pecka9a6e15b2021-01-25 18:57:48 +0100151 expectedNotificationsCount = 6;
152 expectedLastNotificationMsg = "Installing failed.";
Tomáš Peckacb7a5f82021-01-20 15:12:00 +0100153 }
154
155 raucServer.installBundleBehaviour(installType);
156 auto res = client->rpc_send("/czechlight-system:firmware/installation/install", rpcInput);
157 REQUIRE(res->val_cnt() == 0);
158
159 std::this_thread::sleep_for(10ms); // lets wait a while, so the RAUC's callback for operation changed takes place
Tomáš Pecka92b14e42021-01-27 17:33:11 +0100160 REQUIRE(dataFromSysrepo(client, "/czechlight-system:firmware/installation", SR_DS_OPERATIONAL) == expectedInProgress);
Tomáš Peckacb7a5f82021-01-20 15:12:00 +0100161
162 std::this_thread::sleep_for(2s); // lets wait a while, so the installation can finish
Tomáš Pecka92b14e42021-01-27 17:33:11 +0100163 REQUIRE(dataFromSysrepo(client, "/czechlight-system:firmware/installation", SR_DS_OPERATIONAL) == expectedFinished);
Tomáš Pecka9a6e15b2021-01-25 18:57:48 +0100164
165 // check updates notification count and that at least some of them are reasonable
166 REQUIRE(events.count() == expectedNotificationsCount);
167 REQUIRE(events.peek(0).data["/czechlight-system:firmware/installation/update/message"] == "Installing");
168 REQUIRE(events.peek(0).data["/czechlight-system:firmware/installation/update/progress"] == "0");
169 REQUIRE(events.peek(events.count() - 1).data["/czechlight-system:firmware/installation/update/message"] == expectedLastNotificationMsg);
170 REQUIRE(events.peek(events.count() - 1).data["/czechlight-system:firmware/installation/update/progress"] == "100");
171
172 // check updates notification progress is an increasing sequence
173 for (size_t i = 1; i < events.count(); i++) {
174 auto prevProgress = std::stoi(events.peek(i - 1).data["/czechlight-system:firmware/installation/update/progress"]);
175 auto currProgress = std::stoi(events.peek(i).data["/czechlight-system:firmware/installation/update/progress"]);
176 REQUIRE(prevProgress <= currProgress);
177 }
Tomáš Peckacb7a5f82021-01-20 15:12:00 +0100178 }
179
Tomáš Pecka0eefdf12021-02-17 19:28:13 +0100180 SECTION("Unsuccessfull install followed by successfull install")
181 {
182 std::map<std::string, std::string> expected;
183
184 // invoke unsuccessfull install
185 raucServer.installBundleBehaviour(DBusRAUCServer::InstallBehaviour::FAILURE);
186 client->rpc_send("/czechlight-system:firmware/installation/install", rpcInput);
187
188 expected = {
189 {"/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"},
190 {"/status", "failed"},
191 };
192 std::this_thread::sleep_for(2s); // lets wait a while, so the installation can finish
193 REQUIRE(dataFromSysrepo(client, "/czechlight-system:firmware/installation", SR_DS_OPERATIONAL) == expected);
194
195 // invoke successfull install
196 raucServer.installBundleBehaviour(DBusRAUCServer::InstallBehaviour::OK);
197 client->rpc_send("/czechlight-system:firmware/installation/install", rpcInput);
198
199 expected = {
200 {"/message", ""},
201 {"/status", "in-progress"},
202 };
203 std::this_thread::sleep_for(10ms); // lets wait a while, so the RAUC's callback for operation changed takes place
204 REQUIRE(dataFromSysrepo(client, "/czechlight-system:firmware/installation", SR_DS_OPERATIONAL) == expected);
205
206 expected = {
207 {"/message", ""},
208 {"/status", "succeeded"},
209 };
210 std::this_thread::sleep_for(2s); // lets wait a while, so the installation can finish
211 REQUIRE(dataFromSysrepo(client, "/czechlight-system:firmware/installation", SR_DS_OPERATIONAL) == expected);
212
213 }
Tomáš Pecka92b14e42021-01-27 17:33:11 +0100214 SECTION("Installation in progress")
Tomáš Peckacb7a5f82021-01-20 15:12:00 +0100215 {
216 client->rpc_send("/czechlight-system:firmware/installation/install", rpcInput);
217 std::this_thread::sleep_for(10ms);
Tomáš Pecka92b14e42021-01-27 17:33:11 +0100218
219 SECTION("Invoking second installation throws")
220 {
221 REQUIRE_THROWS_WITH_AS(client->rpc_send("/czechlight-system:firmware/installation/install", rpcInput), "User callback failed", sysrepo::sysrepo_exception);
222 }
223
224 SECTION("Firmware slot data are available")
225 {
226 // RAUC does not respond to GetSlotStatus when another operation in progress, so let's check we use the cached data
227 REQUIRE(dataFromSysrepo(client, "/czechlight-system:firmware", SR_DS_OPERATIONAL) == std::map<std::string, std::string> {
228 {"/firmware-slot[name='A']", ""},
229 {"/firmware-slot[name='A']/boot-status", "bad"},
230 {"/firmware-slot[name='A']/installed", "2021-01-13T17:15:50Z"},
231 {"/firmware-slot[name='A']/name", "A"},
232 {"/firmware-slot[name='A']/state", "inactive"},
233 {"/firmware-slot[name='A']/version", "v4-104-ge80fcd4"},
234 {"/firmware-slot[name='B']", ""},
235 {"/firmware-slot[name='B']/boot-status", "good"},
236 {"/firmware-slot[name='B']/installed", "2021-01-13T17:20:15Z"},
237 {"/firmware-slot[name='B']/name", "B"},
238 {"/firmware-slot[name='B']/state", "booted"},
239 {"/firmware-slot[name='B']/version", "v4-103-g34d2f48"},
240 {"/installation", ""},
241 {"/installation/message", ""},
242 {"/installation/status", "in-progress"},
243 });
244 }
Tomáš Peckacb7a5f82021-01-20 15:12:00 +0100245 }
246 }
247}