blob: fb440188a90bca68398b0e8d6e2665acfeb7f634 [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áte7248b22020-06-26 15:38:59 +020011#include "yang_schema.hpp"
12#include "proxy_datastore.hpp"
Václav Kubernát73109382018-09-14 19:52:03 +020013
Václav Kubernátc31bd602019-03-07 11:44:48 +010014#ifdef sysrepo_BACKEND
Václav Kubernát73109382018-09-14 19:52:03 +020015#include "sysrepo_access.hpp"
Jan Kundrát7ec214d2020-06-19 17:05:07 +020016using OnInvalidSchemaPathCreate = DatastoreException;
17using OnInvalidSchemaPathDelete = void;
18using OnInvalidSchemaPathMove = sysrepo::sysrepo_exception;
Václav Kubernát74487df2020-06-04 01:29:28 +020019using OnInvalidRpcPath = sysrepo::sysrepo_exception;
Jan Kundrát7ec214d2020-06-19 17:05:07 +020020using OnKeyNotFound = void;
Václav Kubernát74487df2020-06-04 01:29:28 +020021using OnRPC = void;
Václav Kubernátc31bd602019-03-07 11:44:48 +010022#elif defined(netconf_BACKEND)
Jan Kundrát7ec214d2020-06-19 17:05:07 +020023using OnInvalidSchemaPathCreate = std::runtime_error;
24using OnInvalidSchemaPathDelete = std::runtime_error;
25using OnInvalidSchemaPathMove = std::runtime_error;
Václav Kubernát74487df2020-06-04 01:29:28 +020026using OnInvalidRpcPath = std::runtime_error;
Jan Kundrát7ec214d2020-06-19 17:05:07 +020027using OnKeyNotFound = std::runtime_error;
Václav Kubernát74487df2020-06-04 01:29:28 +020028using OnRPC = void;
Václav Kubernátc31bd602019-03-07 11:44:48 +010029#include "netconf_access.hpp"
30#include "netopeer_vars.hpp"
Václav Kubernát74487df2020-06-04 01:29:28 +020031#elif defined(yang_BACKEND)
32#include <fstream>
33#include "yang_access.hpp"
34#include "yang_access_test_vars.hpp"
35using OnInvalidSchemaPathCreate = DatastoreException;
36using OnInvalidSchemaPathDelete = DatastoreException;
37using OnInvalidSchemaPathMove = DatastoreException;
38using OnInvalidRpcPath = DatastoreException;
39using OnKeyNotFound = DatastoreException;
40using OnRPC = std::logic_error;
Václav Kubernátc31bd602019-03-07 11:44:48 +010041#else
42#error "Unknown backend"
43#endif
Jan Kundrátbb525b42020-02-04 11:56:59 +010044#include "pretty_printers.hpp"
Václav Kubernát73109382018-09-14 19:52:03 +020045#include "sysrepo_subscription.hpp"
Václav Kubernát8e121ff2019-10-15 15:47:45 +020046#include "utils.hpp"
Václav Kubernát73109382018-09-14 19:52:03 +020047
Jan Kundrát6ee84792020-01-24 01:43:36 +010048using namespace std::literals::string_literals;
49
Václav Kubernát69aabe92020-01-24 16:53:12 +010050class MockRecorder : public trompeloeil::mock_interface<Recorder> {
Václav Kubernát73109382018-09-14 19:52:03 +020051public:
Václav Kubernát69aabe92020-01-24 16:53:12 +010052 IMPLEMENT_MOCK3(write);
Václav Kubernát73109382018-09-14 19:52:03 +020053};
54
Jan Kundrátbb525b42020-02-04 11:56:59 +010055class MockDataSupplier : public trompeloeil::mock_interface<DataSupplier> {
56public:
57 IMPLEMENT_CONST_MOCK1(get_data);
58};
59
Jan Kundrát7ec214d2020-06-19 17:05:07 +020060namespace {
61template <class ...> constexpr std::false_type always_false [[maybe_unused]] {};
62template <class Exception, typename Callable> void catching(const Callable& what) {
63
64 if constexpr (std::is_same_v<Exception, void>) {
Jan Kundrát3867c9e2020-06-18 20:26:45 +020065 what();
Jan Kundrát7ec214d2020-06-19 17:05:07 +020066 } else if constexpr (std::is_same<Exception, std::runtime_error>()) {
67 // cannot use REQUIRE_THROWS_AS(..., Exception) directly because that one
68 // needs an extra `typename` deep in the bowels of doctest
69 REQUIRE_THROWS_AS(what(), std::runtime_error);
Václav Kubernát74487df2020-06-04 01:29:28 +020070 } else if constexpr (std::is_same<Exception, std::logic_error>()) {
71 REQUIRE_THROWS_AS(what(), std::logic_error);
Jan Kundrát7ec214d2020-06-19 17:05:07 +020072 } else if constexpr (std::is_same<Exception, DatastoreException>()) {
73 REQUIRE_THROWS_AS(what(), DatastoreException);
74 } else if constexpr (std::is_same<Exception, sysrepo::sysrepo_exception>()) {
75 REQUIRE_THROWS_AS(what(), sysrepo::sysrepo_exception);
76 } else {
77 static_assert(always_false<Exception>); // https://stackoverflow.com/a/53945549/2245623
Jan Kundrát3867c9e2020-06-18 20:26:45 +020078 }
79}
Jan Kundrát7ec214d2020-06-19 17:05:07 +020080}
Jan Kundrát3867c9e2020-06-18 20:26:45 +020081
Václav Kubernát74487df2020-06-04 01:29:28 +020082#if defined(yang_BACKEND)
83class TestYangAccess : public YangAccess {
84public:
85 void commitChanges() override
86 {
87 YangAccess::commitChanges();
88 dumpToSysrepo();
89 }
90
91 void copyConfig(const Datastore source, const Datastore destination) override
92 {
93 YangAccess::copyConfig(source, destination);
94 dumpToSysrepo();
95 }
96
97private:
98 void dumpToSysrepo()
99 {
100 {
101 std::ofstream of(testConfigFile);
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200102 of << dump(DataFormat::Xml);
Václav Kubernát74487df2020-06-04 01:29:28 +0200103 }
104 auto command = std::string(sysrepocfgExecutable) + " --import=" + testConfigFile + " --format=xml --datastore=running example-schema";
105 REQUIRE(std::system(command.c_str()) == 0);
106 }
107};
108#endif
109
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200110TEST_CASE("setting/getting values")
Václav Kubernát73109382018-09-14 19:52:03 +0200111{
Václav Kubernát73109382018-09-14 19:52:03 +0200112 trompeloeil::sequence seq1;
113 MockRecorder mock;
Václav Kubernátab612e92019-11-26 19:51:31 +0100114 SysrepoSubscription subscription("example-schema", &mock);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100115
116#ifdef sysrepo_BACKEND
Václav Kubernát715c85c2020-04-14 01:46:08 +0200117 SysrepoAccess datastore("netconf-cli-test", Datastore::Running);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100118#elif defined(netconf_BACKEND)
119 NetconfAccess datastore(NETOPEER_SOCKET_PATH);
Václav Kubernát74487df2020-06-04 01:29:28 +0200120#elif defined(yang_BACKEND)
121 TestYangAccess datastore;
122 datastore.addSchemaDir(schemaDir);
123 datastore.addSchemaFile(exampleSchemaFile);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100124#else
125#error "Unknown backend"
126#endif
Václav Kubernát73109382018-09-14 19:52:03 +0200127
Václav Kubernát69aabe92020-01-24 16:53:12 +0100128
Václav Kubernát134d78f2019-09-03 16:42:29 +0200129 SECTION("set leafInt8 to -128")
Václav Kubernát73109382018-09-14 19:52:03 +0200130 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100131 REQUIRE_CALL(mock, write("/example-schema:leafInt8", std::nullopt, "-128"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200132 datastore.setLeaf("/example-schema:leafInt8", int8_t{-128});
133 datastore.commitChanges();
134 }
135
136 SECTION("set leafInt16 to -32768")
137 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100138 REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "-32768"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200139 datastore.setLeaf("/example-schema:leafInt16", int16_t{-32768});
140 datastore.commitChanges();
141 }
142
143 SECTION("set leafInt32 to -2147483648")
144 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100145 REQUIRE_CALL(mock, write("/example-schema:leafInt32", std::nullopt, "-2147483648"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200146 datastore.setLeaf("/example-schema:leafInt32", int32_t{-2147483648});
147 datastore.commitChanges();
148 }
149
150 SECTION("set leafInt64 to -50000000000")
151 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100152 REQUIRE_CALL(mock, write("/example-schema:leafInt64", std::nullopt, "-50000000000"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200153 datastore.setLeaf("/example-schema:leafInt64", int64_t{-50000000000});
154 datastore.commitChanges();
155 }
156
157 SECTION("set leafUInt8 to 255")
158 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100159 REQUIRE_CALL(mock, write("/example-schema:leafUInt8", std::nullopt, "255"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200160 datastore.setLeaf("/example-schema:leafUInt8", uint8_t{255});
161 datastore.commitChanges();
162 }
163
164 SECTION("set leafUInt16 to 65535")
165 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100166 REQUIRE_CALL(mock, write("/example-schema:leafUInt16", std::nullopt, "65535"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200167 datastore.setLeaf("/example-schema:leafUInt16", uint16_t{65535});
168 datastore.commitChanges();
169 }
170
171 SECTION("set leafUInt32 to 4294967295")
172 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100173 REQUIRE_CALL(mock, write("/example-schema:leafUInt32", std::nullopt, "4294967295"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200174 datastore.setLeaf("/example-schema:leafUInt32", uint32_t{4294967295});
175 datastore.commitChanges();
176 }
177
178 SECTION("set leafUInt64 to 50000000000")
179 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100180 REQUIRE_CALL(mock, write("/example-schema:leafUInt64", std::nullopt, "50000000000"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200181 datastore.setLeaf("/example-schema:leafUInt64", uint64_t{50000000000});
Václav Kubernát73109382018-09-14 19:52:03 +0200182 datastore.commitChanges();
183 }
184
185 SECTION("set leafEnum to coze")
186 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100187 REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "coze"s));
Václav Kubernát73109382018-09-14 19:52:03 +0200188 datastore.setLeaf("/example-schema:leafEnum", enum_{"coze"});
189 datastore.commitChanges();
190 }
191
192 SECTION("set leafDecimal to 123.544")
193 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100194 REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.544"s));
Václav Kubernát73109382018-09-14 19:52:03 +0200195 datastore.setLeaf("/example-schema:leafDecimal", 123.544);
196 datastore.commitChanges();
197 }
198
Jan Kundrátd2872862020-06-18 21:02:00 +0200199 SECTION("set a string, then delete it")
200 {
201 REQUIRE_CALL(mock, write("/example-schema:leafString", std::nullopt, "blah"s));
202 datastore.setLeaf("/example-schema:leafString", "blah"s);
203 datastore.commitChanges();
204 DatastoreAccess::Tree expected{{"/example-schema:leafString", "blah"s}};
205 REQUIRE(datastore.getItems("/example-schema:leafString") == expected);
206
207 REQUIRE_CALL(mock, write("/example-schema:leafString", "blah"s, std::nullopt));
208 datastore.deleteItem("/example-schema:leafString");
209 datastore.commitChanges();
210 expected.clear();
211 REQUIRE(datastore.getItems("/example-schema:leafString") == expected);
212 }
213
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200214 SECTION("set a non-existing leaf")
215 {
216 catching<OnInvalidSchemaPathCreate>([&]{
217 datastore.setLeaf("/example-schema:non-existing", "what"s);
218 });
219 }
220
Václav Kubernát73109382018-09-14 19:52:03 +0200221 SECTION("create presence container")
222 {
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200223 REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:pContainer") == std::string::npos);
Václav Kubernát69aabe92020-01-24 16:53:12 +0100224 REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200225 datastore.createItem("/example-schema:pContainer");
Václav Kubernát73109382018-09-14 19:52:03 +0200226 datastore.commitChanges();
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200227 REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:pContainer") != std::string::npos);
Václav Kubernát73109382018-09-14 19:52:03 +0200228 }
229
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100230 SECTION("create/delete a list instance")
Václav Kubernát45f4a822019-05-29 21:10:50 +0200231 {
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100232 {
233 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", std::nullopt, ""s));
234 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']/name", std::nullopt, "Nguyen"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200235 datastore.createItem("/example-schema:person[name='Nguyen']");
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100236 datastore.commitChanges();
237 }
238 {
239 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", ""s, std::nullopt));
240 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']/name", "Nguyen"s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200241 datastore.deleteItem("/example-schema:person[name='Nguyen']");
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100242 datastore.commitChanges();
243 }
Václav Kubernát45f4a822019-05-29 21:10:50 +0200244 }
245
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200246 SECTION("deleting non-existing list keys")
247 {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200248 catching<OnKeyNotFound>([&]{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200249 datastore.deleteItem("/example-schema:person[name='non existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200250 datastore.commitChanges();
251 });
252 }
253
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200254 SECTION("accessing non-existing schema nodes as a list")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200255 {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200256 catching<OnInvalidSchemaPathCreate>([&]{
257 datastore.createItem("/example-schema:non-existing-list[xxx='blah']");
258 datastore.commitChanges();
259 });
260 catching<OnInvalidSchemaPathDelete>([&]{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200261 datastore.deleteItem("/example-schema:non-existing-list[xxx='non existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200262 datastore.commitChanges();
263 });
264 }
265
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200266 SECTION("leafref pointing to a key of a list")
267 {
268 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100269 REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']", std::nullopt, ""s));
270 REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']/name", std::nullopt, "Dan"s));
271 REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']", std::nullopt, ""s));
272 REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']/name", std::nullopt, "Elfi"s));
273 REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']", std::nullopt, ""s));
274 REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']/name", std::nullopt, "Kolafa"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200275 datastore.createItem("/example-schema:person[name='Dan']");
276 datastore.createItem("/example-schema:person[name='Elfi']");
277 datastore.createItem("/example-schema:person[name='Kolafa']");
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200278 datastore.commitChanges();
279 }
280
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200281 std::string value;
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200282 SECTION("Dan")
283 {
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200284 value = "Dan";
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200285 }
286
287 SECTION("Elfi")
288 {
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200289 value = "Elfi";
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200290 }
291
292 SECTION("Kolafa")
293 {
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200294 value = "Kolafa";
295 }
296
297 datastore.setLeaf("/example-schema:bossPerson", value);
298 {
299 REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, value));
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200300 datastore.commitChanges();
301 }
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200302 REQUIRE(datastore.getItems("/example-schema:bossPerson") == DatastoreAccess::Tree{{"/example-schema:bossPerson", value}});
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200303 }
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200304 SECTION("bool values get correctly represented as bools")
305 {
306 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100307 REQUIRE_CALL(mock, write("/example-schema:down", std::nullopt, "true"s));
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200308 datastore.setLeaf("/example-schema:down", bool{true});
309 datastore.commitChanges();
310 }
311
Jan Kundrátb331b552020-01-23 15:25:29 +0100312 DatastoreAccess::Tree expected{{"/example-schema:down", bool{true}}};
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200313 REQUIRE(datastore.getItems("/example-schema:down") == expected);
314 }
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200315
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200316 SECTION("getting items from the whole module")
317 {
318 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100319 REQUIRE_CALL(mock, write("/example-schema:up", std::nullopt, "true"s));
320 REQUIRE_CALL(mock, write("/example-schema:down", std::nullopt, "false"s));
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200321 datastore.setLeaf("/example-schema:up", bool{true});
322 datastore.setLeaf("/example-schema:down", bool{false});
323 datastore.commitChanges();
324 }
325
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200326 DatastoreAccess::Tree expected{
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200327 // Sysrepo always returns containers when getting values, but
328 // libnetconf does not. This is fine by the YANG standard:
329 // https://tools.ietf.org/html/rfc7950#section-7.5.7 Furthermore,
330 // NetconfAccess implementation actually only iterates over leafs,
331 // so even if libnetconf did include containers, they wouldn't get
332 // shown here anyway. With sysrepo2, this won't be necessary,
333 // because it'll use the same data structure as libnetconf, so the
334 // results will be consistent.
335#ifdef sysrepo_BACKEND
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100336 {"/example-schema:inventory", special_{SpecialValue::Container}},
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200337 {"/example-schema:lol", special_{SpecialValue::Container}},
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200338#endif
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200339 {"/example-schema:up", bool{true}},
340 {"/example-schema:down", bool{false}}};
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200341 REQUIRE(datastore.getItems("/example-schema:*") == expected);
342 }
343
Václav Kubernát152ce222019-12-19 12:23:32 +0100344 SECTION("getItems returns correct datatypes")
345 {
346 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100347 REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "lol"s));
Václav Kubernát152ce222019-12-19 12:23:32 +0100348 datastore.setLeaf("/example-schema:leafEnum", enum_{"lol"});
349 datastore.commitChanges();
350 }
Jan Kundrátb331b552020-01-23 15:25:29 +0100351 DatastoreAccess::Tree expected{{"/example-schema:leafEnum", enum_{"lol"}}};
Václav Kubernát152ce222019-12-19 12:23:32 +0100352
353 REQUIRE(datastore.getItems("/example-schema:leafEnum") == expected);
354 }
355
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100356 SECTION("getItems on a list")
357 {
358 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100359 REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']", std::nullopt, ""s));
360 REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']/name", std::nullopt, "Jan"s));
361 REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']", std::nullopt, ""s));
362 REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']/name", std::nullopt, "Michal"s));
363 REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']", std::nullopt, ""s));
364 REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']/name", std::nullopt, "Petr"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200365 datastore.createItem("/example-schema:person[name='Jan']");
366 datastore.createItem("/example-schema:person[name='Michal']");
367 datastore.createItem("/example-schema:person[name='Petr']");
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100368 datastore.commitChanges();
369 }
Jan Kundrátb331b552020-01-23 15:25:29 +0100370 DatastoreAccess::Tree expected{
Václav Kubernát144729d2020-01-08 15:20:35 +0100371 {"/example-schema:person[name='Jan']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100372 {"/example-schema:person[name='Jan']/name", std::string{"Jan"}},
Václav Kubernát144729d2020-01-08 15:20:35 +0100373 {"/example-schema:person[name='Michal']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100374 {"/example-schema:person[name='Michal']/name", std::string{"Michal"}},
Václav Kubernát144729d2020-01-08 15:20:35 +0100375 {"/example-schema:person[name='Petr']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100376 {"/example-schema:person[name='Petr']/name", std::string{"Petr"}}
377 };
378
379 REQUIRE(datastore.getItems("/example-schema:person") == expected);
380 }
381
Václav Kubernát69aabe92020-01-24 16:53:12 +0100382 SECTION("presence containers")
383 {
384 DatastoreAccess::Tree expected;
385 // Make sure it's not there before we create it
386 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
387
388 {
389 REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200390 datastore.createItem("/example-schema:pContainer");
Václav Kubernát69aabe92020-01-24 16:53:12 +0100391 datastore.commitChanges();
392 }
393 expected = {
394 {"/example-schema:pContainer", special_{SpecialValue::PresenceContainer}}
395 };
396 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
397
398 // Make sure it's not there after we delete it
399 {
400 REQUIRE_CALL(mock, write("/example-schema:pContainer", ""s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200401 datastore.deleteItem("/example-schema:pContainer");
Václav Kubernát69aabe92020-01-24 16:53:12 +0100402 datastore.commitChanges();
403 }
404 expected = {};
405 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100406 }
Václav Kubernát69aabe92020-01-24 16:53:12 +0100407
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200408 SECTION("creating a non-existing schema node as a container")
409 {
410 catching<OnInvalidSchemaPathCreate>([&]{
411 datastore.createItem("/example-schema:non-existing-presence-container");
412 datastore.commitChanges();
413 });
414 }
415
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200416 SECTION("deleting a non-existing schema node as a container or leaf")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200417 {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200418 catching<OnInvalidSchemaPathDelete>([&]{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200419 datastore.deleteItem("/example-schema:non-existing-presence-container");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200420 datastore.commitChanges();
421 });
422 }
423
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100424 SECTION("nested presence container")
425 {
426 DatastoreAccess::Tree expected;
427 // Make sure it's not there before we create it
428 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
429 {
430 REQUIRE_CALL(mock, write("/example-schema:inventory", std::nullopt, ""s));
431 REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", std::nullopt, ""s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200432 datastore.createItem("/example-schema:inventory/stuff");
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100433 datastore.commitChanges();
434 }
435 expected = {
436 {"/example-schema:inventory/stuff", special_{SpecialValue::PresenceContainer}}
437 };
438 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
439 {
440 REQUIRE_CALL(mock, write("/example-schema:inventory", ""s, std::nullopt));
441 REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", ""s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200442 datastore.deleteItem("/example-schema:inventory/stuff");
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100443 datastore.commitChanges();
444 }
445 expected = {};
446 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
Václav Kubernát69aabe92020-01-24 16:53:12 +0100447 }
448
Jan Kundrátbd3169c2020-02-03 19:31:34 +0100449 SECTION("floats")
450 {
451 datastore.setLeaf("/example-schema:leafDecimal", 123.4);
452 REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.4"s));
453 datastore.commitChanges();
454 DatastoreAccess::Tree expected {
455 {"/example-schema:leafDecimal", 123.4},
456 };
457 REQUIRE(datastore.getItems("/example-schema:leafDecimal") == expected);
458 }
459
Jan Kundrát3ff50122020-05-07 00:37:50 +0200460 SECTION("unions")
461 {
462 datastore.setLeaf("/example-schema:unionIntString", int32_t{10});
463 REQUIRE_CALL(mock, write("/example-schema:unionIntString", std::nullopt, "10"s));
464 datastore.commitChanges();
465 DatastoreAccess::Tree expected {
466 {"/example-schema:unionIntString", int32_t{10}},
467 };
468 REQUIRE(datastore.getItems("/example-schema:unionIntString") == expected);
469 }
470
Jan Kundrát0d8abd12020-05-07 02:00:14 +0200471 SECTION("identityref") {
472 datastore.setLeaf("/example-schema:beast", identityRef_{"example-schema", "Mammal"});
473 REQUIRE_CALL(mock, write("/example-schema:beast", std::nullopt, "example-schema:Mammal"s));
474 datastore.commitChanges();
475 DatastoreAccess::Tree expected {
476 {"/example-schema:beast", identityRef_{"example-schema", "Mammal"}},
477 };
478 REQUIRE(datastore.getItems("/example-schema:beast") == expected);
479
480 datastore.setLeaf("/example-schema:beast", identityRef_{"Whale"});
481 REQUIRE_CALL(mock, write("/example-schema:beast", "example-schema:Mammal", "example-schema:Whale"s));
482 datastore.commitChanges();
483 expected = {
484 {"/example-schema:beast", identityRef_{"example-schema", "Whale"}},
485 };
486 REQUIRE(datastore.getItems("/example-schema:beast") == expected);
487 }
488
Jan Kundrát68985442020-05-07 02:15:34 +0200489 SECTION("binary")
490 {
491 datastore.setLeaf("/example-schema:blob", binary_{"cHduegByIQ=="s});
492 REQUIRE_CALL(mock, write("/example-schema:blob", std::nullopt, "cHduegByIQ=="s));
493 datastore.commitChanges();
494 DatastoreAccess::Tree expected {
495 {"/example-schema:blob", binary_{"cHduegByIQ=="s}},
496 };
497 REQUIRE(datastore.getItems("/example-schema:blob") == expected);
498 }
499
Jan Kundrát379bb572020-05-07 03:23:13 +0200500 SECTION("empty")
501 {
502 datastore.setLeaf("/example-schema:dummy", empty_{});
503 REQUIRE_CALL(mock, write("/example-schema:dummy", std::nullopt, ""s));
504 datastore.commitChanges();
505 DatastoreAccess::Tree expected {
506 {"/example-schema:dummy", empty_{}},
507 };
508 REQUIRE(datastore.getItems("/example-schema:dummy") == expected);
509 }
510
Václav Kubernát74487df2020-06-04 01:29:28 +0200511#if not defined(yang_BACKEND)
Jan Kundrátbb525b42020-02-04 11:56:59 +0100512 SECTION("operational data")
513 {
514 MockDataSupplier mockOpsData;
515 OperationalDataSubscription opsDataSub("/example-schema:temperature", mockOpsData);
516 DatastoreAccess::Tree expected;
517 std::string xpath;
518 SECTION("temperature")
519 {
520 expected = {{"/example-schema:temperature", int32_t{22}}};
521 xpath = "/example-schema:temperature";
522 }
523
524 REQUIRE_CALL(mockOpsData, get_data(xpath)).RETURN(expected);
525 REQUIRE(datastore.getItems(xpath) == expected);
526 }
Václav Kubernát74487df2020-06-04 01:29:28 +0200527#endif
Jan Kundrátbb525b42020-02-04 11:56:59 +0100528
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200529 SECTION("leaf list")
530 {
531 DatastoreAccess::Tree expected;
532 REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "0.0.0.0"s));
533 REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "127.0.0.1"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200534 datastore.createItem("/example-schema:addresses[.='0.0.0.0']");
535 datastore.createItem("/example-schema:addresses[.='127.0.0.1']");
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200536 datastore.commitChanges();
537 expected = {
538 {"/example-schema:addresses", special_{SpecialValue::LeafList}},
539 {"/example-schema:addresses[.='0.0.0.0']", "0.0.0.0"s},
540 {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s},
541 };
542 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
543
544 REQUIRE_CALL(mock, write("/example-schema:addresses", "0.0.0.0"s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200545 datastore.deleteItem("/example-schema:addresses[.='0.0.0.0']");
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200546 datastore.commitChanges();
547 expected = {
548 {"/example-schema:addresses", special_{SpecialValue::LeafList}},
549 {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s},
550 };
551 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
552
553 REQUIRE_CALL(mock, write("/example-schema:addresses", "127.0.0.1"s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200554 datastore.deleteItem("/example-schema:addresses[.='127.0.0.1']");
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200555 datastore.commitChanges();
556 expected = {};
557 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
558 }
Jan Kundrátbb525b42020-02-04 11:56:59 +0100559
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200560 SECTION("deleting a non-existing leaf-list")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200561 {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200562 catching<OnKeyNotFound>([&]{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200563 datastore.deleteItem("/example-schema:addresses[.='non-existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200564 datastore.commitChanges();
565 });
566 }
567
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200568 SECTION("accessing a non-existing schema node as a leaf-list")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200569 {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200570 catching<OnInvalidSchemaPathCreate>([&]{
571 datastore.createItem("/example-schema:non-existing[.='non-existing']");
572 datastore.commitChanges();
573 });
574
575 catching<OnInvalidSchemaPathDelete>([&]{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200576 datastore.deleteItem("/example-schema:non-existing[.='non-existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200577 datastore.commitChanges();
578 });
579 }
580
Václav Kubernát7160a132020-04-03 02:11:01 +0200581 SECTION("copying data from startup refreshes the data")
582 {
583 {
584 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{});
585 REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "123"s));
586 datastore.setLeaf("/example-schema:leafInt16", int16_t{123});
587 datastore.commitChanges();
588 }
589 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{{"/example-schema:leafInt16", int16_t{123}}});
590 REQUIRE_CALL(mock, write("/example-schema:leafInt16", "123"s, std::nullopt));
591 datastore.copyConfig(Datastore::Startup, Datastore::Running);
592 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{});
593 }
594
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200595 SECTION("moving leaflist instances")
596 {
597 DatastoreAccess::Tree expected;
598 {
599 // sysrepo does this twice for some reason, it's possibly a bug
600 REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "http"s)).TIMES(2);
601 REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "ftp"s));
602 REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s));
603 REQUIRE_CALL(mock, write("/example-schema:protocols", "http"s, "ftp"s));
604 REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "pop3"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200605 datastore.createItem("/example-schema:protocols[.='http']");
606 datastore.createItem("/example-schema:protocols[.='ftp']");
607 datastore.createItem("/example-schema:protocols[.='pop3']");
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200608 datastore.commitChanges();
609 expected = {
610 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
611 {"/example-schema:protocols[.='http']", "http"s},
612 {"/example-schema:protocols[.='ftp']", "ftp"s},
613 {"/example-schema:protocols[.='pop3']", "pop3"s},
614 };
615 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
616 }
617
618 std::string sourcePath;
619 SECTION("begin")
620 {
621 REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s));
622 sourcePath = "/example-schema:protocols[.='pop3']";
623 datastore.moveItem(sourcePath, yang::move::Absolute::Begin);
624 datastore.commitChanges();
625 expected = {
626 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
627 {"/example-schema:protocols[.='pop3']", "pop3"s},
628 {"/example-schema:protocols[.='http']", "http"s},
629 {"/example-schema:protocols[.='ftp']", "ftp"s},
630 };
631 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
632 }
633
634 SECTION("end")
635 {
636 sourcePath = "/example-schema:protocols[.='http']";
637 REQUIRE_CALL(mock, write("/example-schema:protocols", "pop3"s, "http"s));
638 datastore.moveItem(sourcePath, yang::move::Absolute::End);
639 datastore.commitChanges();
640 expected = {
641 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
642 {"/example-schema:protocols[.='ftp']", "ftp"s},
643 {"/example-schema:protocols[.='pop3']", "pop3"s},
644 {"/example-schema:protocols[.='http']", "http"s},
645 };
646 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
647 }
648
649 SECTION("after")
650 {
651 sourcePath = "/example-schema:protocols[.='http']";
652 REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "http"s));
653 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{".", "ftp"s}}});
654 datastore.commitChanges();
655 expected = {
656 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
657 {"/example-schema:protocols[.='ftp']", "ftp"s},
658 {"/example-schema:protocols[.='http']", "http"s},
659 {"/example-schema:protocols[.='pop3']", "pop3"s},
660 };
661 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
662 }
663
664 SECTION("before")
665 {
666 sourcePath = "/example-schema:protocols[.='http']";
667 REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "http"s));
668 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{".", "pop3"s}}});
669 datastore.commitChanges();
670 expected = {
671 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
672 {"/example-schema:protocols[.='ftp']", "ftp"s},
673 {"/example-schema:protocols[.='http']", "http"s},
674 {"/example-schema:protocols[.='pop3']", "pop3"s},
675 };
676 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
677 }
678 }
679
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200680 SECTION("moving non-existing schema nodes")
681 {
682 catching<OnInvalidSchemaPathMove>([&]{
683 datastore.moveItem("/example-schema:non-existing", yang::move::Absolute::Begin);
684 datastore.commitChanges();
685 });
686 }
687
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200688 SECTION("moving list instances")
689 {
690 DatastoreAccess::Tree expected;
691 {
692 // sysrepo does this twice for some reason, it's possibly a bug
693 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", std::nullopt, ""s)).TIMES(2);
694 REQUIRE_CALL(mock, write("/example-schema:players[name='John']/name", std::nullopt, "John"s));
695 REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']", std::nullopt, ""s));
696 REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']", ""s, ""s));
697 REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']/name", std::nullopt, "Eve"s));
698 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", std::nullopt, ""s));
699 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']/name", std::nullopt, "Adam"s));
700 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", ""s, ""s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200701 datastore.createItem("/example-schema:players[name='John']");
702 datastore.createItem("/example-schema:players[name='Eve']");
703 datastore.createItem("/example-schema:players[name='Adam']");
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200704 datastore.commitChanges();
705 expected = {
706 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
707 {"/example-schema:players[name='John']/name", "John"s},
708 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
709 {"/example-schema:players[name='Eve']/name", "Eve"s},
710 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
711 {"/example-schema:players[name='Adam']/name", "Adam"s},
712 };
713 REQUIRE(datastore.getItems("/example-schema:players") == expected);
714 }
715
716 std::string sourcePath;
717 SECTION("begin")
718 {
719 sourcePath = "/example-schema:players[name='Adam']";
720 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", std::nullopt, ""s));
721 datastore.moveItem(sourcePath, yang::move::Absolute::Begin);
722 datastore.commitChanges();
723 expected = {
724 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
725 {"/example-schema:players[name='Adam']/name", "Adam"s},
726 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
727 {"/example-schema:players[name='John']/name", "John"s},
728 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
729 {"/example-schema:players[name='Eve']/name", "Eve"s},
730 };
731 REQUIRE(datastore.getItems("/example-schema:players") == expected);
732 }
733
734 SECTION("end")
735 {
736 sourcePath = "/example-schema:players[name='John']";
737 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s));
738 datastore.moveItem(sourcePath, yang::move::Absolute::End);
739 datastore.commitChanges();
740 expected = {
741 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
742 {"/example-schema:players[name='Eve']/name", "Eve"s},
743 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
744 {"/example-schema:players[name='Adam']/name", "Adam"s},
745 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
746 {"/example-schema:players[name='John']/name", "John"s},
747 };
748 REQUIRE(datastore.getItems("/example-schema:players") == expected);
749 }
750
751 SECTION("after")
752 {
753 sourcePath = "/example-schema:players[name='John']";
754 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s));
755 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{"name", "Eve"s}}});
756 datastore.commitChanges();
757 expected = {
758 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
759 {"/example-schema:players[name='Eve']/name", "Eve"s},
760 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
761 {"/example-schema:players[name='John']/name", "John"s},
762 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
763 {"/example-schema:players[name='Adam']/name", "Adam"s},
764 };
765 REQUIRE(datastore.getItems("/example-schema:players") == expected);
766 }
767
768 SECTION("before")
769 {
770 sourcePath = "/example-schema:players[name='John']";
771 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s));
772 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{"name", "Adam"s}}});
773 datastore.commitChanges();
774 expected = {
775 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
776 {"/example-schema:players[name='Eve']/name", "Eve"s},
777 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
778 {"/example-schema:players[name='John']/name", "John"s},
779 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
780 {"/example-schema:players[name='Adam']/name", "Adam"s},
781 };
782 REQUIRE(datastore.getItems("/example-schema:players") == expected);
783 }
784 }
785
Václav Kubernátfa96ac22020-06-18 17:03:52 +0200786 SECTION("getting /")
787 {
788 {
789 REQUIRE_CALL(mock, write("/example-schema:leafInt32", std::nullopt, "64"s));
790 datastore.setLeaf("/example-schema:leafInt32", 64);
791 datastore.commitChanges();
792 }
793
794 DatastoreAccess::Tree expected{
795 // Sysrepo always returns containers when getting values, but
796 // libnetconf does not. This is fine by the YANG standard:
797 // https://tools.ietf.org/html/rfc7950#section-7.5.7 Furthermore,
798 // NetconfAccess implementation actually only iterates over leafs,
799 // so even if libnetconf did include containers, they wouldn't get
800 // shown here anyway. With sysrepo2, this won't be necessary,
801 // because it'll use the same data structure as libnetconf, so the
802 // results will be consistent.
803#ifdef sysrepo_BACKEND
804 {"/example-schema:inventory", special_{SpecialValue::Container}},
805 {"/example-schema:lol", special_{SpecialValue::Container}},
806#endif
807 {"/example-schema:leafInt32", 64}};
808 auto items = datastore.getItems("/");
809 // This tests if we at least get the data WE added.
810 REQUIRE(std::all_of(expected.begin(), expected.end(), [items] (const auto& item) { return std::find(items.begin(), items.end(), item) != items.end(); }));
811 }
812
Václav Kubernát36986c52020-06-25 10:30:05 +0200813 SECTION("setting and removing without commit")
814 {
815 datastore.setLeaf("/example-schema:leafInt32", 64);
816 datastore.deleteItem("/example-schema:leafInt32");
817 }
818
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200819 SECTION("two key lists")
820 {
821 REQUIRE_CALL(mock, write("/example-schema:point[x='12'][y='10']", std::nullopt, ""s));
822 REQUIRE_CALL(mock, write("/example-schema:point[x='12'][y='10']/x", std::nullopt, "12"s));
823 REQUIRE_CALL(mock, write("/example-schema:point[x='12'][y='10']/y", std::nullopt, "10"s));
824 datastore.createItem("/example-schema:point[x='12'][y='10']");
825 datastore.commitChanges();
826 REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:point") != std::string::npos);
827 }
828
Václav Kubernát73109382018-09-14 19:52:03 +0200829 waitForCompletionAndBitMore(seq1);
830}
Jan Kundrát6ee84792020-01-24 01:43:36 +0100831
832class RpcCb: public sysrepo::Callback {
833 int rpc(const char *xpath, const ::sysrepo::S_Vals input, ::sysrepo::S_Vals_Holder output, void *) override
834 {
835 const auto nukes = "/example-schema:launch-nukes"s;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200836 if (xpath == "/example-schema:noop"s || xpath == "/example-schema:fire"s) {
Jan Kundrát6ee84792020-01-24 01:43:36 +0100837 return SR_ERR_OK;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200838 }
839
840 if (xpath == nukes) {
Jan Kundrát6ee84792020-01-24 01:43:36 +0100841 uint64_t kilotons = 0;
842 bool hasCities = false;
843 for (size_t i = 0; i < input->val_cnt(); ++i) {
844 const auto& val = input->val(i);
Václav Kubernátf4b6a932020-07-09 10:34:19 +0200845 if (val->xpath() == nukes + "/payload") {
846 continue; // ignore, container
847 }
848 if (val->xpath() == nukes + "/description") {
849 continue; // unused
850 }
851
Jan Kundrát6ee84792020-01-24 01:43:36 +0100852 if (val->xpath() == nukes + "/payload/kilotons") {
853 kilotons = val->data()->get_uint64();
Jan Kundrát6ee84792020-01-24 01:43:36 +0100854 } else if (std::string_view{val->xpath()}.find(nukes + "/cities") == 0) {
855 hasCities = true;
856 } else {
857 throw std::runtime_error("RPC launch-nukes: unexpected input "s + val->xpath());
858 }
859 }
860 if (kilotons == 333'666) {
861 // magic, just do not generate any output. This is important because the NETCONF RPC returns just <ok/>.
862 return SR_ERR_OK;
863 }
864 auto buf = output->allocate(2);
865 size_t i = 0;
866 buf->val(i++)->set((nukes + "/blast-radius").c_str(), uint32_t{33'666});
867 buf->val(i++)->set((nukes + "/actual-yield").c_str(), static_cast<uint64_t>(1.33 * kilotons));
868 if (hasCities) {
869 buf = output->reallocate(output->val_cnt() + 2);
870 buf->val(i++)->set((nukes + "/damaged-places/targets[city='London']/city").c_str(), "London");
871 buf->val(i++)->set((nukes + "/damaged-places/targets[city='Berlin']/city").c_str(), "Berlin");
872 }
873 return SR_ERR_OK;
874 }
875 throw std::runtime_error("unrecognized RPC");
876 }
877};
878
879TEST_CASE("rpc") {
880 trompeloeil::sequence seq1;
881 auto srConn = std::make_shared<sysrepo::Connection>("netconf-cli-test-rpc");
882 auto srSession = std::make_shared<sysrepo::Session>(srConn);
883 auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSession);
884 auto cb = std::make_shared<RpcCb>();
885 sysrepo::Logs{}.set_stderr(SR_LL_INF);
Václav Kubernáte7248b22020-06-26 15:38:59 +0200886 auto doNothingCb = std::make_shared<sysrepo::Callback>();
887 srSubscription->module_change_subscribe("example-schema", doNothingCb, nullptr, SR_SUBSCR_CTX_REUSE);
888 // careful here, sysrepo insists on module_change CBs being registered before RPC CBs, otherwise there's a memleak
Jan Kundrát6ee84792020-01-24 01:43:36 +0100889 srSubscription->rpc_subscribe("/example-schema:noop", cb, nullptr, SR_SUBSCR_CTX_REUSE);
890 srSubscription->rpc_subscribe("/example-schema:launch-nukes", cb, nullptr, SR_SUBSCR_CTX_REUSE);
Václav Kubernáte7248b22020-06-26 15:38:59 +0200891 srSubscription->rpc_subscribe("/example-schema:fire", cb, nullptr, SR_SUBSCR_CTX_REUSE);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100892
893#ifdef sysrepo_BACKEND
Václav Kubernáte7248b22020-06-26 15:38:59 +0200894 auto datastore = std::make_shared<SysrepoAccess>("netconf-cli-test", Datastore::Running);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100895#elif defined(netconf_BACKEND)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200896 auto datastore = std::make_shared<NetconfAccess>(NETOPEER_SOCKET_PATH);
Václav Kubernát74487df2020-06-04 01:29:28 +0200897#elif defined(yang_BACKEND)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200898 auto datastore = std::make_shared<YangAccess>();
899 datastore->addSchemaDir(schemaDir);
900 datastore->addSchemaFile(exampleSchemaFile);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100901#else
902#error "Unknown backend"
903#endif
904
Václav Kubernáte7248b22020-06-26 15:38:59 +0200905 auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>& datastore) {
906 return std::make_shared<YangAccess>(std::static_pointer_cast<YangSchema>(datastore->schema()));
907 };
908
909 ProxyDatastore proxyDatastore(datastore, createTemporaryDatastore);
910
911 // ProxyDatastore cannot easily read DatastoreAccess::Tree, so we need to set the input via create/setLeaf/etc.
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200912 SECTION("valid")
913 {
914 std::string rpc;
915 DatastoreAccess::Tree input, output;
Jan Kundrát6ee84792020-01-24 01:43:36 +0100916
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200917 SECTION("noop") {
918 rpc = "/example-schema:noop";
Václav Kubernáte7248b22020-06-26 15:38:59 +0200919 proxyDatastore.initiateRpc(rpc);
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200920 }
921
922 SECTION("small nuke") {
923 rpc = "/example-schema:launch-nukes";
924 input = {
925 {"description", "dummy"s},
926 {"payload/kilotons", uint64_t{333'666}},
927 };
Václav Kubernáte7248b22020-06-26 15:38:59 +0200928 proxyDatastore.initiateRpc(rpc);
929 proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{333'666});
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200930 // no data are returned
931 }
932
933 SECTION("small nuke") {
934 rpc = "/example-schema:launch-nukes";
935 input = {
936 {"description", "dummy"s},
937 {"payload/kilotons", uint64_t{4}},
938 };
Václav Kubernáte7248b22020-06-26 15:38:59 +0200939 proxyDatastore.initiateRpc(rpc);
940 proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{4});
941
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200942 output = {
943 {"blast-radius", uint32_t{33'666}},
944 {"actual-yield", uint64_t{5}},
945 };
946 }
947
948 SECTION("with lists") {
949 rpc = "/example-schema:launch-nukes";
950 input = {
951 {"payload/kilotons", uint64_t{6}},
952 {"cities/targets[city='Prague']/city", "Prague"s},
953 };
Václav Kubernáte7248b22020-06-26 15:38:59 +0200954 proxyDatastore.initiateRpc(rpc);
955 proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{6});
956 proxyDatastore.createItem("/example-schema:launch-nukes/example-schema:cities/example-schema:targets[city='Prague']");
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200957 output = {
958 {"blast-radius", uint32_t{33'666}},
959 {"actual-yield", uint64_t{7}},
960 {"damaged-places", special_{SpecialValue::PresenceContainer}},
961 {"damaged-places/targets[city='London']", special_{SpecialValue::List}},
962 {"damaged-places/targets[city='London']/city", "London"s},
963 {"damaged-places/targets[city='Berlin']", special_{SpecialValue::List}},
964 {"damaged-places/targets[city='Berlin']/city", "Berlin"s},
965 };
966 }
967
Václav Kubernáte7248b22020-06-26 15:38:59 +0200968 SECTION("with leafref") {
969 datastore->createItem("/example-schema:person[name='Colton']");
970 datastore->commitChanges();
971
972 rpc = "/example-schema:fire";
973 input = {
974 {"whom", "Colton"s}
975 };
976 proxyDatastore.initiateRpc(rpc);
977 proxyDatastore.setLeaf("/example-schema:fire/example-schema:whom", "Colton"s);
978 }
979
980 catching<OnRPC>([&] {REQUIRE(datastore->executeRpc(rpc, input) == output);});
981 catching<OnRPC>([&] {REQUIRE(proxyDatastore.executeRpc() == output);});
Jan Kundrát6ee84792020-01-24 01:43:36 +0100982 }
983
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200984 SECTION("non-existing RPC")
985 {
Václav Kubernáte7248b22020-06-26 15:38:59 +0200986 catching<OnInvalidRpcPath>([&] {datastore->executeRpc("/example-schema:non-existing", DatastoreAccess::Tree{});});
Jan Kundrát6ee84792020-01-24 01:43:36 +0100987 }
988
Jan Kundrát6ee84792020-01-24 01:43:36 +0100989 waitForCompletionAndBitMore(seq1);
990}