blob: e50585188ef7f84dc546792fa7f5d9bfeb0ca21d [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"
Jan Kundrát7ec214d2020-06-19 17:05:07 +020014using OnInvalidSchemaPathCreate = DatastoreException;
15using OnInvalidSchemaPathDelete = void;
16using OnInvalidSchemaPathMove = sysrepo::sysrepo_exception;
Václav Kubernát74487df2020-06-04 01:29:28 +020017using OnInvalidRpcPath = sysrepo::sysrepo_exception;
Jan Kundrát7ec214d2020-06-19 17:05:07 +020018using OnKeyNotFound = void;
Václav Kubernát74487df2020-06-04 01:29:28 +020019using OnRPC = void;
Václav Kubernátc31bd602019-03-07 11:44:48 +010020#elif defined(netconf_BACKEND)
Jan Kundrát7ec214d2020-06-19 17:05:07 +020021using OnInvalidSchemaPathCreate = std::runtime_error;
22using OnInvalidSchemaPathDelete = std::runtime_error;
23using OnInvalidSchemaPathMove = std::runtime_error;
Václav Kubernát74487df2020-06-04 01:29:28 +020024using OnInvalidRpcPath = std::runtime_error;
Jan Kundrát7ec214d2020-06-19 17:05:07 +020025using OnKeyNotFound = std::runtime_error;
Václav Kubernát74487df2020-06-04 01:29:28 +020026using OnRPC = void;
Václav Kubernátc31bd602019-03-07 11:44:48 +010027#include "netconf_access.hpp"
28#include "netopeer_vars.hpp"
Václav Kubernát74487df2020-06-04 01:29:28 +020029#elif defined(yang_BACKEND)
30#include <fstream>
31#include "yang_access.hpp"
32#include "yang_access_test_vars.hpp"
33using OnInvalidSchemaPathCreate = DatastoreException;
34using OnInvalidSchemaPathDelete = DatastoreException;
35using OnInvalidSchemaPathMove = DatastoreException;
36using OnInvalidRpcPath = DatastoreException;
37using OnKeyNotFound = DatastoreException;
38using OnRPC = std::logic_error;
Václav Kubernátc31bd602019-03-07 11:44:48 +010039#else
40#error "Unknown backend"
41#endif
Jan Kundrátbb525b42020-02-04 11:56:59 +010042#include "pretty_printers.hpp"
Václav Kubernát73109382018-09-14 19:52:03 +020043#include "sysrepo_subscription.hpp"
Václav Kubernát8e121ff2019-10-15 15:47:45 +020044#include "utils.hpp"
Václav Kubernát73109382018-09-14 19:52:03 +020045
Jan Kundrát6ee84792020-01-24 01:43:36 +010046using namespace std::literals::string_literals;
47
Václav Kubernát69aabe92020-01-24 16:53:12 +010048class MockRecorder : public trompeloeil::mock_interface<Recorder> {
Václav Kubernát73109382018-09-14 19:52:03 +020049public:
Václav Kubernát69aabe92020-01-24 16:53:12 +010050 IMPLEMENT_MOCK3(write);
Václav Kubernát73109382018-09-14 19:52:03 +020051};
52
Jan Kundrátbb525b42020-02-04 11:56:59 +010053class MockDataSupplier : public trompeloeil::mock_interface<DataSupplier> {
54public:
55 IMPLEMENT_CONST_MOCK1(get_data);
56};
57
Jan Kundrát7ec214d2020-06-19 17:05:07 +020058namespace {
59template <class ...> constexpr std::false_type always_false [[maybe_unused]] {};
60template <class Exception, typename Callable> void catching(const Callable& what) {
61
62 if constexpr (std::is_same_v<Exception, void>) {
Jan Kundrát3867c9e2020-06-18 20:26:45 +020063 what();
Jan Kundrát7ec214d2020-06-19 17:05:07 +020064 } else if constexpr (std::is_same<Exception, std::runtime_error>()) {
65 // cannot use REQUIRE_THROWS_AS(..., Exception) directly because that one
66 // needs an extra `typename` deep in the bowels of doctest
67 REQUIRE_THROWS_AS(what(), std::runtime_error);
Václav Kubernát74487df2020-06-04 01:29:28 +020068 } else if constexpr (std::is_same<Exception, std::logic_error>()) {
69 REQUIRE_THROWS_AS(what(), std::logic_error);
Jan Kundrát7ec214d2020-06-19 17:05:07 +020070 } else if constexpr (std::is_same<Exception, DatastoreException>()) {
71 REQUIRE_THROWS_AS(what(), DatastoreException);
72 } else if constexpr (std::is_same<Exception, sysrepo::sysrepo_exception>()) {
73 REQUIRE_THROWS_AS(what(), sysrepo::sysrepo_exception);
74 } else {
75 static_assert(always_false<Exception>); // https://stackoverflow.com/a/53945549/2245623
Jan Kundrát3867c9e2020-06-18 20:26:45 +020076 }
77}
Jan Kundrát7ec214d2020-06-19 17:05:07 +020078}
Jan Kundrát3867c9e2020-06-18 20:26:45 +020079
Václav Kubernát74487df2020-06-04 01:29:28 +020080#if defined(yang_BACKEND)
81class TestYangAccess : public YangAccess {
82public:
83 void commitChanges() override
84 {
85 YangAccess::commitChanges();
86 dumpToSysrepo();
87 }
88
89 void copyConfig(const Datastore source, const Datastore destination) override
90 {
91 YangAccess::copyConfig(source, destination);
92 dumpToSysrepo();
93 }
94
95private:
96 void dumpToSysrepo()
97 {
98 {
99 std::ofstream of(testConfigFile);
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200100 of << dump(DataFormat::Xml);
Václav Kubernát74487df2020-06-04 01:29:28 +0200101 }
102 auto command = std::string(sysrepocfgExecutable) + " --import=" + testConfigFile + " --format=xml --datastore=running example-schema";
103 REQUIRE(std::system(command.c_str()) == 0);
104 }
105};
106#endif
107
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200108TEST_CASE("setting/getting values")
Václav Kubernát73109382018-09-14 19:52:03 +0200109{
Václav Kubernát73109382018-09-14 19:52:03 +0200110 trompeloeil::sequence seq1;
111 MockRecorder mock;
Václav Kubernátab612e92019-11-26 19:51:31 +0100112 SysrepoSubscription subscription("example-schema", &mock);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100113
114#ifdef sysrepo_BACKEND
Václav Kubernát715c85c2020-04-14 01:46:08 +0200115 SysrepoAccess datastore("netconf-cli-test", Datastore::Running);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100116#elif defined(netconf_BACKEND)
117 NetconfAccess datastore(NETOPEER_SOCKET_PATH);
Václav Kubernát74487df2020-06-04 01:29:28 +0200118#elif defined(yang_BACKEND)
119 TestYangAccess datastore;
120 datastore.addSchemaDir(schemaDir);
121 datastore.addSchemaFile(exampleSchemaFile);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100122#else
123#error "Unknown backend"
124#endif
Václav Kubernát73109382018-09-14 19:52:03 +0200125
Václav Kubernát69aabe92020-01-24 16:53:12 +0100126
Václav Kubernát134d78f2019-09-03 16:42:29 +0200127 SECTION("set leafInt8 to -128")
Václav Kubernát73109382018-09-14 19:52:03 +0200128 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100129 REQUIRE_CALL(mock, write("/example-schema:leafInt8", std::nullopt, "-128"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200130 datastore.setLeaf("/example-schema:leafInt8", int8_t{-128});
131 datastore.commitChanges();
132 }
133
134 SECTION("set leafInt16 to -32768")
135 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100136 REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "-32768"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200137 datastore.setLeaf("/example-schema:leafInt16", int16_t{-32768});
138 datastore.commitChanges();
139 }
140
141 SECTION("set leafInt32 to -2147483648")
142 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100143 REQUIRE_CALL(mock, write("/example-schema:leafInt32", std::nullopt, "-2147483648"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200144 datastore.setLeaf("/example-schema:leafInt32", int32_t{-2147483648});
145 datastore.commitChanges();
146 }
147
148 SECTION("set leafInt64 to -50000000000")
149 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100150 REQUIRE_CALL(mock, write("/example-schema:leafInt64", std::nullopt, "-50000000000"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200151 datastore.setLeaf("/example-schema:leafInt64", int64_t{-50000000000});
152 datastore.commitChanges();
153 }
154
155 SECTION("set leafUInt8 to 255")
156 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100157 REQUIRE_CALL(mock, write("/example-schema:leafUInt8", std::nullopt, "255"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200158 datastore.setLeaf("/example-schema:leafUInt8", uint8_t{255});
159 datastore.commitChanges();
160 }
161
162 SECTION("set leafUInt16 to 65535")
163 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100164 REQUIRE_CALL(mock, write("/example-schema:leafUInt16", std::nullopt, "65535"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200165 datastore.setLeaf("/example-schema:leafUInt16", uint16_t{65535});
166 datastore.commitChanges();
167 }
168
169 SECTION("set leafUInt32 to 4294967295")
170 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100171 REQUIRE_CALL(mock, write("/example-schema:leafUInt32", std::nullopt, "4294967295"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200172 datastore.setLeaf("/example-schema:leafUInt32", uint32_t{4294967295});
173 datastore.commitChanges();
174 }
175
176 SECTION("set leafUInt64 to 50000000000")
177 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100178 REQUIRE_CALL(mock, write("/example-schema:leafUInt64", std::nullopt, "50000000000"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200179 datastore.setLeaf("/example-schema:leafUInt64", uint64_t{50000000000});
Václav Kubernát73109382018-09-14 19:52:03 +0200180 datastore.commitChanges();
181 }
182
183 SECTION("set leafEnum to coze")
184 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100185 REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "coze"s));
Václav Kubernát73109382018-09-14 19:52:03 +0200186 datastore.setLeaf("/example-schema:leafEnum", enum_{"coze"});
187 datastore.commitChanges();
188 }
189
190 SECTION("set leafDecimal to 123.544")
191 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100192 REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.544"s));
Václav Kubernát73109382018-09-14 19:52:03 +0200193 datastore.setLeaf("/example-schema:leafDecimal", 123.544);
194 datastore.commitChanges();
195 }
196
Jan Kundrátd2872862020-06-18 21:02:00 +0200197 SECTION("set a string, then delete it")
198 {
199 REQUIRE_CALL(mock, write("/example-schema:leafString", std::nullopt, "blah"s));
200 datastore.setLeaf("/example-schema:leafString", "blah"s);
201 datastore.commitChanges();
202 DatastoreAccess::Tree expected{{"/example-schema:leafString", "blah"s}};
203 REQUIRE(datastore.getItems("/example-schema:leafString") == expected);
204
205 REQUIRE_CALL(mock, write("/example-schema:leafString", "blah"s, std::nullopt));
206 datastore.deleteItem("/example-schema:leafString");
207 datastore.commitChanges();
208 expected.clear();
209 REQUIRE(datastore.getItems("/example-schema:leafString") == expected);
210 }
211
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200212 SECTION("set a non-existing leaf")
213 {
214 catching<OnInvalidSchemaPathCreate>([&]{
215 datastore.setLeaf("/example-schema:non-existing", "what"s);
216 });
217 }
218
Václav Kubernát73109382018-09-14 19:52:03 +0200219 SECTION("create presence container")
220 {
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200221 REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:pContainer") == std::string::npos);
Václav Kubernát69aabe92020-01-24 16:53:12 +0100222 REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200223 datastore.createItem("/example-schema:pContainer");
Václav Kubernát73109382018-09-14 19:52:03 +0200224 datastore.commitChanges();
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200225 REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:pContainer") != std::string::npos);
Václav Kubernát73109382018-09-14 19:52:03 +0200226 }
227
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100228 SECTION("create/delete a list instance")
Václav Kubernát45f4a822019-05-29 21:10:50 +0200229 {
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100230 {
231 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", std::nullopt, ""s));
232 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']/name", std::nullopt, "Nguyen"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200233 datastore.createItem("/example-schema:person[name='Nguyen']");
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100234 datastore.commitChanges();
235 }
236 {
237 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", ""s, std::nullopt));
238 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']/name", "Nguyen"s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200239 datastore.deleteItem("/example-schema:person[name='Nguyen']");
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100240 datastore.commitChanges();
241 }
Václav Kubernát45f4a822019-05-29 21:10:50 +0200242 }
243
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200244 SECTION("deleting non-existing list keys")
245 {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200246 catching<OnKeyNotFound>([&]{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200247 datastore.deleteItem("/example-schema:person[name='non existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200248 datastore.commitChanges();
249 });
250 }
251
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200252 SECTION("accessing non-existing schema nodes as a list")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200253 {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200254 catching<OnInvalidSchemaPathCreate>([&]{
255 datastore.createItem("/example-schema:non-existing-list[xxx='blah']");
256 datastore.commitChanges();
257 });
258 catching<OnInvalidSchemaPathDelete>([&]{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200259 datastore.deleteItem("/example-schema:non-existing-list[xxx='non existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200260 datastore.commitChanges();
261 });
262 }
263
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200264 SECTION("leafref pointing to a key of a list")
265 {
266 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100267 REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']", std::nullopt, ""s));
268 REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']/name", std::nullopt, "Dan"s));
269 REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']", std::nullopt, ""s));
270 REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']/name", std::nullopt, "Elfi"s));
271 REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']", std::nullopt, ""s));
272 REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']/name", std::nullopt, "Kolafa"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200273 datastore.createItem("/example-schema:person[name='Dan']");
274 datastore.createItem("/example-schema:person[name='Elfi']");
275 datastore.createItem("/example-schema:person[name='Kolafa']");
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200276 datastore.commitChanges();
277 }
278
279 // The commitChanges method has to be called in each of the
280 // SECTIONs, because the REQUIRE_CALL only works inside the given
281 // SECTION.
282 SECTION("Dan")
283 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100284 REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Dan"s));
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200285 datastore.setLeaf("/example-schema:bossPerson", std::string{"Dan"});
286 datastore.commitChanges();
287 }
288
289 SECTION("Elfi")
290 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100291 REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Elfi"s));
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200292 datastore.setLeaf("/example-schema:bossPerson", std::string{"Elfi"});
293 datastore.commitChanges();
294 }
295
296 SECTION("Kolafa")
297 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100298 REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Kolafa"s));
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200299 datastore.setLeaf("/example-schema:bossPerson", std::string{"Kolafa"});
300 datastore.commitChanges();
301 }
302 }
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200303 SECTION("bool values get correctly represented as bools")
304 {
305 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100306 REQUIRE_CALL(mock, write("/example-schema:down", std::nullopt, "true"s));
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200307 datastore.setLeaf("/example-schema:down", bool{true});
308 datastore.commitChanges();
309 }
310
Jan Kundrátb331b552020-01-23 15:25:29 +0100311 DatastoreAccess::Tree expected{{"/example-schema:down", bool{true}}};
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200312 REQUIRE(datastore.getItems("/example-schema:down") == expected);
313 }
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200314
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200315 SECTION("getting items from the whole module")
316 {
317 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100318 REQUIRE_CALL(mock, write("/example-schema:up", std::nullopt, "true"s));
319 REQUIRE_CALL(mock, write("/example-schema:down", std::nullopt, "false"s));
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200320 datastore.setLeaf("/example-schema:up", bool{true});
321 datastore.setLeaf("/example-schema:down", bool{false});
322 datastore.commitChanges();
323 }
324
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200325 DatastoreAccess::Tree expected{
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200326 // Sysrepo always returns containers when getting values, but
327 // libnetconf does not. This is fine by the YANG standard:
328 // https://tools.ietf.org/html/rfc7950#section-7.5.7 Furthermore,
329 // NetconfAccess implementation actually only iterates over leafs,
330 // so even if libnetconf did include containers, they wouldn't get
331 // shown here anyway. With sysrepo2, this won't be necessary,
332 // because it'll use the same data structure as libnetconf, so the
333 // results will be consistent.
334#ifdef sysrepo_BACKEND
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100335 {"/example-schema:inventory", special_{SpecialValue::Container}},
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200336 {"/example-schema:lol", special_{SpecialValue::Container}},
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200337#endif
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200338 {"/example-schema:up", bool{true}},
339 {"/example-schema:down", bool{false}}};
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200340 REQUIRE(datastore.getItems("/example-schema:*") == expected);
341 }
342
Václav Kubernát152ce222019-12-19 12:23:32 +0100343 SECTION("getItems returns correct datatypes")
344 {
345 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100346 REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "lol"s));
Václav Kubernát152ce222019-12-19 12:23:32 +0100347 datastore.setLeaf("/example-schema:leafEnum", enum_{"lol"});
348 datastore.commitChanges();
349 }
Jan Kundrátb331b552020-01-23 15:25:29 +0100350 DatastoreAccess::Tree expected{{"/example-schema:leafEnum", enum_{"lol"}}};
Václav Kubernát152ce222019-12-19 12:23:32 +0100351
352 REQUIRE(datastore.getItems("/example-schema:leafEnum") == expected);
353 }
354
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100355 SECTION("getItems on a list")
356 {
357 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100358 REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']", std::nullopt, ""s));
359 REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']/name", std::nullopt, "Jan"s));
360 REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']", std::nullopt, ""s));
361 REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']/name", std::nullopt, "Michal"s));
362 REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']", std::nullopt, ""s));
363 REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']/name", std::nullopt, "Petr"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200364 datastore.createItem("/example-schema:person[name='Jan']");
365 datastore.createItem("/example-schema:person[name='Michal']");
366 datastore.createItem("/example-schema:person[name='Petr']");
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100367 datastore.commitChanges();
368 }
Jan Kundrátb331b552020-01-23 15:25:29 +0100369 DatastoreAccess::Tree expected{
Václav Kubernát144729d2020-01-08 15:20:35 +0100370 {"/example-schema:person[name='Jan']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100371 {"/example-schema:person[name='Jan']/name", std::string{"Jan"}},
Václav Kubernát144729d2020-01-08 15:20:35 +0100372 {"/example-schema:person[name='Michal']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100373 {"/example-schema:person[name='Michal']/name", std::string{"Michal"}},
Václav Kubernát144729d2020-01-08 15:20:35 +0100374 {"/example-schema:person[name='Petr']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100375 {"/example-schema:person[name='Petr']/name", std::string{"Petr"}}
376 };
377
378 REQUIRE(datastore.getItems("/example-schema:person") == expected);
379 }
380
Václav Kubernát69aabe92020-01-24 16:53:12 +0100381 SECTION("presence containers")
382 {
383 DatastoreAccess::Tree expected;
384 // Make sure it's not there before we create it
385 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
386
387 {
388 REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200389 datastore.createItem("/example-schema:pContainer");
Václav Kubernát69aabe92020-01-24 16:53:12 +0100390 datastore.commitChanges();
391 }
392 expected = {
393 {"/example-schema:pContainer", special_{SpecialValue::PresenceContainer}}
394 };
395 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
396
397 // Make sure it's not there after we delete it
398 {
399 REQUIRE_CALL(mock, write("/example-schema:pContainer", ""s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200400 datastore.deleteItem("/example-schema:pContainer");
Václav Kubernát69aabe92020-01-24 16:53:12 +0100401 datastore.commitChanges();
402 }
403 expected = {};
404 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100405 }
Václav Kubernát69aabe92020-01-24 16:53:12 +0100406
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200407 SECTION("creating a non-existing schema node as a container")
408 {
409 catching<OnInvalidSchemaPathCreate>([&]{
410 datastore.createItem("/example-schema:non-existing-presence-container");
411 datastore.commitChanges();
412 });
413 }
414
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200415 SECTION("deleting a non-existing schema node as a container or leaf")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200416 {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200417 catching<OnInvalidSchemaPathDelete>([&]{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200418 datastore.deleteItem("/example-schema:non-existing-presence-container");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200419 datastore.commitChanges();
420 });
421 }
422
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100423 SECTION("nested presence container")
424 {
425 DatastoreAccess::Tree expected;
426 // Make sure it's not there before we create it
427 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
428 {
429 REQUIRE_CALL(mock, write("/example-schema:inventory", std::nullopt, ""s));
430 REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", std::nullopt, ""s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200431 datastore.createItem("/example-schema:inventory/stuff");
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100432 datastore.commitChanges();
433 }
434 expected = {
435 {"/example-schema:inventory/stuff", special_{SpecialValue::PresenceContainer}}
436 };
437 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
438 {
439 REQUIRE_CALL(mock, write("/example-schema:inventory", ""s, std::nullopt));
440 REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", ""s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200441 datastore.deleteItem("/example-schema:inventory/stuff");
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100442 datastore.commitChanges();
443 }
444 expected = {};
445 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
Václav Kubernát69aabe92020-01-24 16:53:12 +0100446 }
447
Jan Kundrátbd3169c2020-02-03 19:31:34 +0100448 SECTION("floats")
449 {
450 datastore.setLeaf("/example-schema:leafDecimal", 123.4);
451 REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.4"s));
452 datastore.commitChanges();
453 DatastoreAccess::Tree expected {
454 {"/example-schema:leafDecimal", 123.4},
455 };
456 REQUIRE(datastore.getItems("/example-schema:leafDecimal") == expected);
457 }
458
Jan Kundrát3ff50122020-05-07 00:37:50 +0200459 SECTION("unions")
460 {
461 datastore.setLeaf("/example-schema:unionIntString", int32_t{10});
462 REQUIRE_CALL(mock, write("/example-schema:unionIntString", std::nullopt, "10"s));
463 datastore.commitChanges();
464 DatastoreAccess::Tree expected {
465 {"/example-schema:unionIntString", int32_t{10}},
466 };
467 REQUIRE(datastore.getItems("/example-schema:unionIntString") == expected);
468 }
469
Jan Kundrát0d8abd12020-05-07 02:00:14 +0200470 SECTION("identityref") {
471 datastore.setLeaf("/example-schema:beast", identityRef_{"example-schema", "Mammal"});
472 REQUIRE_CALL(mock, write("/example-schema:beast", std::nullopt, "example-schema:Mammal"s));
473 datastore.commitChanges();
474 DatastoreAccess::Tree expected {
475 {"/example-schema:beast", identityRef_{"example-schema", "Mammal"}},
476 };
477 REQUIRE(datastore.getItems("/example-schema:beast") == expected);
478
479 datastore.setLeaf("/example-schema:beast", identityRef_{"Whale"});
480 REQUIRE_CALL(mock, write("/example-schema:beast", "example-schema:Mammal", "example-schema:Whale"s));
481 datastore.commitChanges();
482 expected = {
483 {"/example-schema:beast", identityRef_{"example-schema", "Whale"}},
484 };
485 REQUIRE(datastore.getItems("/example-schema:beast") == expected);
486 }
487
Jan Kundrát68985442020-05-07 02:15:34 +0200488 SECTION("binary")
489 {
490 datastore.setLeaf("/example-schema:blob", binary_{"cHduegByIQ=="s});
491 REQUIRE_CALL(mock, write("/example-schema:blob", std::nullopt, "cHduegByIQ=="s));
492 datastore.commitChanges();
493 DatastoreAccess::Tree expected {
494 {"/example-schema:blob", binary_{"cHduegByIQ=="s}},
495 };
496 REQUIRE(datastore.getItems("/example-schema:blob") == expected);
497 }
498
Jan Kundrát379bb572020-05-07 03:23:13 +0200499 SECTION("empty")
500 {
501 datastore.setLeaf("/example-schema:dummy", empty_{});
502 REQUIRE_CALL(mock, write("/example-schema:dummy", std::nullopt, ""s));
503 datastore.commitChanges();
504 DatastoreAccess::Tree expected {
505 {"/example-schema:dummy", empty_{}},
506 };
507 REQUIRE(datastore.getItems("/example-schema:dummy") == expected);
508 }
509
Václav Kubernát74487df2020-06-04 01:29:28 +0200510#if not defined(yang_BACKEND)
Jan Kundrátbb525b42020-02-04 11:56:59 +0100511 SECTION("operational data")
512 {
513 MockDataSupplier mockOpsData;
514 OperationalDataSubscription opsDataSub("/example-schema:temperature", mockOpsData);
515 DatastoreAccess::Tree expected;
516 std::string xpath;
517 SECTION("temperature")
518 {
519 expected = {{"/example-schema:temperature", int32_t{22}}};
520 xpath = "/example-schema:temperature";
521 }
522
523 REQUIRE_CALL(mockOpsData, get_data(xpath)).RETURN(expected);
524 REQUIRE(datastore.getItems(xpath) == expected);
525 }
Václav Kubernát74487df2020-06-04 01:29:28 +0200526#endif
Jan Kundrátbb525b42020-02-04 11:56:59 +0100527
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200528 SECTION("leaf list")
529 {
530 DatastoreAccess::Tree expected;
531 REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "0.0.0.0"s));
532 REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "127.0.0.1"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200533 datastore.createItem("/example-schema:addresses[.='0.0.0.0']");
534 datastore.createItem("/example-schema:addresses[.='127.0.0.1']");
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200535 datastore.commitChanges();
536 expected = {
537 {"/example-schema:addresses", special_{SpecialValue::LeafList}},
538 {"/example-schema:addresses[.='0.0.0.0']", "0.0.0.0"s},
539 {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s},
540 };
541 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
542
543 REQUIRE_CALL(mock, write("/example-schema:addresses", "0.0.0.0"s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200544 datastore.deleteItem("/example-schema:addresses[.='0.0.0.0']");
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200545 datastore.commitChanges();
546 expected = {
547 {"/example-schema:addresses", special_{SpecialValue::LeafList}},
548 {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s},
549 };
550 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
551
552 REQUIRE_CALL(mock, write("/example-schema:addresses", "127.0.0.1"s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200553 datastore.deleteItem("/example-schema:addresses[.='127.0.0.1']");
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200554 datastore.commitChanges();
555 expected = {};
556 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
557 }
Jan Kundrátbb525b42020-02-04 11:56:59 +0100558
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200559 SECTION("deleting a non-existing leaf-list")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200560 {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200561 catching<OnKeyNotFound>([&]{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200562 datastore.deleteItem("/example-schema:addresses[.='non-existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200563 datastore.commitChanges();
564 });
565 }
566
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200567 SECTION("accessing a non-existing schema node as a leaf-list")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200568 {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200569 catching<OnInvalidSchemaPathCreate>([&]{
570 datastore.createItem("/example-schema:non-existing[.='non-existing']");
571 datastore.commitChanges();
572 });
573
574 catching<OnInvalidSchemaPathDelete>([&]{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200575 datastore.deleteItem("/example-schema:non-existing[.='non-existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200576 datastore.commitChanges();
577 });
578 }
579
Václav Kubernát7160a132020-04-03 02:11:01 +0200580 SECTION("copying data from startup refreshes the data")
581 {
582 {
583 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{});
584 REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "123"s));
585 datastore.setLeaf("/example-schema:leafInt16", int16_t{123});
586 datastore.commitChanges();
587 }
588 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{{"/example-schema:leafInt16", int16_t{123}}});
589 REQUIRE_CALL(mock, write("/example-schema:leafInt16", "123"s, std::nullopt));
590 datastore.copyConfig(Datastore::Startup, Datastore::Running);
591 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{});
592 }
593
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200594 SECTION("moving leaflist instances")
595 {
596 DatastoreAccess::Tree expected;
597 {
598 // sysrepo does this twice for some reason, it's possibly a bug
599 REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "http"s)).TIMES(2);
600 REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "ftp"s));
601 REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s));
602 REQUIRE_CALL(mock, write("/example-schema:protocols", "http"s, "ftp"s));
603 REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "pop3"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200604 datastore.createItem("/example-schema:protocols[.='http']");
605 datastore.createItem("/example-schema:protocols[.='ftp']");
606 datastore.createItem("/example-schema:protocols[.='pop3']");
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200607 datastore.commitChanges();
608 expected = {
609 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
610 {"/example-schema:protocols[.='http']", "http"s},
611 {"/example-schema:protocols[.='ftp']", "ftp"s},
612 {"/example-schema:protocols[.='pop3']", "pop3"s},
613 };
614 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
615 }
616
617 std::string sourcePath;
618 SECTION("begin")
619 {
620 REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s));
621 sourcePath = "/example-schema:protocols[.='pop3']";
622 datastore.moveItem(sourcePath, yang::move::Absolute::Begin);
623 datastore.commitChanges();
624 expected = {
625 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
626 {"/example-schema:protocols[.='pop3']", "pop3"s},
627 {"/example-schema:protocols[.='http']", "http"s},
628 {"/example-schema:protocols[.='ftp']", "ftp"s},
629 };
630 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
631 }
632
633 SECTION("end")
634 {
635 sourcePath = "/example-schema:protocols[.='http']";
636 REQUIRE_CALL(mock, write("/example-schema:protocols", "pop3"s, "http"s));
637 datastore.moveItem(sourcePath, yang::move::Absolute::End);
638 datastore.commitChanges();
639 expected = {
640 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
641 {"/example-schema:protocols[.='ftp']", "ftp"s},
642 {"/example-schema:protocols[.='pop3']", "pop3"s},
643 {"/example-schema:protocols[.='http']", "http"s},
644 };
645 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
646 }
647
648 SECTION("after")
649 {
650 sourcePath = "/example-schema:protocols[.='http']";
651 REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "http"s));
652 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{".", "ftp"s}}});
653 datastore.commitChanges();
654 expected = {
655 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
656 {"/example-schema:protocols[.='ftp']", "ftp"s},
657 {"/example-schema:protocols[.='http']", "http"s},
658 {"/example-schema:protocols[.='pop3']", "pop3"s},
659 };
660 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
661 }
662
663 SECTION("before")
664 {
665 sourcePath = "/example-schema:protocols[.='http']";
666 REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "http"s));
667 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{".", "pop3"s}}});
668 datastore.commitChanges();
669 expected = {
670 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
671 {"/example-schema:protocols[.='ftp']", "ftp"s},
672 {"/example-schema:protocols[.='http']", "http"s},
673 {"/example-schema:protocols[.='pop3']", "pop3"s},
674 };
675 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
676 }
677 }
678
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200679 SECTION("moving non-existing schema nodes")
680 {
681 catching<OnInvalidSchemaPathMove>([&]{
682 datastore.moveItem("/example-schema:non-existing", yang::move::Absolute::Begin);
683 datastore.commitChanges();
684 });
685 }
686
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200687 SECTION("moving list instances")
688 {
689 DatastoreAccess::Tree expected;
690 {
691 // sysrepo does this twice for some reason, it's possibly a bug
692 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", std::nullopt, ""s)).TIMES(2);
693 REQUIRE_CALL(mock, write("/example-schema:players[name='John']/name", std::nullopt, "John"s));
694 REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']", std::nullopt, ""s));
695 REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']", ""s, ""s));
696 REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']/name", std::nullopt, "Eve"s));
697 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", std::nullopt, ""s));
698 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']/name", std::nullopt, "Adam"s));
699 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", ""s, ""s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200700 datastore.createItem("/example-schema:players[name='John']");
701 datastore.createItem("/example-schema:players[name='Eve']");
702 datastore.createItem("/example-schema:players[name='Adam']");
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200703 datastore.commitChanges();
704 expected = {
705 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
706 {"/example-schema:players[name='John']/name", "John"s},
707 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
708 {"/example-schema:players[name='Eve']/name", "Eve"s},
709 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
710 {"/example-schema:players[name='Adam']/name", "Adam"s},
711 };
712 REQUIRE(datastore.getItems("/example-schema:players") == expected);
713 }
714
715 std::string sourcePath;
716 SECTION("begin")
717 {
718 sourcePath = "/example-schema:players[name='Adam']";
719 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", std::nullopt, ""s));
720 datastore.moveItem(sourcePath, yang::move::Absolute::Begin);
721 datastore.commitChanges();
722 expected = {
723 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
724 {"/example-schema:players[name='Adam']/name", "Adam"s},
725 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
726 {"/example-schema:players[name='John']/name", "John"s},
727 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
728 {"/example-schema:players[name='Eve']/name", "Eve"s},
729 };
730 REQUIRE(datastore.getItems("/example-schema:players") == expected);
731 }
732
733 SECTION("end")
734 {
735 sourcePath = "/example-schema:players[name='John']";
736 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s));
737 datastore.moveItem(sourcePath, yang::move::Absolute::End);
738 datastore.commitChanges();
739 expected = {
740 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
741 {"/example-schema:players[name='Eve']/name", "Eve"s},
742 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
743 {"/example-schema:players[name='Adam']/name", "Adam"s},
744 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
745 {"/example-schema:players[name='John']/name", "John"s},
746 };
747 REQUIRE(datastore.getItems("/example-schema:players") == expected);
748 }
749
750 SECTION("after")
751 {
752 sourcePath = "/example-schema:players[name='John']";
753 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s));
754 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{"name", "Eve"s}}});
755 datastore.commitChanges();
756 expected = {
757 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
758 {"/example-schema:players[name='Eve']/name", "Eve"s},
759 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
760 {"/example-schema:players[name='John']/name", "John"s},
761 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
762 {"/example-schema:players[name='Adam']/name", "Adam"s},
763 };
764 REQUIRE(datastore.getItems("/example-schema:players") == expected);
765 }
766
767 SECTION("before")
768 {
769 sourcePath = "/example-schema:players[name='John']";
770 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s));
771 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{"name", "Adam"s}}});
772 datastore.commitChanges();
773 expected = {
774 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
775 {"/example-schema:players[name='Eve']/name", "Eve"s},
776 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
777 {"/example-schema:players[name='John']/name", "John"s},
778 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
779 {"/example-schema:players[name='Adam']/name", "Adam"s},
780 };
781 REQUIRE(datastore.getItems("/example-schema:players") == expected);
782 }
783 }
784
Václav Kubernátfa96ac22020-06-18 17:03:52 +0200785 SECTION("getting /")
786 {
787 {
788 REQUIRE_CALL(mock, write("/example-schema:leafInt32", std::nullopt, "64"s));
789 datastore.setLeaf("/example-schema:leafInt32", 64);
790 datastore.commitChanges();
791 }
792
793 DatastoreAccess::Tree expected{
794 // Sysrepo always returns containers when getting values, but
795 // libnetconf does not. This is fine by the YANG standard:
796 // https://tools.ietf.org/html/rfc7950#section-7.5.7 Furthermore,
797 // NetconfAccess implementation actually only iterates over leafs,
798 // so even if libnetconf did include containers, they wouldn't get
799 // shown here anyway. With sysrepo2, this won't be necessary,
800 // because it'll use the same data structure as libnetconf, so the
801 // results will be consistent.
802#ifdef sysrepo_BACKEND
803 {"/example-schema:inventory", special_{SpecialValue::Container}},
804 {"/example-schema:lol", special_{SpecialValue::Container}},
805#endif
806 {"/example-schema:leafInt32", 64}};
807 auto items = datastore.getItems("/");
808 // This tests if we at least get the data WE added.
809 REQUIRE(std::all_of(expected.begin(), expected.end(), [items] (const auto& item) { return std::find(items.begin(), items.end(), item) != items.end(); }));
810 }
811
Václav Kubernát36986c52020-06-25 10:30:05 +0200812 SECTION("setting and removing without commit")
813 {
814 datastore.setLeaf("/example-schema:leafInt32", 64);
815 datastore.deleteItem("/example-schema:leafInt32");
816 }
817
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200818 SECTION("two key lists")
819 {
820 REQUIRE_CALL(mock, write("/example-schema:point[x='12'][y='10']", std::nullopt, ""s));
821 REQUIRE_CALL(mock, write("/example-schema:point[x='12'][y='10']/x", std::nullopt, "12"s));
822 REQUIRE_CALL(mock, write("/example-schema:point[x='12'][y='10']/y", std::nullopt, "10"s));
823 datastore.createItem("/example-schema:point[x='12'][y='10']");
824 datastore.commitChanges();
825 REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:point") != std::string::npos);
826 }
827
Václav Kubernát73109382018-09-14 19:52:03 +0200828 waitForCompletionAndBitMore(seq1);
829}
Jan Kundrát6ee84792020-01-24 01:43:36 +0100830
831class RpcCb: public sysrepo::Callback {
832 int rpc(const char *xpath, const ::sysrepo::S_Vals input, ::sysrepo::S_Vals_Holder output, void *) override
833 {
834 const auto nukes = "/example-schema:launch-nukes"s;
835 if (xpath == "/example-schema:noop"s) {
836 return SR_ERR_OK;
837 } else if (xpath == nukes) {
838 uint64_t kilotons = 0;
839 bool hasCities = false;
840 for (size_t i = 0; i < input->val_cnt(); ++i) {
841 const auto& val = input->val(i);
842 if (val->xpath() == nukes + "/payload/kilotons") {
843 kilotons = val->data()->get_uint64();
844 } else if (val->xpath() == nukes + "/payload") {
845 // ignore, container
846 } else if (val->xpath() == nukes + "/description") {
847 // unused
848 } else if (std::string_view{val->xpath()}.find(nukes + "/cities") == 0) {
849 hasCities = true;
850 } else {
851 throw std::runtime_error("RPC launch-nukes: unexpected input "s + val->xpath());
852 }
853 }
854 if (kilotons == 333'666) {
855 // magic, just do not generate any output. This is important because the NETCONF RPC returns just <ok/>.
856 return SR_ERR_OK;
857 }
858 auto buf = output->allocate(2);
859 size_t i = 0;
860 buf->val(i++)->set((nukes + "/blast-radius").c_str(), uint32_t{33'666});
861 buf->val(i++)->set((nukes + "/actual-yield").c_str(), static_cast<uint64_t>(1.33 * kilotons));
862 if (hasCities) {
863 buf = output->reallocate(output->val_cnt() + 2);
864 buf->val(i++)->set((nukes + "/damaged-places/targets[city='London']/city").c_str(), "London");
865 buf->val(i++)->set((nukes + "/damaged-places/targets[city='Berlin']/city").c_str(), "Berlin");
866 }
867 return SR_ERR_OK;
868 }
869 throw std::runtime_error("unrecognized RPC");
870 }
871};
872
873TEST_CASE("rpc") {
874 trompeloeil::sequence seq1;
875 auto srConn = std::make_shared<sysrepo::Connection>("netconf-cli-test-rpc");
876 auto srSession = std::make_shared<sysrepo::Session>(srConn);
877 auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSession);
878 auto cb = std::make_shared<RpcCb>();
879 sysrepo::Logs{}.set_stderr(SR_LL_INF);
880 srSubscription->rpc_subscribe("/example-schema:noop", cb, nullptr, SR_SUBSCR_CTX_REUSE);
881 srSubscription->rpc_subscribe("/example-schema:launch-nukes", cb, nullptr, SR_SUBSCR_CTX_REUSE);
882
883#ifdef sysrepo_BACKEND
Václav Kubernát715c85c2020-04-14 01:46:08 +0200884 SysrepoAccess datastore("netconf-cli-test", Datastore::Running);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100885#elif defined(netconf_BACKEND)
886 NetconfAccess datastore(NETOPEER_SOCKET_PATH);
Václav Kubernát74487df2020-06-04 01:29:28 +0200887#elif defined(yang_BACKEND)
888 YangAccess datastore;
889 datastore.addSchemaDir(schemaDir);
890 datastore.addSchemaFile(exampleSchemaFile);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100891#else
892#error "Unknown backend"
893#endif
894
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200895 SECTION("valid")
896 {
897 std::string rpc;
898 DatastoreAccess::Tree input, output;
Jan Kundrát6ee84792020-01-24 01:43:36 +0100899
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200900 SECTION("noop") {
901 rpc = "/example-schema:noop";
902 }
903
904 SECTION("small nuke") {
905 rpc = "/example-schema:launch-nukes";
906 input = {
907 {"description", "dummy"s},
908 {"payload/kilotons", uint64_t{333'666}},
909 };
910 // no data are returned
911 }
912
913 SECTION("small nuke") {
914 rpc = "/example-schema:launch-nukes";
915 input = {
916 {"description", "dummy"s},
917 {"payload/kilotons", uint64_t{4}},
918 };
919 output = {
920 {"blast-radius", uint32_t{33'666}},
921 {"actual-yield", uint64_t{5}},
922 };
923 }
924
925 SECTION("with lists") {
926 rpc = "/example-schema:launch-nukes";
927 input = {
928 {"payload/kilotons", uint64_t{6}},
929 {"cities/targets[city='Prague']/city", "Prague"s},
930 };
931 output = {
932 {"blast-radius", uint32_t{33'666}},
933 {"actual-yield", uint64_t{7}},
934 {"damaged-places", special_{SpecialValue::PresenceContainer}},
935 {"damaged-places/targets[city='London']", special_{SpecialValue::List}},
936 {"damaged-places/targets[city='London']/city", "London"s},
937 {"damaged-places/targets[city='Berlin']", special_{SpecialValue::List}},
938 {"damaged-places/targets[city='Berlin']/city", "Berlin"s},
939 };
940 }
941
Václav Kubernát74487df2020-06-04 01:29:28 +0200942 catching<OnRPC>([&] {REQUIRE(datastore.executeRpc(rpc, input) == output);});
Jan Kundrát6ee84792020-01-24 01:43:36 +0100943 }
944
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200945 SECTION("non-existing RPC")
946 {
Václav Kubernát74487df2020-06-04 01:29:28 +0200947 catching<OnInvalidRpcPath>([&] {datastore.executeRpc("/example-schema:non-existing", DatastoreAccess::Tree{});});
Jan Kundrát6ee84792020-01-24 01:43:36 +0100948 }
949
Jan Kundrát6ee84792020-01-24 01:43:36 +0100950 waitForCompletionAndBitMore(seq1);
951}