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