blob: b08a39e210f32d18f5d305456a16e1583ffdaa6b [file] [log] [blame]
Tomáš Pecka7acf3922021-08-10 11:16:57 +02001#include "trompeloeil_doctest.h"
Tomáš Pecka7acf3922021-08-10 11:16:57 +02002#include "system/LLDP.h"
Tomáš Pecka070f60d2021-10-13 21:48:14 +02003#include "system_vars.h"
Tomáš Pecka73cb5072024-09-11 20:45:30 +02004#include "tests/pretty_printers.h"
5#include "tests/test_log_setup.h"
Tomáš Pecka7acf3922021-08-10 11:16:57 +02006
7using namespace std::literals;
8
Tomáš Pecka7acf3922021-08-10 11:16:57 +02009TEST_CASE("Parsing with the mock")
10{
11 TEST_INIT_LOGS;
12
Tomáš Pecka7acf3922021-08-10 11:16:57 +020013 std::vector<velia::system::NeighborEntry> expected;
Tomáš Pecka070f60d2021-10-13 21:48:14 +020014 std::string json;
Tomáš Pecka7acf3922021-08-10 11:16:57 +020015
16 SECTION("LLDP active on a single link")
17 {
Tomáš Pecka070f60d2021-10-13 21:48:14 +020018 json = R"({"ve-image": [{"neighbor": {"systemName": "image", "portId": "host0", "chassisId": "7062a9e41c924ac6942da39c56e6b820", "enabledCapabilities": "a"}}]})";
Tomáš Pecka7acf3922021-08-10 11:16:57 +020019 expected = {
20 {"ve-image", {
21 {"remoteSysName", "image"},
22 {"remotePortId", "host0"},
23 {"remoteChassisId", "7062a9e41c924ac6942da39c56e6b820"},
Tomáš Pecka7acf3922021-08-10 11:16:57 +020024 {"systemCapabilitiesEnabled", "station-only"},
25 }}};
26 }
27
28 SECTION("No LLDP enabled")
29 {
Tomáš Pecka070f60d2021-10-13 21:48:14 +020030 json = "{}";
Tomáš Pecka7acf3922021-08-10 11:16:57 +020031 expected = {};
32 }
33
34 SECTION("Two LLDP links")
35 {
Tomáš Pecka070f60d2021-10-13 21:48:14 +020036 json = R"({
37"enp0s31f6": [{"neighbor": {"systemName": "sw-a1128-01.fit.cvut.cz", "portId": "Gi3/0/7", "chassisId": "00:b8:b3:e6:17:80", "enabledCapabilities": "b"}}],
38"ve-image": [{"neighbor": {"systemName": "image", "portId": "host0", "chassisId": "8b90f96f448140fb9b5d9d68e86d052e", "enabledCapabilities": "a"}}]
39})";
Tomáš Pecka7acf3922021-08-10 11:16:57 +020040 expected = {
41 {"enp0s31f6", {
42 {"remoteSysName", "sw-a1128-01.fit.cvut.cz"},
43 {"remotePortId", "Gi3/0/7"},
44 {"remoteChassisId", "00:b8:b3:e6:17:80"},
Tomáš Pecka7acf3922021-08-10 11:16:57 +020045 {"systemCapabilitiesEnabled", "bridge"},
46 }},
47 {"ve-image", {
48 {"remoteSysName", "image"},
49 {"remotePortId", "host0"},
50 {"remoteChassisId", "8b90f96f448140fb9b5d9d68e86d052e"},
Tomáš Pecka7acf3922021-08-10 11:16:57 +020051 {"systemCapabilitiesEnabled", "station-only"},
52 }},
53 };
54 }
55
56 SECTION("Multiple neighbors on one interface")
57 {
Tomáš Pecka070f60d2021-10-13 21:48:14 +020058 json = R"({"host0": [
59{"neighbor": {"systemName": "image", "portId": "host0", "chassisId": "1631331c24bb499bb644fcdf7c9fd467", "enabledCapabilities": "a"}},
60{"neighbor": {"systemName": "enterprise", "portId": "vb-image2", "chassisId": "1efe5cecbfc248a09065ad6177a98b41", "enabledCapabilities": "a"}}
61]})";
62
Tomáš Pecka7acf3922021-08-10 11:16:57 +020063 expected = {
64 {"host0", {
65 {"remoteSysName", "image"},
66 {"remotePortId", "host0"},
67 {"remoteChassisId", "1631331c24bb499bb644fcdf7c9fd467"},
Tomáš Pecka7acf3922021-08-10 11:16:57 +020068 {"systemCapabilitiesEnabled", "station-only"},
69 }},
70 {"host0", {
71 {"remoteSysName", "enterprise"},
72 {"remotePortId", "vb-image2"},
73 {"remoteChassisId", "1efe5cecbfc248a09065ad6177a98b41"},
Tomáš Pecka7acf3922021-08-10 11:16:57 +020074 {"systemCapabilitiesEnabled", "station-only"},
75 }},
76 };
77 }
78
Tomáš Pecka070f60d2021-10-13 21:48:14 +020079 auto lldp = std::make_shared<velia::system::LLDPDataProvider>([&]() { return json; });
Tomáš Pecka7acf3922021-08-10 11:16:57 +020080 REQUIRE(lldp->getNeighbors() == expected);
81}
82
83#if LIST_NEIGHBORS_RUN
84TEST_CASE("Real systemd")
85{
86 TEST_INIT_LOGS;
87
88 auto dbusConnection = sdbus::createSystemBusConnection();
Tomáš Pecka070f60d2021-10-13 21:48:14 +020089 auto lldp = std::make_shared<velia::system::LLDPDataProvider>([]() { return velia::utils::execAndWait(spdlog::get("system"), NETWORKCTL_EXECUTABLE, {"lldp", "--json=short"}, ""); });
Tomáš Pecka7acf3922021-08-10 11:16:57 +020090 [[maybe_unused]] auto x = lldp->getNeighbors();
91}
92#endif