1 #include "test_network_manager.hpp"
2
3 #include "config_parser.hpp"
4
5 #include <net/if_arp.h>
6
7 #include <sdbusplus/bus.hpp>
8 #include <stdplus/gtest/tmp.hpp>
9
10 #include <filesystem>
11
12 #include <gtest/gtest.h>
13
14 namespace phosphor
15 {
16 namespace network
17 {
18
19 using ::testing::Key;
20 using ::testing::UnorderedElementsAre;
21
22 class TestNetworkManager : public stdplus::gtest::TestWithTmp
23 {
24 protected:
25 stdplus::Pinned<sdbusplus::bus_t> bus;
26 TestManager manager;
TestNetworkManager()27 TestNetworkManager() :
28 bus(sdbusplus::bus::new_default()),
29 manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir())
30 {}
31
deleteVLAN(std::string_view ifname)32 void deleteVLAN(std::string_view ifname)
33 {
34 manager.interfaces.find(ifname)->second->vlan->delete_();
35 }
36 };
37
TEST_F(TestNetworkManager,NoInterface)38 TEST_F(TestNetworkManager, NoInterface)
39 {
40 EXPECT_TRUE(manager.interfaces.empty());
41 }
42
TEST_F(TestNetworkManager,WithSingleInterface)43 TEST_F(TestNetworkManager, WithSingleInterface)
44 {
45 manager.addInterface(
46 {.type = ARPHRD_ETHER, .idx = 2, .flags = 0, .name = "igb1"});
47 manager.handleAdminState("managed", 2);
48
49 // Now create the interfaces which will call the mocked getifaddrs
50 // which returns the above interface detail.
51 EXPECT_THAT(manager.interfaces, UnorderedElementsAre(Key("igb1")));
52 }
53
54 // getifaddrs returns two interfaces.
TEST_F(TestNetworkManager,WithMultipleInterfaces)55 TEST_F(TestNetworkManager, WithMultipleInterfaces)
56 {
57 manager.addInterface(
58 {.type = ARPHRD_ETHER, .idx = 1, .flags = 0, .name = "igb0"});
59 manager.handleAdminState("managed", 1);
60 manager.handleAdminState("unmanaged", 2);
61 manager.addInterface(
62 {.type = ARPHRD_ETHER, .idx = 2, .flags = 0, .name = "igb1"});
63
64 EXPECT_THAT(manager.interfaces,
65 UnorderedElementsAre(Key("igb0"), Key("igb1")));
66 }
67
TEST_F(TestNetworkManager,WithVLAN)68 TEST_F(TestNetworkManager, WithVLAN)
69 {
70 EXPECT_THROW(manager.vlan("", 8000), std::exception);
71 EXPECT_THROW(manager.vlan("", 0), std::exception);
72 EXPECT_THROW(manager.vlan("eth0", 2), std::exception);
73
74 manager.addInterface(
75 {.type = ARPHRD_ETHER, .idx = 1, .flags = 0, .name = "eth0"});
76 manager.handleAdminState("managed", 1);
77 EXPECT_NO_THROW(manager.vlan("eth0", 2));
78 EXPECT_NO_THROW(manager.vlan("eth0", 4094));
79 EXPECT_THAT(
80 manager.interfaces,
81 UnorderedElementsAre(Key("eth0"), Key("eth0.2"), Key("eth0.4094")));
82 auto netdev1 = config::pathForIntfDev(CaseTmpDir(), "eth0.2");
83 auto netdev2 = config::pathForIntfDev(CaseTmpDir(), "eth0.4094");
84 EXPECT_TRUE(std::filesystem::is_regular_file(netdev1));
85 EXPECT_TRUE(std::filesystem::is_regular_file(netdev2));
86
87 deleteVLAN("eth0.2");
88 EXPECT_THAT(manager.interfaces,
89 UnorderedElementsAre(Key("eth0"), Key("eth0.4094")));
90 EXPECT_FALSE(std::filesystem::is_regular_file(netdev1));
91 EXPECT_TRUE(std::filesystem::is_regular_file(netdev2));
92 }
93
94 } // namespace network
95 } // namespace phosphor
96