blob: 7962d9e8458f872e8e062c203f7b596114907ab9 [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"
9#include "pretty_printers.h"
10#include "system/IETFInterfaces.h"
11#include "test_log_setup.h"
12#include "test_sysrepo_helpers.h"
13#include "tests/mock/system.h"
14
Tomáš Pecka71a98b02021-05-17 15:59:40 +020015using namespace std::string_literals;
16
Tomáš Peckaf10b9302021-02-23 19:02:02 +010017TEST_CASE("ietf-interfaces localhost")
18{
19 TEST_SYSREPO_INIT_LOGS;
20 TEST_SYSREPO_INIT;
Tomáš Pecka70e54562021-03-10 12:39:03 +010021
Tomáš Peckaf10b9302021-02-23 19:02:02 +010022 TEST_SYSREPO_INIT_CLIENT;
23
24 auto network = std::make_shared<velia::system::IETFInterfaces>(srSess);
25
26 // We didn't came up with some way of mocking netlink. At least check that there is the loopback
27 // 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 +010028 auto lo = dataFromSysrepo(client, "/ietf-interfaces:interfaces/interface[name='lo']", SR_DS_OPERATIONAL);
29
30 // ensure statistics keys exist and remove them ; we can't really predict the content
31 for (const auto& key : {"/statistics", "/statistics/in-discards", "/statistics/in-errors", "/statistics/in-octets", "/statistics/out-discards", "/statistics/out-errors", "/statistics/out-octets"}) {
32 auto it = lo.find(key);
33 REQUIRE(it != lo.end());
34 lo.erase(it);
35 }
36
37 REQUIRE(lo == std::map<std::string, std::string> {
Tomáš Peckaf10b9302021-02-23 19:02:02 +010038 {"/name", "lo"},
39 {"/type", "iana-if-type:softwareLoopback"},
40 {"/phys-address", "00:00:00:00:00:00"},
41 {"/oper-status", "unknown"},
Tomáš Pecka09729382021-03-08 19:36:50 +010042 {"/ietf-ip:ipv4", ""},
43 {"/ietf-ip:ipv4/address[ip='127.0.0.1']", ""},
44 {"/ietf-ip:ipv4/address[ip='127.0.0.1']/ip", "127.0.0.1"},
45 {"/ietf-ip:ipv4/address[ip='127.0.0.1']/prefix-length", "8"},
46 {"/ietf-ip:ipv6", ""},
47 {"/ietf-ip:ipv6/address[ip='::1']", ""},
48 {"/ietf-ip:ipv6/address[ip='::1']/ip", "::1"},
49 {"/ietf-ip:ipv6/address[ip='::1']/prefix-length", "128"},
Tomáš Peckaf10b9302021-02-23 19:02:02 +010050 });
Tomáš Pecka3663aae2021-03-10 13:46:31 +010051 // NOTE: There are no neighbours on loopback
Tomáš Pecka71a98b02021-05-17 15:59:40 +020052
53 SECTION("Link changes disabled")
54 {
55 client->session_switch_ds(SR_DS_STARTUP);
56
57 SECTION("Only specified link names can appear in configurable datastore")
58 {
59 for (const auto& [name, type] : {std::pair<const char*, const char*>{"eth0", "iana-if-type:ethernetCsmacd"},
60 {"eth1", "iana-if-type:ethernetCsmacd"},
61 {"br0", "iana-if-type:bridge"},
62 {"osc", "iana-if-type:ethernetCsmacd"},
63 {"oscW", "iana-if-type:ethernetCsmacd"},
64 {"oscE", "iana-if-type:ethernetCsmacd"}}) {
65 client->set_item_str(("/ietf-interfaces:interfaces/interface[name='"s + name + "']/type").c_str(), type);
66 }
67 client->apply_changes();
68 }
69
70 SECTION("Invalid type for a valid link")
71 {
72 client->set_item_str("/ietf-interfaces:interfaces/interface[name='eth0']/type", "iana-if-type:softwareLoopback");
73 REQUIRE_THROWS_AS(client->apply_changes(), sysrepo::sysrepo_exception);
74 }
75
76 SECTION("Invalid name")
77 {
78 client->set_item_str("/ietf-interfaces:interfaces/interface[name='blah0']/type", "iana-if-type:ethernetCsmacd");
79 REQUIRE_THROWS_AS(client->apply_changes(), sysrepo::sysrepo_exception);
80 }
81 }
Tomáš Peckaf10b9302021-02-23 19:02:02 +010082}