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