blob: fde4de3ee9554e2d931952413e9bb1f8c544f396 [file] [log] [blame]
Tomáš Pecka7acf3922021-08-10 11:16:57 +02001#include "trompeloeil_doctest.h"
Tomáš Pecka7acf3922021-08-10 11:16:57 +02002#include "pretty_printers.h"
3#include "system/LLDP.h"
Tomáš Pecka070f60d2021-10-13 21:48:14 +02004#include "system_vars.h"
Tomáš Pecka7acf3922021-08-10 11:16:57 +02005#include "test_log_setup.h"
6#include "tests/configure.cmake.h"
Tomáš Pecka070f60d2021-10-13 21:48:14 +02007#include "utils/exec.h"
Tomáš Pecka7acf3922021-08-10 11:16:57 +02008
9using namespace std::literals;
10
11namespace velia::system {
12bool operator==(const NeighborEntry& a, const NeighborEntry& b)
13{
14 return std::tie(a.m_portId, a.m_properties) == std::tie(b.m_portId, b.m_properties);
15}
16}
17
18TEST_CASE("Parsing with the mock")
19{
20 TEST_INIT_LOGS;
21
Tomáš Pecka7acf3922021-08-10 11:16:57 +020022 std::vector<velia::system::NeighborEntry> expected;
Tomáš Pecka070f60d2021-10-13 21:48:14 +020023 std::string json;
Tomáš Pecka7acf3922021-08-10 11:16:57 +020024
25 SECTION("LLDP active on a single link")
26 {
Tomáš Pecka070f60d2021-10-13 21:48:14 +020027 json = R"({"ve-image": [{"neighbor": {"systemName": "image", "portId": "host0", "chassisId": "7062a9e41c924ac6942da39c56e6b820", "enabledCapabilities": "a"}}]})";
Tomáš Pecka7acf3922021-08-10 11:16:57 +020028 expected = {
29 {"ve-image", {
30 {"remoteSysName", "image"},
31 {"remotePortId", "host0"},
32 {"remoteChassisId", "7062a9e41c924ac6942da39c56e6b820"},
Tomáš Pecka7acf3922021-08-10 11:16:57 +020033 {"systemCapabilitiesEnabled", "station-only"},
34 }}};
35 }
36
37 SECTION("No LLDP enabled")
38 {
Tomáš Pecka070f60d2021-10-13 21:48:14 +020039 json = "{}";
Tomáš Pecka7acf3922021-08-10 11:16:57 +020040 expected = {};
41 }
42
43 SECTION("Two LLDP links")
44 {
Tomáš Pecka070f60d2021-10-13 21:48:14 +020045 json = R"({
46"enp0s31f6": [{"neighbor": {"systemName": "sw-a1128-01.fit.cvut.cz", "portId": "Gi3/0/7", "chassisId": "00:b8:b3:e6:17:80", "enabledCapabilities": "b"}}],
47"ve-image": [{"neighbor": {"systemName": "image", "portId": "host0", "chassisId": "8b90f96f448140fb9b5d9d68e86d052e", "enabledCapabilities": "a"}}]
48})";
Tomáš Pecka7acf3922021-08-10 11:16:57 +020049 expected = {
50 {"enp0s31f6", {
51 {"remoteSysName", "sw-a1128-01.fit.cvut.cz"},
52 {"remotePortId", "Gi3/0/7"},
53 {"remoteChassisId", "00:b8:b3:e6:17:80"},
Tomáš Pecka7acf3922021-08-10 11:16:57 +020054 {"systemCapabilitiesEnabled", "bridge"},
55 }},
56 {"ve-image", {
57 {"remoteSysName", "image"},
58 {"remotePortId", "host0"},
59 {"remoteChassisId", "8b90f96f448140fb9b5d9d68e86d052e"},
Tomáš Pecka7acf3922021-08-10 11:16:57 +020060 {"systemCapabilitiesEnabled", "station-only"},
61 }},
62 };
63 }
64
65 SECTION("Multiple neighbors on one interface")
66 {
Tomáš Pecka070f60d2021-10-13 21:48:14 +020067 json = R"({"host0": [
68{"neighbor": {"systemName": "image", "portId": "host0", "chassisId": "1631331c24bb499bb644fcdf7c9fd467", "enabledCapabilities": "a"}},
69{"neighbor": {"systemName": "enterprise", "portId": "vb-image2", "chassisId": "1efe5cecbfc248a09065ad6177a98b41", "enabledCapabilities": "a"}}
70]})";
71
Tomáš Pecka7acf3922021-08-10 11:16:57 +020072 expected = {
73 {"host0", {
74 {"remoteSysName", "image"},
75 {"remotePortId", "host0"},
76 {"remoteChassisId", "1631331c24bb499bb644fcdf7c9fd467"},
Tomáš Pecka7acf3922021-08-10 11:16:57 +020077 {"systemCapabilitiesEnabled", "station-only"},
78 }},
79 {"host0", {
80 {"remoteSysName", "enterprise"},
81 {"remotePortId", "vb-image2"},
82 {"remoteChassisId", "1efe5cecbfc248a09065ad6177a98b41"},
Tomáš Pecka7acf3922021-08-10 11:16:57 +020083 {"systemCapabilitiesEnabled", "station-only"},
84 }},
85 };
86 }
87
Tomáš Pecka070f60d2021-10-13 21:48:14 +020088 auto lldp = std::make_shared<velia::system::LLDPDataProvider>([&]() { return json; });
Tomáš Pecka7acf3922021-08-10 11:16:57 +020089 REQUIRE(lldp->getNeighbors() == expected);
90}
91
92#if LIST_NEIGHBORS_RUN
93TEST_CASE("Real systemd")
94{
95 TEST_INIT_LOGS;
96
97 auto dbusConnection = sdbus::createSystemBusConnection();
Tomáš Pecka070f60d2021-10-13 21:48:14 +020098 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 +020099 [[maybe_unused]] auto x = lldp->getNeighbors();
100}
101#endif