blob: ffffb44da7b66d5031b956c021e928bc7eac20e8 [file] [log] [blame]
William A. Kennington III09f3a4a2022-10-25 02:59:16 -07001#include "config_parser.hpp"
Ratan Gupta35297172018-11-28 18:40:16 +05302#include "mock_network_manager.hpp"
Ratan Gupta255d5142017-08-10 09:02:08 +05303
William A. Kennington III09f3a4a2022-10-25 02:59:16 -07004#include <filesystem>
William A. Kennington IIIf51620d2019-04-21 18:54:13 -07005#include <sdbusplus/bus.hpp>
William A. Kennington III84bfe672022-07-13 14:15:30 -07006#include <stdplus/gtest/tmp.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -05007
8#include <gtest/gtest.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +05309
10namespace phosphor
11{
12namespace network
13{
14
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070015using ::testing::Key;
16using ::testing::UnorderedElementsAre;
Ratan Gupta255d5142017-08-10 09:02:08 +053017
William A. Kennington III84bfe672022-07-13 14:15:30 -070018class TestNetworkManager : public stdplus::gtest::TestWithTmp
Ratan Gupta8ab17922017-05-25 13:07:05 +053019{
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070020 protected:
Patrick Williamsc38b0712022-07-22 19:26:54 -050021 sdbusplus::bus_t bus;
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053022 MockManager manager;
Gunnar Mills57d9c502018-09-14 14:42:34 -050023 TestNetworkManager() :
William A. Kennington IIIf51620d2019-04-21 18:54:13 -070024 bus(sdbusplus::bus::new_default()),
William A. Kennington III84bfe672022-07-13 14:15:30 -070025 manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir())
Gunnar Mills57d9c502018-09-14 14:42:34 -050026 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050027 }
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070028
29 void deleteVLAN(std::string_view ifname)
30 {
31 manager.interfaces.find(ifname)->second->vlan->delete_();
32 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053033};
34
Ratan Gupta8ab17922017-05-25 13:07:05 +053035TEST_F(TestNetworkManager, NoInterface)
36{
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070037 EXPECT_TRUE(manager.interfaces.empty());
Ratan Gupta8ab17922017-05-25 13:07:05 +053038}
William A. Kennington III5b179382022-11-15 15:23:26 -080039
Ratan Gupta8ab17922017-05-25 13:07:05 +053040TEST_F(TestNetworkManager, WithSingleInterface)
41{
William A. Kennington III5b179382022-11-15 15:23:26 -080042 manager.addInterface({.idx = 2, .flags = 0, .name = "igb1"});
43 manager.handleAdminState("managed", 2);
Ratan Gupta8ab17922017-05-25 13:07:05 +053044
William A. Kennington III8cff3802019-04-21 20:17:27 -070045 // Now create the interfaces which will call the mocked getifaddrs
46 // which returns the above interface detail.
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070047 EXPECT_THAT(manager.interfaces, UnorderedElementsAre(Key("igb1")));
Ratan Gupta8ab17922017-05-25 13:07:05 +053048}
49
50// getifaddrs returns two interfaces.
51TEST_F(TestNetworkManager, WithMultipleInterfaces)
52{
William A. Kennington III5b179382022-11-15 15:23:26 -080053 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 Gupta8ab17922017-05-25 13:07:05 +053057
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070058 EXPECT_THAT(manager.interfaces,
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070059 UnorderedElementsAre(Key("igb0"), Key("igb1")));
Ratan Gupta8ab17922017-05-25 13:07:05 +053060}
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070061
62TEST_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 III5b179382022-11-15 15:23:26 -080068 manager.addInterface({.idx = 1, .flags = 0, .name = "eth0"});
69 manager.handleAdminState("managed", 1);
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070070 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 Mills57d9c502018-09-14 14:42:34 -050087} // namespace network
88} // namespace phosphor