Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 1 | #include "trompeloeil_doctest.h" |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 2 | #include "pretty_printers.h" |
| 3 | #include "system/LLDP.h" |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 4 | #include "system_vars.h" |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 5 | #include "test_log_setup.h" |
| 6 | #include "tests/configure.cmake.h" |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 7 | #include "utils/exec.h" |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 8 | |
| 9 | using namespace std::literals; |
| 10 | |
| 11 | namespace velia::system { |
| 12 | bool 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 | |
| 18 | TEST_CASE("Parsing with the mock") |
| 19 | { |
| 20 | TEST_INIT_LOGS; |
| 21 | |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 22 | std::vector<velia::system::NeighborEntry> expected; |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 23 | std::string json; |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 24 | |
| 25 | SECTION("LLDP active on a single link") |
| 26 | { |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 27 | json = R"({"ve-image": [{"neighbor": {"systemName": "image", "portId": "host0", "chassisId": "7062a9e41c924ac6942da39c56e6b820", "enabledCapabilities": "a"}}]})"; |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 28 | expected = { |
| 29 | {"ve-image", { |
| 30 | {"remoteSysName", "image"}, |
| 31 | {"remotePortId", "host0"}, |
| 32 | {"remoteChassisId", "7062a9e41c924ac6942da39c56e6b820"}, |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 33 | {"systemCapabilitiesEnabled", "station-only"}, |
| 34 | }}}; |
| 35 | } |
| 36 | |
| 37 | SECTION("No LLDP enabled") |
| 38 | { |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 39 | json = "{}"; |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 40 | expected = {}; |
| 41 | } |
| 42 | |
| 43 | SECTION("Two LLDP links") |
| 44 | { |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 45 | 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áš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 49 | 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áš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 54 | {"systemCapabilitiesEnabled", "bridge"}, |
| 55 | }}, |
| 56 | {"ve-image", { |
| 57 | {"remoteSysName", "image"}, |
| 58 | {"remotePortId", "host0"}, |
| 59 | {"remoteChassisId", "8b90f96f448140fb9b5d9d68e86d052e"}, |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 60 | {"systemCapabilitiesEnabled", "station-only"}, |
| 61 | }}, |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | SECTION("Multiple neighbors on one interface") |
| 66 | { |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 67 | 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áš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 72 | expected = { |
| 73 | {"host0", { |
| 74 | {"remoteSysName", "image"}, |
| 75 | {"remotePortId", "host0"}, |
| 76 | {"remoteChassisId", "1631331c24bb499bb644fcdf7c9fd467"}, |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 77 | {"systemCapabilitiesEnabled", "station-only"}, |
| 78 | }}, |
| 79 | {"host0", { |
| 80 | {"remoteSysName", "enterprise"}, |
| 81 | {"remotePortId", "vb-image2"}, |
| 82 | {"remoteChassisId", "1efe5cecbfc248a09065ad6177a98b41"}, |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 83 | {"systemCapabilitiesEnabled", "station-only"}, |
| 84 | }}, |
| 85 | }; |
| 86 | } |
| 87 | |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 88 | auto lldp = std::make_shared<velia::system::LLDPDataProvider>([&]() { return json; }); |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 89 | REQUIRE(lldp->getNeighbors() == expected); |
| 90 | } |
| 91 | |
| 92 | #if LIST_NEIGHBORS_RUN |
| 93 | TEST_CASE("Real systemd") |
| 94 | { |
| 95 | TEST_INIT_LOGS; |
| 96 | |
| 97 | auto dbusConnection = sdbus::createSystemBusConnection(); |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 98 | auto lldp = std::make_shared<velia::system::LLDPDataProvider>([]() { return velia::utils::execAndWait(spdlog::get("system"), NETWORKCTL_EXECUTABLE, {"lldp", "--json=short"}, ""); }); |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 99 | [[maybe_unused]] auto x = lldp->getNeighbors(); |
| 100 | } |
| 101 | #endif |