Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 1 | /* |
| 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át | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 9 | #include "trompeloeil_doctest.hpp" |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 10 | #include <sysrepo-cpp/Session.hpp> |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 11 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 12 | #ifdef sysrepo_BACKEND |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 13 | #include "sysrepo_access.hpp" |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 14 | using OnInvalidSchemaPathCreate = DatastoreException; |
| 15 | using OnInvalidSchemaPathDelete = void; |
| 16 | using OnInvalidSchemaPathMove = sysrepo::sysrepo_exception; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 17 | using OnInvalidRpcPath = sysrepo::sysrepo_exception; |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 18 | using OnKeyNotFound = void; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 19 | using OnRPC = void; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 20 | #elif defined(netconf_BACKEND) |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 21 | using OnInvalidSchemaPathCreate = std::runtime_error; |
| 22 | using OnInvalidSchemaPathDelete = std::runtime_error; |
| 23 | using OnInvalidSchemaPathMove = std::runtime_error; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 24 | using OnInvalidRpcPath = std::runtime_error; |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 25 | using OnKeyNotFound = std::runtime_error; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 26 | using OnRPC = void; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 27 | #include "netconf_access.hpp" |
| 28 | #include "netopeer_vars.hpp" |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 29 | #elif defined(yang_BACKEND) |
| 30 | #include <fstream> |
| 31 | #include "yang_access.hpp" |
| 32 | #include "yang_access_test_vars.hpp" |
| 33 | using OnInvalidSchemaPathCreate = DatastoreException; |
| 34 | using OnInvalidSchemaPathDelete = DatastoreException; |
| 35 | using OnInvalidSchemaPathMove = DatastoreException; |
| 36 | using OnInvalidRpcPath = DatastoreException; |
| 37 | using OnKeyNotFound = DatastoreException; |
| 38 | using OnRPC = std::logic_error; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 39 | #else |
| 40 | #error "Unknown backend" |
| 41 | #endif |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 42 | #include "pretty_printers.hpp" |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 43 | #include "sysrepo_subscription.hpp" |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 44 | #include "utils.hpp" |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 45 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 46 | using namespace std::literals::string_literals; |
| 47 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 48 | class MockRecorder : public trompeloeil::mock_interface<Recorder> { |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 49 | public: |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 50 | IMPLEMENT_MOCK3(write); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 53 | class MockDataSupplier : public trompeloeil::mock_interface<DataSupplier> { |
| 54 | public: |
| 55 | IMPLEMENT_CONST_MOCK1(get_data); |
| 56 | }; |
| 57 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 58 | namespace { |
| 59 | template <class ...> constexpr std::false_type always_false [[maybe_unused]] {}; |
| 60 | template <class Exception, typename Callable> void catching(const Callable& what) { |
| 61 | |
| 62 | if constexpr (std::is_same_v<Exception, void>) { |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 63 | what(); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 64 | } else if constexpr (std::is_same<Exception, std::runtime_error>()) { |
| 65 | // cannot use REQUIRE_THROWS_AS(..., Exception) directly because that one |
| 66 | // needs an extra `typename` deep in the bowels of doctest |
| 67 | REQUIRE_THROWS_AS(what(), std::runtime_error); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 68 | } else if constexpr (std::is_same<Exception, std::logic_error>()) { |
| 69 | REQUIRE_THROWS_AS(what(), std::logic_error); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 70 | } else if constexpr (std::is_same<Exception, DatastoreException>()) { |
| 71 | REQUIRE_THROWS_AS(what(), DatastoreException); |
| 72 | } else if constexpr (std::is_same<Exception, sysrepo::sysrepo_exception>()) { |
| 73 | REQUIRE_THROWS_AS(what(), sysrepo::sysrepo_exception); |
| 74 | } else { |
| 75 | static_assert(always_false<Exception>); // https://stackoverflow.com/a/53945549/2245623 |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 76 | } |
| 77 | } |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 78 | } |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 79 | |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 80 | #if defined(yang_BACKEND) |
| 81 | class TestYangAccess : public YangAccess { |
| 82 | public: |
| 83 | void commitChanges() override |
| 84 | { |
| 85 | YangAccess::commitChanges(); |
| 86 | dumpToSysrepo(); |
| 87 | } |
| 88 | |
| 89 | void copyConfig(const Datastore source, const Datastore destination) override |
| 90 | { |
| 91 | YangAccess::copyConfig(source, destination); |
| 92 | dumpToSysrepo(); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | void dumpToSysrepo() |
| 97 | { |
| 98 | { |
| 99 | std::ofstream of(testConfigFile); |
| 100 | of << dumpXML(); |
| 101 | } |
| 102 | auto command = std::string(sysrepocfgExecutable) + " --import=" + testConfigFile + " --format=xml --datastore=running example-schema"; |
| 103 | REQUIRE(std::system(command.c_str()) == 0); |
| 104 | } |
| 105 | }; |
| 106 | #endif |
| 107 | |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 108 | TEST_CASE("setting/getting values") |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 109 | { |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 110 | trompeloeil::sequence seq1; |
| 111 | MockRecorder mock; |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 112 | SysrepoSubscription subscription("example-schema", &mock); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 113 | |
| 114 | #ifdef sysrepo_BACKEND |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 115 | SysrepoAccess datastore("netconf-cli-test", Datastore::Running); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 116 | #elif defined(netconf_BACKEND) |
| 117 | NetconfAccess datastore(NETOPEER_SOCKET_PATH); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 118 | #elif defined(yang_BACKEND) |
| 119 | TestYangAccess datastore; |
| 120 | datastore.addSchemaDir(schemaDir); |
| 121 | datastore.addSchemaFile(exampleSchemaFile); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 122 | #else |
| 123 | #error "Unknown backend" |
| 124 | #endif |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 125 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 126 | |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 127 | SECTION("set leafInt8 to -128") |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 128 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 129 | REQUIRE_CALL(mock, write("/example-schema:leafInt8", std::nullopt, "-128"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 130 | datastore.setLeaf("/example-schema:leafInt8", int8_t{-128}); |
| 131 | datastore.commitChanges(); |
| 132 | } |
| 133 | |
| 134 | SECTION("set leafInt16 to -32768") |
| 135 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 136 | REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "-32768"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 137 | datastore.setLeaf("/example-schema:leafInt16", int16_t{-32768}); |
| 138 | datastore.commitChanges(); |
| 139 | } |
| 140 | |
| 141 | SECTION("set leafInt32 to -2147483648") |
| 142 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 143 | REQUIRE_CALL(mock, write("/example-schema:leafInt32", std::nullopt, "-2147483648"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 144 | datastore.setLeaf("/example-schema:leafInt32", int32_t{-2147483648}); |
| 145 | datastore.commitChanges(); |
| 146 | } |
| 147 | |
| 148 | SECTION("set leafInt64 to -50000000000") |
| 149 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 150 | REQUIRE_CALL(mock, write("/example-schema:leafInt64", std::nullopt, "-50000000000"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 151 | datastore.setLeaf("/example-schema:leafInt64", int64_t{-50000000000}); |
| 152 | datastore.commitChanges(); |
| 153 | } |
| 154 | |
| 155 | SECTION("set leafUInt8 to 255") |
| 156 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 157 | REQUIRE_CALL(mock, write("/example-schema:leafUInt8", std::nullopt, "255"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 158 | datastore.setLeaf("/example-schema:leafUInt8", uint8_t{255}); |
| 159 | datastore.commitChanges(); |
| 160 | } |
| 161 | |
| 162 | SECTION("set leafUInt16 to 65535") |
| 163 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 164 | REQUIRE_CALL(mock, write("/example-schema:leafUInt16", std::nullopt, "65535"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 165 | datastore.setLeaf("/example-schema:leafUInt16", uint16_t{65535}); |
| 166 | datastore.commitChanges(); |
| 167 | } |
| 168 | |
| 169 | SECTION("set leafUInt32 to 4294967295") |
| 170 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 171 | REQUIRE_CALL(mock, write("/example-schema:leafUInt32", std::nullopt, "4294967295"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 172 | datastore.setLeaf("/example-schema:leafUInt32", uint32_t{4294967295}); |
| 173 | datastore.commitChanges(); |
| 174 | } |
| 175 | |
| 176 | SECTION("set leafUInt64 to 50000000000") |
| 177 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 178 | REQUIRE_CALL(mock, write("/example-schema:leafUInt64", std::nullopt, "50000000000"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 179 | datastore.setLeaf("/example-schema:leafUInt64", uint64_t{50000000000}); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 180 | datastore.commitChanges(); |
| 181 | } |
| 182 | |
| 183 | SECTION("set leafEnum to coze") |
| 184 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 185 | REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "coze"s)); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 186 | datastore.setLeaf("/example-schema:leafEnum", enum_{"coze"}); |
| 187 | datastore.commitChanges(); |
| 188 | } |
| 189 | |
| 190 | SECTION("set leafDecimal to 123.544") |
| 191 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 192 | REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.544"s)); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 193 | datastore.setLeaf("/example-schema:leafDecimal", 123.544); |
| 194 | datastore.commitChanges(); |
| 195 | } |
| 196 | |
Jan Kundrát | d287286 | 2020-06-18 21:02:00 +0200 | [diff] [blame] | 197 | SECTION("set a string, then delete it") |
| 198 | { |
| 199 | REQUIRE_CALL(mock, write("/example-schema:leafString", std::nullopt, "blah"s)); |
| 200 | datastore.setLeaf("/example-schema:leafString", "blah"s); |
| 201 | datastore.commitChanges(); |
| 202 | DatastoreAccess::Tree expected{{"/example-schema:leafString", "blah"s}}; |
| 203 | REQUIRE(datastore.getItems("/example-schema:leafString") == expected); |
| 204 | |
| 205 | REQUIRE_CALL(mock, write("/example-schema:leafString", "blah"s, std::nullopt)); |
| 206 | datastore.deleteItem("/example-schema:leafString"); |
| 207 | datastore.commitChanges(); |
| 208 | expected.clear(); |
| 209 | REQUIRE(datastore.getItems("/example-schema:leafString") == expected); |
| 210 | } |
| 211 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 212 | SECTION("set a non-existing leaf") |
| 213 | { |
| 214 | catching<OnInvalidSchemaPathCreate>([&]{ |
| 215 | datastore.setLeaf("/example-schema:non-existing", "what"s); |
| 216 | }); |
| 217 | } |
| 218 | |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 219 | SECTION("create presence container") |
| 220 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 221 | REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 222 | datastore.createItem("/example-schema:pContainer"); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 223 | datastore.commitChanges(); |
| 224 | } |
| 225 | |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 226 | SECTION("create/delete a list instance") |
Václav Kubernát | 45f4a82 | 2019-05-29 21:10:50 +0200 | [diff] [blame] | 227 | { |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 228 | { |
| 229 | REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", std::nullopt, ""s)); |
| 230 | REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']/name", std::nullopt, "Nguyen"s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 231 | datastore.createItem("/example-schema:person[name='Nguyen']"); |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 232 | datastore.commitChanges(); |
| 233 | } |
| 234 | { |
| 235 | REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", ""s, std::nullopt)); |
| 236 | REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']/name", "Nguyen"s, std::nullopt)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 237 | datastore.deleteItem("/example-schema:person[name='Nguyen']"); |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 238 | datastore.commitChanges(); |
| 239 | } |
Václav Kubernát | 45f4a82 | 2019-05-29 21:10:50 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 242 | SECTION("deleting non-existing list keys") |
| 243 | { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 244 | catching<OnKeyNotFound>([&]{ |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 245 | datastore.deleteItem("/example-schema:person[name='non existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 246 | datastore.commitChanges(); |
| 247 | }); |
| 248 | } |
| 249 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 250 | SECTION("accessing non-existing schema nodes as a list") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 251 | { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 252 | catching<OnInvalidSchemaPathCreate>([&]{ |
| 253 | datastore.createItem("/example-schema:non-existing-list[xxx='blah']"); |
| 254 | datastore.commitChanges(); |
| 255 | }); |
| 256 | catching<OnInvalidSchemaPathDelete>([&]{ |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 257 | datastore.deleteItem("/example-schema:non-existing-list[xxx='non existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 258 | datastore.commitChanges(); |
| 259 | }); |
| 260 | } |
| 261 | |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 262 | SECTION("leafref pointing to a key of a list") |
| 263 | { |
| 264 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 265 | REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']", std::nullopt, ""s)); |
| 266 | REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']/name", std::nullopt, "Dan"s)); |
| 267 | REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']", std::nullopt, ""s)); |
| 268 | REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']/name", std::nullopt, "Elfi"s)); |
| 269 | REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']", std::nullopt, ""s)); |
| 270 | REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']/name", std::nullopt, "Kolafa"s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 271 | datastore.createItem("/example-schema:person[name='Dan']"); |
| 272 | datastore.createItem("/example-schema:person[name='Elfi']"); |
| 273 | datastore.createItem("/example-schema:person[name='Kolafa']"); |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 274 | datastore.commitChanges(); |
| 275 | } |
| 276 | |
| 277 | // The commitChanges method has to be called in each of the |
| 278 | // SECTIONs, because the REQUIRE_CALL only works inside the given |
| 279 | // SECTION. |
| 280 | SECTION("Dan") |
| 281 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 282 | REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Dan"s)); |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 283 | datastore.setLeaf("/example-schema:bossPerson", std::string{"Dan"}); |
| 284 | datastore.commitChanges(); |
| 285 | } |
| 286 | |
| 287 | SECTION("Elfi") |
| 288 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 289 | REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Elfi"s)); |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 290 | datastore.setLeaf("/example-schema:bossPerson", std::string{"Elfi"}); |
| 291 | datastore.commitChanges(); |
| 292 | } |
| 293 | |
| 294 | SECTION("Kolafa") |
| 295 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 296 | REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Kolafa"s)); |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 297 | datastore.setLeaf("/example-schema:bossPerson", std::string{"Kolafa"}); |
| 298 | datastore.commitChanges(); |
| 299 | } |
| 300 | } |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 301 | SECTION("bool values get correctly represented as bools") |
| 302 | { |
| 303 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 304 | REQUIRE_CALL(mock, write("/example-schema:down", std::nullopt, "true"s)); |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 305 | datastore.setLeaf("/example-schema:down", bool{true}); |
| 306 | datastore.commitChanges(); |
| 307 | } |
| 308 | |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 309 | DatastoreAccess::Tree expected{{"/example-schema:down", bool{true}}}; |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 310 | REQUIRE(datastore.getItems("/example-schema:down") == expected); |
| 311 | } |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 312 | |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 313 | SECTION("getting items from the whole module") |
| 314 | { |
| 315 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 316 | REQUIRE_CALL(mock, write("/example-schema:up", std::nullopt, "true"s)); |
| 317 | REQUIRE_CALL(mock, write("/example-schema:down", std::nullopt, "false"s)); |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 318 | datastore.setLeaf("/example-schema:up", bool{true}); |
| 319 | datastore.setLeaf("/example-schema:down", bool{false}); |
| 320 | datastore.commitChanges(); |
| 321 | } |
| 322 | |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 323 | DatastoreAccess::Tree expected{ |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 324 | // Sysrepo always returns containers when getting values, but |
| 325 | // libnetconf does not. This is fine by the YANG standard: |
| 326 | // https://tools.ietf.org/html/rfc7950#section-7.5.7 Furthermore, |
| 327 | // NetconfAccess implementation actually only iterates over leafs, |
| 328 | // so even if libnetconf did include containers, they wouldn't get |
| 329 | // shown here anyway. With sysrepo2, this won't be necessary, |
| 330 | // because it'll use the same data structure as libnetconf, so the |
| 331 | // results will be consistent. |
| 332 | #ifdef sysrepo_BACKEND |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 333 | {"/example-schema:inventory", special_{SpecialValue::Container}}, |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 334 | {"/example-schema:lol", special_{SpecialValue::Container}}, |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 335 | #endif |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 336 | {"/example-schema:up", bool{true}}, |
| 337 | {"/example-schema:down", bool{false}}}; |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 338 | REQUIRE(datastore.getItems("/example-schema:*") == expected); |
| 339 | } |
| 340 | |
Václav Kubernát | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 341 | SECTION("getItems returns correct datatypes") |
| 342 | { |
| 343 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 344 | REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "lol"s)); |
Václav Kubernát | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 345 | datastore.setLeaf("/example-schema:leafEnum", enum_{"lol"}); |
| 346 | datastore.commitChanges(); |
| 347 | } |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 348 | DatastoreAccess::Tree expected{{"/example-schema:leafEnum", enum_{"lol"}}}; |
Václav Kubernát | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 349 | |
| 350 | REQUIRE(datastore.getItems("/example-schema:leafEnum") == expected); |
| 351 | } |
| 352 | |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 353 | SECTION("getItems on a list") |
| 354 | { |
| 355 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 356 | REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']", std::nullopt, ""s)); |
| 357 | REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']/name", std::nullopt, "Jan"s)); |
| 358 | REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']", std::nullopt, ""s)); |
| 359 | REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']/name", std::nullopt, "Michal"s)); |
| 360 | REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']", std::nullopt, ""s)); |
| 361 | REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']/name", std::nullopt, "Petr"s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 362 | datastore.createItem("/example-schema:person[name='Jan']"); |
| 363 | datastore.createItem("/example-schema:person[name='Michal']"); |
| 364 | datastore.createItem("/example-schema:person[name='Petr']"); |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 365 | datastore.commitChanges(); |
| 366 | } |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 367 | DatastoreAccess::Tree expected{ |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 368 | {"/example-schema:person[name='Jan']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 369 | {"/example-schema:person[name='Jan']/name", std::string{"Jan"}}, |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 370 | {"/example-schema:person[name='Michal']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 371 | {"/example-schema:person[name='Michal']/name", std::string{"Michal"}}, |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 372 | {"/example-schema:person[name='Petr']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 373 | {"/example-schema:person[name='Petr']/name", std::string{"Petr"}} |
| 374 | }; |
| 375 | |
| 376 | REQUIRE(datastore.getItems("/example-schema:person") == expected); |
| 377 | } |
| 378 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 379 | SECTION("presence containers") |
| 380 | { |
| 381 | DatastoreAccess::Tree expected; |
| 382 | // Make sure it's not there before we create it |
| 383 | REQUIRE(datastore.getItems("/example-schema:pContainer") == expected); |
| 384 | |
| 385 | { |
| 386 | REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 387 | datastore.createItem("/example-schema:pContainer"); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 388 | datastore.commitChanges(); |
| 389 | } |
| 390 | expected = { |
| 391 | {"/example-schema:pContainer", special_{SpecialValue::PresenceContainer}} |
| 392 | }; |
| 393 | REQUIRE(datastore.getItems("/example-schema:pContainer") == expected); |
| 394 | |
| 395 | // Make sure it's not there after we delete it |
| 396 | { |
| 397 | REQUIRE_CALL(mock, write("/example-schema:pContainer", ""s, std::nullopt)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 398 | datastore.deleteItem("/example-schema:pContainer"); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 399 | datastore.commitChanges(); |
| 400 | } |
| 401 | expected = {}; |
| 402 | REQUIRE(datastore.getItems("/example-schema:pContainer") == expected); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 403 | } |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 404 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 405 | SECTION("creating a non-existing schema node as a container") |
| 406 | { |
| 407 | catching<OnInvalidSchemaPathCreate>([&]{ |
| 408 | datastore.createItem("/example-schema:non-existing-presence-container"); |
| 409 | datastore.commitChanges(); |
| 410 | }); |
| 411 | } |
| 412 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 413 | SECTION("deleting a non-existing schema node as a container or leaf") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 414 | { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 415 | catching<OnInvalidSchemaPathDelete>([&]{ |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 416 | datastore.deleteItem("/example-schema:non-existing-presence-container"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 417 | datastore.commitChanges(); |
| 418 | }); |
| 419 | } |
| 420 | |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 421 | SECTION("nested presence container") |
| 422 | { |
| 423 | DatastoreAccess::Tree expected; |
| 424 | // Make sure it's not there before we create it |
| 425 | REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected); |
| 426 | { |
| 427 | REQUIRE_CALL(mock, write("/example-schema:inventory", std::nullopt, ""s)); |
| 428 | REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", std::nullopt, ""s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 429 | datastore.createItem("/example-schema:inventory/stuff"); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 430 | datastore.commitChanges(); |
| 431 | } |
| 432 | expected = { |
| 433 | {"/example-schema:inventory/stuff", special_{SpecialValue::PresenceContainer}} |
| 434 | }; |
| 435 | REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected); |
| 436 | { |
| 437 | REQUIRE_CALL(mock, write("/example-schema:inventory", ""s, std::nullopt)); |
| 438 | REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", ""s, std::nullopt)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 439 | datastore.deleteItem("/example-schema:inventory/stuff"); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 440 | datastore.commitChanges(); |
| 441 | } |
| 442 | expected = {}; |
| 443 | REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 444 | } |
| 445 | |
Jan Kundrát | bd3169c | 2020-02-03 19:31:34 +0100 | [diff] [blame] | 446 | SECTION("floats") |
| 447 | { |
| 448 | datastore.setLeaf("/example-schema:leafDecimal", 123.4); |
| 449 | REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.4"s)); |
| 450 | datastore.commitChanges(); |
| 451 | DatastoreAccess::Tree expected { |
| 452 | {"/example-schema:leafDecimal", 123.4}, |
| 453 | }; |
| 454 | REQUIRE(datastore.getItems("/example-schema:leafDecimal") == expected); |
| 455 | } |
| 456 | |
Jan Kundrát | 3ff5012 | 2020-05-07 00:37:50 +0200 | [diff] [blame] | 457 | SECTION("unions") |
| 458 | { |
| 459 | datastore.setLeaf("/example-schema:unionIntString", int32_t{10}); |
| 460 | REQUIRE_CALL(mock, write("/example-schema:unionIntString", std::nullopt, "10"s)); |
| 461 | datastore.commitChanges(); |
| 462 | DatastoreAccess::Tree expected { |
| 463 | {"/example-schema:unionIntString", int32_t{10}}, |
| 464 | }; |
| 465 | REQUIRE(datastore.getItems("/example-schema:unionIntString") == expected); |
| 466 | } |
| 467 | |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame] | 468 | SECTION("identityref") { |
| 469 | datastore.setLeaf("/example-schema:beast", identityRef_{"example-schema", "Mammal"}); |
| 470 | REQUIRE_CALL(mock, write("/example-schema:beast", std::nullopt, "example-schema:Mammal"s)); |
| 471 | datastore.commitChanges(); |
| 472 | DatastoreAccess::Tree expected { |
| 473 | {"/example-schema:beast", identityRef_{"example-schema", "Mammal"}}, |
| 474 | }; |
| 475 | REQUIRE(datastore.getItems("/example-schema:beast") == expected); |
| 476 | |
| 477 | datastore.setLeaf("/example-schema:beast", identityRef_{"Whale"}); |
| 478 | REQUIRE_CALL(mock, write("/example-schema:beast", "example-schema:Mammal", "example-schema:Whale"s)); |
| 479 | datastore.commitChanges(); |
| 480 | expected = { |
| 481 | {"/example-schema:beast", identityRef_{"example-schema", "Whale"}}, |
| 482 | }; |
| 483 | REQUIRE(datastore.getItems("/example-schema:beast") == expected); |
| 484 | } |
| 485 | |
Jan Kundrát | 6898544 | 2020-05-07 02:15:34 +0200 | [diff] [blame] | 486 | SECTION("binary") |
| 487 | { |
| 488 | datastore.setLeaf("/example-schema:blob", binary_{"cHduegByIQ=="s}); |
| 489 | REQUIRE_CALL(mock, write("/example-schema:blob", std::nullopt, "cHduegByIQ=="s)); |
| 490 | datastore.commitChanges(); |
| 491 | DatastoreAccess::Tree expected { |
| 492 | {"/example-schema:blob", binary_{"cHduegByIQ=="s}}, |
| 493 | }; |
| 494 | REQUIRE(datastore.getItems("/example-schema:blob") == expected); |
| 495 | } |
| 496 | |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 497 | SECTION("empty") |
| 498 | { |
| 499 | datastore.setLeaf("/example-schema:dummy", empty_{}); |
| 500 | REQUIRE_CALL(mock, write("/example-schema:dummy", std::nullopt, ""s)); |
| 501 | datastore.commitChanges(); |
| 502 | DatastoreAccess::Tree expected { |
| 503 | {"/example-schema:dummy", empty_{}}, |
| 504 | }; |
| 505 | REQUIRE(datastore.getItems("/example-schema:dummy") == expected); |
| 506 | } |
| 507 | |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 508 | #if not defined(yang_BACKEND) |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 509 | SECTION("operational data") |
| 510 | { |
| 511 | MockDataSupplier mockOpsData; |
| 512 | OperationalDataSubscription opsDataSub("/example-schema:temperature", mockOpsData); |
| 513 | DatastoreAccess::Tree expected; |
| 514 | std::string xpath; |
| 515 | SECTION("temperature") |
| 516 | { |
| 517 | expected = {{"/example-schema:temperature", int32_t{22}}}; |
| 518 | xpath = "/example-schema:temperature"; |
| 519 | } |
| 520 | |
| 521 | REQUIRE_CALL(mockOpsData, get_data(xpath)).RETURN(expected); |
| 522 | REQUIRE(datastore.getItems(xpath) == expected); |
| 523 | } |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 524 | #endif |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 525 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 526 | SECTION("leaf list") |
| 527 | { |
| 528 | DatastoreAccess::Tree expected; |
| 529 | REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "0.0.0.0"s)); |
| 530 | REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "127.0.0.1"s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 531 | datastore.createItem("/example-schema:addresses[.='0.0.0.0']"); |
| 532 | datastore.createItem("/example-schema:addresses[.='127.0.0.1']"); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 533 | datastore.commitChanges(); |
| 534 | expected = { |
| 535 | {"/example-schema:addresses", special_{SpecialValue::LeafList}}, |
| 536 | {"/example-schema:addresses[.='0.0.0.0']", "0.0.0.0"s}, |
| 537 | {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s}, |
| 538 | }; |
| 539 | REQUIRE(datastore.getItems("/example-schema:addresses") == expected); |
| 540 | |
| 541 | REQUIRE_CALL(mock, write("/example-schema:addresses", "0.0.0.0"s, std::nullopt)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 542 | datastore.deleteItem("/example-schema:addresses[.='0.0.0.0']"); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 543 | datastore.commitChanges(); |
| 544 | expected = { |
| 545 | {"/example-schema:addresses", special_{SpecialValue::LeafList}}, |
| 546 | {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s}, |
| 547 | }; |
| 548 | REQUIRE(datastore.getItems("/example-schema:addresses") == expected); |
| 549 | |
| 550 | REQUIRE_CALL(mock, write("/example-schema:addresses", "127.0.0.1"s, std::nullopt)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 551 | datastore.deleteItem("/example-schema:addresses[.='127.0.0.1']"); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 552 | datastore.commitChanges(); |
| 553 | expected = {}; |
| 554 | REQUIRE(datastore.getItems("/example-schema:addresses") == expected); |
| 555 | } |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 556 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 557 | SECTION("deleting a non-existing leaf-list") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 558 | { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 559 | catching<OnKeyNotFound>([&]{ |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 560 | datastore.deleteItem("/example-schema:addresses[.='non-existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 561 | datastore.commitChanges(); |
| 562 | }); |
| 563 | } |
| 564 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 565 | SECTION("accessing a non-existing schema node as a leaf-list") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 566 | { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 567 | catching<OnInvalidSchemaPathCreate>([&]{ |
| 568 | datastore.createItem("/example-schema:non-existing[.='non-existing']"); |
| 569 | datastore.commitChanges(); |
| 570 | }); |
| 571 | |
| 572 | catching<OnInvalidSchemaPathDelete>([&]{ |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 573 | datastore.deleteItem("/example-schema:non-existing[.='non-existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 574 | datastore.commitChanges(); |
| 575 | }); |
| 576 | } |
| 577 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 578 | SECTION("copying data from startup refreshes the data") |
| 579 | { |
| 580 | { |
| 581 | REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{}); |
| 582 | REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "123"s)); |
| 583 | datastore.setLeaf("/example-schema:leafInt16", int16_t{123}); |
| 584 | datastore.commitChanges(); |
| 585 | } |
| 586 | REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{{"/example-schema:leafInt16", int16_t{123}}}); |
| 587 | REQUIRE_CALL(mock, write("/example-schema:leafInt16", "123"s, std::nullopt)); |
| 588 | datastore.copyConfig(Datastore::Startup, Datastore::Running); |
| 589 | REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{}); |
| 590 | } |
| 591 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 592 | SECTION("moving leaflist instances") |
| 593 | { |
| 594 | DatastoreAccess::Tree expected; |
| 595 | { |
| 596 | // sysrepo does this twice for some reason, it's possibly a bug |
| 597 | REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "http"s)).TIMES(2); |
| 598 | REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "ftp"s)); |
| 599 | REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s)); |
| 600 | REQUIRE_CALL(mock, write("/example-schema:protocols", "http"s, "ftp"s)); |
| 601 | REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "pop3"s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 602 | datastore.createItem("/example-schema:protocols[.='http']"); |
| 603 | datastore.createItem("/example-schema:protocols[.='ftp']"); |
| 604 | datastore.createItem("/example-schema:protocols[.='pop3']"); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 605 | datastore.commitChanges(); |
| 606 | expected = { |
| 607 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 608 | {"/example-schema:protocols[.='http']", "http"s}, |
| 609 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 610 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 611 | }; |
| 612 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 613 | } |
| 614 | |
| 615 | std::string sourcePath; |
| 616 | SECTION("begin") |
| 617 | { |
| 618 | REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s)); |
| 619 | sourcePath = "/example-schema:protocols[.='pop3']"; |
| 620 | datastore.moveItem(sourcePath, yang::move::Absolute::Begin); |
| 621 | datastore.commitChanges(); |
| 622 | expected = { |
| 623 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 624 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 625 | {"/example-schema:protocols[.='http']", "http"s}, |
| 626 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 627 | }; |
| 628 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 629 | } |
| 630 | |
| 631 | SECTION("end") |
| 632 | { |
| 633 | sourcePath = "/example-schema:protocols[.='http']"; |
| 634 | REQUIRE_CALL(mock, write("/example-schema:protocols", "pop3"s, "http"s)); |
| 635 | datastore.moveItem(sourcePath, yang::move::Absolute::End); |
| 636 | datastore.commitChanges(); |
| 637 | expected = { |
| 638 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 639 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 640 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 641 | {"/example-schema:protocols[.='http']", "http"s}, |
| 642 | }; |
| 643 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 644 | } |
| 645 | |
| 646 | SECTION("after") |
| 647 | { |
| 648 | sourcePath = "/example-schema:protocols[.='http']"; |
| 649 | REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "http"s)); |
| 650 | datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{".", "ftp"s}}}); |
| 651 | datastore.commitChanges(); |
| 652 | expected = { |
| 653 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 654 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 655 | {"/example-schema:protocols[.='http']", "http"s}, |
| 656 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 657 | }; |
| 658 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 659 | } |
| 660 | |
| 661 | SECTION("before") |
| 662 | { |
| 663 | sourcePath = "/example-schema:protocols[.='http']"; |
| 664 | REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "http"s)); |
| 665 | datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{".", "pop3"s}}}); |
| 666 | datastore.commitChanges(); |
| 667 | expected = { |
| 668 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 669 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 670 | {"/example-schema:protocols[.='http']", "http"s}, |
| 671 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 672 | }; |
| 673 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 674 | } |
| 675 | } |
| 676 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 677 | SECTION("moving non-existing schema nodes") |
| 678 | { |
| 679 | catching<OnInvalidSchemaPathMove>([&]{ |
| 680 | datastore.moveItem("/example-schema:non-existing", yang::move::Absolute::Begin); |
| 681 | datastore.commitChanges(); |
| 682 | }); |
| 683 | } |
| 684 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 685 | SECTION("moving list instances") |
| 686 | { |
| 687 | DatastoreAccess::Tree expected; |
| 688 | { |
| 689 | // sysrepo does this twice for some reason, it's possibly a bug |
| 690 | REQUIRE_CALL(mock, write("/example-schema:players[name='John']", std::nullopt, ""s)).TIMES(2); |
| 691 | REQUIRE_CALL(mock, write("/example-schema:players[name='John']/name", std::nullopt, "John"s)); |
| 692 | REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']", std::nullopt, ""s)); |
| 693 | REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']", ""s, ""s)); |
| 694 | REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']/name", std::nullopt, "Eve"s)); |
| 695 | REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", std::nullopt, ""s)); |
| 696 | REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']/name", std::nullopt, "Adam"s)); |
| 697 | REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", ""s, ""s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 698 | datastore.createItem("/example-schema:players[name='John']"); |
| 699 | datastore.createItem("/example-schema:players[name='Eve']"); |
| 700 | datastore.createItem("/example-schema:players[name='Adam']"); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 701 | datastore.commitChanges(); |
| 702 | expected = { |
| 703 | {"/example-schema:players[name='John']", special_{SpecialValue::List}}, |
| 704 | {"/example-schema:players[name='John']/name", "John"s}, |
| 705 | {"/example-schema:players[name='Eve']", special_{SpecialValue::List}}, |
| 706 | {"/example-schema:players[name='Eve']/name", "Eve"s}, |
| 707 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 708 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 709 | }; |
| 710 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 711 | } |
| 712 | |
| 713 | std::string sourcePath; |
| 714 | SECTION("begin") |
| 715 | { |
| 716 | sourcePath = "/example-schema:players[name='Adam']"; |
| 717 | REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", std::nullopt, ""s)); |
| 718 | datastore.moveItem(sourcePath, yang::move::Absolute::Begin); |
| 719 | datastore.commitChanges(); |
| 720 | expected = { |
| 721 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 722 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 723 | {"/example-schema:players[name='John']", special_{SpecialValue::List}}, |
| 724 | {"/example-schema:players[name='John']/name", "John"s}, |
| 725 | {"/example-schema:players[name='Eve']", special_{SpecialValue::List}}, |
| 726 | {"/example-schema:players[name='Eve']/name", "Eve"s}, |
| 727 | }; |
| 728 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 729 | } |
| 730 | |
| 731 | SECTION("end") |
| 732 | { |
| 733 | sourcePath = "/example-schema:players[name='John']"; |
| 734 | REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s)); |
| 735 | datastore.moveItem(sourcePath, yang::move::Absolute::End); |
| 736 | datastore.commitChanges(); |
| 737 | expected = { |
| 738 | {"/example-schema:players[name='Eve']", special_{SpecialValue::List}}, |
| 739 | {"/example-schema:players[name='Eve']/name", "Eve"s}, |
| 740 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 741 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 742 | {"/example-schema:players[name='John']", special_{SpecialValue::List}}, |
| 743 | {"/example-schema:players[name='John']/name", "John"s}, |
| 744 | }; |
| 745 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 746 | } |
| 747 | |
| 748 | SECTION("after") |
| 749 | { |
| 750 | sourcePath = "/example-schema:players[name='John']"; |
| 751 | REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s)); |
| 752 | datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{"name", "Eve"s}}}); |
| 753 | datastore.commitChanges(); |
| 754 | expected = { |
| 755 | {"/example-schema:players[name='Eve']", special_{SpecialValue::List}}, |
| 756 | {"/example-schema:players[name='Eve']/name", "Eve"s}, |
| 757 | {"/example-schema:players[name='John']", special_{SpecialValue::List}}, |
| 758 | {"/example-schema:players[name='John']/name", "John"s}, |
| 759 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 760 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 761 | }; |
| 762 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 763 | } |
| 764 | |
| 765 | SECTION("before") |
| 766 | { |
| 767 | sourcePath = "/example-schema:players[name='John']"; |
| 768 | REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s)); |
| 769 | datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{"name", "Adam"s}}}); |
| 770 | datastore.commitChanges(); |
| 771 | expected = { |
| 772 | {"/example-schema:players[name='Eve']", special_{SpecialValue::List}}, |
| 773 | {"/example-schema:players[name='Eve']/name", "Eve"s}, |
| 774 | {"/example-schema:players[name='John']", special_{SpecialValue::List}}, |
| 775 | {"/example-schema:players[name='John']/name", "John"s}, |
| 776 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 777 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 778 | }; |
| 779 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 780 | } |
| 781 | } |
| 782 | |
Václav Kubernát | fa96ac2 | 2020-06-18 17:03:52 +0200 | [diff] [blame] | 783 | SECTION("getting /") |
| 784 | { |
| 785 | { |
| 786 | REQUIRE_CALL(mock, write("/example-schema:leafInt32", std::nullopt, "64"s)); |
| 787 | datastore.setLeaf("/example-schema:leafInt32", 64); |
| 788 | datastore.commitChanges(); |
| 789 | } |
| 790 | |
| 791 | DatastoreAccess::Tree expected{ |
| 792 | // Sysrepo always returns containers when getting values, but |
| 793 | // libnetconf does not. This is fine by the YANG standard: |
| 794 | // https://tools.ietf.org/html/rfc7950#section-7.5.7 Furthermore, |
| 795 | // NetconfAccess implementation actually only iterates over leafs, |
| 796 | // so even if libnetconf did include containers, they wouldn't get |
| 797 | // shown here anyway. With sysrepo2, this won't be necessary, |
| 798 | // because it'll use the same data structure as libnetconf, so the |
| 799 | // results will be consistent. |
| 800 | #ifdef sysrepo_BACKEND |
| 801 | {"/example-schema:inventory", special_{SpecialValue::Container}}, |
| 802 | {"/example-schema:lol", special_{SpecialValue::Container}}, |
| 803 | #endif |
| 804 | {"/example-schema:leafInt32", 64}}; |
| 805 | auto items = datastore.getItems("/"); |
| 806 | // This tests if we at least get the data WE added. |
| 807 | REQUIRE(std::all_of(expected.begin(), expected.end(), [items] (const auto& item) { return std::find(items.begin(), items.end(), item) != items.end(); })); |
| 808 | } |
| 809 | |
Václav Kubernát | 36986c5 | 2020-06-25 10:30:05 +0200 | [diff] [blame] | 810 | SECTION("setting and removing without commit") |
| 811 | { |
| 812 | datastore.setLeaf("/example-schema:leafInt32", 64); |
| 813 | datastore.deleteItem("/example-schema:leafInt32"); |
| 814 | } |
| 815 | |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 816 | waitForCompletionAndBitMore(seq1); |
| 817 | } |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 818 | |
| 819 | class RpcCb: public sysrepo::Callback { |
| 820 | int rpc(const char *xpath, const ::sysrepo::S_Vals input, ::sysrepo::S_Vals_Holder output, void *) override |
| 821 | { |
| 822 | const auto nukes = "/example-schema:launch-nukes"s; |
| 823 | if (xpath == "/example-schema:noop"s) { |
| 824 | return SR_ERR_OK; |
| 825 | } else if (xpath == nukes) { |
| 826 | uint64_t kilotons = 0; |
| 827 | bool hasCities = false; |
| 828 | for (size_t i = 0; i < input->val_cnt(); ++i) { |
| 829 | const auto& val = input->val(i); |
| 830 | if (val->xpath() == nukes + "/payload/kilotons") { |
| 831 | kilotons = val->data()->get_uint64(); |
| 832 | } else if (val->xpath() == nukes + "/payload") { |
| 833 | // ignore, container |
| 834 | } else if (val->xpath() == nukes + "/description") { |
| 835 | // unused |
| 836 | } else if (std::string_view{val->xpath()}.find(nukes + "/cities") == 0) { |
| 837 | hasCities = true; |
| 838 | } else { |
| 839 | throw std::runtime_error("RPC launch-nukes: unexpected input "s + val->xpath()); |
| 840 | } |
| 841 | } |
| 842 | if (kilotons == 333'666) { |
| 843 | // magic, just do not generate any output. This is important because the NETCONF RPC returns just <ok/>. |
| 844 | return SR_ERR_OK; |
| 845 | } |
| 846 | auto buf = output->allocate(2); |
| 847 | size_t i = 0; |
| 848 | buf->val(i++)->set((nukes + "/blast-radius").c_str(), uint32_t{33'666}); |
| 849 | buf->val(i++)->set((nukes + "/actual-yield").c_str(), static_cast<uint64_t>(1.33 * kilotons)); |
| 850 | if (hasCities) { |
| 851 | buf = output->reallocate(output->val_cnt() + 2); |
| 852 | buf->val(i++)->set((nukes + "/damaged-places/targets[city='London']/city").c_str(), "London"); |
| 853 | buf->val(i++)->set((nukes + "/damaged-places/targets[city='Berlin']/city").c_str(), "Berlin"); |
| 854 | } |
| 855 | return SR_ERR_OK; |
| 856 | } |
| 857 | throw std::runtime_error("unrecognized RPC"); |
| 858 | } |
| 859 | }; |
| 860 | |
| 861 | TEST_CASE("rpc") { |
| 862 | trompeloeil::sequence seq1; |
| 863 | auto srConn = std::make_shared<sysrepo::Connection>("netconf-cli-test-rpc"); |
| 864 | auto srSession = std::make_shared<sysrepo::Session>(srConn); |
| 865 | auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSession); |
| 866 | auto cb = std::make_shared<RpcCb>(); |
| 867 | sysrepo::Logs{}.set_stderr(SR_LL_INF); |
| 868 | srSubscription->rpc_subscribe("/example-schema:noop", cb, nullptr, SR_SUBSCR_CTX_REUSE); |
| 869 | srSubscription->rpc_subscribe("/example-schema:launch-nukes", cb, nullptr, SR_SUBSCR_CTX_REUSE); |
| 870 | |
| 871 | #ifdef sysrepo_BACKEND |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 872 | SysrepoAccess datastore("netconf-cli-test", Datastore::Running); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 873 | #elif defined(netconf_BACKEND) |
| 874 | NetconfAccess datastore(NETOPEER_SOCKET_PATH); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 875 | #elif defined(yang_BACKEND) |
| 876 | YangAccess datastore; |
| 877 | datastore.addSchemaDir(schemaDir); |
| 878 | datastore.addSchemaFile(exampleSchemaFile); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 879 | #else |
| 880 | #error "Unknown backend" |
| 881 | #endif |
| 882 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 883 | SECTION("valid") |
| 884 | { |
| 885 | std::string rpc; |
| 886 | DatastoreAccess::Tree input, output; |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 887 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 888 | SECTION("noop") { |
| 889 | rpc = "/example-schema:noop"; |
| 890 | } |
| 891 | |
| 892 | SECTION("small nuke") { |
| 893 | rpc = "/example-schema:launch-nukes"; |
| 894 | input = { |
| 895 | {"description", "dummy"s}, |
| 896 | {"payload/kilotons", uint64_t{333'666}}, |
| 897 | }; |
| 898 | // no data are returned |
| 899 | } |
| 900 | |
| 901 | SECTION("small nuke") { |
| 902 | rpc = "/example-schema:launch-nukes"; |
| 903 | input = { |
| 904 | {"description", "dummy"s}, |
| 905 | {"payload/kilotons", uint64_t{4}}, |
| 906 | }; |
| 907 | output = { |
| 908 | {"blast-radius", uint32_t{33'666}}, |
| 909 | {"actual-yield", uint64_t{5}}, |
| 910 | }; |
| 911 | } |
| 912 | |
| 913 | SECTION("with lists") { |
| 914 | rpc = "/example-schema:launch-nukes"; |
| 915 | input = { |
| 916 | {"payload/kilotons", uint64_t{6}}, |
| 917 | {"cities/targets[city='Prague']/city", "Prague"s}, |
| 918 | }; |
| 919 | output = { |
| 920 | {"blast-radius", uint32_t{33'666}}, |
| 921 | {"actual-yield", uint64_t{7}}, |
| 922 | {"damaged-places", special_{SpecialValue::PresenceContainer}}, |
| 923 | {"damaged-places/targets[city='London']", special_{SpecialValue::List}}, |
| 924 | {"damaged-places/targets[city='London']/city", "London"s}, |
| 925 | {"damaged-places/targets[city='Berlin']", special_{SpecialValue::List}}, |
| 926 | {"damaged-places/targets[city='Berlin']/city", "Berlin"s}, |
| 927 | }; |
| 928 | } |
| 929 | |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 930 | catching<OnRPC>([&] {REQUIRE(datastore.executeRpc(rpc, input) == output);}); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 931 | } |
| 932 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 933 | SECTION("non-existing RPC") |
| 934 | { |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 935 | catching<OnInvalidRpcPath>([&] {datastore.executeRpc("/example-schema:non-existing", DatastoreAccess::Tree{});}); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 936 | } |
| 937 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 938 | waitForCompletionAndBitMore(seq1); |
| 939 | } |