Tomáš Pecka | f10b930 | 2021-02-23 19:02:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@cesnet.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "trompeloeil_doctest.h" |
Tomáš Pecka | 39bbb51 | 2021-05-23 22:07:22 +0200 | [diff] [blame] | 9 | #include <filesystem> |
Tomáš Pecka | f10b930 | 2021-02-23 19:02:02 +0100 | [diff] [blame] | 10 | #include "pretty_printers.h" |
| 11 | #include "system/IETFInterfaces.h" |
Tomáš Pecka | 39bbb51 | 2021-05-23 22:07:22 +0200 | [diff] [blame] | 12 | #include "system/IETFInterfacesConfig.h" |
Tomáš Pecka | f10b930 | 2021-02-23 19:02:02 +0100 | [diff] [blame] | 13 | #include "test_log_setup.h" |
| 14 | #include "test_sysrepo_helpers.h" |
Tomáš Pecka | 39bbb51 | 2021-05-23 22:07:22 +0200 | [diff] [blame] | 15 | #include "tests/configure.cmake.h" |
Tomáš Pecka | f10b930 | 2021-02-23 19:02:02 +0100 | [diff] [blame] | 16 | #include "tests/mock/system.h" |
Tomáš Pecka | 39bbb51 | 2021-05-23 22:07:22 +0200 | [diff] [blame] | 17 | #include "utils/io.h" |
Tomáš Pecka | f10b930 | 2021-02-23 19:02:02 +0100 | [diff] [blame] | 18 | |
Tomáš Pecka | 71a98b0 | 2021-05-17 15:59:40 +0200 | [diff] [blame] | 19 | using namespace std::string_literals; |
| 20 | |
Tomáš Pecka | f10b930 | 2021-02-23 19:02:02 +0100 | [diff] [blame] | 21 | TEST_CASE("ietf-interfaces localhost") |
| 22 | { |
| 23 | TEST_SYSREPO_INIT_LOGS; |
| 24 | TEST_SYSREPO_INIT; |
| 25 | TEST_SYSREPO_INIT_CLIENT; |
| 26 | |
| 27 | auto network = std::make_shared<velia::system::IETFInterfaces>(srSess); |
| 28 | |
| 29 | // We didn't came up with some way of mocking netlink. At least check that there is the loopback |
| 30 | // interface with expected values. It is *probably* safe to assume that there is at least the lo device. |
Tomáš Pecka | 70e5456 | 2021-03-10 12:39:03 +0100 | [diff] [blame] | 31 | auto lo = dataFromSysrepo(client, "/ietf-interfaces:interfaces/interface[name='lo']", SR_DS_OPERATIONAL); |
| 32 | |
| 33 | // ensure statistics keys exist and remove them ; we can't really predict the content |
| 34 | for (const auto& key : {"/statistics", "/statistics/in-discards", "/statistics/in-errors", "/statistics/in-octets", "/statistics/out-discards", "/statistics/out-errors", "/statistics/out-octets"}) { |
| 35 | auto it = lo.find(key); |
| 36 | REQUIRE(it != lo.end()); |
| 37 | lo.erase(it); |
| 38 | } |
| 39 | |
| 40 | REQUIRE(lo == std::map<std::string, std::string> { |
Tomáš Pecka | f10b930 | 2021-02-23 19:02:02 +0100 | [diff] [blame] | 41 | {"/name", "lo"}, |
| 42 | {"/type", "iana-if-type:softwareLoopback"}, |
| 43 | {"/phys-address", "00:00:00:00:00:00"}, |
| 44 | {"/oper-status", "unknown"}, |
Tomáš Pecka | 0972938 | 2021-03-08 19:36:50 +0100 | [diff] [blame] | 45 | {"/ietf-ip:ipv4", ""}, |
| 46 | {"/ietf-ip:ipv4/address[ip='127.0.0.1']", ""}, |
| 47 | {"/ietf-ip:ipv4/address[ip='127.0.0.1']/ip", "127.0.0.1"}, |
| 48 | {"/ietf-ip:ipv4/address[ip='127.0.0.1']/prefix-length", "8"}, |
| 49 | {"/ietf-ip:ipv6", ""}, |
| 50 | {"/ietf-ip:ipv6/address[ip='::1']", ""}, |
| 51 | {"/ietf-ip:ipv6/address[ip='::1']/ip", "::1"}, |
| 52 | {"/ietf-ip:ipv6/address[ip='::1']/prefix-length", "128"}, |
Tomáš Pecka | f10b930 | 2021-02-23 19:02:02 +0100 | [diff] [blame] | 53 | }); |
Tomáš Pecka | 3663aae | 2021-03-10 13:46:31 +0100 | [diff] [blame] | 54 | // NOTE: There are no neighbours on loopback |
Tomáš Pecka | 71a98b0 | 2021-05-17 15:59:40 +0200 | [diff] [blame] | 55 | |
| 56 | SECTION("Link changes disabled") |
| 57 | { |
| 58 | client->session_switch_ds(SR_DS_STARTUP); |
| 59 | |
| 60 | SECTION("Only specified link names can appear in configurable datastore") |
| 61 | { |
| 62 | for (const auto& [name, type] : {std::pair<const char*, const char*>{"eth0", "iana-if-type:ethernetCsmacd"}, |
| 63 | {"eth1", "iana-if-type:ethernetCsmacd"}, |
| 64 | {"br0", "iana-if-type:bridge"}, |
| 65 | {"osc", "iana-if-type:ethernetCsmacd"}, |
| 66 | {"oscW", "iana-if-type:ethernetCsmacd"}, |
| 67 | {"oscE", "iana-if-type:ethernetCsmacd"}}) { |
| 68 | client->set_item_str(("/ietf-interfaces:interfaces/interface[name='"s + name + "']/type").c_str(), type); |
Tomáš Pecka | de0ef2f | 2021-07-08 13:23:50 +0200 | [diff] [blame^] | 69 | client->set_item_str(("/ietf-interfaces:interfaces/interface[name='"s + name + "']/enabled").c_str(), "false"); |
Tomáš Pecka | 71a98b0 | 2021-05-17 15:59:40 +0200 | [diff] [blame] | 70 | } |
| 71 | client->apply_changes(); |
| 72 | } |
| 73 | |
| 74 | SECTION("Invalid type for a valid link") |
| 75 | { |
| 76 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/type", "iana-if-type:softwareLoopback"); |
| 77 | REQUIRE_THROWS_AS(client->apply_changes(), sysrepo::sysrepo_exception); |
| 78 | } |
| 79 | |
| 80 | SECTION("Invalid name") |
| 81 | { |
| 82 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='blah0']/type", "iana-if-type:ethernetCsmacd"); |
| 83 | REQUIRE_THROWS_AS(client->apply_changes(), sysrepo::sysrepo_exception); |
| 84 | } |
| 85 | } |
Tomáš Pecka | de0ef2f | 2021-07-08 13:23:50 +0200 | [diff] [blame^] | 86 | |
| 87 | SECTION("There must always be enabled protocol or the interface must be explicitely disabled") |
| 88 | { |
| 89 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/type", "iana-if-type:ethernetCsmacd"); |
| 90 | |
| 91 | SECTION("Disabled protocols; enabled link") |
| 92 | { |
| 93 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/enabled", "true"); |
| 94 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4/ietf-ip:enabled", "false"); |
| 95 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv6/ietf-ip:enabled", "false"); |
| 96 | REQUIRE_THROWS_AS(client->apply_changes(), sysrepo::sysrepo_exception); |
| 97 | } |
| 98 | |
| 99 | SECTION("Active protocols; disabled link") |
| 100 | { |
| 101 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/enabled", "false"); |
| 102 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4/ietf-ip:enabled", "false"); |
| 103 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv6/ietf-ip:enabled", "true"); |
| 104 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv6/ietf-ip:address[ip='2001:db8::1']/ietf-ip:prefix-length", "32"); |
| 105 | client->apply_changes(); |
| 106 | } |
| 107 | |
| 108 | SECTION("IPv4 only; enabled link") |
| 109 | { |
| 110 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/enabled", "true"); |
| 111 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4/ietf-ip:enabled", "true"); |
| 112 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4/ietf-ip:address[ip='192.0.2.1']/ietf-ip:prefix-length", "24"); |
| 113 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv6/ietf-ip:enabled", "false"); |
| 114 | client->apply_changes(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | SECTION("Every active protocol must have at least one IP address assigned") |
| 119 | { |
| 120 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/enabled", "false"); |
| 121 | client->delete_item("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4"); |
| 122 | client->delete_item("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv6"); |
| 123 | client->apply_changes(); |
| 124 | |
| 125 | SECTION("Enabled IPv4 protocol with some IPs assigned is valid") |
| 126 | { |
| 127 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/enabled", "true"); |
| 128 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4/ietf-ip:enabled", "true"); |
| 129 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4/ietf-ip:address[ip='192.0.2.1']/ietf-ip:prefix-length", "24"); |
| 130 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4/ietf-ip:address[ip='192.0.2.2']/ietf-ip:prefix-length", "24"); |
| 131 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv6/ietf-ip:enabled", "false"); |
| 132 | client->apply_changes(); |
| 133 | } |
| 134 | |
| 135 | SECTION("Enabled IPv6 protocol with some IPs assigned is valid") |
| 136 | { |
| 137 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/enabled", "true"); |
| 138 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4/ietf-ip:enabled", "false"); |
| 139 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv6/ietf-ip:enabled", "true"); |
| 140 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv6/ietf-ip:address[ip='2001:db8::1']/ietf-ip:prefix-length", "32"); |
| 141 | client->apply_changes(); |
| 142 | } |
| 143 | |
| 144 | SECTION("Enabled IPv4 protocol must have at least one IP") |
| 145 | { |
| 146 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/enabled", "true"); |
| 147 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4/ietf-ip:enabled", "true"); |
| 148 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv6/ietf-ip:enabled", "false"); |
| 149 | REQUIRE_THROWS_AS(client->apply_changes(), sysrepo::sysrepo_exception); |
| 150 | } |
| 151 | |
| 152 | SECTION("Enabled IPv6 protocol must have at least one IP") |
| 153 | { |
| 154 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/enabled", "true"); |
| 155 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4/ietf-ip:enabled", "false"); |
| 156 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv6/ietf-ip:enabled", "true"); |
| 157 | REQUIRE_THROWS_AS(client->apply_changes(), sysrepo::sysrepo_exception); |
| 158 | } |
| 159 | } |
Tomáš Pecka | f10b930 | 2021-02-23 19:02:02 +0100 | [diff] [blame] | 160 | } |
Tomáš Pecka | 39bbb51 | 2021-05-23 22:07:22 +0200 | [diff] [blame] | 161 | |
| 162 | struct FakeNetworkReload { |
| 163 | public: |
| 164 | MAKE_CONST_MOCK1(cb, void(const std::vector<std::string>&)); |
| 165 | }; |
| 166 | |
| 167 | TEST_CASE("Config data in ietf-interfaces") |
| 168 | { |
| 169 | TEST_SYSREPO_INIT_LOGS; |
| 170 | TEST_SYSREPO_INIT; |
| 171 | TEST_SYSREPO_INIT_CLIENT; |
| 172 | trompeloeil::sequence seq1; |
| 173 | |
| 174 | auto fake = FakeNetworkReload(); |
| 175 | |
| 176 | const auto fakeConfigDir = std::filesystem::path(CMAKE_CURRENT_BINARY_DIR) / "tests/network/"s; |
| 177 | std::filesystem::remove_all(fakeConfigDir); |
| 178 | std::filesystem::create_directories(fakeConfigDir); |
| 179 | |
| 180 | auto expectedFilePath = fakeConfigDir / "eth0.network"; |
| 181 | |
| 182 | auto network = std::make_shared<velia::system::IETFInterfacesConfig>(srSess, fakeConfigDir, std::vector<std::string>{"lo", "eth0"}, [&fake](const std::vector<std::string>& updatedInterfaces) { fake.cb(updatedInterfaces); }); |
| 183 | |
| 184 | std::string expectedContents; |
| 185 | SECTION("With description") |
| 186 | { |
| 187 | expectedContents = R"([Match] |
| 188 | Name=eth0 |
| 189 | |
| 190 | [Network] |
| 191 | Description=Hello world |
| 192 | Bridge=br0 |
| 193 | LLDP=true |
| 194 | EmitLLDP=nearest-bridge |
| 195 | )"; |
| 196 | |
| 197 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/description", "Hello world"); |
| 198 | } |
| 199 | |
| 200 | SECTION("No description") |
| 201 | { |
| 202 | expectedContents = R"([Match] |
| 203 | Name=eth0 |
| 204 | |
| 205 | [Network] |
| 206 | Bridge=br0 |
| 207 | LLDP=true |
| 208 | EmitLLDP=nearest-bridge |
| 209 | )"; |
| 210 | } |
| 211 | |
Tomáš Pecka | de0ef2f | 2021-07-08 13:23:50 +0200 | [diff] [blame^] | 212 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4/ietf-ip:enabled", "true"); |
| 213 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4/ietf-ip:address[ip='192.0.2.1']/ietf-ip:prefix-length", "24"); |
Tomáš Pecka | 39bbb51 | 2021-05-23 22:07:22 +0200 | [diff] [blame] | 214 | client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/type", "iana-if-type:ethernetCsmacd"); |
| 215 | |
| 216 | REQUIRE_CALL(fake, cb(std::vector<std::string>{"eth0"})).IN_SEQUENCE(seq1); |
| 217 | client->apply_changes(); |
| 218 | REQUIRE(std::filesystem::exists(expectedFilePath)); |
| 219 | REQUIRE(velia::utils::readFileToString(expectedFilePath) == expectedContents); |
| 220 | |
| 221 | // reset the contents |
| 222 | client->delete_item("/ietf-interfaces:interfaces/interface[name='eth0']"); |
| 223 | REQUIRE_CALL(fake, cb(std::vector<std::string>{"eth0"})).IN_SEQUENCE(seq1); |
| 224 | client->apply_changes(); |
| 225 | REQUIRE(!std::filesystem::exists(expectedFilePath)); |
| 226 | } |