blob: 07320c9eee40fb1b7e830b3b46a53775e60e63f9 [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
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200205 DatastoreAccess::Tree expected{
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átda8e4b92020-02-04 11:56:30 +0100215 {"/example-schema:inventory", special_{SpecialValue::Container}},
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200216 {"/example-schema:lol", special_{SpecialValue::Container}},
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200217#endif
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200218 {"/example-schema:up", bool{true}},
219 {"/example-schema:down", bool{false}}};
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200220 REQUIRE(datastore.getItems("/example-schema:*") == expected);
221 }
222
Václav Kubernát152ce222019-12-19 12:23:32 +0100223 SECTION("getItems returns correct datatypes")
224 {
225 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100226 REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "lol"s));
Václav Kubernát152ce222019-12-19 12:23:32 +0100227 datastore.setLeaf("/example-schema:leafEnum", enum_{"lol"});
228 datastore.commitChanges();
229 }
Jan Kundrátb331b552020-01-23 15:25:29 +0100230 DatastoreAccess::Tree expected{{"/example-schema:leafEnum", enum_{"lol"}}};
Václav Kubernát152ce222019-12-19 12:23:32 +0100231
232 REQUIRE(datastore.getItems("/example-schema:leafEnum") == expected);
233 }
234
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100235 SECTION("getItems on a list")
236 {
237 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100238 REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']", std::nullopt, ""s));
239 REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']/name", std::nullopt, "Jan"s));
240 REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']", std::nullopt, ""s));
241 REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']/name", std::nullopt, "Michal"s));
242 REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']", std::nullopt, ""s));
243 REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']/name", std::nullopt, "Petr"s));
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100244 datastore.createListInstance("/example-schema:person[name='Jan']");
245 datastore.createListInstance("/example-schema:person[name='Michal']");
246 datastore.createListInstance("/example-schema:person[name='Petr']");
247 datastore.commitChanges();
248 }
Jan Kundrátb331b552020-01-23 15:25:29 +0100249 DatastoreAccess::Tree expected{
Václav Kubernát144729d2020-01-08 15:20:35 +0100250 {"/example-schema:person[name='Jan']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100251 {"/example-schema:person[name='Jan']/name", std::string{"Jan"}},
Václav Kubernát144729d2020-01-08 15:20:35 +0100252 {"/example-schema:person[name='Michal']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100253 {"/example-schema:person[name='Michal']/name", std::string{"Michal"}},
Václav Kubernát144729d2020-01-08 15:20:35 +0100254 {"/example-schema:person[name='Petr']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100255 {"/example-schema:person[name='Petr']/name", std::string{"Petr"}}
256 };
257
258 REQUIRE(datastore.getItems("/example-schema:person") == expected);
259 }
260
Václav Kubernát69aabe92020-01-24 16:53:12 +0100261 SECTION("presence containers")
262 {
263 DatastoreAccess::Tree expected;
264 // Make sure it's not there before we create it
265 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
266
267 {
268 REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s));
269 datastore.createPresenceContainer("/example-schema:pContainer");
270 datastore.commitChanges();
271 }
272 expected = {
273 {"/example-schema:pContainer", special_{SpecialValue::PresenceContainer}}
274 };
275 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
276
277 // Make sure it's not there after we delete it
278 {
279 REQUIRE_CALL(mock, write("/example-schema:pContainer", ""s, std::nullopt));
280 datastore.deletePresenceContainer("/example-schema:pContainer");
281 datastore.commitChanges();
282 }
283 expected = {};
284 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100285 }
Václav Kubernát69aabe92020-01-24 16:53:12 +0100286
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100287 SECTION("nested presence container")
288 {
289 DatastoreAccess::Tree expected;
290 // Make sure it's not there before we create it
291 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
292 {
293 REQUIRE_CALL(mock, write("/example-schema:inventory", std::nullopt, ""s));
294 REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", std::nullopt, ""s));
295 datastore.createPresenceContainer("/example-schema:inventory/stuff");
296 datastore.commitChanges();
297 }
298 expected = {
299 {"/example-schema:inventory/stuff", special_{SpecialValue::PresenceContainer}}
300 };
301 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
302 {
303 REQUIRE_CALL(mock, write("/example-schema:inventory", ""s, std::nullopt));
304 REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", ""s, std::nullopt));
305 datastore.deletePresenceContainer("/example-schema:inventory/stuff");
306 datastore.commitChanges();
307 }
308 expected = {};
309 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
Václav Kubernát69aabe92020-01-24 16:53:12 +0100310 }
311
Jan Kundrátbd3169c2020-02-03 19:31:34 +0100312 SECTION("floats")
313 {
314 datastore.setLeaf("/example-schema:leafDecimal", 123.4);
315 REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.4"s));
316 datastore.commitChanges();
317 DatastoreAccess::Tree expected {
318 {"/example-schema:leafDecimal", 123.4},
319 };
320 REQUIRE(datastore.getItems("/example-schema:leafDecimal") == expected);
321 }
322
Jan Kundrát3ff50122020-05-07 00:37:50 +0200323 SECTION("unions")
324 {
325 datastore.setLeaf("/example-schema:unionIntString", int32_t{10});
326 REQUIRE_CALL(mock, write("/example-schema:unionIntString", std::nullopt, "10"s));
327 datastore.commitChanges();
328 DatastoreAccess::Tree expected {
329 {"/example-schema:unionIntString", int32_t{10}},
330 };
331 REQUIRE(datastore.getItems("/example-schema:unionIntString") == expected);
332 }
333
Jan Kundrát0d8abd12020-05-07 02:00:14 +0200334 SECTION("identityref") {
335 datastore.setLeaf("/example-schema:beast", identityRef_{"example-schema", "Mammal"});
336 REQUIRE_CALL(mock, write("/example-schema:beast", std::nullopt, "example-schema:Mammal"s));
337 datastore.commitChanges();
338 DatastoreAccess::Tree expected {
339 {"/example-schema:beast", identityRef_{"example-schema", "Mammal"}},
340 };
341 REQUIRE(datastore.getItems("/example-schema:beast") == expected);
342
343 datastore.setLeaf("/example-schema:beast", identityRef_{"Whale"});
344 REQUIRE_CALL(mock, write("/example-schema:beast", "example-schema:Mammal", "example-schema:Whale"s));
345 datastore.commitChanges();
346 expected = {
347 {"/example-schema:beast", identityRef_{"example-schema", "Whale"}},
348 };
349 REQUIRE(datastore.getItems("/example-schema:beast") == expected);
350 }
351
Jan Kundrát68985442020-05-07 02:15:34 +0200352 SECTION("binary")
353 {
354 datastore.setLeaf("/example-schema:blob", binary_{"cHduegByIQ=="s});
355 REQUIRE_CALL(mock, write("/example-schema:blob", std::nullopt, "cHduegByIQ=="s));
356 datastore.commitChanges();
357 DatastoreAccess::Tree expected {
358 {"/example-schema:blob", binary_{"cHduegByIQ=="s}},
359 };
360 REQUIRE(datastore.getItems("/example-schema:blob") == expected);
361 }
362
Jan Kundrát379bb572020-05-07 03:23:13 +0200363 SECTION("empty")
364 {
365 datastore.setLeaf("/example-schema:dummy", empty_{});
366 REQUIRE_CALL(mock, write("/example-schema:dummy", std::nullopt, ""s));
367 datastore.commitChanges();
368 DatastoreAccess::Tree expected {
369 {"/example-schema:dummy", empty_{}},
370 };
371 REQUIRE(datastore.getItems("/example-schema:dummy") == expected);
372 }
373
Jan Kundrátbb525b42020-02-04 11:56:59 +0100374 SECTION("operational data")
375 {
376 MockDataSupplier mockOpsData;
377 OperationalDataSubscription opsDataSub("/example-schema:temperature", mockOpsData);
378 DatastoreAccess::Tree expected;
379 std::string xpath;
380 SECTION("temperature")
381 {
382 expected = {{"/example-schema:temperature", int32_t{22}}};
383 xpath = "/example-schema:temperature";
384 }
385
386 REQUIRE_CALL(mockOpsData, get_data(xpath)).RETURN(expected);
387 REQUIRE(datastore.getItems(xpath) == expected);
388 }
389
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200390 SECTION("leaf list")
391 {
392 DatastoreAccess::Tree expected;
393 REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "0.0.0.0"s));
394 REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "127.0.0.1"s));
395 datastore.createLeafListInstance("/example-schema:addresses[.='0.0.0.0']");
396 datastore.createLeafListInstance("/example-schema:addresses[.='127.0.0.1']");
397 datastore.commitChanges();
398 expected = {
399 {"/example-schema:addresses", special_{SpecialValue::LeafList}},
400 {"/example-schema:addresses[.='0.0.0.0']", "0.0.0.0"s},
401 {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s},
402 };
403 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
404
405 REQUIRE_CALL(mock, write("/example-schema:addresses", "0.0.0.0"s, std::nullopt));
406 datastore.deleteLeafListInstance("/example-schema:addresses[.='0.0.0.0']");
407 datastore.commitChanges();
408 expected = {
409 {"/example-schema:addresses", special_{SpecialValue::LeafList}},
410 {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s},
411 };
412 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
413
414 REQUIRE_CALL(mock, write("/example-schema:addresses", "127.0.0.1"s, std::nullopt));
415 datastore.deleteLeafListInstance("/example-schema:addresses[.='127.0.0.1']");
416 datastore.commitChanges();
417 expected = {};
418 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
419 }
Jan Kundrátbb525b42020-02-04 11:56:59 +0100420
Václav Kubernát7160a132020-04-03 02:11:01 +0200421 SECTION("copying data from startup refreshes the data")
422 {
423 {
424 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{});
425 REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "123"s));
426 datastore.setLeaf("/example-schema:leafInt16", int16_t{123});
427 datastore.commitChanges();
428 }
429 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{{"/example-schema:leafInt16", int16_t{123}}});
430 REQUIRE_CALL(mock, write("/example-schema:leafInt16", "123"s, std::nullopt));
431 datastore.copyConfig(Datastore::Startup, Datastore::Running);
432 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{});
433 }
434
Václav Kubernát73109382018-09-14 19:52:03 +0200435 waitForCompletionAndBitMore(seq1);
436}
Jan Kundrát6ee84792020-01-24 01:43:36 +0100437
438class RpcCb: public sysrepo::Callback {
439 int rpc(const char *xpath, const ::sysrepo::S_Vals input, ::sysrepo::S_Vals_Holder output, void *) override
440 {
441 const auto nukes = "/example-schema:launch-nukes"s;
442 if (xpath == "/example-schema:noop"s) {
443 return SR_ERR_OK;
444 } else if (xpath == nukes) {
445 uint64_t kilotons = 0;
446 bool hasCities = false;
447 for (size_t i = 0; i < input->val_cnt(); ++i) {
448 const auto& val = input->val(i);
449 if (val->xpath() == nukes + "/payload/kilotons") {
450 kilotons = val->data()->get_uint64();
451 } else if (val->xpath() == nukes + "/payload") {
452 // ignore, container
453 } else if (val->xpath() == nukes + "/description") {
454 // unused
455 } else if (std::string_view{val->xpath()}.find(nukes + "/cities") == 0) {
456 hasCities = true;
457 } else {
458 throw std::runtime_error("RPC launch-nukes: unexpected input "s + val->xpath());
459 }
460 }
461 if (kilotons == 333'666) {
462 // magic, just do not generate any output. This is important because the NETCONF RPC returns just <ok/>.
463 return SR_ERR_OK;
464 }
465 auto buf = output->allocate(2);
466 size_t i = 0;
467 buf->val(i++)->set((nukes + "/blast-radius").c_str(), uint32_t{33'666});
468 buf->val(i++)->set((nukes + "/actual-yield").c_str(), static_cast<uint64_t>(1.33 * kilotons));
469 if (hasCities) {
470 buf = output->reallocate(output->val_cnt() + 2);
471 buf->val(i++)->set((nukes + "/damaged-places/targets[city='London']/city").c_str(), "London");
472 buf->val(i++)->set((nukes + "/damaged-places/targets[city='Berlin']/city").c_str(), "Berlin");
473 }
474 return SR_ERR_OK;
475 }
476 throw std::runtime_error("unrecognized RPC");
477 }
478};
479
480TEST_CASE("rpc") {
481 trompeloeil::sequence seq1;
482 auto srConn = std::make_shared<sysrepo::Connection>("netconf-cli-test-rpc");
483 auto srSession = std::make_shared<sysrepo::Session>(srConn);
484 auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSession);
485 auto cb = std::make_shared<RpcCb>();
486 sysrepo::Logs{}.set_stderr(SR_LL_INF);
487 srSubscription->rpc_subscribe("/example-schema:noop", cb, nullptr, SR_SUBSCR_CTX_REUSE);
488 srSubscription->rpc_subscribe("/example-schema:launch-nukes", cb, nullptr, SR_SUBSCR_CTX_REUSE);
489
490#ifdef sysrepo_BACKEND
Václav Kubernát715c85c2020-04-14 01:46:08 +0200491 SysrepoAccess datastore("netconf-cli-test", Datastore::Running);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100492#elif defined(netconf_BACKEND)
493 NetconfAccess datastore(NETOPEER_SOCKET_PATH);
494#else
495#error "Unknown backend"
496#endif
497
498 std::string rpc;
499 DatastoreAccess::Tree input, output;
500
501 SECTION("noop") {
502 rpc = "/example-schema:noop";
503 }
504
505 SECTION("small nuke") {
506 rpc = "/example-schema:launch-nukes";
507 input = {
508 {"description", "dummy"s},
509 {"payload/kilotons", uint64_t{333'666}},
510 };
511 // no data are returned
512 }
513
514 SECTION("small nuke") {
515 rpc = "/example-schema:launch-nukes";
516 input = {
517 {"description", "dummy"s},
518 {"payload/kilotons", uint64_t{4}},
519 };
520 output = {
521 {"blast-radius", uint32_t{33'666}},
522 {"actual-yield", uint64_t{5}},
523 };
524 }
525
526 SECTION("with lists") {
527 rpc = "/example-schema:launch-nukes";
528 input = {
529 {"payload/kilotons", uint64_t{6}},
530 {"cities/targets[city='Prague']/city", "Prague"s},
531 };
532 output = {
533 {"blast-radius", uint32_t{33'666}},
534 {"actual-yield", uint64_t{7}},
535 {"damaged-places", special_{SpecialValue::PresenceContainer}},
536 {"damaged-places/targets[city='London']", special_{SpecialValue::List}},
537 {"damaged-places/targets[city='London']/city", "London"s},
538 {"damaged-places/targets[city='Berlin']", special_{SpecialValue::List}},
539 {"damaged-places/targets[city='Berlin']/city", "Berlin"s},
540 };
541 }
542
543 REQUIRE(datastore.executeRpc(rpc, input) == output);
544
545 waitForCompletionAndBitMore(seq1);
546}