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 "system/LLDP.h" |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 3 | #include "system_vars.h" |
Tomáš Pecka | 73cb507 | 2024-09-11 20:45:30 +0200 | [diff] [blame^] | 4 | #include "tests/pretty_printers.h" |
| 5 | #include "tests/test_log_setup.h" |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 6 | |
| 7 | using namespace std::literals; |
| 8 | |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 9 | TEST_CASE("Parsing with the mock") |
| 10 | { |
| 11 | TEST_INIT_LOGS; |
| 12 | |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 13 | std::vector<velia::system::NeighborEntry> expected; |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 14 | std::string json; |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 15 | |
| 16 | SECTION("LLDP active on a single link") |
| 17 | { |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 18 | 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] | 19 | expected = { |
| 20 | {"ve-image", { |
| 21 | {"remoteSysName", "image"}, |
| 22 | {"remotePortId", "host0"}, |
| 23 | {"remoteChassisId", "7062a9e41c924ac6942da39c56e6b820"}, |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 24 | {"systemCapabilitiesEnabled", "station-only"}, |
| 25 | }}}; |
| 26 | } |
| 27 | |
| 28 | SECTION("No LLDP enabled") |
| 29 | { |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 30 | json = "{}"; |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 31 | expected = {}; |
| 32 | } |
| 33 | |
| 34 | SECTION("Two LLDP links") |
| 35 | { |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 36 | 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áš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 40 | 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áš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 45 | {"systemCapabilitiesEnabled", "bridge"}, |
| 46 | }}, |
| 47 | {"ve-image", { |
| 48 | {"remoteSysName", "image"}, |
| 49 | {"remotePortId", "host0"}, |
| 50 | {"remoteChassisId", "8b90f96f448140fb9b5d9d68e86d052e"}, |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 51 | {"systemCapabilitiesEnabled", "station-only"}, |
| 52 | }}, |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | SECTION("Multiple neighbors on one interface") |
| 57 | { |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 58 | 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áš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 63 | expected = { |
| 64 | {"host0", { |
| 65 | {"remoteSysName", "image"}, |
| 66 | {"remotePortId", "host0"}, |
| 67 | {"remoteChassisId", "1631331c24bb499bb644fcdf7c9fd467"}, |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 68 | {"systemCapabilitiesEnabled", "station-only"}, |
| 69 | }}, |
| 70 | {"host0", { |
| 71 | {"remoteSysName", "enterprise"}, |
| 72 | {"remotePortId", "vb-image2"}, |
| 73 | {"remoteChassisId", "1efe5cecbfc248a09065ad6177a98b41"}, |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 74 | {"systemCapabilitiesEnabled", "station-only"}, |
| 75 | }}, |
| 76 | }; |
| 77 | } |
| 78 | |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 79 | auto lldp = std::make_shared<velia::system::LLDPDataProvider>([&]() { return json; }); |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 80 | REQUIRE(lldp->getNeighbors() == expected); |
| 81 | } |
| 82 | |
| 83 | #if LIST_NEIGHBORS_RUN |
| 84 | TEST_CASE("Real systemd") |
| 85 | { |
| 86 | TEST_INIT_LOGS; |
| 87 | |
| 88 | auto dbusConnection = sdbus::createSystemBusConnection(); |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 89 | 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] | 90 | [[maybe_unused]] auto x = lldp->getNeighbors(); |
| 91 | } |
| 92 | #endif |