1 #include "test_network_manager.hpp"
2 
3 #include "config_parser.hpp"
4 
5 #include <filesystem>
6 #include <sdbusplus/bus.hpp>
7 #include <stdplus/gtest/tmp.hpp>
8 
9 #include <gtest/gtest.h>
10 
11 namespace phosphor
12 {
13 namespace network
14 {
15 
16 using ::testing::Key;
17 using ::testing::UnorderedElementsAre;
18 
19 class TestNetworkManager : public stdplus::gtest::TestWithTmp
20 {
21   protected:
22     sdbusplus::bus_t bus;
23     TestManager manager;
24     TestNetworkManager() :
25         bus(sdbusplus::bus::new_default()),
26         manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir())
27     {
28     }
29 
30     void deleteVLAN(std::string_view ifname)
31     {
32         manager.interfaces.find(ifname)->second->vlan->delete_();
33     }
34 };
35 
36 TEST_F(TestNetworkManager, NoInterface)
37 {
38     EXPECT_TRUE(manager.interfaces.empty());
39 }
40 
41 TEST_F(TestNetworkManager, WithSingleInterface)
42 {
43     manager.addInterface({.idx = 2, .flags = 0, .name = "igb1"});
44     manager.handleAdminState("managed", 2);
45 
46     // Now create the interfaces which will call the mocked getifaddrs
47     // which returns the above interface detail.
48     EXPECT_THAT(manager.interfaces, UnorderedElementsAre(Key("igb1")));
49 }
50 
51 // getifaddrs returns two interfaces.
52 TEST_F(TestNetworkManager, WithMultipleInterfaces)
53 {
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"});
58 
59     EXPECT_THAT(manager.interfaces,
60                 UnorderedElementsAre(Key("igb0"), Key("igb1")));
61 }
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 
69     manager.addInterface({.idx = 1, .flags = 0, .name = "eth0"});
70     manager.handleAdminState("managed", 1);
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 
88 } // namespace network
89 } // namespace phosphor
90