blob: a0e716eebb0517bfeb9a19b10d72122aa670d135 [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);
69 }
70 client->apply_changes();
71 }
72
73 SECTION("Invalid type for a valid link")
74 {
75 client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/type", "iana-if-type:softwareLoopback");
76 REQUIRE_THROWS_AS(client->apply_changes(), sysrepo::sysrepo_exception);
77 }
78
79 SECTION("Invalid name")
80 {
81 client->set_item_str("/ietf-interfaces:interfaces/interface[name='blah0']/type", "iana-if-type:ethernetCsmacd");
82 REQUIRE_THROWS_AS(client->apply_changes(), sysrepo::sysrepo_exception);
83 }
84 }
Tomáš Peckaf10b9302021-02-23 19:02:02 +010085}
Tomáš Pecka39bbb512021-05-23 22:07:22 +020086
87struct FakeNetworkReload {
88public:
89 MAKE_CONST_MOCK1(cb, void(const std::vector<std::string>&));
90};
91
92TEST_CASE("Config data in ietf-interfaces")
93{
94 TEST_SYSREPO_INIT_LOGS;
95 TEST_SYSREPO_INIT;
96 TEST_SYSREPO_INIT_CLIENT;
97 trompeloeil::sequence seq1;
98
99 auto fake = FakeNetworkReload();
100
101 const auto fakeConfigDir = std::filesystem::path(CMAKE_CURRENT_BINARY_DIR) / "tests/network/"s;
102 std::filesystem::remove_all(fakeConfigDir);
103 std::filesystem::create_directories(fakeConfigDir);
104
105 auto expectedFilePath = fakeConfigDir / "eth0.network";
106
107 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); });
108
109 std::string expectedContents;
110 SECTION("With description")
111 {
112 expectedContents = R"([Match]
113Name=eth0
114
115[Network]
116Description=Hello world
117Bridge=br0
118LLDP=true
119EmitLLDP=nearest-bridge
120)";
121
122 client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/description", "Hello world");
123 }
124
125 SECTION("No description")
126 {
127 expectedContents = R"([Match]
128Name=eth0
129
130[Network]
131Bridge=br0
132LLDP=true
133EmitLLDP=nearest-bridge
134)";
135 }
136
137 client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/type", "iana-if-type:ethernetCsmacd");
138
139 REQUIRE_CALL(fake, cb(std::vector<std::string>{"eth0"})).IN_SEQUENCE(seq1);
140 client->apply_changes();
141 REQUIRE(std::filesystem::exists(expectedFilePath));
142 REQUIRE(velia::utils::readFileToString(expectedFilePath) == expectedContents);
143
144 // reset the contents
145 client->delete_item("/ietf-interfaces:interfaces/interface[name='eth0']");
146 REQUIRE_CALL(fake, cb(std::vector<std::string>{"eth0"})).IN_SEQUENCE(seq1);
147 client->apply_changes();
148 REQUIRE(!std::filesystem::exists(expectedFilePath));
149}