William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 1 | #include "test_network_manager.hpp" |
| 2 | |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 3 | #include "config_parser.hpp" |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 4 | |
William A. Kennington III | f51620d | 2019-04-21 18:54:13 -0700 | [diff] [blame] | 5 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 6 | #include <stdplus/gtest/tmp.hpp> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 7 | |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 8 | #include <filesystem> |
| 9 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 11 | |
| 12 | namespace phosphor |
| 13 | { |
| 14 | namespace network |
| 15 | { |
| 16 | |
William A. Kennington III | 9ecb90e | 2022-10-14 03:12:43 -0700 | [diff] [blame] | 17 | using ::testing::Key; |
| 18 | using ::testing::UnorderedElementsAre; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 19 | |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 20 | class TestNetworkManager : public stdplus::gtest::TestWithTmp |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 21 | { |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 22 | protected: |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 23 | stdplus::Pinned<sdbusplus::bus_t> bus; |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 24 | TestManager manager; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 25 | TestNetworkManager() : |
William A. Kennington III | f51620d | 2019-04-21 18:54:13 -0700 | [diff] [blame] | 26 | bus(sdbusplus::bus::new_default()), |
William A. Kennington III | 84bfe67 | 2022-07-13 14:15:30 -0700 | [diff] [blame] | 27 | manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir()) |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 28 | {} |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 29 | |
| 30 | void deleteVLAN(std::string_view ifname) |
| 31 | { |
| 32 | manager.interfaces.find(ifname)->second->vlan->delete_(); |
| 33 | } |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 34 | }; |
| 35 | |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 36 | TEST_F(TestNetworkManager, NoInterface) |
| 37 | { |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 38 | EXPECT_TRUE(manager.interfaces.empty()); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 39 | } |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 40 | |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 41 | TEST_F(TestNetworkManager, WithSingleInterface) |
| 42 | { |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 43 | manager.addInterface({.idx = 2, .flags = 0, .name = "igb1"}); |
| 44 | manager.handleAdminState("managed", 2); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 45 | |
William A. Kennington III | 8cff380 | 2019-04-21 20:17:27 -0700 | [diff] [blame] | 46 | // Now create the interfaces which will call the mocked getifaddrs |
| 47 | // which returns the above interface detail. |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 48 | EXPECT_THAT(manager.interfaces, UnorderedElementsAre(Key("igb1"))); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // getifaddrs returns two interfaces. |
| 52 | TEST_F(TestNetworkManager, WithMultipleInterfaces) |
| 53 | { |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 54 | manager.addInterface({.idx = 1, .flags = 0, .name = "igb0"}); |
| 55 | manager.handleAdminState("managed", 1); |
| 56 | manager.handleAdminState("unmanaged", 2); |
| 57 | manager.addInterface({.idx = 2, .flags = 0, .name = "igb1"}); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 58 | |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 59 | EXPECT_THAT(manager.interfaces, |
William A. Kennington III | 9ecb90e | 2022-10-14 03:12:43 -0700 | [diff] [blame] | 60 | UnorderedElementsAre(Key("igb0"), Key("igb1"))); |
Ratan Gupta | 8ab1792 | 2017-05-25 13:07:05 +0530 | [diff] [blame] | 61 | } |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 62 | |
| 63 | TEST_F(TestNetworkManager, WithVLAN) |
| 64 | { |
| 65 | EXPECT_THROW(manager.vlan("", 8000), std::exception); |
| 66 | EXPECT_THROW(manager.vlan("", 0), std::exception); |
| 67 | EXPECT_THROW(manager.vlan("eth0", 2), std::exception); |
| 68 | |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 69 | manager.addInterface({.idx = 1, .flags = 0, .name = "eth0"}); |
| 70 | manager.handleAdminState("managed", 1); |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 71 | EXPECT_NO_THROW(manager.vlan("eth0", 2)); |
| 72 | EXPECT_NO_THROW(manager.vlan("eth0", 4094)); |
| 73 | EXPECT_THAT( |
| 74 | manager.interfaces, |
| 75 | UnorderedElementsAre(Key("eth0"), Key("eth0.2"), Key("eth0.4094"))); |
| 76 | auto netdev1 = config::pathForIntfDev(CaseTmpDir(), "eth0.2"); |
| 77 | auto netdev2 = config::pathForIntfDev(CaseTmpDir(), "eth0.4094"); |
| 78 | EXPECT_TRUE(std::filesystem::is_regular_file(netdev1)); |
| 79 | EXPECT_TRUE(std::filesystem::is_regular_file(netdev2)); |
| 80 | |
| 81 | deleteVLAN("eth0.2"); |
| 82 | EXPECT_THAT(manager.interfaces, |
| 83 | UnorderedElementsAre(Key("eth0"), Key("eth0.4094"))); |
| 84 | EXPECT_FALSE(std::filesystem::is_regular_file(netdev1)); |
| 85 | EXPECT_TRUE(std::filesystem::is_regular_file(netdev2)); |
| 86 | } |
| 87 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 88 | } // namespace network |
| 89 | } // namespace phosphor |