blob: b9870f3aa8cafe8286c5775df60985e5762db6f8 [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átbf65dd72020-05-28 02:32:31 +0200435 SECTION("moving leaflist instances")
436 {
437 DatastoreAccess::Tree expected;
438 {
439 // sysrepo does this twice for some reason, it's possibly a bug
440 REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "http"s)).TIMES(2);
441 REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "ftp"s));
442 REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s));
443 REQUIRE_CALL(mock, write("/example-schema:protocols", "http"s, "ftp"s));
444 REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "pop3"s));
445 datastore.createLeafListInstance("/example-schema:protocols[.='http']");
446 datastore.createLeafListInstance("/example-schema:protocols[.='ftp']");
447 datastore.createLeafListInstance("/example-schema:protocols[.='pop3']");
448 datastore.commitChanges();
449 expected = {
450 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
451 {"/example-schema:protocols[.='http']", "http"s},
452 {"/example-schema:protocols[.='ftp']", "ftp"s},
453 {"/example-schema:protocols[.='pop3']", "pop3"s},
454 };
455 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
456 }
457
458 std::string sourcePath;
459 SECTION("begin")
460 {
461 REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s));
462 sourcePath = "/example-schema:protocols[.='pop3']";
463 datastore.moveItem(sourcePath, yang::move::Absolute::Begin);
464 datastore.commitChanges();
465 expected = {
466 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
467 {"/example-schema:protocols[.='pop3']", "pop3"s},
468 {"/example-schema:protocols[.='http']", "http"s},
469 {"/example-schema:protocols[.='ftp']", "ftp"s},
470 };
471 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
472 }
473
474 SECTION("end")
475 {
476 sourcePath = "/example-schema:protocols[.='http']";
477 REQUIRE_CALL(mock, write("/example-schema:protocols", "pop3"s, "http"s));
478 datastore.moveItem(sourcePath, yang::move::Absolute::End);
479 datastore.commitChanges();
480 expected = {
481 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
482 {"/example-schema:protocols[.='ftp']", "ftp"s},
483 {"/example-schema:protocols[.='pop3']", "pop3"s},
484 {"/example-schema:protocols[.='http']", "http"s},
485 };
486 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
487 }
488
489 SECTION("after")
490 {
491 sourcePath = "/example-schema:protocols[.='http']";
492 REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "http"s));
493 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{".", "ftp"s}}});
494 datastore.commitChanges();
495 expected = {
496 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
497 {"/example-schema:protocols[.='ftp']", "ftp"s},
498 {"/example-schema:protocols[.='http']", "http"s},
499 {"/example-schema:protocols[.='pop3']", "pop3"s},
500 };
501 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
502 }
503
504 SECTION("before")
505 {
506 sourcePath = "/example-schema:protocols[.='http']";
507 REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "http"s));
508 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{".", "pop3"s}}});
509 datastore.commitChanges();
510 expected = {
511 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
512 {"/example-schema:protocols[.='ftp']", "ftp"s},
513 {"/example-schema:protocols[.='http']", "http"s},
514 {"/example-schema:protocols[.='pop3']", "pop3"s},
515 };
516 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
517 }
518 }
519
520 SECTION("moving list instances")
521 {
522 DatastoreAccess::Tree expected;
523 {
524 // sysrepo does this twice for some reason, it's possibly a bug
525 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", std::nullopt, ""s)).TIMES(2);
526 REQUIRE_CALL(mock, write("/example-schema:players[name='John']/name", std::nullopt, "John"s));
527 REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']", std::nullopt, ""s));
528 REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']", ""s, ""s));
529 REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']/name", std::nullopt, "Eve"s));
530 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", std::nullopt, ""s));
531 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']/name", std::nullopt, "Adam"s));
532 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", ""s, ""s));
533 datastore.createListInstance("/example-schema:players[name='John']");
534 datastore.createListInstance("/example-schema:players[name='Eve']");
535 datastore.createListInstance("/example-schema:players[name='Adam']");
536 datastore.commitChanges();
537 expected = {
538 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
539 {"/example-schema:players[name='John']/name", "John"s},
540 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
541 {"/example-schema:players[name='Eve']/name", "Eve"s},
542 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
543 {"/example-schema:players[name='Adam']/name", "Adam"s},
544 };
545 REQUIRE(datastore.getItems("/example-schema:players") == expected);
546 }
547
548 std::string sourcePath;
549 SECTION("begin")
550 {
551 sourcePath = "/example-schema:players[name='Adam']";
552 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", std::nullopt, ""s));
553 datastore.moveItem(sourcePath, yang::move::Absolute::Begin);
554 datastore.commitChanges();
555 expected = {
556 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
557 {"/example-schema:players[name='Adam']/name", "Adam"s},
558 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
559 {"/example-schema:players[name='John']/name", "John"s},
560 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
561 {"/example-schema:players[name='Eve']/name", "Eve"s},
562 };
563 REQUIRE(datastore.getItems("/example-schema:players") == expected);
564 }
565
566 SECTION("end")
567 {
568 sourcePath = "/example-schema:players[name='John']";
569 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s));
570 datastore.moveItem(sourcePath, yang::move::Absolute::End);
571 datastore.commitChanges();
572 expected = {
573 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
574 {"/example-schema:players[name='Eve']/name", "Eve"s},
575 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
576 {"/example-schema:players[name='Adam']/name", "Adam"s},
577 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
578 {"/example-schema:players[name='John']/name", "John"s},
579 };
580 REQUIRE(datastore.getItems("/example-schema:players") == expected);
581 }
582
583 SECTION("after")
584 {
585 sourcePath = "/example-schema:players[name='John']";
586 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s));
587 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{"name", "Eve"s}}});
588 datastore.commitChanges();
589 expected = {
590 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
591 {"/example-schema:players[name='Eve']/name", "Eve"s},
592 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
593 {"/example-schema:players[name='John']/name", "John"s},
594 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
595 {"/example-schema:players[name='Adam']/name", "Adam"s},
596 };
597 REQUIRE(datastore.getItems("/example-schema:players") == expected);
598 }
599
600 SECTION("before")
601 {
602 sourcePath = "/example-schema:players[name='John']";
603 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s));
604 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{"name", "Adam"s}}});
605 datastore.commitChanges();
606 expected = {
607 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
608 {"/example-schema:players[name='Eve']/name", "Eve"s},
609 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
610 {"/example-schema:players[name='John']/name", "John"s},
611 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
612 {"/example-schema:players[name='Adam']/name", "Adam"s},
613 };
614 REQUIRE(datastore.getItems("/example-schema:players") == expected);
615 }
616 }
617
Václav Kubernát73109382018-09-14 19:52:03 +0200618 waitForCompletionAndBitMore(seq1);
619}
Jan Kundrát6ee84792020-01-24 01:43:36 +0100620
621class RpcCb: public sysrepo::Callback {
622 int rpc(const char *xpath, const ::sysrepo::S_Vals input, ::sysrepo::S_Vals_Holder output, void *) override
623 {
624 const auto nukes = "/example-schema:launch-nukes"s;
625 if (xpath == "/example-schema:noop"s) {
626 return SR_ERR_OK;
627 } else if (xpath == nukes) {
628 uint64_t kilotons = 0;
629 bool hasCities = false;
630 for (size_t i = 0; i < input->val_cnt(); ++i) {
631 const auto& val = input->val(i);
632 if (val->xpath() == nukes + "/payload/kilotons") {
633 kilotons = val->data()->get_uint64();
634 } else if (val->xpath() == nukes + "/payload") {
635 // ignore, container
636 } else if (val->xpath() == nukes + "/description") {
637 // unused
638 } else if (std::string_view{val->xpath()}.find(nukes + "/cities") == 0) {
639 hasCities = true;
640 } else {
641 throw std::runtime_error("RPC launch-nukes: unexpected input "s + val->xpath());
642 }
643 }
644 if (kilotons == 333'666) {
645 // magic, just do not generate any output. This is important because the NETCONF RPC returns just <ok/>.
646 return SR_ERR_OK;
647 }
648 auto buf = output->allocate(2);
649 size_t i = 0;
650 buf->val(i++)->set((nukes + "/blast-radius").c_str(), uint32_t{33'666});
651 buf->val(i++)->set((nukes + "/actual-yield").c_str(), static_cast<uint64_t>(1.33 * kilotons));
652 if (hasCities) {
653 buf = output->reallocate(output->val_cnt() + 2);
654 buf->val(i++)->set((nukes + "/damaged-places/targets[city='London']/city").c_str(), "London");
655 buf->val(i++)->set((nukes + "/damaged-places/targets[city='Berlin']/city").c_str(), "Berlin");
656 }
657 return SR_ERR_OK;
658 }
659 throw std::runtime_error("unrecognized RPC");
660 }
661};
662
663TEST_CASE("rpc") {
664 trompeloeil::sequence seq1;
665 auto srConn = std::make_shared<sysrepo::Connection>("netconf-cli-test-rpc");
666 auto srSession = std::make_shared<sysrepo::Session>(srConn);
667 auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSession);
668 auto cb = std::make_shared<RpcCb>();
669 sysrepo::Logs{}.set_stderr(SR_LL_INF);
670 srSubscription->rpc_subscribe("/example-schema:noop", cb, nullptr, SR_SUBSCR_CTX_REUSE);
671 srSubscription->rpc_subscribe("/example-schema:launch-nukes", cb, nullptr, SR_SUBSCR_CTX_REUSE);
672
673#ifdef sysrepo_BACKEND
Václav Kubernát715c85c2020-04-14 01:46:08 +0200674 SysrepoAccess datastore("netconf-cli-test", Datastore::Running);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100675#elif defined(netconf_BACKEND)
676 NetconfAccess datastore(NETOPEER_SOCKET_PATH);
677#else
678#error "Unknown backend"
679#endif
680
681 std::string rpc;
682 DatastoreAccess::Tree input, output;
683
684 SECTION("noop") {
685 rpc = "/example-schema:noop";
686 }
687
688 SECTION("small nuke") {
689 rpc = "/example-schema:launch-nukes";
690 input = {
691 {"description", "dummy"s},
692 {"payload/kilotons", uint64_t{333'666}},
693 };
694 // no data are returned
695 }
696
697 SECTION("small nuke") {
698 rpc = "/example-schema:launch-nukes";
699 input = {
700 {"description", "dummy"s},
701 {"payload/kilotons", uint64_t{4}},
702 };
703 output = {
704 {"blast-radius", uint32_t{33'666}},
705 {"actual-yield", uint64_t{5}},
706 };
707 }
708
709 SECTION("with lists") {
710 rpc = "/example-schema:launch-nukes";
711 input = {
712 {"payload/kilotons", uint64_t{6}},
713 {"cities/targets[city='Prague']/city", "Prague"s},
714 };
715 output = {
716 {"blast-radius", uint32_t{33'666}},
717 {"actual-yield", uint64_t{7}},
718 {"damaged-places", special_{SpecialValue::PresenceContainer}},
719 {"damaged-places/targets[city='London']", special_{SpecialValue::List}},
720 {"damaged-places/targets[city='London']/city", "London"s},
721 {"damaged-places/targets[city='Berlin']", special_{SpecialValue::List}},
722 {"damaged-places/targets[city='Berlin']/city", "Berlin"s},
723 };
724 }
725
726 REQUIRE(datastore.executeRpc(rpc, input) == output);
727
728 waitForCompletionAndBitMore(seq1);
729}