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