blob: 304ddcd396cfbcb2f51cef9f46fae6868c631e65 [file] [log] [blame]
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -08001#include "test_network_manager.hpp"
2
William A. Kennington III09f3a4a2022-10-25 02:59:16 -07003#include "config_parser.hpp"
Ratan Gupta255d5142017-08-10 09:02:08 +05304
William A. Kennington III9c441fd2023-02-24 13:40:01 -08005#include <net/if_arp.h>
6
William A. Kennington IIIf51620d2019-04-21 18:54:13 -07007#include <sdbusplus/bus.hpp>
William A. Kennington III84bfe672022-07-13 14:15:30 -07008#include <stdplus/gtest/tmp.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -05009
Patrick Williams89d734b2023-05-10 07:50:25 -050010#include <filesystem>
11
Gunnar Mills57d9c502018-09-14 14:42:34 -050012#include <gtest/gtest.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +053013
14namespace phosphor
15{
16namespace network
17{
18
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070019using ::testing::Key;
20using ::testing::UnorderedElementsAre;
Ratan Gupta255d5142017-08-10 09:02:08 +053021
William A. Kennington III84bfe672022-07-13 14:15:30 -070022class TestNetworkManager : public stdplus::gtest::TestWithTmp
Ratan Gupta8ab17922017-05-25 13:07:05 +053023{
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070024 protected:
William A. Kennington III9ede1b72022-11-21 01:59:28 -080025 stdplus::Pinned<sdbusplus::bus_t> bus;
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080026 TestManager manager;
Gunnar Mills57d9c502018-09-14 14:42:34 -050027 TestNetworkManager() :
William A. Kennington IIIf51620d2019-04-21 18:54:13 -070028 bus(sdbusplus::bus::new_default()),
William A. Kennington III84bfe672022-07-13 14:15:30 -070029 manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir())
Patrick Williams89d734b2023-05-10 07:50:25 -050030 {}
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070031
32 void deleteVLAN(std::string_view ifname)
33 {
34 manager.interfaces.find(ifname)->second->vlan->delete_();
35 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053036};
37
Ratan Gupta8ab17922017-05-25 13:07:05 +053038TEST_F(TestNetworkManager, NoInterface)
39{
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070040 EXPECT_TRUE(manager.interfaces.empty());
Ratan Gupta8ab17922017-05-25 13:07:05 +053041}
William A. Kennington III5b179382022-11-15 15:23:26 -080042
Ratan Gupta8ab17922017-05-25 13:07:05 +053043TEST_F(TestNetworkManager, WithSingleInterface)
44{
William A. Kennington III9c441fd2023-02-24 13:40:01 -080045 manager.addInterface(
46 {.type = ARPHRD_ETHER, .idx = 2, .flags = 0, .name = "igb1"});
William A. Kennington III5b179382022-11-15 15:23:26 -080047 manager.handleAdminState("managed", 2);
Ratan Gupta8ab17922017-05-25 13:07:05 +053048
William A. Kennington III8cff3802019-04-21 20:17:27 -070049 // Now create the interfaces which will call the mocked getifaddrs
50 // which returns the above interface detail.
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070051 EXPECT_THAT(manager.interfaces, UnorderedElementsAre(Key("igb1")));
Ratan Gupta8ab17922017-05-25 13:07:05 +053052}
53
54// getifaddrs returns two interfaces.
55TEST_F(TestNetworkManager, WithMultipleInterfaces)
56{
William A. Kennington III9c441fd2023-02-24 13:40:01 -080057 manager.addInterface(
58 {.type = ARPHRD_ETHER, .idx = 1, .flags = 0, .name = "igb0"});
William A. Kennington III5b179382022-11-15 15:23:26 -080059 manager.handleAdminState("managed", 1);
60 manager.handleAdminState("unmanaged", 2);
William A. Kennington III9c441fd2023-02-24 13:40:01 -080061 manager.addInterface(
62 {.type = ARPHRD_ETHER, .idx = 2, .flags = 0, .name = "igb1"});
Ratan Gupta8ab17922017-05-25 13:07:05 +053063
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070064 EXPECT_THAT(manager.interfaces,
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070065 UnorderedElementsAre(Key("igb0"), Key("igb1")));
Ratan Gupta8ab17922017-05-25 13:07:05 +053066}
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070067
68TEST_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
William A. Kennington III9c441fd2023-02-24 13:40:01 -080074 manager.addInterface(
75 {.type = ARPHRD_ETHER, .idx = 1, .flags = 0, .name = "eth0"});
William A. Kennington III5b179382022-11-15 15:23:26 -080076 manager.handleAdminState("managed", 1);
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070077 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
Gunnar Mills57d9c502018-09-14 14:42:34 -050094} // namespace network
95} // namespace phosphor