blob: 27c301a6af502527be4ef2492d3289b1456e660e [file] [log] [blame]
Tomáš Peckaf10b9302021-02-23 19:02:02 +01001/*
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áš Pecka39bbb512021-05-23 22:07:22 +02009#include <filesystem>
Tomáš Peckaf10b9302021-02-23 19:02:02 +010010#include "pretty_printers.h"
11#include "system/IETFInterfaces.h"
Tomáš Pecka39bbb512021-05-23 22:07:22 +020012#include "system/IETFInterfacesConfig.h"
Tomáš Peckaf10b9302021-02-23 19:02:02 +010013#include "test_log_setup.h"
14#include "test_sysrepo_helpers.h"
Tomáš Pecka39bbb512021-05-23 22:07:22 +020015#include "tests/configure.cmake.h"
Tomáš Peckaf10b9302021-02-23 19:02:02 +010016#include "tests/mock/system.h"
Tomáš Pecka39bbb512021-05-23 22:07:22 +020017#include "utils/io.h"
Tomáš Peckaf10b9302021-02-23 19:02:02 +010018
Tomáš Pecka71a98b02021-05-17 15:59:40 +020019using namespace std::string_literals;
20
Tomáš Peckaf10b9302021-02-23 19:02:02 +010021TEST_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áš Pecka70e54562021-03-10 12:39:03 +010031 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áš Peckaf10b9302021-02-23 19:02:02 +010041 {"/name", "lo"},
42 {"/type", "iana-if-type:softwareLoopback"},
43 {"/phys-address", "00:00:00:00:00:00"},
44 {"/oper-status", "unknown"},
Tomáš Pecka09729382021-03-08 19:36:50 +010045 {"/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áš Peckaf10b9302021-02-23 19:02:02 +010053 });
Tomáš Pecka3663aae2021-03-10 13:46:31 +010054 // NOTE: There are no neighbours on loopback
Tomáš Pecka71a98b02021-05-17 15:59:40 +020055
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áš Peckade0ef2f2021-07-08 13:23:50 +020069 client->set_item_str(("/ietf-interfaces:interfaces/interface[name='"s + name + "']/enabled").c_str(), "false");
Tomáš Pecka71a98b02021-05-17 15:59:40 +020070 }
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áš Peckade0ef2f2021-07-08 13:23:50 +020086
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áš Peckaf10b9302021-02-23 19:02:02 +0100160}
Tomáš Pecka39bbb512021-05-23 22:07:22 +0200161
162struct FakeNetworkReload {
163public:
164 MAKE_CONST_MOCK1(cb, void(const std::vector<std::string>&));
165};
166
167TEST_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
Tomáš Pecka85f77752021-06-03 10:09:58 +0200180 auto network = std::make_shared<velia::system::IETFInterfacesConfig>(srSess, fakeConfigDir, std::vector<std::string>{"eth0", "eth1"}, [&fake](const std::vector<std::string>& updatedInterfaces) { fake.cb(updatedInterfaces); });
Tomáš Pecka39bbb512021-05-23 22:07:22 +0200181
Tomáš Pecka39bbb512021-05-23 22:07:22 +0200182
183 std::string expectedContents;
Tomáš Pecka85f77752021-06-03 10:09:58 +0200184 SECTION("Setting IPs to eth0")
Tomáš Pecka39bbb512021-05-23 22:07:22 +0200185 {
Tomáš Pecka85f77752021-06-03 10:09:58 +0200186 const auto expectedFilePath = fakeConfigDir / "eth0.network";
187
188 client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/type", "iana-if-type:ethernetCsmacd");
189
190 SECTION("Single IPv4 address")
191 {
192 client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/description", "Hello world");
193 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");
194 expectedContents = R"([Match]
Tomáš Pecka39bbb512021-05-23 22:07:22 +0200195Name=eth0
196
197[Network]
198Description=Hello world
Tomáš Pecka85f77752021-06-03 10:09:58 +0200199Address=192.0.2.1/24
200LinkLocalAddressing=no
201DHCP=no
Tomáš Pecka39bbb512021-05-23 22:07:22 +0200202LLDP=true
203EmitLLDP=nearest-bridge
204)";
Tomáš Pecka85f77752021-06-03 10:09:58 +0200205 }
Tomáš Pecka39bbb512021-05-23 22:07:22 +0200206
Tomáš Pecka85f77752021-06-03 10:09:58 +0200207 SECTION("Two IPv4 addresses")
208 {
209 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");
210 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");
211 client->delete_item("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv6");
212 expectedContents = R"([Match]
Tomáš Pecka39bbb512021-05-23 22:07:22 +0200213Name=eth0
214
215[Network]
Tomáš Pecka85f77752021-06-03 10:09:58 +0200216Address=192.0.2.1/24
217Address=192.0.2.2/24
218LinkLocalAddressing=no
219DHCP=no
Tomáš Pecka39bbb512021-05-23 22:07:22 +0200220LLDP=true
221EmitLLDP=nearest-bridge
222)";
Tomáš Pecka85f77752021-06-03 10:09:58 +0200223 }
224
225 SECTION("IPv4 and IPv6 addresses")
226 {
227 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");
228 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");
229 expectedContents = R"([Match]
230Name=eth0
231
232[Network]
233Address=192.0.2.1/24
234Address=2001:db8::1/32
235DHCP=no
236LLDP=true
237EmitLLDP=nearest-bridge
238)";
239 }
240
241 SECTION("IPv4 and IPv6 addresses but IPv6 disabled")
242 {
243 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");
244 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");
245 client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv6/enabled", "false");
246 expectedContents = R"([Match]
247Name=eth0
248
249[Network]
250Address=192.0.2.1/24
251LinkLocalAddressing=no
252DHCP=no
253LLDP=true
254EmitLLDP=nearest-bridge
255)";
256 }
257
258 REQUIRE_CALL(fake, cb(std::vector<std::string>{"eth0"})).IN_SEQUENCE(seq1);
259 client->apply_changes();
260 REQUIRE(std::filesystem::exists(expectedFilePath));
261 REQUIRE(velia::utils::readFileToString(expectedFilePath) == expectedContents);
262
263 // reset the contents
264 client->delete_item("/ietf-interfaces:interfaces/interface[name='eth0']");
265 REQUIRE_CALL(fake, cb(std::vector<std::string>{"eth0"})).IN_SEQUENCE(seq1);
266 client->apply_changes();
267 REQUIRE(!std::filesystem::exists(expectedFilePath));
Tomáš Pecka39bbb512021-05-23 22:07:22 +0200268 }
269
Tomáš Pecka85f77752021-06-03 10:09:58 +0200270 SECTION("Setting IPs to eth0 and eth1")
271 {
272 const auto expectedFilePathEth0 = fakeConfigDir / "eth0.network";
273 const auto expectedFilePathEth1 = fakeConfigDir / "eth1.network";
274 const std::string expectedContentsEth0 = R"([Match]
275Name=eth0
Tomáš Pecka39bbb512021-05-23 22:07:22 +0200276
Tomáš Pecka85f77752021-06-03 10:09:58 +0200277[Network]
278Address=192.0.2.1/24
279LinkLocalAddressing=no
280DHCP=no
281LLDP=true
282EmitLLDP=nearest-bridge
283)";
284 const std::string expectedContentsEth1 = R"([Match]
285Name=eth1
Tomáš Pecka39bbb512021-05-23 22:07:22 +0200286
Tomáš Pecka85f77752021-06-03 10:09:58 +0200287[Network]
288Address=2001:db8::1/32
289DHCP=no
290LLDP=true
291EmitLLDP=nearest-bridge
292)";
293
294 client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/type", "iana-if-type:ethernetCsmacd");
295 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");
296 client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth1']/type", "iana-if-type:ethernetCsmacd");
297 client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth1']/ietf-ip:ipv6/ietf-ip:address[ip='2001:db8::1']/ietf-ip:prefix-length", "32");
298
299 REQUIRE_CALL(fake, cb(std::vector<std::string>{"eth0", "eth1"})).IN_SEQUENCE(seq1);
300 client->apply_changes();
301 REQUIRE(std::filesystem::exists(expectedFilePathEth0));
302 REQUIRE(std::filesystem::exists(expectedFilePathEth1));
303 REQUIRE(velia::utils::readFileToString(expectedFilePathEth0) == expectedContentsEth0);
304 REQUIRE(velia::utils::readFileToString(expectedFilePathEth1) == expectedContentsEth1);
305
306 // reset the contents
307 client->delete_item("/ietf-interfaces:interfaces/interface[name='eth0']");
308 client->delete_item("/ietf-interfaces:interfaces/interface[name='eth1']");
309 REQUIRE_CALL(fake, cb(std::vector<std::string>{"eth0", "eth1"})).IN_SEQUENCE(seq1);
310 client->apply_changes();
311 REQUIRE(!std::filesystem::exists(expectedFilePathEth0));
312 REQUIRE(!std::filesystem::exists(expectedFilePathEth1));
313 }
Tomáš Pecka39bbb512021-05-23 22:07:22 +0200314}