blob: bbcae254931a9ab79e92ec954afc377f5a496325 [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 "proxy_datastore.hpp"
Václav Kubernátb4e5b182020-11-16 19:55:09 +010012#include "yang_schema.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;
Václav Kubernát654303f2020-07-31 13:16:54 +020017using OnInvalidSchemaPathDelete = DatastoreException;
Jan Kundrát7ec214d2020-06-19 17:05:07 +020018using OnInvalidSchemaPathMove = sysrepo::sysrepo_exception;
Václav Kubernát40776132021-02-03 08:47:33 +010019using OnInvalidRpcPath = std::runtime_error;
Václav Kubernát2edfe542021-02-03 08:08:29 +010020using OnInvalidRpcInput = sysrepo::sysrepo_exception;
Jan Kundrát7ec214d2020-06-19 17:05:07 +020021using OnKeyNotFound = void;
Václav Kubernáta8789602020-07-20 15:18:19 +020022using OnExec = void;
Václav Kubernátc31bd602019-03-07 11:44:48 +010023#elif defined(netconf_BACKEND)
Jan Kundrát7ec214d2020-06-19 17:05:07 +020024using OnInvalidSchemaPathCreate = std::runtime_error;
25using OnInvalidSchemaPathDelete = std::runtime_error;
26using OnInvalidSchemaPathMove = std::runtime_error;
Václav Kubernát74487df2020-06-04 01:29:28 +020027using OnInvalidRpcPath = std::runtime_error;
Václav Kubernát2edfe542021-02-03 08:08:29 +010028using OnInvalidRpcInput = std::runtime_error;
Jan Kundrát7ec214d2020-06-19 17:05:07 +020029using OnKeyNotFound = std::runtime_error;
Václav Kubernáta8789602020-07-20 15:18:19 +020030using OnExec = void;
Václav Kubernátc31bd602019-03-07 11:44:48 +010031#include "netconf_access.hpp"
Václav Kubernát74487df2020-06-04 01:29:28 +020032#elif defined(yang_BACKEND)
33#include <fstream>
34#include "yang_access.hpp"
35#include "yang_access_test_vars.hpp"
36using OnInvalidSchemaPathCreate = DatastoreException;
37using OnInvalidSchemaPathDelete = DatastoreException;
38using OnInvalidSchemaPathMove = DatastoreException;
39using OnInvalidRpcPath = DatastoreException;
Václav Kubernát2edfe542021-02-03 08:08:29 +010040using OnInvalidRpcInput = std::logic_error;
Václav Kubernát74487df2020-06-04 01:29:28 +020041using OnKeyNotFound = DatastoreException;
Václav Kubernáta8789602020-07-20 15:18:19 +020042using OnExec = std::logic_error;
Václav Kubernátc31bd602019-03-07 11:44:48 +010043#else
44#error "Unknown backend"
45#endif
Jan Kundrátbb525b42020-02-04 11:56:59 +010046#include "pretty_printers.hpp"
Václav Kubernát73109382018-09-14 19:52:03 +020047#include "sysrepo_subscription.hpp"
Václav Kubernát8e121ff2019-10-15 15:47:45 +020048#include "utils.hpp"
Václav Kubernát73109382018-09-14 19:52:03 +020049
Jan Kundrát6ee84792020-01-24 01:43:36 +010050using namespace std::literals::string_literals;
51
Václav Kubernát69aabe92020-01-24 16:53:12 +010052class MockRecorder : public trompeloeil::mock_interface<Recorder> {
Václav Kubernát73109382018-09-14 19:52:03 +020053public:
Václav Kubernát69aabe92020-01-24 16:53:12 +010054 IMPLEMENT_MOCK3(write);
Václav Kubernát73109382018-09-14 19:52:03 +020055};
56
Jan Kundrátbb525b42020-02-04 11:56:59 +010057class MockDataSupplier : public trompeloeil::mock_interface<DataSupplier> {
58public:
59 IMPLEMENT_CONST_MOCK1(get_data);
60};
61
Jan Kundrát7ec214d2020-06-19 17:05:07 +020062namespace {
63template <class ...> constexpr std::false_type always_false [[maybe_unused]] {};
64template <class Exception, typename Callable> void catching(const Callable& what) {
65
66 if constexpr (std::is_same_v<Exception, void>) {
Jan Kundrát3867c9e2020-06-18 20:26:45 +020067 what();
Jan Kundrát7ec214d2020-06-19 17:05:07 +020068 } else if constexpr (std::is_same<Exception, std::runtime_error>()) {
69 // cannot use REQUIRE_THROWS_AS(..., Exception) directly because that one
70 // needs an extra `typename` deep in the bowels of doctest
71 REQUIRE_THROWS_AS(what(), std::runtime_error);
Václav Kubernát74487df2020-06-04 01:29:28 +020072 } else if constexpr (std::is_same<Exception, std::logic_error>()) {
73 REQUIRE_THROWS_AS(what(), std::logic_error);
Jan Kundrát7ec214d2020-06-19 17:05:07 +020074 } else if constexpr (std::is_same<Exception, DatastoreException>()) {
75 REQUIRE_THROWS_AS(what(), DatastoreException);
76 } else if constexpr (std::is_same<Exception, sysrepo::sysrepo_exception>()) {
77 REQUIRE_THROWS_AS(what(), sysrepo::sysrepo_exception);
78 } else {
79 static_assert(always_false<Exception>); // https://stackoverflow.com/a/53945549/2245623
Jan Kundrát3867c9e2020-06-18 20:26:45 +020080 }
81}
Jan Kundrát7ec214d2020-06-19 17:05:07 +020082}
Jan Kundrát3867c9e2020-06-18 20:26:45 +020083
Václav Kubernát74487df2020-06-04 01:29:28 +020084#if defined(yang_BACKEND)
85class TestYangAccess : public YangAccess {
86public:
87 void commitChanges() override
88 {
89 YangAccess::commitChanges();
90 dumpToSysrepo();
91 }
92
93 void copyConfig(const Datastore source, const Datastore destination) override
94 {
95 YangAccess::copyConfig(source, destination);
96 dumpToSysrepo();
97 }
98
99private:
100 void dumpToSysrepo()
101 {
102 {
103 std::ofstream of(testConfigFile);
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200104 of << dump(DataFormat::Xml);
Václav Kubernát74487df2020-06-04 01:29:28 +0200105 }
Jan Kundrát86840ec2021-01-27 15:38:08 +0100106 auto command = std::string(sysrepocfgExecutable) + " --import=" + testConfigFile + " --format=xml --datastore=running --module=example-schema";
Václav Kubernát74487df2020-06-04 01:29:28 +0200107 REQUIRE(std::system(command.c_str()) == 0);
108 }
109};
110#endif
111
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200112TEST_CASE("setting/getting values")
Václav Kubernát73109382018-09-14 19:52:03 +0200113{
Václav Kubernát654303f2020-07-31 13:16:54 +0200114 sr_log_stderr(SR_LL_DBG);
Václav Kubernát73109382018-09-14 19:52:03 +0200115 trompeloeil::sequence seq1;
Václav Kubernáteaf56682021-02-22 17:15:41 +0100116 MockRecorder mockRunning;
117 MockRecorder mockStartup;
Václav Kubernát654303f2020-07-31 13:16:54 +0200118 {
119 auto conn = std::make_shared<sysrepo::Connection>();
120 auto sess = std::make_shared<sysrepo::Session>(conn);
121 sess->copy_config(SR_DS_STARTUP, "example-schema", 1000, true);
122 }
Václav Kubernáteaf56682021-02-22 17:15:41 +0100123 SysrepoSubscription subRunning("example-schema", &mockRunning);
124 SysrepoSubscription subStartup("example-schema", &mockStartup, SR_DS_STARTUP);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100125
126#ifdef sysrepo_BACKEND
Václav Kubernátf5d75152020-12-03 03:52:34 +0100127 SysrepoAccess datastore;
Václav Kubernátc31bd602019-03-07 11:44:48 +0100128#elif defined(netconf_BACKEND)
Václav Kubernátd1beedc2020-09-07 12:09:05 +0200129 const auto NETOPEER_SOCKET = getenv("NETOPEER_SOCKET");
130 NetconfAccess datastore(NETOPEER_SOCKET);
Václav Kubernát74487df2020-06-04 01:29:28 +0200131#elif defined(yang_BACKEND)
132 TestYangAccess datastore;
133 datastore.addSchemaDir(schemaDir);
134 datastore.addSchemaFile(exampleSchemaFile);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100135#else
136#error "Unknown backend"
137#endif
Václav Kubernát73109382018-09-14 19:52:03 +0200138
Václav Kubernát69aabe92020-01-24 16:53:12 +0100139
Václav Kubernát134d78f2019-09-03 16:42:29 +0200140 SECTION("set leafInt8 to -128")
Václav Kubernát73109382018-09-14 19:52:03 +0200141 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100142 REQUIRE_CALL(mockRunning, write("/example-schema:leafInt8", std::nullopt, "-128"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200143 datastore.setLeaf("/example-schema:leafInt8", int8_t{-128});
144 datastore.commitChanges();
145 }
146
147 SECTION("set leafInt16 to -32768")
148 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100149 REQUIRE_CALL(mockRunning, write("/example-schema:leafInt16", std::nullopt, "-32768"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200150 datastore.setLeaf("/example-schema:leafInt16", int16_t{-32768});
151 datastore.commitChanges();
152 }
153
154 SECTION("set leafInt32 to -2147483648")
155 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100156 REQUIRE_CALL(mockRunning, write("/example-schema:leafInt32", std::nullopt, "-2147483648"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200157 datastore.setLeaf("/example-schema:leafInt32", int32_t{-2147483648});
158 datastore.commitChanges();
159 }
160
161 SECTION("set leafInt64 to -50000000000")
162 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100163 REQUIRE_CALL(mockRunning, write("/example-schema:leafInt64", std::nullopt, "-50000000000"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200164 datastore.setLeaf("/example-schema:leafInt64", int64_t{-50000000000});
165 datastore.commitChanges();
166 }
167
168 SECTION("set leafUInt8 to 255")
169 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100170 REQUIRE_CALL(mockRunning, write("/example-schema:leafUInt8", std::nullopt, "255"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200171 datastore.setLeaf("/example-schema:leafUInt8", uint8_t{255});
172 datastore.commitChanges();
173 }
174
175 SECTION("set leafUInt16 to 65535")
176 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100177 REQUIRE_CALL(mockRunning, write("/example-schema:leafUInt16", std::nullopt, "65535"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200178 datastore.setLeaf("/example-schema:leafUInt16", uint16_t{65535});
179 datastore.commitChanges();
180 }
181
182 SECTION("set leafUInt32 to 4294967295")
183 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100184 REQUIRE_CALL(mockRunning, write("/example-schema:leafUInt32", std::nullopt, "4294967295"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200185 datastore.setLeaf("/example-schema:leafUInt32", uint32_t{4294967295});
186 datastore.commitChanges();
187 }
188
189 SECTION("set leafUInt64 to 50000000000")
190 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100191 REQUIRE_CALL(mockRunning, write("/example-schema:leafUInt64", std::nullopt, "50000000000"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200192 datastore.setLeaf("/example-schema:leafUInt64", uint64_t{50000000000});
Václav Kubernát73109382018-09-14 19:52:03 +0200193 datastore.commitChanges();
194 }
195
196 SECTION("set leafEnum to coze")
197 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100198 REQUIRE_CALL(mockRunning, write("/example-schema:leafEnum", std::nullopt, "coze"s));
Václav Kubernát73109382018-09-14 19:52:03 +0200199 datastore.setLeaf("/example-schema:leafEnum", enum_{"coze"});
200 datastore.commitChanges();
201 }
202
203 SECTION("set leafDecimal to 123.544")
204 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100205 REQUIRE_CALL(mockRunning, write("/example-schema:leafDecimal", std::nullopt, "123.544"s));
Václav Kubernát73109382018-09-14 19:52:03 +0200206 datastore.setLeaf("/example-schema:leafDecimal", 123.544);
207 datastore.commitChanges();
208 }
209
Jan Kundrátd2872862020-06-18 21:02:00 +0200210 SECTION("set a string, then delete it")
211 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100212 REQUIRE_CALL(mockRunning, write("/example-schema:leafString", std::nullopt, "blah"s));
Jan Kundrátd2872862020-06-18 21:02:00 +0200213 datastore.setLeaf("/example-schema:leafString", "blah"s);
214 datastore.commitChanges();
215 DatastoreAccess::Tree expected{{"/example-schema:leafString", "blah"s}};
216 REQUIRE(datastore.getItems("/example-schema:leafString") == expected);
217
Václav Kubernáteaf56682021-02-22 17:15:41 +0100218 REQUIRE_CALL(mockRunning, write("/example-schema:leafString", "blah"s, std::nullopt));
Jan Kundrátd2872862020-06-18 21:02:00 +0200219 datastore.deleteItem("/example-schema:leafString");
220 datastore.commitChanges();
221 expected.clear();
222 REQUIRE(datastore.getItems("/example-schema:leafString") == expected);
223 }
224
Václav Kubernát8e756642021-11-05 00:04:54 +0100225 SECTION("set a string, then set it to something else without commiting")
226 {
227 REQUIRE_CALL(mockRunning, write("/example-schema:leafString", std::nullopt, "oops"s));
228 datastore.setLeaf("/example-schema:leafString", "blah"s);
229 datastore.setLeaf("/example-schema:leafString", "oops"s);
230 datastore.commitChanges();
231 DatastoreAccess::Tree expected{{"/example-schema:leafString", "oops"s}};
232 REQUIRE(datastore.getItems("/example-schema:leafString") == expected);
233
234 }
235
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200236 SECTION("set a non-existing leaf")
237 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100238 catching<OnInvalidSchemaPathCreate>([&] {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200239 datastore.setLeaf("/example-schema:non-existing", "what"s);
240 });
241 }
242
Václav Kubernát73109382018-09-14 19:52:03 +0200243 SECTION("create presence container")
244 {
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200245 REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:pContainer") == std::string::npos);
Václav Kubernáteaf56682021-02-22 17:15:41 +0100246 REQUIRE_CALL(mockRunning, write("/example-schema:pContainer", std::nullopt, ""s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200247 datastore.createItem("/example-schema:pContainer");
Václav Kubernát73109382018-09-14 19:52:03 +0200248 datastore.commitChanges();
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200249 REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:pContainer") != std::string::npos);
Václav Kubernát73109382018-09-14 19:52:03 +0200250 }
251
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100252 SECTION("create/delete a list instance")
Václav Kubernát45f4a822019-05-29 21:10:50 +0200253 {
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100254 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100255 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Nguyen']", std::nullopt, ""s));
256 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Nguyen']/name", std::nullopt, "Nguyen"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200257 datastore.createItem("/example-schema:person[name='Nguyen']");
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100258 datastore.commitChanges();
259 }
260 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100261 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Nguyen']", ""s, std::nullopt));
262 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Nguyen']/name", "Nguyen"s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200263 datastore.deleteItem("/example-schema:person[name='Nguyen']");
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100264 datastore.commitChanges();
265 }
Václav Kubernát45f4a822019-05-29 21:10:50 +0200266 }
267
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200268 SECTION("deleting non-existing list keys")
269 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100270 catching<OnKeyNotFound>([&] {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200271 datastore.deleteItem("/example-schema:person[name='non existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200272 datastore.commitChanges();
273 });
274 }
275
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200276 SECTION("accessing non-existing schema nodes as a list")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200277 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100278 catching<OnInvalidSchemaPathCreate>([&] {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200279 datastore.createItem("/example-schema:non-existing-list[xxx='blah']");
280 datastore.commitChanges();
281 });
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100282 catching<OnInvalidSchemaPathDelete>([&] {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200283 datastore.deleteItem("/example-schema:non-existing-list[xxx='non existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200284 datastore.commitChanges();
285 });
286 }
287
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200288 SECTION("leafref pointing to a key of a list")
289 {
290 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100291 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Dan']", std::nullopt, ""s));
292 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Dan']/name", std::nullopt, "Dan"s));
293 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Elfi']", std::nullopt, ""s));
294 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Elfi']/name", std::nullopt, "Elfi"s));
295 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Kolafa']", std::nullopt, ""s));
296 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Kolafa']/name", std::nullopt, "Kolafa"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200297 datastore.createItem("/example-schema:person[name='Dan']");
298 datastore.createItem("/example-schema:person[name='Elfi']");
299 datastore.createItem("/example-schema:person[name='Kolafa']");
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200300 datastore.commitChanges();
301 }
302
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200303 std::string value;
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200304 SECTION("Dan")
305 {
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200306 value = "Dan";
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200307 }
308
309 SECTION("Elfi")
310 {
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200311 value = "Elfi";
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200312 }
313
314 SECTION("Kolafa")
315 {
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200316 value = "Kolafa";
317 }
318
319 datastore.setLeaf("/example-schema:bossPerson", value);
320 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100321 REQUIRE_CALL(mockRunning, write("/example-schema:bossPerson", std::nullopt, value));
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200322 datastore.commitChanges();
323 }
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200324 REQUIRE(datastore.getItems("/example-schema:bossPerson") == DatastoreAccess::Tree{{"/example-schema:bossPerson", value}});
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200325 }
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200326 SECTION("bool values get correctly represented as bools")
327 {
328 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100329 REQUIRE_CALL(mockRunning, write("/example-schema:down", std::nullopt, "true"s));
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200330 datastore.setLeaf("/example-schema:down", bool{true});
331 datastore.commitChanges();
332 }
333
Jan Kundrátb331b552020-01-23 15:25:29 +0100334 DatastoreAccess::Tree expected{{"/example-schema:down", bool{true}}};
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200335 REQUIRE(datastore.getItems("/example-schema:down") == expected);
336 }
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200337
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200338 SECTION("getting items from the whole module")
339 {
340 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100341 REQUIRE_CALL(mockRunning, write("/example-schema:up", std::nullopt, "true"s));
342 REQUIRE_CALL(mockRunning, write("/example-schema:down", std::nullopt, "false"s));
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200343 datastore.setLeaf("/example-schema:up", bool{true});
344 datastore.setLeaf("/example-schema:down", bool{false});
345 datastore.commitChanges();
346 }
347
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200348 DatastoreAccess::Tree expected{
Václav Kubernát654303f2020-07-31 13:16:54 +0200349 {"/example-schema:up", bool{true}},
350 {"/example-schema:down", bool{false}}
351 };
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200352 REQUIRE(datastore.getItems("/example-schema:*") == expected);
353 }
354
Václav Kubernát152ce222019-12-19 12:23:32 +0100355 SECTION("getItems returns correct datatypes")
356 {
357 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100358 REQUIRE_CALL(mockRunning, write("/example-schema:leafEnum", std::nullopt, "lol"s));
Václav Kubernát152ce222019-12-19 12:23:32 +0100359 datastore.setLeaf("/example-schema:leafEnum", enum_{"lol"});
360 datastore.commitChanges();
361 }
Jan Kundrátb331b552020-01-23 15:25:29 +0100362 DatastoreAccess::Tree expected{{"/example-schema:leafEnum", enum_{"lol"}}};
Václav Kubernát152ce222019-12-19 12:23:32 +0100363
364 REQUIRE(datastore.getItems("/example-schema:leafEnum") == expected);
365 }
366
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100367 SECTION("getItems on a list")
368 {
369 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100370 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Jan']", std::nullopt, ""s));
371 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Jan']/name", std::nullopt, "Jan"s));
372 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Michal']", std::nullopt, ""s));
373 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Michal']/name", std::nullopt, "Michal"s));
374 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Petr']", std::nullopt, ""s));
375 REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Petr']/name", std::nullopt, "Petr"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200376 datastore.createItem("/example-schema:person[name='Jan']");
377 datastore.createItem("/example-schema:person[name='Michal']");
378 datastore.createItem("/example-schema:person[name='Petr']");
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100379 datastore.commitChanges();
380 }
Jan Kundrátb331b552020-01-23 15:25:29 +0100381 DatastoreAccess::Tree expected{
Václav Kubernát144729d2020-01-08 15:20:35 +0100382 {"/example-schema:person[name='Jan']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100383 {"/example-schema:person[name='Jan']/name", std::string{"Jan"}},
Václav Kubernát144729d2020-01-08 15:20:35 +0100384 {"/example-schema:person[name='Michal']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100385 {"/example-schema:person[name='Michal']/name", std::string{"Michal"}},
Václav Kubernát144729d2020-01-08 15:20:35 +0100386 {"/example-schema:person[name='Petr']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100387 {"/example-schema:person[name='Petr']/name", std::string{"Petr"}}
388 };
389
390 REQUIRE(datastore.getItems("/example-schema:person") == expected);
391 }
392
Václav Kubernát69aabe92020-01-24 16:53:12 +0100393 SECTION("presence containers")
394 {
395 DatastoreAccess::Tree expected;
396 // Make sure it's not there before we create it
397 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
398
399 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100400 REQUIRE_CALL(mockRunning, write("/example-schema:pContainer", std::nullopt, ""s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200401 datastore.createItem("/example-schema:pContainer");
Václav Kubernát69aabe92020-01-24 16:53:12 +0100402 datastore.commitChanges();
403 }
404 expected = {
405 {"/example-schema:pContainer", special_{SpecialValue::PresenceContainer}}
406 };
407 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
408
409 // Make sure it's not there after we delete it
410 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100411 REQUIRE_CALL(mockRunning, write("/example-schema:pContainer", ""s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200412 datastore.deleteItem("/example-schema:pContainer");
Václav Kubernát69aabe92020-01-24 16:53:12 +0100413 datastore.commitChanges();
414 }
415 expected = {};
416 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100417 }
Václav Kubernát69aabe92020-01-24 16:53:12 +0100418
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200419 SECTION("creating a non-existing schema node as a container")
420 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100421 catching<OnInvalidSchemaPathCreate>([&] {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200422 datastore.createItem("/example-schema:non-existing-presence-container");
423 datastore.commitChanges();
424 });
425 }
426
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200427 SECTION("deleting a non-existing schema node as a container or leaf")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200428 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100429 catching<OnInvalidSchemaPathDelete>([&] {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200430 datastore.deleteItem("/example-schema:non-existing-presence-container");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200431 datastore.commitChanges();
432 });
433 }
434
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100435 SECTION("nested presence container")
436 {
437 DatastoreAccess::Tree expected;
438 // Make sure it's not there before we create it
439 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
440 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100441 REQUIRE_CALL(mockRunning, write("/example-schema:inventory/stuff", std::nullopt, ""s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200442 datastore.createItem("/example-schema:inventory/stuff");
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100443 datastore.commitChanges();
444 }
445 expected = {
446 {"/example-schema:inventory/stuff", special_{SpecialValue::PresenceContainer}}
447 };
448 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
449 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100450 REQUIRE_CALL(mockRunning, write("/example-schema:inventory/stuff", ""s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200451 datastore.deleteItem("/example-schema:inventory/stuff");
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100452 datastore.commitChanges();
453 }
454 expected = {};
455 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
Václav Kubernát69aabe92020-01-24 16:53:12 +0100456 }
457
Jan Kundrátbd3169c2020-02-03 19:31:34 +0100458 SECTION("floats")
459 {
460 datastore.setLeaf("/example-schema:leafDecimal", 123.4);
Václav Kubernáteaf56682021-02-22 17:15:41 +0100461 REQUIRE_CALL(mockRunning, write("/example-schema:leafDecimal", std::nullopt, "123.4"s));
Jan Kundrátbd3169c2020-02-03 19:31:34 +0100462 datastore.commitChanges();
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100463 DatastoreAccess::Tree expected{
Jan Kundrátbd3169c2020-02-03 19:31:34 +0100464 {"/example-schema:leafDecimal", 123.4},
465 };
466 REQUIRE(datastore.getItems("/example-schema:leafDecimal") == expected);
467 }
468
Jan Kundrát3ff50122020-05-07 00:37:50 +0200469 SECTION("unions")
470 {
471 datastore.setLeaf("/example-schema:unionIntString", int32_t{10});
Václav Kubernáteaf56682021-02-22 17:15:41 +0100472 REQUIRE_CALL(mockRunning, write("/example-schema:unionIntString", std::nullopt, "10"s));
Jan Kundrát3ff50122020-05-07 00:37:50 +0200473 datastore.commitChanges();
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100474 DatastoreAccess::Tree expected{
Jan Kundrát3ff50122020-05-07 00:37:50 +0200475 {"/example-schema:unionIntString", int32_t{10}},
476 };
477 REQUIRE(datastore.getItems("/example-schema:unionIntString") == expected);
478 }
479
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100480 SECTION("identityref")
481 {
Jan Kundrát0d8abd12020-05-07 02:00:14 +0200482 datastore.setLeaf("/example-schema:beast", identityRef_{"example-schema", "Mammal"});
Václav Kubernáteaf56682021-02-22 17:15:41 +0100483 REQUIRE_CALL(mockRunning, write("/example-schema:beast", std::nullopt, "example-schema:Mammal"s));
Jan Kundrát0d8abd12020-05-07 02:00:14 +0200484 datastore.commitChanges();
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100485 DatastoreAccess::Tree expected{
Jan Kundrát0d8abd12020-05-07 02:00:14 +0200486 {"/example-schema:beast", identityRef_{"example-schema", "Mammal"}},
487 };
488 REQUIRE(datastore.getItems("/example-schema:beast") == expected);
489
490 datastore.setLeaf("/example-schema:beast", identityRef_{"Whale"});
Václav Kubernáteaf56682021-02-22 17:15:41 +0100491 REQUIRE_CALL(mockRunning, write("/example-schema:beast", "example-schema:Mammal", "example-schema:Whale"s));
Jan Kundrát0d8abd12020-05-07 02:00:14 +0200492 datastore.commitChanges();
493 expected = {
494 {"/example-schema:beast", identityRef_{"example-schema", "Whale"}},
495 };
496 REQUIRE(datastore.getItems("/example-schema:beast") == expected);
497 }
498
Jan Kundrát68985442020-05-07 02:15:34 +0200499 SECTION("binary")
500 {
501 datastore.setLeaf("/example-schema:blob", binary_{"cHduegByIQ=="s});
Václav Kubernáteaf56682021-02-22 17:15:41 +0100502 REQUIRE_CALL(mockRunning, write("/example-schema:blob", std::nullopt, "cHduegByIQ=="s));
Jan Kundrát68985442020-05-07 02:15:34 +0200503 datastore.commitChanges();
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100504 DatastoreAccess::Tree expected{
Jan Kundrát68985442020-05-07 02:15:34 +0200505 {"/example-schema:blob", binary_{"cHduegByIQ=="s}},
506 };
507 REQUIRE(datastore.getItems("/example-schema:blob") == expected);
508 }
509
Jan Kundrát379bb572020-05-07 03:23:13 +0200510 SECTION("empty")
511 {
512 datastore.setLeaf("/example-schema:dummy", empty_{});
Václav Kubernáteaf56682021-02-22 17:15:41 +0100513 REQUIRE_CALL(mockRunning, write("/example-schema:dummy", std::nullopt, ""s));
Jan Kundrát379bb572020-05-07 03:23:13 +0200514 datastore.commitChanges();
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100515 DatastoreAccess::Tree expected{
Jan Kundrát379bb572020-05-07 03:23:13 +0200516 {"/example-schema:dummy", empty_{}},
517 };
518 REQUIRE(datastore.getItems("/example-schema:dummy") == expected);
519 }
520
Václav Kubernát19097f32020-10-05 10:08:29 +0200521 SECTION("bits")
522 {
523 datastore.setLeaf("/example-schema:flags", bits_{{"sign", "carry"}});
Václav Kubernáteaf56682021-02-22 17:15:41 +0100524 REQUIRE_CALL(mockRunning, write("/example-schema:flags", std::nullopt, "carry sign"s));
Václav Kubernát19097f32020-10-05 10:08:29 +0200525 datastore.commitChanges();
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100526 DatastoreAccess::Tree expected{
Václav Kubernát19097f32020-10-05 10:08:29 +0200527 {"/example-schema:flags", bits_{{"carry", "sign"}}},
528 };
529 REQUIRE(datastore.getItems("/example-schema:flags") == expected);
530 }
531
Václav Kubernát74487df2020-06-04 01:29:28 +0200532#if not defined(yang_BACKEND)
Jan Kundrátbb525b42020-02-04 11:56:59 +0100533 SECTION("operational data")
534 {
535 MockDataSupplier mockOpsData;
Václav Kubernát654303f2020-07-31 13:16:54 +0200536 OperationalDataSubscription opsDataSub("example-schema", "/example-schema:temperature", mockOpsData);
Václav Kubernátf90a0b52020-11-06 05:53:03 +0100537 OperationalDataSubscription opsDataSub2("example-schema", "/example-schema:users", mockOpsData);
Jan Kundrátbb525b42020-02-04 11:56:59 +0100538 DatastoreAccess::Tree expected;
539 std::string xpath;
Václav Kubernátf90a0b52020-11-06 05:53:03 +0100540
Jan Kundrátbb525b42020-02-04 11:56:59 +0100541 SECTION("temperature")
542 {
543 expected = {{"/example-schema:temperature", int32_t{22}}};
544 xpath = "/example-schema:temperature";
545 }
546
Václav Kubernátf90a0b52020-11-06 05:53:03 +0100547 SECTION("key-less lists")
548 {
549 expected = {
550 {"/example-schema:users/userList[1]", special_{SpecialValue::List}},
551 {"/example-schema:users/userList[1]/name", std::string{"John"}},
552 {"/example-schema:users/userList[1]/otherfield", std::string{"LOL"}},
553 {"/example-schema:users/userList[2]", special_{SpecialValue::List}},
554 {"/example-schema:users/userList[2]/name", std::string{"Foo"}},
555 {"/example-schema:users/userList[2]/otherfield", std::string{"Bar"}},
556 };
557 xpath = "/example-schema:users";
558 }
Jan Kundrátbb525b42020-02-04 11:56:59 +0100559 REQUIRE_CALL(mockOpsData, get_data(xpath)).RETURN(expected);
560 REQUIRE(datastore.getItems(xpath) == expected);
561 }
Václav Kubernát74487df2020-06-04 01:29:28 +0200562#endif
Jan Kundrátbb525b42020-02-04 11:56:59 +0100563
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200564 SECTION("leaf list")
565 {
566 DatastoreAccess::Tree expected;
Václav Kubernáteaf56682021-02-22 17:15:41 +0100567 REQUIRE_CALL(mockRunning, write("/example-schema:addresses", std::nullopt, "0.0.0.0"s));
568 REQUIRE_CALL(mockRunning, write("/example-schema:addresses", std::nullopt, "127.0.0.1"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200569 datastore.createItem("/example-schema:addresses[.='0.0.0.0']");
570 datastore.createItem("/example-schema:addresses[.='127.0.0.1']");
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200571 datastore.commitChanges();
572 expected = {
573 {"/example-schema:addresses", special_{SpecialValue::LeafList}},
574 {"/example-schema:addresses[.='0.0.0.0']", "0.0.0.0"s},
575 {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s},
576 };
577 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
578
Václav Kubernáteaf56682021-02-22 17:15:41 +0100579 REQUIRE_CALL(mockRunning, write("/example-schema:addresses", "0.0.0.0"s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200580 datastore.deleteItem("/example-schema:addresses[.='0.0.0.0']");
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200581 datastore.commitChanges();
582 expected = {
583 {"/example-schema:addresses", special_{SpecialValue::LeafList}},
584 {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s},
585 };
586 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
587
Václav Kubernáteaf56682021-02-22 17:15:41 +0100588 REQUIRE_CALL(mockRunning, write("/example-schema:addresses", "127.0.0.1"s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200589 datastore.deleteItem("/example-schema:addresses[.='127.0.0.1']");
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200590 datastore.commitChanges();
591 expected = {};
592 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
593 }
Jan Kundrátbb525b42020-02-04 11:56:59 +0100594
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200595 SECTION("deleting a non-existing leaf-list")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200596 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100597 catching<OnKeyNotFound>([&] {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200598 datastore.deleteItem("/example-schema:addresses[.='non-existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200599 datastore.commitChanges();
600 });
601 }
602
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200603 SECTION("accessing a non-existing schema node as a leaf-list")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200604 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100605 catching<OnInvalidSchemaPathCreate>([&] {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200606 datastore.createItem("/example-schema:non-existing[.='non-existing']");
607 datastore.commitChanges();
608 });
609
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100610 catching<OnInvalidSchemaPathDelete>([&] {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200611 datastore.deleteItem("/example-schema:non-existing[.='non-existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200612 datastore.commitChanges();
613 });
614 }
615
Václav Kubernát7160a132020-04-03 02:11:01 +0200616 SECTION("copying data from startup refreshes the data")
617 {
618 {
619 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{});
Václav Kubernáteaf56682021-02-22 17:15:41 +0100620 REQUIRE_CALL(mockRunning, write("/example-schema:leafInt16", std::nullopt, "123"s));
Václav Kubernát7160a132020-04-03 02:11:01 +0200621 datastore.setLeaf("/example-schema:leafInt16", int16_t{123});
622 datastore.commitChanges();
623 }
624 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{{"/example-schema:leafInt16", int16_t{123}}});
Václav Kubernáteaf56682021-02-22 17:15:41 +0100625 REQUIRE_CALL(mockRunning, write("/example-schema:leafInt16", "123"s, std::nullopt));
Václav Kubernát7160a132020-04-03 02:11:01 +0200626 datastore.copyConfig(Datastore::Startup, Datastore::Running);
627 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{});
628 }
629
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200630 SECTION("moving leaflist instances")
631 {
632 DatastoreAccess::Tree expected;
633 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100634 REQUIRE_CALL(mockRunning, write("/example-schema:protocols", std::nullopt, "http"s));
Václav Kubernát654303f2020-07-31 13:16:54 +0200635 // FIXME: Why no notifications for these??
636 // ... possibly because my subscription doesn't extract it properly?
637 // REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "ftp"s));
638 // REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s));
Václav Kubernáteaf56682021-02-22 17:15:41 +0100639 REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "http"s, "ftp"s));
640 REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "ftp"s, "pop3"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200641 datastore.createItem("/example-schema:protocols[.='http']");
642 datastore.createItem("/example-schema:protocols[.='ftp']");
643 datastore.createItem("/example-schema:protocols[.='pop3']");
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200644 datastore.commitChanges();
645 expected = {
646 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
647 {"/example-schema:protocols[.='http']", "http"s},
648 {"/example-schema:protocols[.='ftp']", "ftp"s},
649 {"/example-schema:protocols[.='pop3']", "pop3"s},
650 };
651 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
652 }
653
654 std::string sourcePath;
655 SECTION("begin")
656 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100657 REQUIRE_CALL(mockRunning, write("/example-schema:protocols", std::nullopt, "pop3"s));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200658 sourcePath = "/example-schema:protocols[.='pop3']";
659 datastore.moveItem(sourcePath, yang::move::Absolute::Begin);
660 datastore.commitChanges();
661 expected = {
662 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
663 {"/example-schema:protocols[.='pop3']", "pop3"s},
664 {"/example-schema:protocols[.='http']", "http"s},
665 {"/example-schema:protocols[.='ftp']", "ftp"s},
666 };
667 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
668 }
669
670 SECTION("end")
671 {
672 sourcePath = "/example-schema:protocols[.='http']";
Václav Kubernáteaf56682021-02-22 17:15:41 +0100673 REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "pop3"s, "http"s));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200674 datastore.moveItem(sourcePath, yang::move::Absolute::End);
675 datastore.commitChanges();
676 expected = {
677 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
678 {"/example-schema:protocols[.='ftp']", "ftp"s},
679 {"/example-schema:protocols[.='pop3']", "pop3"s},
680 {"/example-schema:protocols[.='http']", "http"s},
681 };
682 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
683 }
684
685 SECTION("after")
686 {
687 sourcePath = "/example-schema:protocols[.='http']";
Václav Kubernáteaf56682021-02-22 17:15:41 +0100688 REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "ftp"s, "http"s));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200689 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{".", "ftp"s}}});
690 datastore.commitChanges();
691 expected = {
692 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
693 {"/example-schema:protocols[.='ftp']", "ftp"s},
694 {"/example-schema:protocols[.='http']", "http"s},
695 {"/example-schema:protocols[.='pop3']", "pop3"s},
696 };
697 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
698 }
699
700 SECTION("before")
701 {
702 sourcePath = "/example-schema:protocols[.='http']";
Václav Kubernáteaf56682021-02-22 17:15:41 +0100703 REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "ftp"s, "http"s));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200704 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{".", "pop3"s}}});
705 datastore.commitChanges();
706 expected = {
707 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
708 {"/example-schema:protocols[.='ftp']", "ftp"s},
709 {"/example-schema:protocols[.='http']", "http"s},
710 {"/example-schema:protocols[.='pop3']", "pop3"s},
711 };
712 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
713 }
714 }
715
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200716 SECTION("moving non-existing schema nodes")
717 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100718 catching<OnInvalidSchemaPathMove>([&] {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200719 datastore.moveItem("/example-schema:non-existing", yang::move::Absolute::Begin);
720 datastore.commitChanges();
721 });
722 }
723
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200724 SECTION("moving list instances")
725 {
726 DatastoreAccess::Tree expected;
727 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100728 REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']", std::nullopt, ""s));
729 REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']/name", std::nullopt, "John"s));
730 REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Eve']", ""s, ""s));
731 REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Eve']/name", std::nullopt, "Eve"s));
732 REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Adam']", ""s, ""s));
733 REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Adam']/name", std::nullopt, "Adam"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200734 datastore.createItem("/example-schema:players[name='John']");
735 datastore.createItem("/example-schema:players[name='Eve']");
736 datastore.createItem("/example-schema:players[name='Adam']");
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200737 datastore.commitChanges();
738 expected = {
739 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
740 {"/example-schema:players[name='John']/name", "John"s},
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 };
746 REQUIRE(datastore.getItems("/example-schema:players") == expected);
747 }
748
749 std::string sourcePath;
750 SECTION("begin")
751 {
752 sourcePath = "/example-schema:players[name='Adam']";
Václav Kubernáteaf56682021-02-22 17:15:41 +0100753 REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Adam']", std::nullopt, ""s));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200754 datastore.moveItem(sourcePath, yang::move::Absolute::Begin);
755 datastore.commitChanges();
756 expected = {
757 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
758 {"/example-schema:players[name='Adam']/name", "Adam"s},
759 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
760 {"/example-schema:players[name='John']/name", "John"s},
761 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
762 {"/example-schema:players[name='Eve']/name", "Eve"s},
763 };
764 REQUIRE(datastore.getItems("/example-schema:players") == expected);
765 }
766
767 SECTION("end")
768 {
769 sourcePath = "/example-schema:players[name='John']";
Václav Kubernáteaf56682021-02-22 17:15:41 +0100770 REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']", ""s, ""s));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200771 datastore.moveItem(sourcePath, yang::move::Absolute::End);
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='Adam']", special_{SpecialValue::List}},
777 {"/example-schema:players[name='Adam']/name", "Adam"s},
778 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
779 {"/example-schema:players[name='John']/name", "John"s},
780 };
781 REQUIRE(datastore.getItems("/example-schema:players") == expected);
782 }
783
784 SECTION("after")
785 {
786 sourcePath = "/example-schema:players[name='John']";
Václav Kubernáteaf56682021-02-22 17:15:41 +0100787 REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']", ""s, ""s));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200788 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{"name", "Eve"s}}});
789 datastore.commitChanges();
790 expected = {
791 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
792 {"/example-schema:players[name='Eve']/name", "Eve"s},
793 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
794 {"/example-schema:players[name='John']/name", "John"s},
795 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
796 {"/example-schema:players[name='Adam']/name", "Adam"s},
797 };
798 REQUIRE(datastore.getItems("/example-schema:players") == expected);
799 }
800
801 SECTION("before")
802 {
803 sourcePath = "/example-schema:players[name='John']";
Václav Kubernáteaf56682021-02-22 17:15:41 +0100804 REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']", ""s, ""s));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200805 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{"name", "Adam"s}}});
806 datastore.commitChanges();
807 expected = {
808 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
809 {"/example-schema:players[name='Eve']/name", "Eve"s},
810 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
811 {"/example-schema:players[name='John']/name", "John"s},
812 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
813 {"/example-schema:players[name='Adam']/name", "Adam"s},
814 };
815 REQUIRE(datastore.getItems("/example-schema:players") == expected);
816 }
817 }
818
Václav Kubernátfa96ac22020-06-18 17:03:52 +0200819 SECTION("getting /")
820 {
821 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100822 REQUIRE_CALL(mockRunning, write("/example-schema:leafInt32", std::nullopt, "64"s));
Václav Kubernátfa96ac22020-06-18 17:03:52 +0200823 datastore.setLeaf("/example-schema:leafInt32", 64);
824 datastore.commitChanges();
825 }
826
827 DatastoreAccess::Tree expected{
Václav Kubernát654303f2020-07-31 13:16:54 +0200828 {"/example-schema:leafInt32", 64}
829 };
Václav Kubernátfa96ac22020-06-18 17:03:52 +0200830 // This tests if we at least get the data WE added.
Václav Kubernát654303f2020-07-31 13:16:54 +0200831 REQUIRE(std::all_of(expected.begin(), expected.end(), [items = datastore.getItems("/")] (const auto& item) {
832 return std::find(items.begin(), items.end(), item) != items.end();
833 }));
Václav Kubernátfa96ac22020-06-18 17:03:52 +0200834 }
835
Václav Kubernát36986c52020-06-25 10:30:05 +0200836 SECTION("setting and removing without commit")
837 {
838 datastore.setLeaf("/example-schema:leafInt32", 64);
839 datastore.deleteItem("/example-schema:leafInt32");
840 }
841
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200842 SECTION("two key lists")
843 {
Václav Kubernáteaf56682021-02-22 17:15:41 +0100844 REQUIRE_CALL(mockRunning, write("/example-schema:point[x='12'][y='10']", std::nullopt, ""s));
845 REQUIRE_CALL(mockRunning, write("/example-schema:point[x='12'][y='10']/x", std::nullopt, "12"s));
846 REQUIRE_CALL(mockRunning, write("/example-schema:point[x='12'][y='10']/y", std::nullopt, "10"s));
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200847 datastore.createItem("/example-schema:point[x='12'][y='10']");
848 datastore.commitChanges();
849 REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:point") != std::string::npos);
850 }
851
Václav Kubernát73109382018-09-14 19:52:03 +0200852 waitForCompletionAndBitMore(seq1);
853}
Jan Kundrát6ee84792020-01-24 01:43:36 +0100854
Václav Kubernát654303f2020-07-31 13:16:54 +0200855struct ActionCb {
856 int operator()(sysrepo::S_Session session,
857 const char* xpath,
858 [[maybe_unused]] const sysrepo::S_Vals input,
859 [[maybe_unused]] sr_event_t event,
860 [[maybe_unused]] uint32_t request_id,
861 sysrepo::S_Vals_Holder output)
Václav Kubernáta8789602020-07-20 15:18:19 +0200862 {
Václav Kubernát654303f2020-07-31 13:16:54 +0200863 if (session->get_context()->get_node(nullptr, xpath)->path(LYS_PATH_FIRST_PREFIX) == "/example-schema:ports/shutdown") {
Václav Kubernáta8789602020-07-20 15:18:19 +0200864 auto buf = output->allocate(1);
865 buf->val(0)->set(joinPaths(xpath, "success").c_str(), true);
866 return SR_ERR_OK;
867 }
868 throw std::runtime_error("unrecognized RPC");
869 }
Václav Kubernát654303f2020-07-31 13:16:54 +0200870};
Václav Kubernáta8789602020-07-20 15:18:19 +0200871
Václav Kubernát654303f2020-07-31 13:16:54 +0200872struct RpcCb {
873 int operator()([[maybe_unused]] sysrepo::S_Session session,
874 const char* xpath,
875 const sysrepo::S_Vals input,
876 [[maybe_unused]] sr_event_t event,
877 [[maybe_unused]] uint32_t request_id,
878 sysrepo::S_Vals_Holder output)
Jan Kundrát6ee84792020-01-24 01:43:36 +0100879 {
880 const auto nukes = "/example-schema:launch-nukes"s;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200881 if (xpath == "/example-schema:noop"s || xpath == "/example-schema:fire"s) {
Jan Kundrát6ee84792020-01-24 01:43:36 +0100882 return SR_ERR_OK;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200883 }
884
885 if (xpath == nukes) {
Jan Kundrát6ee84792020-01-24 01:43:36 +0100886 uint64_t kilotons = 0;
887 bool hasCities = false;
888 for (size_t i = 0; i < input->val_cnt(); ++i) {
889 const auto& val = input->val(i);
Václav Kubernátf4b6a932020-07-09 10:34:19 +0200890 if (val->xpath() == nukes + "/payload") {
891 continue; // ignore, container
892 }
893 if (val->xpath() == nukes + "/description") {
894 continue; // unused
895 }
896
Jan Kundrát6ee84792020-01-24 01:43:36 +0100897 if (val->xpath() == nukes + "/payload/kilotons") {
898 kilotons = val->data()->get_uint64();
Jan Kundrát6ee84792020-01-24 01:43:36 +0100899 } else if (std::string_view{val->xpath()}.find(nukes + "/cities") == 0) {
900 hasCities = true;
901 } else {
902 throw std::runtime_error("RPC launch-nukes: unexpected input "s + val->xpath());
903 }
904 }
905 if (kilotons == 333'666) {
906 // magic, just do not generate any output. This is important because the NETCONF RPC returns just <ok/>.
907 return SR_ERR_OK;
908 }
909 auto buf = output->allocate(2);
910 size_t i = 0;
911 buf->val(i++)->set((nukes + "/blast-radius").c_str(), uint32_t{33'666});
912 buf->val(i++)->set((nukes + "/actual-yield").c_str(), static_cast<uint64_t>(1.33 * kilotons));
913 if (hasCities) {
914 buf = output->reallocate(output->val_cnt() + 2);
915 buf->val(i++)->set((nukes + "/damaged-places/targets[city='London']/city").c_str(), "London");
916 buf->val(i++)->set((nukes + "/damaged-places/targets[city='Berlin']/city").c_str(), "Berlin");
917 }
918 return SR_ERR_OK;
919 }
920 throw std::runtime_error("unrecognized RPC");
921 }
922};
923
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100924TEST_CASE("rpc/action")
925{
Jan Kundrát6ee84792020-01-24 01:43:36 +0100926 trompeloeil::sequence seq1;
Jan Kundrát6ee84792020-01-24 01:43:36 +0100927
928#ifdef sysrepo_BACKEND
Václav Kubernátf5d75152020-12-03 03:52:34 +0100929 auto datastore = std::make_shared<SysrepoAccess>();
Jan Kundrát6ee84792020-01-24 01:43:36 +0100930#elif defined(netconf_BACKEND)
Václav Kubernátd1beedc2020-09-07 12:09:05 +0200931 const auto NETOPEER_SOCKET = getenv("NETOPEER_SOCKET");
932 auto datastore = std::make_shared<NetconfAccess>(NETOPEER_SOCKET);
Václav Kubernát74487df2020-06-04 01:29:28 +0200933#elif defined(yang_BACKEND)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200934 auto datastore = std::make_shared<YangAccess>();
935 datastore->addSchemaDir(schemaDir);
936 datastore->addSchemaFile(exampleSchemaFile);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100937#else
938#error "Unknown backend"
939#endif
940
Václav Kubernát654303f2020-07-31 13:16:54 +0200941 auto srConn = std::make_shared<sysrepo::Connection>();
Václav Kubernáta8789602020-07-20 15:18:19 +0200942 auto srSession = std::make_shared<sysrepo::Session>(srConn);
943 auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSession);
Václav Kubernát654303f2020-07-31 13:16:54 +0200944 auto rpcCb = std::make_shared<RpcCb>();
945 auto actionCb = std::make_shared<ActionCb>();
Václav Kubernáta8789602020-07-20 15:18:19 +0200946 sysrepo::Logs{}.set_stderr(SR_LL_INF);
Václav Kubernát654303f2020-07-31 13:16:54 +0200947 SysrepoSubscription subscription("example-schema", nullptr);
Václav Kubernáta8789602020-07-20 15:18:19 +0200948 // careful here, sysrepo insists on module_change CBs being registered before RPC CBs, otherwise there's a memleak
Václav Kubernát654303f2020-07-31 13:16:54 +0200949 srSubscription->rpc_subscribe("/example-schema:noop", RpcCb{}, 0, SR_SUBSCR_CTX_REUSE);
950 srSubscription->rpc_subscribe("/example-schema:launch-nukes", RpcCb{}, 0, SR_SUBSCR_CTX_REUSE);
951 srSubscription->rpc_subscribe("/example-schema:fire", RpcCb{}, 0, SR_SUBSCR_CTX_REUSE);
952 srSubscription->rpc_subscribe("/example-schema:ports/shutdown", ActionCb{}, 0, SR_SUBSCR_CTX_REUSE);
Václav Kubernáte7248b22020-06-26 15:38:59 +0200953
Václav Kubernáta8789602020-07-20 15:18:19 +0200954 SECTION("rpc")
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200955 {
Václav Kubernáta8789602020-07-20 15:18:19 +0200956 auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>& datastore) {
957 return std::make_shared<YangAccess>(std::static_pointer_cast<YangSchema>(datastore->schema()));
958 };
959 ProxyDatastore proxyDatastore(datastore, createTemporaryDatastore);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100960
Václav Kubernáta8789602020-07-20 15:18:19 +0200961 // ProxyDatastore cannot easily read DatastoreAccess::Tree, so we need to set the input via create/setLeaf/etc.
962 SECTION("valid")
963 {
964 std::string rpc;
965 DatastoreAccess::Tree input, output;
966
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100967 SECTION("noop")
968 {
Václav Kubernáta8789602020-07-20 15:18:19 +0200969 rpc = "/example-schema:noop";
Václav Kubernátb3960f82020-12-01 03:21:48 +0100970 proxyDatastore.initiate(rpc);
Václav Kubernáta8789602020-07-20 15:18:19 +0200971 }
972
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100973 SECTION("small nuke")
974 {
Václav Kubernáta8789602020-07-20 15:18:19 +0200975 rpc = "/example-schema:launch-nukes";
976 input = {
Václav Kubernát94bb7cf2021-02-03 09:59:39 +0100977 {joinPaths(rpc, "description"), "dummy"s},
978 {joinPaths(rpc, "payload/kilotons"), uint64_t{333'666}},
Václav Kubernáta8789602020-07-20 15:18:19 +0200979 };
Václav Kubernátb3960f82020-12-01 03:21:48 +0100980 proxyDatastore.initiate(rpc);
Václav Kubernáta8789602020-07-20 15:18:19 +0200981 proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{333'666});
982 // no data are returned
983 }
984
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100985 SECTION("small nuke")
986 {
Václav Kubernáta8789602020-07-20 15:18:19 +0200987 rpc = "/example-schema:launch-nukes";
988 input = {
Václav Kubernát94bb7cf2021-02-03 09:59:39 +0100989 {joinPaths(rpc, "description"), "dummy"s},
990 {joinPaths(rpc, "payload/kilotons"), uint64_t{4}},
Václav Kubernáta8789602020-07-20 15:18:19 +0200991 };
Václav Kubernátb3960f82020-12-01 03:21:48 +0100992 proxyDatastore.initiate(rpc);
Václav Kubernáta8789602020-07-20 15:18:19 +0200993 proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{4});
994
995 output = {
996 {"blast-radius", uint32_t{33'666}},
997 {"actual-yield", uint64_t{5}},
998 };
999 }
1000
Václav Kubernátb4e5b182020-11-16 19:55:09 +01001001 SECTION("with lists")
1002 {
Václav Kubernáta8789602020-07-20 15:18:19 +02001003 rpc = "/example-schema:launch-nukes";
1004 input = {
Václav Kubernát94bb7cf2021-02-03 09:59:39 +01001005 {joinPaths(rpc, "payload/kilotons"), uint64_t{6}},
1006 {joinPaths(rpc, "cities/targets[city='Prague']/city"), "Prague"s},
Václav Kubernáta8789602020-07-20 15:18:19 +02001007 };
Václav Kubernátb3960f82020-12-01 03:21:48 +01001008 proxyDatastore.initiate(rpc);
Václav Kubernáta8789602020-07-20 15:18:19 +02001009 proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{6});
1010 proxyDatastore.createItem("/example-schema:launch-nukes/example-schema:cities/example-schema:targets[city='Prague']");
1011 output = {
1012 {"blast-radius", uint32_t{33'666}},
1013 {"actual-yield", uint64_t{7}},
1014 {"damaged-places", special_{SpecialValue::PresenceContainer}},
1015 {"damaged-places/targets[city='London']", special_{SpecialValue::List}},
1016 {"damaged-places/targets[city='London']/city", "London"s},
1017 {"damaged-places/targets[city='Berlin']", special_{SpecialValue::List}},
1018 {"damaged-places/targets[city='Berlin']/city", "Berlin"s},
1019 };
1020 }
1021
Václav Kubernátb4e5b182020-11-16 19:55:09 +01001022 SECTION("with leafref")
1023 {
Václav Kubernáta8789602020-07-20 15:18:19 +02001024 datastore->createItem("/example-schema:person[name='Colton']");
1025 datastore->commitChanges();
1026
1027 rpc = "/example-schema:fire";
1028 input = {
Václav Kubernát94bb7cf2021-02-03 09:59:39 +01001029 {joinPaths(rpc, "whom"), "Colton"s}
Václav Kubernáta8789602020-07-20 15:18:19 +02001030 };
Václav Kubernátb3960f82020-12-01 03:21:48 +01001031 proxyDatastore.initiate(rpc);
Václav Kubernáta8789602020-07-20 15:18:19 +02001032 proxyDatastore.setLeaf("/example-schema:fire/example-schema:whom", "Colton"s);
1033 }
1034
Václav Kubernátb3960f82020-12-01 03:21:48 +01001035 catching<OnExec>([&] { REQUIRE(datastore->execute(rpc, input) == output); });
Václav Kubernátb4e5b182020-11-16 19:55:09 +01001036 catching<OnExec>([&] { REQUIRE(proxyDatastore.execute() == output); });
Jan Kundrát7ec214d2020-06-19 17:05:07 +02001037 }
1038
Václav Kubernáta8789602020-07-20 15:18:19 +02001039 SECTION("non-existing RPC")
1040 {
Václav Kubernátb3960f82020-12-01 03:21:48 +01001041 catching<OnInvalidRpcPath>([&] { datastore->execute("/example-schema:non-existing", DatastoreAccess::Tree{}); });
Jan Kundrát7ec214d2020-06-19 17:05:07 +02001042 }
Václav Kubernát2edfe542021-02-03 08:08:29 +01001043
1044 SECTION("invalid RPC exec resets temporary datastore")
1045 {
1046 proxyDatastore.initiate("/example-schema:setIp");
1047 catching<OnInvalidRpcInput>([&] { auto output = proxyDatastore.execute(); });
1048 REQUIRE(proxyDatastore.inputDatastorePath() == std::nullopt);
1049 }
Jan Kundrát6ee84792020-01-24 01:43:36 +01001050 }
1051
Václav Kubernáta8789602020-07-20 15:18:19 +02001052 SECTION("action")
Jan Kundrát7ec214d2020-06-19 17:05:07 +02001053 {
Václav Kubernát94bb7cf2021-02-03 09:59:39 +01001054 auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>& datastore) {
1055 return std::make_shared<YangAccess>(std::static_pointer_cast<YangSchema>(datastore->schema()));
1056 };
1057 ProxyDatastore proxyDatastore(datastore, createTemporaryDatastore);
Václav Kubernáta8789602020-07-20 15:18:19 +02001058 std::string path;
1059 DatastoreAccess::Tree input, output;
1060
1061 output = {
Václav Kubernáta8789602020-07-20 15:18:19 +02001062 {"success", true}
1063 };
1064 datastore->createItem("/example-schema:ports[name='A']");
1065 datastore->commitChanges();
Václav Kubernátb4e5b182020-11-16 19:55:09 +01001066 SECTION("shutdown")
1067 {
Václav Kubernát94bb7cf2021-02-03 09:59:39 +01001068 path = "/example-schema:ports[name='A']/example-schema:shutdown";
1069 input = {
1070 {"/example-schema:ports[name='A']/shutdown/force", true}
1071 };
1072 proxyDatastore.initiate(path);
1073 proxyDatastore.setLeaf("/example-schema:ports[name='A']/example-schema:shutdown/example-schema:force", true);
1074
Václav Kubernáta8789602020-07-20 15:18:19 +02001075 }
1076
Václav Kubernátb3960f82020-12-01 03:21:48 +01001077 catching<OnExec>([&] { REQUIRE(datastore->execute(path, input) == output); });
Václav Kubernát94bb7cf2021-02-03 09:59:39 +01001078 catching<OnExec>([&] { REQUIRE(proxyDatastore.execute() == output); });
Jan Kundrát6ee84792020-01-24 01:43:36 +01001079 }
1080
Jan Kundrát6ee84792020-01-24 01:43:36 +01001081 waitForCompletionAndBitMore(seq1);
1082}
Václav Kubernátf5d75152020-12-03 03:52:34 +01001083
1084#if not defined(yang_BACKEND)
1085TEST_CASE("datastore targets")
1086{
1087 const auto testNode = "/example-schema:leafInt32";
1088 {
1089 auto conn = std::make_shared<sysrepo::Connection>();
1090 auto sess = std::make_shared<sysrepo::Session>(conn);
1091 sess->delete_item(testNode);
1092 sess->apply_changes(1000, 1);
1093 sess->session_switch_ds(SR_DS_STARTUP);
1094 sess->delete_item(testNode);
1095 sess->apply_changes(1000, 1);
1096 }
1097 MockRecorder mockRunning;
1098 MockRecorder mockStartup;
1099
1100#ifdef sysrepo_BACKEND
1101 SysrepoAccess datastore;
1102#elif defined(netconf_BACKEND)
1103 const auto NETOPEER_SOCKET = getenv("NETOPEER_SOCKET");
1104 NetconfAccess datastore(NETOPEER_SOCKET);
1105#else
1106#error "Unknown backend"
1107#endif
1108
1109 auto testGetItems = [&datastore] (const auto& path, const DatastoreAccess::Tree& expected) {
1110 REQUIRE(datastore.getItems(path) == expected);
1111 };
1112
1113 SECTION("subscriptions change operational target")
1114 {
1115 // Default target is operational, so setting a value and reading it should return no values as there are no
1116 // subscriptions yet.
1117 datastore.setLeaf(testNode, 10);
1118 datastore.commitChanges();
1119 testGetItems(testNode, {});
1120
1121 // Now we create a subscription and try again.
1122 SysrepoSubscription subRunning("example-schema", &mockRunning);
1123 testGetItems(testNode, {{testNode, 10}});
1124 }
1125
1126 SECTION("running shows stuff even without subscriptions")
1127 {
1128 datastore.setTarget(DatastoreTarget::Running);
1129 datastore.setLeaf(testNode, 10);
1130 datastore.commitChanges();
1131 testGetItems(testNode, {{testNode, 10}});
1132
1133 // Actually creating a subscription shouldn't make a difference.
1134 SysrepoSubscription subRunning("example-schema", &mockRunning);
1135 testGetItems(testNode, {{testNode, 10}});
1136 }
1137
1138 SECTION("startup changes only affect startup")
1139 {
1140 datastore.setTarget(DatastoreTarget::Startup);
1141 datastore.setLeaf(testNode, 10);
1142 datastore.commitChanges();
1143 testGetItems(testNode, {{testNode, 10}});
1144 datastore.setTarget(DatastoreTarget::Running);
1145 testGetItems(testNode, {});
1146 datastore.setTarget(DatastoreTarget::Operational);
1147 testGetItems(testNode, {});
1148 }
1149
1150}
1151#endif