blob: e6bcdf0ff7a6f4921ed0453902d63b304fe559dc [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
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]
188Name=eth0
189
190[Network]
191Description=Hello world
192Bridge=br0
193LLDP=true
194EmitLLDP=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]
203Name=eth0
204
205[Network]
206Bridge=br0
207LLDP=true
208EmitLLDP=nearest-bridge
209)";
210 }
211
Tomáš Peckade0ef2f2021-07-08 13:23:50 +0200212 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áš Pecka39bbb512021-05-23 22:07:22 +0200214 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}