blob: e11eec5a0e553271db85bac6db884d029be86449 [file] [log] [blame]
Václav Kubernát73109382018-09-14 19:52:03 +02001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
4 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8
Václav Kubernát26b56082020-02-03 18:28:56 +01009#include "trompeloeil_doctest.hpp"
Jan Kundrát6ee84792020-01-24 01:43:36 +010010#include <sysrepo-cpp/Session.hpp>
Václav Kubernát73109382018-09-14 19:52:03 +020011
Václav Kubernátc31bd602019-03-07 11:44:48 +010012#ifdef sysrepo_BACKEND
Václav Kubernát73109382018-09-14 19:52:03 +020013#include "sysrepo_access.hpp"
Václav Kubernátc31bd602019-03-07 11:44:48 +010014#elif defined(netconf_BACKEND)
15#include "netconf_access.hpp"
16#include "netopeer_vars.hpp"
17#else
18#error "Unknown backend"
19#endif
Jan Kundrátbb525b42020-02-04 11:56:59 +010020#include "pretty_printers.hpp"
Václav Kubernát73109382018-09-14 19:52:03 +020021#include "sysrepo_subscription.hpp"
Václav Kubernát8e121ff2019-10-15 15:47:45 +020022#include "utils.hpp"
Václav Kubernát73109382018-09-14 19:52:03 +020023
Jan Kundrát6ee84792020-01-24 01:43:36 +010024using namespace std::literals::string_literals;
25
Václav Kubernát69aabe92020-01-24 16:53:12 +010026class MockRecorder : public trompeloeil::mock_interface<Recorder> {
Václav Kubernát73109382018-09-14 19:52:03 +020027public:
Václav Kubernát69aabe92020-01-24 16:53:12 +010028 IMPLEMENT_MOCK3(write);
Václav Kubernát73109382018-09-14 19:52:03 +020029};
30
Jan Kundrátbb525b42020-02-04 11:56:59 +010031class MockDataSupplier : public trompeloeil::mock_interface<DataSupplier> {
32public:
33 IMPLEMENT_CONST_MOCK1(get_data);
34};
35
Václav Kubernát8e121ff2019-10-15 15:47:45 +020036TEST_CASE("setting/getting values")
Václav Kubernát73109382018-09-14 19:52:03 +020037{
Václav Kubernát73109382018-09-14 19:52:03 +020038 trompeloeil::sequence seq1;
39 MockRecorder mock;
Václav Kubernátab612e92019-11-26 19:51:31 +010040 SysrepoSubscription subscription("example-schema", &mock);
Václav Kubernátc31bd602019-03-07 11:44:48 +010041
42#ifdef sysrepo_BACKEND
Václav Kubernát715c85c2020-04-14 01:46:08 +020043 SysrepoAccess datastore("netconf-cli-test", Datastore::Running);
Václav Kubernátc31bd602019-03-07 11:44:48 +010044#elif defined(netconf_BACKEND)
45 NetconfAccess datastore(NETOPEER_SOCKET_PATH);
46#else
47#error "Unknown backend"
48#endif
Václav Kubernát73109382018-09-14 19:52:03 +020049
Václav Kubernát69aabe92020-01-24 16:53:12 +010050
Václav Kubernát134d78f2019-09-03 16:42:29 +020051 SECTION("set leafInt8 to -128")
Václav Kubernát73109382018-09-14 19:52:03 +020052 {
Václav Kubernát69aabe92020-01-24 16:53:12 +010053 REQUIRE_CALL(mock, write("/example-schema:leafInt8", std::nullopt, "-128"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +020054 datastore.setLeaf("/example-schema:leafInt8", int8_t{-128});
55 datastore.commitChanges();
56 }
57
58 SECTION("set leafInt16 to -32768")
59 {
Václav Kubernát69aabe92020-01-24 16:53:12 +010060 REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "-32768"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +020061 datastore.setLeaf("/example-schema:leafInt16", int16_t{-32768});
62 datastore.commitChanges();
63 }
64
65 SECTION("set leafInt32 to -2147483648")
66 {
Václav Kubernát69aabe92020-01-24 16:53:12 +010067 REQUIRE_CALL(mock, write("/example-schema:leafInt32", std::nullopt, "-2147483648"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +020068 datastore.setLeaf("/example-schema:leafInt32", int32_t{-2147483648});
69 datastore.commitChanges();
70 }
71
72 SECTION("set leafInt64 to -50000000000")
73 {
Václav Kubernát69aabe92020-01-24 16:53:12 +010074 REQUIRE_CALL(mock, write("/example-schema:leafInt64", std::nullopt, "-50000000000"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +020075 datastore.setLeaf("/example-schema:leafInt64", int64_t{-50000000000});
76 datastore.commitChanges();
77 }
78
79 SECTION("set leafUInt8 to 255")
80 {
Václav Kubernát69aabe92020-01-24 16:53:12 +010081 REQUIRE_CALL(mock, write("/example-schema:leafUInt8", std::nullopt, "255"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +020082 datastore.setLeaf("/example-schema:leafUInt8", uint8_t{255});
83 datastore.commitChanges();
84 }
85
86 SECTION("set leafUInt16 to 65535")
87 {
Václav Kubernát69aabe92020-01-24 16:53:12 +010088 REQUIRE_CALL(mock, write("/example-schema:leafUInt16", std::nullopt, "65535"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +020089 datastore.setLeaf("/example-schema:leafUInt16", uint16_t{65535});
90 datastore.commitChanges();
91 }
92
93 SECTION("set leafUInt32 to 4294967295")
94 {
Václav Kubernát69aabe92020-01-24 16:53:12 +010095 REQUIRE_CALL(mock, write("/example-schema:leafUInt32", std::nullopt, "4294967295"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +020096 datastore.setLeaf("/example-schema:leafUInt32", uint32_t{4294967295});
97 datastore.commitChanges();
98 }
99
100 SECTION("set leafUInt64 to 50000000000")
101 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100102 REQUIRE_CALL(mock, write("/example-schema:leafUInt64", std::nullopt, "50000000000"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200103 datastore.setLeaf("/example-schema:leafUInt64", uint64_t{50000000000});
Václav Kubernát73109382018-09-14 19:52:03 +0200104 datastore.commitChanges();
105 }
106
107 SECTION("set leafEnum to coze")
108 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100109 REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "coze"s));
Václav Kubernát73109382018-09-14 19:52:03 +0200110 datastore.setLeaf("/example-schema:leafEnum", enum_{"coze"});
111 datastore.commitChanges();
112 }
113
114 SECTION("set leafDecimal to 123.544")
115 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100116 REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.544"s));
Václav Kubernát73109382018-09-14 19:52:03 +0200117 datastore.setLeaf("/example-schema:leafDecimal", 123.544);
118 datastore.commitChanges();
119 }
120
121 SECTION("create presence container")
122 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100123 REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s));
Václav Kubernát73109382018-09-14 19:52:03 +0200124 datastore.createPresenceContainer("/example-schema:pContainer");
125 datastore.commitChanges();
126 }
127
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100128 SECTION("create/delete a list instance")
Václav Kubernát45f4a822019-05-29 21:10:50 +0200129 {
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100130 {
131 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", std::nullopt, ""s));
132 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']/name", std::nullopt, "Nguyen"s));
133 datastore.createListInstance("/example-schema:person[name='Nguyen']");
134 datastore.commitChanges();
135 }
136 {
137 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", ""s, std::nullopt));
138 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']/name", "Nguyen"s, std::nullopt));
139 datastore.deleteListInstance("/example-schema:person[name='Nguyen']");
140 datastore.commitChanges();
141 }
Václav Kubernát45f4a822019-05-29 21:10:50 +0200142 }
143
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200144 SECTION("leafref pointing to a key of a list")
145 {
146 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100147 REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']", std::nullopt, ""s));
148 REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']/name", std::nullopt, "Dan"s));
149 REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']", std::nullopt, ""s));
150 REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']/name", std::nullopt, "Elfi"s));
151 REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']", std::nullopt, ""s));
152 REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']/name", std::nullopt, "Kolafa"s));
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200153 datastore.createListInstance("/example-schema:person[name='Dan']");
154 datastore.createListInstance("/example-schema:person[name='Elfi']");
155 datastore.createListInstance("/example-schema:person[name='Kolafa']");
156 datastore.commitChanges();
157 }
158
159 // The commitChanges method has to be called in each of the
160 // SECTIONs, because the REQUIRE_CALL only works inside the given
161 // SECTION.
162 SECTION("Dan")
163 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100164 REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Dan"s));
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200165 datastore.setLeaf("/example-schema:bossPerson", std::string{"Dan"});
166 datastore.commitChanges();
167 }
168
169 SECTION("Elfi")
170 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100171 REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Elfi"s));
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200172 datastore.setLeaf("/example-schema:bossPerson", std::string{"Elfi"});
173 datastore.commitChanges();
174 }
175
176 SECTION("Kolafa")
177 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100178 REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Kolafa"s));
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200179 datastore.setLeaf("/example-schema:bossPerson", std::string{"Kolafa"});
180 datastore.commitChanges();
181 }
182 }
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200183 SECTION("bool values get correctly represented as bools")
184 {
185 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100186 REQUIRE_CALL(mock, write("/example-schema:down", std::nullopt, "true"s));
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200187 datastore.setLeaf("/example-schema:down", bool{true});
188 datastore.commitChanges();
189 }
190
Jan Kundrátb331b552020-01-23 15:25:29 +0100191 DatastoreAccess::Tree expected{{"/example-schema:down", bool{true}}};
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200192 REQUIRE(datastore.getItems("/example-schema:down") == expected);
193 }
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200194
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200195 SECTION("getting items from the whole module")
196 {
197 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100198 REQUIRE_CALL(mock, write("/example-schema:up", std::nullopt, "true"s));
199 REQUIRE_CALL(mock, write("/example-schema:down", std::nullopt, "false"s));
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200200 datastore.setLeaf("/example-schema:up", bool{true});
201 datastore.setLeaf("/example-schema:down", bool{false});
202 datastore.commitChanges();
203 }
204
Jan Kundrátb331b552020-01-23 15:25:29 +0100205 DatastoreAccess::Tree expected{{"/example-schema:down", bool{false}},
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200206 // Sysrepo always returns containers when getting values, but
207 // libnetconf does not. This is fine by the YANG standard:
208 // https://tools.ietf.org/html/rfc7950#section-7.5.7 Furthermore,
209 // NetconfAccess implementation actually only iterates over leafs,
210 // so even if libnetconf did include containers, they wouldn't get
211 // shown here anyway. With sysrepo2, this won't be necessary,
212 // because it'll use the same data structure as libnetconf, so the
213 // results will be consistent.
214#ifdef sysrepo_BACKEND
Václav Kubernát144729d2020-01-08 15:20:35 +0100215 {"/example-schema:lol", special_{SpecialValue::Container}},
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100216 {"/example-schema:inventory", special_{SpecialValue::Container}},
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200217#endif
218 {"/example-schema:up", bool{true}}};
219 REQUIRE(datastore.getItems("/example-schema:*") == expected);
220 }
221
Václav Kubernát152ce222019-12-19 12:23:32 +0100222 SECTION("getItems returns correct datatypes")
223 {
224 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100225 REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "lol"s));
Václav Kubernát152ce222019-12-19 12:23:32 +0100226 datastore.setLeaf("/example-schema:leafEnum", enum_{"lol"});
227 datastore.commitChanges();
228 }
Jan Kundrátb331b552020-01-23 15:25:29 +0100229 DatastoreAccess::Tree expected{{"/example-schema:leafEnum", enum_{"lol"}}};
Václav Kubernát152ce222019-12-19 12:23:32 +0100230
231 REQUIRE(datastore.getItems("/example-schema:leafEnum") == expected);
232 }
233
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100234 SECTION("getItems on a list")
235 {
236 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100237 REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']", std::nullopt, ""s));
238 REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']/name", std::nullopt, "Jan"s));
239 REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']", std::nullopt, ""s));
240 REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']/name", std::nullopt, "Michal"s));
241 REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']", std::nullopt, ""s));
242 REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']/name", std::nullopt, "Petr"s));
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100243 datastore.createListInstance("/example-schema:person[name='Jan']");
244 datastore.createListInstance("/example-schema:person[name='Michal']");
245 datastore.createListInstance("/example-schema:person[name='Petr']");
246 datastore.commitChanges();
247 }
Jan Kundrátb331b552020-01-23 15:25:29 +0100248 DatastoreAccess::Tree expected{
Václav Kubernát144729d2020-01-08 15:20:35 +0100249 {"/example-schema:person[name='Jan']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100250 {"/example-schema:person[name='Jan']/name", std::string{"Jan"}},
Václav Kubernát144729d2020-01-08 15:20:35 +0100251 {"/example-schema:person[name='Michal']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100252 {"/example-schema:person[name='Michal']/name", std::string{"Michal"}},
Václav Kubernát144729d2020-01-08 15:20:35 +0100253 {"/example-schema:person[name='Petr']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100254 {"/example-schema:person[name='Petr']/name", std::string{"Petr"}}
255 };
256
257 REQUIRE(datastore.getItems("/example-schema:person") == expected);
258 }
259
Václav Kubernát69aabe92020-01-24 16:53:12 +0100260 SECTION("presence containers")
261 {
262 DatastoreAccess::Tree expected;
263 // Make sure it's not there before we create it
264 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
265
266 {
267 REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s));
268 datastore.createPresenceContainer("/example-schema:pContainer");
269 datastore.commitChanges();
270 }
271 expected = {
272 {"/example-schema:pContainer", special_{SpecialValue::PresenceContainer}}
273 };
274 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
275
276 // Make sure it's not there after we delete it
277 {
278 REQUIRE_CALL(mock, write("/example-schema:pContainer", ""s, std::nullopt));
279 datastore.deletePresenceContainer("/example-schema:pContainer");
280 datastore.commitChanges();
281 }
282 expected = {};
283 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100284 }
Václav Kubernát69aabe92020-01-24 16:53:12 +0100285
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100286 SECTION("nested presence container")
287 {
288 DatastoreAccess::Tree expected;
289 // Make sure it's not there before we create it
290 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
291 {
292 REQUIRE_CALL(mock, write("/example-schema:inventory", std::nullopt, ""s));
293 REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", std::nullopt, ""s));
294 datastore.createPresenceContainer("/example-schema:inventory/stuff");
295 datastore.commitChanges();
296 }
297 expected = {
298 {"/example-schema:inventory/stuff", special_{SpecialValue::PresenceContainer}}
299 };
300 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
301 {
302 REQUIRE_CALL(mock, write("/example-schema:inventory", ""s, std::nullopt));
303 REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", ""s, std::nullopt));
304 datastore.deletePresenceContainer("/example-schema:inventory/stuff");
305 datastore.commitChanges();
306 }
307 expected = {};
308 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
Václav Kubernát69aabe92020-01-24 16:53:12 +0100309 }
310
Jan Kundrátbd3169c2020-02-03 19:31:34 +0100311 SECTION("floats")
312 {
313 datastore.setLeaf("/example-schema:leafDecimal", 123.4);
314 REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.4"s));
315 datastore.commitChanges();
316 DatastoreAccess::Tree expected {
317 {"/example-schema:leafDecimal", 123.4},
318 };
319 REQUIRE(datastore.getItems("/example-schema:leafDecimal") == expected);
320 }
321
Jan Kundrát3ff50122020-05-07 00:37:50 +0200322 SECTION("unions")
323 {
324 datastore.setLeaf("/example-schema:unionIntString", int32_t{10});
325 REQUIRE_CALL(mock, write("/example-schema:unionIntString", std::nullopt, "10"s));
326 datastore.commitChanges();
327 DatastoreAccess::Tree expected {
328 {"/example-schema:unionIntString", int32_t{10}},
329 };
330 REQUIRE(datastore.getItems("/example-schema:unionIntString") == expected);
331 }
332
Jan Kundrát0d8abd12020-05-07 02:00:14 +0200333 SECTION("identityref") {
334 datastore.setLeaf("/example-schema:beast", identityRef_{"example-schema", "Mammal"});
335 REQUIRE_CALL(mock, write("/example-schema:beast", std::nullopt, "example-schema:Mammal"s));
336 datastore.commitChanges();
337 DatastoreAccess::Tree expected {
338 {"/example-schema:beast", identityRef_{"example-schema", "Mammal"}},
339 };
340 REQUIRE(datastore.getItems("/example-schema:beast") == expected);
341
342 datastore.setLeaf("/example-schema:beast", identityRef_{"Whale"});
343 REQUIRE_CALL(mock, write("/example-schema:beast", "example-schema:Mammal", "example-schema:Whale"s));
344 datastore.commitChanges();
345 expected = {
346 {"/example-schema:beast", identityRef_{"example-schema", "Whale"}},
347 };
348 REQUIRE(datastore.getItems("/example-schema:beast") == expected);
349 }
350
Jan Kundrát68985442020-05-07 02:15:34 +0200351 SECTION("binary")
352 {
353 datastore.setLeaf("/example-schema:blob", binary_{"cHduegByIQ=="s});
354 REQUIRE_CALL(mock, write("/example-schema:blob", std::nullopt, "cHduegByIQ=="s));
355 datastore.commitChanges();
356 DatastoreAccess::Tree expected {
357 {"/example-schema:blob", binary_{"cHduegByIQ=="s}},
358 };
359 REQUIRE(datastore.getItems("/example-schema:blob") == expected);
360 }
361
Jan Kundrát379bb572020-05-07 03:23:13 +0200362 SECTION("empty")
363 {
364 datastore.setLeaf("/example-schema:dummy", empty_{});
365 REQUIRE_CALL(mock, write("/example-schema:dummy", std::nullopt, ""s));
366 datastore.commitChanges();
367 DatastoreAccess::Tree expected {
368 {"/example-schema:dummy", empty_{}},
369 };
370 REQUIRE(datastore.getItems("/example-schema:dummy") == expected);
371 }
372
Jan Kundrátbb525b42020-02-04 11:56:59 +0100373 SECTION("operational data")
374 {
375 MockDataSupplier mockOpsData;
376 OperationalDataSubscription opsDataSub("/example-schema:temperature", mockOpsData);
377 DatastoreAccess::Tree expected;
378 std::string xpath;
379 SECTION("temperature")
380 {
381 expected = {{"/example-schema:temperature", int32_t{22}}};
382 xpath = "/example-schema:temperature";
383 }
384
385 REQUIRE_CALL(mockOpsData, get_data(xpath)).RETURN(expected);
386 REQUIRE(datastore.getItems(xpath) == expected);
387 }
388
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200389 SECTION("leaf list")
390 {
391 DatastoreAccess::Tree expected;
392 REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "0.0.0.0"s));
393 REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "127.0.0.1"s));
394 datastore.createLeafListInstance("/example-schema:addresses[.='0.0.0.0']");
395 datastore.createLeafListInstance("/example-schema:addresses[.='127.0.0.1']");
396 datastore.commitChanges();
397 expected = {
398 {"/example-schema:addresses", special_{SpecialValue::LeafList}},
399 {"/example-schema:addresses[.='0.0.0.0']", "0.0.0.0"s},
400 {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s},
401 };
402 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
403
404 REQUIRE_CALL(mock, write("/example-schema:addresses", "0.0.0.0"s, std::nullopt));
405 datastore.deleteLeafListInstance("/example-schema:addresses[.='0.0.0.0']");
406 datastore.commitChanges();
407 expected = {
408 {"/example-schema:addresses", special_{SpecialValue::LeafList}},
409 {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s},
410 };
411 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
412
413 REQUIRE_CALL(mock, write("/example-schema:addresses", "127.0.0.1"s, std::nullopt));
414 datastore.deleteLeafListInstance("/example-schema:addresses[.='127.0.0.1']");
415 datastore.commitChanges();
416 expected = {};
417 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
418 }
Jan Kundrátbb525b42020-02-04 11:56:59 +0100419
Václav Kubernát7160a132020-04-03 02:11:01 +0200420 SECTION("copying data from startup refreshes the data")
421 {
422 {
423 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{});
424 REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "123"s));
425 datastore.setLeaf("/example-schema:leafInt16", int16_t{123});
426 datastore.commitChanges();
427 }
428 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{{"/example-schema:leafInt16", int16_t{123}}});
429 REQUIRE_CALL(mock, write("/example-schema:leafInt16", "123"s, std::nullopt));
430 datastore.copyConfig(Datastore::Startup, Datastore::Running);
431 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{});
432 }
433
Václav Kubernát73109382018-09-14 19:52:03 +0200434 waitForCompletionAndBitMore(seq1);
435}
Jan Kundrát6ee84792020-01-24 01:43:36 +0100436
437class RpcCb: public sysrepo::Callback {
438 int rpc(const char *xpath, const ::sysrepo::S_Vals input, ::sysrepo::S_Vals_Holder output, void *) override
439 {
440 const auto nukes = "/example-schema:launch-nukes"s;
441 if (xpath == "/example-schema:noop"s) {
442 return SR_ERR_OK;
443 } else if (xpath == nukes) {
444 uint64_t kilotons = 0;
445 bool hasCities = false;
446 for (size_t i = 0; i < input->val_cnt(); ++i) {
447 const auto& val = input->val(i);
448 if (val->xpath() == nukes + "/payload/kilotons") {
449 kilotons = val->data()->get_uint64();
450 } else if (val->xpath() == nukes + "/payload") {
451 // ignore, container
452 } else if (val->xpath() == nukes + "/description") {
453 // unused
454 } else if (std::string_view{val->xpath()}.find(nukes + "/cities") == 0) {
455 hasCities = true;
456 } else {
457 throw std::runtime_error("RPC launch-nukes: unexpected input "s + val->xpath());
458 }
459 }
460 if (kilotons == 333'666) {
461 // magic, just do not generate any output. This is important because the NETCONF RPC returns just <ok/>.
462 return SR_ERR_OK;
463 }
464 auto buf = output->allocate(2);
465 size_t i = 0;
466 buf->val(i++)->set((nukes + "/blast-radius").c_str(), uint32_t{33'666});
467 buf->val(i++)->set((nukes + "/actual-yield").c_str(), static_cast<uint64_t>(1.33 * kilotons));
468 if (hasCities) {
469 buf = output->reallocate(output->val_cnt() + 2);
470 buf->val(i++)->set((nukes + "/damaged-places/targets[city='London']/city").c_str(), "London");
471 buf->val(i++)->set((nukes + "/damaged-places/targets[city='Berlin']/city").c_str(), "Berlin");
472 }
473 return SR_ERR_OK;
474 }
475 throw std::runtime_error("unrecognized RPC");
476 }
477};
478
479TEST_CASE("rpc") {
480 trompeloeil::sequence seq1;
481 auto srConn = std::make_shared<sysrepo::Connection>("netconf-cli-test-rpc");
482 auto srSession = std::make_shared<sysrepo::Session>(srConn);
483 auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSession);
484 auto cb = std::make_shared<RpcCb>();
485 sysrepo::Logs{}.set_stderr(SR_LL_INF);
486 srSubscription->rpc_subscribe("/example-schema:noop", cb, nullptr, SR_SUBSCR_CTX_REUSE);
487 srSubscription->rpc_subscribe("/example-schema:launch-nukes", cb, nullptr, SR_SUBSCR_CTX_REUSE);
488
489#ifdef sysrepo_BACKEND
Václav Kubernát715c85c2020-04-14 01:46:08 +0200490 SysrepoAccess datastore("netconf-cli-test", Datastore::Running);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100491#elif defined(netconf_BACKEND)
492 NetconfAccess datastore(NETOPEER_SOCKET_PATH);
493#else
494#error "Unknown backend"
495#endif
496
497 std::string rpc;
498 DatastoreAccess::Tree input, output;
499
500 SECTION("noop") {
501 rpc = "/example-schema:noop";
502 }
503
504 SECTION("small nuke") {
505 rpc = "/example-schema:launch-nukes";
506 input = {
507 {"description", "dummy"s},
508 {"payload/kilotons", uint64_t{333'666}},
509 };
510 // no data are returned
511 }
512
513 SECTION("small nuke") {
514 rpc = "/example-schema:launch-nukes";
515 input = {
516 {"description", "dummy"s},
517 {"payload/kilotons", uint64_t{4}},
518 };
519 output = {
520 {"blast-radius", uint32_t{33'666}},
521 {"actual-yield", uint64_t{5}},
522 };
523 }
524
525 SECTION("with lists") {
526 rpc = "/example-schema:launch-nukes";
527 input = {
528 {"payload/kilotons", uint64_t{6}},
529 {"cities/targets[city='Prague']/city", "Prague"s},
530 };
531 output = {
532 {"blast-radius", uint32_t{33'666}},
533 {"actual-yield", uint64_t{7}},
534 {"damaged-places", special_{SpecialValue::PresenceContainer}},
535 {"damaged-places/targets[city='London']", special_{SpecialValue::List}},
536 {"damaged-places/targets[city='London']/city", "London"s},
537 {"damaged-places/targets[city='Berlin']", special_{SpecialValue::List}},
538 {"damaged-places/targets[city='Berlin']/city", "Berlin"s},
539 };
540 }
541
542 REQUIRE(datastore.executeRpc(rpc, input) == output);
543
544 waitForCompletionAndBitMore(seq1);
545}