Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame] | 1 | /* |
| 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 <filesystem> |
| 10 | #include "fs-helpers/utils.h" |
| 11 | #include "pretty_printers.h" |
| 12 | #include "system/Network.h" |
| 13 | #include "test_log_setup.h" |
| 14 | #include "test_sysrepo_helpers.h" |
| 15 | #include "tests/configure.cmake.h" |
| 16 | #include "utils/io.h" |
| 17 | |
| 18 | using namespace std::string_literals; |
| 19 | using namespace std::chrono_literals; |
| 20 | |
| 21 | struct FakeNetworkReload { |
| 22 | public: |
| 23 | MAKE_CONST_MOCK1(cb, void(const std::vector<std::string>&)); |
| 24 | }; |
| 25 | |
| 26 | TEST_CASE("Standalone eth1") |
| 27 | { |
| 28 | const auto PRESENCE_CONTAINER = "/czechlight-system:networking/standalone-eth1"; |
| 29 | |
| 30 | static const std::string EXPECTED_CONTENT_BRIDGE = R"([Match] |
| 31 | Name=eth1 |
| 32 | |
| 33 | [Network] |
| 34 | Bridge=br0 |
| 35 | LLDP=true |
| 36 | EmitLLDP=nearest-bridge |
| 37 | )"; |
| 38 | |
| 39 | static const std::string EXPECTED_CONTENT_DHCP = R"([Match] |
| 40 | Name=eth1 |
| 41 | |
| 42 | [Network] |
| 43 | DHCP=yes |
| 44 | LLDP=true |
| 45 | EmitLLDP=nearest-bridge |
| 46 | )"; |
| 47 | |
| 48 | TEST_SYSREPO_INIT_LOGS; |
| 49 | TEST_SYSREPO_INIT; |
| 50 | TEST_SYSREPO_INIT_CLIENT; |
Tomáš Pecka | a936ee7 | 2021-02-15 21:37:10 +0100 | [diff] [blame^] | 51 | |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame] | 52 | trompeloeil::sequence seq1; |
| 53 | std::string expectedContent; |
| 54 | auto fake = FakeNetworkReload(); |
| 55 | auto fakeDir = std::filesystem::path(CMAKE_CURRENT_BINARY_DIR) / "tests/network/"s; |
| 56 | auto expectedFilePath = fakeDir / "eth1.network"s; |
| 57 | |
| 58 | // reset |
| 59 | removeDirectoryTreeIfExists(fakeDir); |
| 60 | std::filesystem::create_directories(fakeDir); |
| 61 | |
Tomáš Pecka | a936ee7 | 2021-02-15 21:37:10 +0100 | [diff] [blame^] | 62 | SECTION("Running DS") |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame] | 63 | { |
Tomáš Pecka | a936ee7 | 2021-02-15 21:37:10 +0100 | [diff] [blame^] | 64 | SECTION("Container not present. Start with bridge configuration") |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame] | 65 | { |
Tomáš Pecka | a936ee7 | 2021-02-15 21:37:10 +0100 | [diff] [blame^] | 66 | client->delete_item(PRESENCE_CONTAINER); |
| 67 | client->apply_changes(); |
| 68 | |
| 69 | REQUIRE_CALL(fake, cb(std::vector<std::string> {"eth1"})).IN_SEQUENCE(seq1); |
| 70 | auto network = std::make_shared<velia::system::Network>(srSess, fakeDir, [&fake](const std::vector<std::string>& updatedInterfaces) { fake.cb(updatedInterfaces); }); |
| 71 | |
| 72 | REQUIRE(std::filesystem::exists(expectedFilePath)); |
| 73 | REQUIRE(velia::utils::readFileToString(expectedFilePath) == EXPECTED_CONTENT_BRIDGE); |
| 74 | |
| 75 | SECTION("Nothing happens") |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | SECTION("Change: Container present. Switch to DHCP configuration") |
| 80 | { |
| 81 | REQUIRE_CALL(fake, cb(std::vector<std::string> {"eth1"})).IN_SEQUENCE(seq1); |
| 82 | |
| 83 | client->set_item(PRESENCE_CONTAINER); |
| 84 | client->apply_changes(); |
| 85 | waitForCompletionAndBitMore(seq1); |
| 86 | |
| 87 | REQUIRE(std::filesystem::exists(expectedFilePath)); |
| 88 | REQUIRE(velia::utils::readFileToString(expectedFilePath) == EXPECTED_CONTENT_DHCP); |
| 89 | } |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame] | 90 | |
| 91 | } |
| 92 | |
Tomáš Pecka | a936ee7 | 2021-02-15 21:37:10 +0100 | [diff] [blame^] | 93 | SECTION("Container present. Start with DHCP configuration") |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame] | 94 | { |
Tomáš Pecka | a936ee7 | 2021-02-15 21:37:10 +0100 | [diff] [blame^] | 95 | client->set_item(PRESENCE_CONTAINER); |
| 96 | client->apply_changes(); |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame] | 97 | |
Tomáš Pecka | a936ee7 | 2021-02-15 21:37:10 +0100 | [diff] [blame^] | 98 | REQUIRE_CALL(fake, cb(std::vector<std::string> {"eth1"})).IN_SEQUENCE(seq1); |
| 99 | auto network = std::make_shared<velia::system::Network>(srSess, fakeDir, [&fake](const std::vector<std::string>& updatedInterfaces) { fake.cb(updatedInterfaces); }); |
| 100 | |
| 101 | REQUIRE(std::filesystem::exists(expectedFilePath)); |
| 102 | REQUIRE(velia::utils::readFileToString(expectedFilePath) == EXPECTED_CONTENT_DHCP); |
| 103 | |
| 104 | SECTION("Nothing happens") |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | SECTION("Change: Container not present. Switch to bridge configuration") |
| 109 | { |
| 110 | REQUIRE_CALL(fake, cb(std::vector<std::string> {"eth1"})).IN_SEQUENCE(seq1); |
| 111 | |
| 112 | client->delete_item(PRESENCE_CONTAINER); |
| 113 | client->apply_changes(); |
| 114 | waitForCompletionAndBitMore(seq1); |
| 115 | |
| 116 | REQUIRE(std::filesystem::exists(expectedFilePath)); |
| 117 | REQUIRE(velia::utils::readFileToString(expectedFilePath) == EXPECTED_CONTENT_BRIDGE); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | SECTION("Startup DS") |
| 123 | { |
| 124 | srSess->session_switch_ds(SR_DS_STARTUP); |
| 125 | client->session_switch_ds(SR_DS_STARTUP); |
| 126 | |
| 127 | auto network = std::make_shared<velia::system::Network>(srSess, fakeDir, []([[maybe_unused]] const std::vector<std::string>& updatedInterfaces) {}); |
| 128 | |
| 129 | SECTION("Change in startup datastore: Persist DHCP configuration") |
| 130 | { |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame] | 131 | client->set_item(PRESENCE_CONTAINER); |
| 132 | client->apply_changes(); |
| 133 | |
| 134 | REQUIRE(std::filesystem::exists(expectedFilePath)); |
| 135 | REQUIRE(velia::utils::readFileToString(expectedFilePath) == EXPECTED_CONTENT_DHCP); |
| 136 | } |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame] | 137 | |
Tomáš Pecka | a936ee7 | 2021-02-15 21:37:10 +0100 | [diff] [blame^] | 138 | SECTION("Change in startup datastore: Persist bridge configuration") |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame] | 139 | { |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame] | 140 | client->delete_item(PRESENCE_CONTAINER); |
| 141 | client->apply_changes(); |
| 142 | |
| 143 | REQUIRE(std::filesystem::exists(expectedFilePath)); |
| 144 | REQUIRE(velia::utils::readFileToString(expectedFilePath) == EXPECTED_CONTENT_BRIDGE); |
| 145 | } |
| 146 | } |
| 147 | } |