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