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" |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 14 | #elif defined(netconf_BACKEND) |
| 15 | #include "netconf_access.hpp" |
| 16 | #include "netopeer_vars.hpp" |
| 17 | #else |
| 18 | #error "Unknown backend" |
| 19 | #endif |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 20 | #include "pretty_printers.hpp" |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 21 | #include "sysrepo_subscription.hpp" |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 22 | #include "utils.hpp" |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 23 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 24 | using namespace std::literals::string_literals; |
| 25 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 26 | class MockRecorder : public trompeloeil::mock_interface<Recorder> { |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 27 | public: |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 28 | IMPLEMENT_MOCK3(write); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 29 | }; |
| 30 | |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 31 | class MockDataSupplier : public trompeloeil::mock_interface<DataSupplier> { |
| 32 | public: |
| 33 | IMPLEMENT_CONST_MOCK1(get_data); |
| 34 | }; |
| 35 | |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 36 | TEST_CASE("setting/getting values") |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 37 | { |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 38 | trompeloeil::sequence seq1; |
| 39 | MockRecorder mock; |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 40 | SysrepoSubscription subscription("example-schema", &mock); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 41 | |
| 42 | #ifdef sysrepo_BACKEND |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 43 | SysrepoAccess datastore("netconf-cli-test", Datastore::Running); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 44 | #elif defined(netconf_BACKEND) |
| 45 | NetconfAccess datastore(NETOPEER_SOCKET_PATH); |
| 46 | #else |
| 47 | #error "Unknown backend" |
| 48 | #endif |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 49 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 50 | |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 51 | SECTION("set leafInt8 to -128") |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 52 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 53 | 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] | 54 | datastore.setLeaf("/example-schema:leafInt8", int8_t{-128}); |
| 55 | datastore.commitChanges(); |
| 56 | } |
| 57 | |
| 58 | SECTION("set leafInt16 to -32768") |
| 59 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 60 | 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] | 61 | datastore.setLeaf("/example-schema:leafInt16", int16_t{-32768}); |
| 62 | datastore.commitChanges(); |
| 63 | } |
| 64 | |
| 65 | SECTION("set leafInt32 to -2147483648") |
| 66 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 67 | 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] | 68 | datastore.setLeaf("/example-schema:leafInt32", int32_t{-2147483648}); |
| 69 | datastore.commitChanges(); |
| 70 | } |
| 71 | |
| 72 | SECTION("set leafInt64 to -50000000000") |
| 73 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 74 | 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] | 75 | datastore.setLeaf("/example-schema:leafInt64", int64_t{-50000000000}); |
| 76 | datastore.commitChanges(); |
| 77 | } |
| 78 | |
| 79 | SECTION("set leafUInt8 to 255") |
| 80 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 81 | 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] | 82 | datastore.setLeaf("/example-schema:leafUInt8", uint8_t{255}); |
| 83 | datastore.commitChanges(); |
| 84 | } |
| 85 | |
| 86 | SECTION("set leafUInt16 to 65535") |
| 87 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 88 | 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] | 89 | datastore.setLeaf("/example-schema:leafUInt16", uint16_t{65535}); |
| 90 | datastore.commitChanges(); |
| 91 | } |
| 92 | |
| 93 | SECTION("set leafUInt32 to 4294967295") |
| 94 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 95 | 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] | 96 | datastore.setLeaf("/example-schema:leafUInt32", uint32_t{4294967295}); |
| 97 | datastore.commitChanges(); |
| 98 | } |
| 99 | |
| 100 | SECTION("set leafUInt64 to 50000000000") |
| 101 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 102 | 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] | 103 | datastore.setLeaf("/example-schema:leafUInt64", uint64_t{50000000000}); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 104 | datastore.commitChanges(); |
| 105 | } |
| 106 | |
| 107 | SECTION("set leafEnum to coze") |
| 108 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 109 | 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] | 110 | datastore.setLeaf("/example-schema:leafEnum", enum_{"coze"}); |
| 111 | datastore.commitChanges(); |
| 112 | } |
| 113 | |
| 114 | SECTION("set leafDecimal to 123.544") |
| 115 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 116 | 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] | 117 | datastore.setLeaf("/example-schema:leafDecimal", 123.544); |
| 118 | datastore.commitChanges(); |
| 119 | } |
| 120 | |
| 121 | SECTION("create presence container") |
| 122 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 123 | REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s)); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 124 | datastore.createPresenceContainer("/example-schema:pContainer"); |
| 125 | datastore.commitChanges(); |
| 126 | } |
| 127 | |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 128 | SECTION("create/delete a list instance") |
Václav Kubernát | 45f4a82 | 2019-05-29 21:10:50 +0200 | [diff] [blame] | 129 | { |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 130 | { |
| 131 | REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", std::nullopt, ""s)); |
| 132 | REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']/name", std::nullopt, "Nguyen"s)); |
| 133 | datastore.createListInstance("/example-schema:person[name='Nguyen']"); |
| 134 | datastore.commitChanges(); |
| 135 | } |
| 136 | { |
| 137 | REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", ""s, std::nullopt)); |
| 138 | REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']/name", "Nguyen"s, std::nullopt)); |
| 139 | datastore.deleteListInstance("/example-schema:person[name='Nguyen']"); |
| 140 | datastore.commitChanges(); |
| 141 | } |
Václav Kubernát | 45f4a82 | 2019-05-29 21:10:50 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 144 | SECTION("leafref pointing to a key of a list") |
| 145 | { |
| 146 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 147 | REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']", std::nullopt, ""s)); |
| 148 | REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']/name", std::nullopt, "Dan"s)); |
| 149 | REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']", std::nullopt, ""s)); |
| 150 | REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']/name", std::nullopt, "Elfi"s)); |
| 151 | REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']", std::nullopt, ""s)); |
| 152 | REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']/name", std::nullopt, "Kolafa"s)); |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 153 | datastore.createListInstance("/example-schema:person[name='Dan']"); |
| 154 | datastore.createListInstance("/example-schema:person[name='Elfi']"); |
| 155 | datastore.createListInstance("/example-schema:person[name='Kolafa']"); |
| 156 | datastore.commitChanges(); |
| 157 | } |
| 158 | |
| 159 | // The commitChanges method has to be called in each of the |
| 160 | // SECTIONs, because the REQUIRE_CALL only works inside the given |
| 161 | // SECTION. |
| 162 | SECTION("Dan") |
| 163 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 164 | 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] | 165 | datastore.setLeaf("/example-schema:bossPerson", std::string{"Dan"}); |
| 166 | datastore.commitChanges(); |
| 167 | } |
| 168 | |
| 169 | SECTION("Elfi") |
| 170 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 171 | 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] | 172 | datastore.setLeaf("/example-schema:bossPerson", std::string{"Elfi"}); |
| 173 | datastore.commitChanges(); |
| 174 | } |
| 175 | |
| 176 | SECTION("Kolafa") |
| 177 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 178 | 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] | 179 | datastore.setLeaf("/example-schema:bossPerson", std::string{"Kolafa"}); |
| 180 | datastore.commitChanges(); |
| 181 | } |
| 182 | } |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 183 | SECTION("bool values get correctly represented as bools") |
| 184 | { |
| 185 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 186 | 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] | 187 | datastore.setLeaf("/example-schema:down", bool{true}); |
| 188 | datastore.commitChanges(); |
| 189 | } |
| 190 | |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 191 | DatastoreAccess::Tree expected{{"/example-schema:down", bool{true}}}; |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 192 | REQUIRE(datastore.getItems("/example-schema:down") == expected); |
| 193 | } |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 194 | |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 195 | SECTION("getting items from the whole module") |
| 196 | { |
| 197 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 198 | REQUIRE_CALL(mock, write("/example-schema:up", std::nullopt, "true"s)); |
| 199 | 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] | 200 | datastore.setLeaf("/example-schema:up", bool{true}); |
| 201 | datastore.setLeaf("/example-schema:down", bool{false}); |
| 202 | datastore.commitChanges(); |
| 203 | } |
| 204 | |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 205 | DatastoreAccess::Tree expected{{"/example-schema:down", bool{false}}, |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 206 | // Sysrepo always returns containers when getting values, but |
| 207 | // libnetconf does not. This is fine by the YANG standard: |
| 208 | // https://tools.ietf.org/html/rfc7950#section-7.5.7 Furthermore, |
| 209 | // NetconfAccess implementation actually only iterates over leafs, |
| 210 | // so even if libnetconf did include containers, they wouldn't get |
| 211 | // shown here anyway. With sysrepo2, this won't be necessary, |
| 212 | // because it'll use the same data structure as libnetconf, so the |
| 213 | // results will be consistent. |
| 214 | #ifdef sysrepo_BACKEND |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 215 | {"/example-schema:lol", special_{SpecialValue::Container}}, |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 216 | {"/example-schema:inventory", special_{SpecialValue::Container}}, |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 217 | #endif |
| 218 | {"/example-schema:up", bool{true}}}; |
| 219 | REQUIRE(datastore.getItems("/example-schema:*") == expected); |
| 220 | } |
| 221 | |
Václav Kubernát | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 222 | SECTION("getItems returns correct datatypes") |
| 223 | { |
| 224 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 225 | 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] | 226 | datastore.setLeaf("/example-schema:leafEnum", enum_{"lol"}); |
| 227 | datastore.commitChanges(); |
| 228 | } |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 229 | DatastoreAccess::Tree expected{{"/example-schema:leafEnum", enum_{"lol"}}}; |
Václav Kubernát | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 230 | |
| 231 | REQUIRE(datastore.getItems("/example-schema:leafEnum") == expected); |
| 232 | } |
| 233 | |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 234 | SECTION("getItems on a list") |
| 235 | { |
| 236 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 237 | REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']", std::nullopt, ""s)); |
| 238 | REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']/name", std::nullopt, "Jan"s)); |
| 239 | REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']", std::nullopt, ""s)); |
| 240 | REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']/name", std::nullopt, "Michal"s)); |
| 241 | REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']", std::nullopt, ""s)); |
| 242 | REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']/name", std::nullopt, "Petr"s)); |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 243 | datastore.createListInstance("/example-schema:person[name='Jan']"); |
| 244 | datastore.createListInstance("/example-schema:person[name='Michal']"); |
| 245 | datastore.createListInstance("/example-schema:person[name='Petr']"); |
| 246 | datastore.commitChanges(); |
| 247 | } |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 248 | DatastoreAccess::Tree expected{ |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 249 | {"/example-schema:person[name='Jan']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 250 | {"/example-schema:person[name='Jan']/name", std::string{"Jan"}}, |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 251 | {"/example-schema:person[name='Michal']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 252 | {"/example-schema:person[name='Michal']/name", std::string{"Michal"}}, |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 253 | {"/example-schema:person[name='Petr']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 254 | {"/example-schema:person[name='Petr']/name", std::string{"Petr"}} |
| 255 | }; |
| 256 | |
| 257 | REQUIRE(datastore.getItems("/example-schema:person") == expected); |
| 258 | } |
| 259 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 260 | SECTION("presence containers") |
| 261 | { |
| 262 | DatastoreAccess::Tree expected; |
| 263 | // Make sure it's not there before we create it |
| 264 | REQUIRE(datastore.getItems("/example-schema:pContainer") == expected); |
| 265 | |
| 266 | { |
| 267 | REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s)); |
| 268 | datastore.createPresenceContainer("/example-schema:pContainer"); |
| 269 | datastore.commitChanges(); |
| 270 | } |
| 271 | expected = { |
| 272 | {"/example-schema:pContainer", special_{SpecialValue::PresenceContainer}} |
| 273 | }; |
| 274 | REQUIRE(datastore.getItems("/example-schema:pContainer") == expected); |
| 275 | |
| 276 | // Make sure it's not there after we delete it |
| 277 | { |
| 278 | REQUIRE_CALL(mock, write("/example-schema:pContainer", ""s, std::nullopt)); |
| 279 | datastore.deletePresenceContainer("/example-schema:pContainer"); |
| 280 | datastore.commitChanges(); |
| 281 | } |
| 282 | expected = {}; |
| 283 | REQUIRE(datastore.getItems("/example-schema:pContainer") == expected); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 284 | } |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 285 | |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 286 | SECTION("nested presence container") |
| 287 | { |
| 288 | DatastoreAccess::Tree expected; |
| 289 | // Make sure it's not there before we create it |
| 290 | REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected); |
| 291 | { |
| 292 | REQUIRE_CALL(mock, write("/example-schema:inventory", std::nullopt, ""s)); |
| 293 | REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", std::nullopt, ""s)); |
| 294 | datastore.createPresenceContainer("/example-schema:inventory/stuff"); |
| 295 | datastore.commitChanges(); |
| 296 | } |
| 297 | expected = { |
| 298 | {"/example-schema:inventory/stuff", special_{SpecialValue::PresenceContainer}} |
| 299 | }; |
| 300 | REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected); |
| 301 | { |
| 302 | REQUIRE_CALL(mock, write("/example-schema:inventory", ""s, std::nullopt)); |
| 303 | REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", ""s, std::nullopt)); |
| 304 | datastore.deletePresenceContainer("/example-schema:inventory/stuff"); |
| 305 | datastore.commitChanges(); |
| 306 | } |
| 307 | expected = {}; |
| 308 | REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Jan Kundrát | bd3169c | 2020-02-03 19:31:34 +0100 | [diff] [blame] | 311 | SECTION("floats") |
| 312 | { |
| 313 | datastore.setLeaf("/example-schema:leafDecimal", 123.4); |
| 314 | REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.4"s)); |
| 315 | datastore.commitChanges(); |
| 316 | DatastoreAccess::Tree expected { |
| 317 | {"/example-schema:leafDecimal", 123.4}, |
| 318 | }; |
| 319 | REQUIRE(datastore.getItems("/example-schema:leafDecimal") == expected); |
| 320 | } |
| 321 | |
Jan Kundrát | 3ff5012 | 2020-05-07 00:37:50 +0200 | [diff] [blame] | 322 | SECTION("unions") |
| 323 | { |
| 324 | datastore.setLeaf("/example-schema:unionIntString", int32_t{10}); |
| 325 | REQUIRE_CALL(mock, write("/example-schema:unionIntString", std::nullopt, "10"s)); |
| 326 | datastore.commitChanges(); |
| 327 | DatastoreAccess::Tree expected { |
| 328 | {"/example-schema:unionIntString", int32_t{10}}, |
| 329 | }; |
| 330 | REQUIRE(datastore.getItems("/example-schema:unionIntString") == expected); |
| 331 | } |
| 332 | |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame] | 333 | SECTION("identityref") { |
| 334 | datastore.setLeaf("/example-schema:beast", identityRef_{"example-schema", "Mammal"}); |
| 335 | REQUIRE_CALL(mock, write("/example-schema:beast", std::nullopt, "example-schema:Mammal"s)); |
| 336 | datastore.commitChanges(); |
| 337 | DatastoreAccess::Tree expected { |
| 338 | {"/example-schema:beast", identityRef_{"example-schema", "Mammal"}}, |
| 339 | }; |
| 340 | REQUIRE(datastore.getItems("/example-schema:beast") == expected); |
| 341 | |
| 342 | datastore.setLeaf("/example-schema:beast", identityRef_{"Whale"}); |
| 343 | REQUIRE_CALL(mock, write("/example-schema:beast", "example-schema:Mammal", "example-schema:Whale"s)); |
| 344 | datastore.commitChanges(); |
| 345 | expected = { |
| 346 | {"/example-schema:beast", identityRef_{"example-schema", "Whale"}}, |
| 347 | }; |
| 348 | REQUIRE(datastore.getItems("/example-schema:beast") == expected); |
| 349 | } |
| 350 | |
Jan Kundrát | 6898544 | 2020-05-07 02:15:34 +0200 | [diff] [blame] | 351 | SECTION("binary") |
| 352 | { |
| 353 | datastore.setLeaf("/example-schema:blob", binary_{"cHduegByIQ=="s}); |
| 354 | REQUIRE_CALL(mock, write("/example-schema:blob", std::nullopt, "cHduegByIQ=="s)); |
| 355 | datastore.commitChanges(); |
| 356 | DatastoreAccess::Tree expected { |
| 357 | {"/example-schema:blob", binary_{"cHduegByIQ=="s}}, |
| 358 | }; |
| 359 | REQUIRE(datastore.getItems("/example-schema:blob") == expected); |
| 360 | } |
| 361 | |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 362 | SECTION("empty") |
| 363 | { |
| 364 | datastore.setLeaf("/example-schema:dummy", empty_{}); |
| 365 | REQUIRE_CALL(mock, write("/example-schema:dummy", std::nullopt, ""s)); |
| 366 | datastore.commitChanges(); |
| 367 | DatastoreAccess::Tree expected { |
| 368 | {"/example-schema:dummy", empty_{}}, |
| 369 | }; |
| 370 | REQUIRE(datastore.getItems("/example-schema:dummy") == expected); |
| 371 | } |
| 372 | |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 373 | SECTION("operational data") |
| 374 | { |
| 375 | MockDataSupplier mockOpsData; |
| 376 | OperationalDataSubscription opsDataSub("/example-schema:temperature", mockOpsData); |
| 377 | DatastoreAccess::Tree expected; |
| 378 | std::string xpath; |
| 379 | SECTION("temperature") |
| 380 | { |
| 381 | expected = {{"/example-schema:temperature", int32_t{22}}}; |
| 382 | xpath = "/example-schema:temperature"; |
| 383 | } |
| 384 | |
| 385 | REQUIRE_CALL(mockOpsData, get_data(xpath)).RETURN(expected); |
| 386 | REQUIRE(datastore.getItems(xpath) == expected); |
| 387 | } |
| 388 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 389 | SECTION("leaf list") |
| 390 | { |
| 391 | DatastoreAccess::Tree expected; |
| 392 | REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "0.0.0.0"s)); |
| 393 | REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "127.0.0.1"s)); |
| 394 | datastore.createLeafListInstance("/example-schema:addresses[.='0.0.0.0']"); |
| 395 | datastore.createLeafListInstance("/example-schema:addresses[.='127.0.0.1']"); |
| 396 | datastore.commitChanges(); |
| 397 | expected = { |
| 398 | {"/example-schema:addresses", special_{SpecialValue::LeafList}}, |
| 399 | {"/example-schema:addresses[.='0.0.0.0']", "0.0.0.0"s}, |
| 400 | {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s}, |
| 401 | }; |
| 402 | REQUIRE(datastore.getItems("/example-schema:addresses") == expected); |
| 403 | |
| 404 | REQUIRE_CALL(mock, write("/example-schema:addresses", "0.0.0.0"s, std::nullopt)); |
| 405 | datastore.deleteLeafListInstance("/example-schema:addresses[.='0.0.0.0']"); |
| 406 | datastore.commitChanges(); |
| 407 | expected = { |
| 408 | {"/example-schema:addresses", special_{SpecialValue::LeafList}}, |
| 409 | {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s}, |
| 410 | }; |
| 411 | REQUIRE(datastore.getItems("/example-schema:addresses") == expected); |
| 412 | |
| 413 | REQUIRE_CALL(mock, write("/example-schema:addresses", "127.0.0.1"s, std::nullopt)); |
| 414 | datastore.deleteLeafListInstance("/example-schema:addresses[.='127.0.0.1']"); |
| 415 | datastore.commitChanges(); |
| 416 | expected = {}; |
| 417 | REQUIRE(datastore.getItems("/example-schema:addresses") == expected); |
| 418 | } |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 419 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 420 | SECTION("copying data from startup refreshes the data") |
| 421 | { |
| 422 | { |
| 423 | REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{}); |
| 424 | REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "123"s)); |
| 425 | datastore.setLeaf("/example-schema:leafInt16", int16_t{123}); |
| 426 | datastore.commitChanges(); |
| 427 | } |
| 428 | REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{{"/example-schema:leafInt16", int16_t{123}}}); |
| 429 | REQUIRE_CALL(mock, write("/example-schema:leafInt16", "123"s, std::nullopt)); |
| 430 | datastore.copyConfig(Datastore::Startup, Datastore::Running); |
| 431 | REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{}); |
| 432 | } |
| 433 | |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 434 | waitForCompletionAndBitMore(seq1); |
| 435 | } |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 436 | |
| 437 | class RpcCb: public sysrepo::Callback { |
| 438 | int rpc(const char *xpath, const ::sysrepo::S_Vals input, ::sysrepo::S_Vals_Holder output, void *) override |
| 439 | { |
| 440 | const auto nukes = "/example-schema:launch-nukes"s; |
| 441 | if (xpath == "/example-schema:noop"s) { |
| 442 | return SR_ERR_OK; |
| 443 | } else if (xpath == nukes) { |
| 444 | uint64_t kilotons = 0; |
| 445 | bool hasCities = false; |
| 446 | for (size_t i = 0; i < input->val_cnt(); ++i) { |
| 447 | const auto& val = input->val(i); |
| 448 | if (val->xpath() == nukes + "/payload/kilotons") { |
| 449 | kilotons = val->data()->get_uint64(); |
| 450 | } else if (val->xpath() == nukes + "/payload") { |
| 451 | // ignore, container |
| 452 | } else if (val->xpath() == nukes + "/description") { |
| 453 | // unused |
| 454 | } else if (std::string_view{val->xpath()}.find(nukes + "/cities") == 0) { |
| 455 | hasCities = true; |
| 456 | } else { |
| 457 | throw std::runtime_error("RPC launch-nukes: unexpected input "s + val->xpath()); |
| 458 | } |
| 459 | } |
| 460 | if (kilotons == 333'666) { |
| 461 | // magic, just do not generate any output. This is important because the NETCONF RPC returns just <ok/>. |
| 462 | return SR_ERR_OK; |
| 463 | } |
| 464 | auto buf = output->allocate(2); |
| 465 | size_t i = 0; |
| 466 | buf->val(i++)->set((nukes + "/blast-radius").c_str(), uint32_t{33'666}); |
| 467 | buf->val(i++)->set((nukes + "/actual-yield").c_str(), static_cast<uint64_t>(1.33 * kilotons)); |
| 468 | if (hasCities) { |
| 469 | buf = output->reallocate(output->val_cnt() + 2); |
| 470 | buf->val(i++)->set((nukes + "/damaged-places/targets[city='London']/city").c_str(), "London"); |
| 471 | buf->val(i++)->set((nukes + "/damaged-places/targets[city='Berlin']/city").c_str(), "Berlin"); |
| 472 | } |
| 473 | return SR_ERR_OK; |
| 474 | } |
| 475 | throw std::runtime_error("unrecognized RPC"); |
| 476 | } |
| 477 | }; |
| 478 | |
| 479 | TEST_CASE("rpc") { |
| 480 | trompeloeil::sequence seq1; |
| 481 | auto srConn = std::make_shared<sysrepo::Connection>("netconf-cli-test-rpc"); |
| 482 | auto srSession = std::make_shared<sysrepo::Session>(srConn); |
| 483 | auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSession); |
| 484 | auto cb = std::make_shared<RpcCb>(); |
| 485 | sysrepo::Logs{}.set_stderr(SR_LL_INF); |
| 486 | srSubscription->rpc_subscribe("/example-schema:noop", cb, nullptr, SR_SUBSCR_CTX_REUSE); |
| 487 | srSubscription->rpc_subscribe("/example-schema:launch-nukes", cb, nullptr, SR_SUBSCR_CTX_REUSE); |
| 488 | |
| 489 | #ifdef sysrepo_BACKEND |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 490 | SysrepoAccess datastore("netconf-cli-test", Datastore::Running); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 491 | #elif defined(netconf_BACKEND) |
| 492 | NetconfAccess datastore(NETOPEER_SOCKET_PATH); |
| 493 | #else |
| 494 | #error "Unknown backend" |
| 495 | #endif |
| 496 | |
| 497 | std::string rpc; |
| 498 | DatastoreAccess::Tree input, output; |
| 499 | |
| 500 | SECTION("noop") { |
| 501 | rpc = "/example-schema:noop"; |
| 502 | } |
| 503 | |
| 504 | SECTION("small nuke") { |
| 505 | rpc = "/example-schema:launch-nukes"; |
| 506 | input = { |
| 507 | {"description", "dummy"s}, |
| 508 | {"payload/kilotons", uint64_t{333'666}}, |
| 509 | }; |
| 510 | // no data are returned |
| 511 | } |
| 512 | |
| 513 | SECTION("small nuke") { |
| 514 | rpc = "/example-schema:launch-nukes"; |
| 515 | input = { |
| 516 | {"description", "dummy"s}, |
| 517 | {"payload/kilotons", uint64_t{4}}, |
| 518 | }; |
| 519 | output = { |
| 520 | {"blast-radius", uint32_t{33'666}}, |
| 521 | {"actual-yield", uint64_t{5}}, |
| 522 | }; |
| 523 | } |
| 524 | |
| 525 | SECTION("with lists") { |
| 526 | rpc = "/example-schema:launch-nukes"; |
| 527 | input = { |
| 528 | {"payload/kilotons", uint64_t{6}}, |
| 529 | {"cities/targets[city='Prague']/city", "Prague"s}, |
| 530 | }; |
| 531 | output = { |
| 532 | {"blast-radius", uint32_t{33'666}}, |
| 533 | {"actual-yield", uint64_t{7}}, |
| 534 | {"damaged-places", special_{SpecialValue::PresenceContainer}}, |
| 535 | {"damaged-places/targets[city='London']", special_{SpecialValue::List}}, |
| 536 | {"damaged-places/targets[city='London']/city", "London"s}, |
| 537 | {"damaged-places/targets[city='Berlin']", special_{SpecialValue::List}}, |
| 538 | {"damaged-places/targets[city='Berlin']/city", "Berlin"s}, |
| 539 | }; |
| 540 | } |
| 541 | |
| 542 | REQUIRE(datastore.executeRpc(rpc, input) == output); |
| 543 | |
| 544 | waitForCompletionAndBitMore(seq1); |
| 545 | } |