|
| 1 | +# Copyright (C) 2025 Intel Corporation |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +from textwrap import dedent |
| 4 | +from unittest.mock import call |
| 5 | + |
| 6 | +import pytest |
| 7 | +from mfd_connect import RPyCConnection |
| 8 | +from mfd_connect.base import ConnectionCompletedProcess |
| 9 | +from mfd_typing import PCIAddress, OSName |
| 10 | +from mfd_typing.network_interface import LinuxInterfaceInfo |
| 11 | + |
| 12 | +from mfd_network_adapter.exceptions import VlanNotFoundException, VlanAlreadyExistsException |
| 13 | +from mfd_network_adapter.network_adapter_owner.linux import LinuxNetworkAdapterOwner |
| 14 | +from mfd_network_adapter.network_interface.feature.vlan import LinuxVLAN |
| 15 | +from mfd_network_adapter.network_interface.linux import LinuxNetworkInterface |
| 16 | + |
| 17 | + |
| 18 | +class TestVlanLinux: |
| 19 | + @pytest.fixture() |
| 20 | + def vlan(self, mocker): |
| 21 | + pci_address = PCIAddress(0, 0, 0, 0) |
| 22 | + name = "eth1" |
| 23 | + mock_connection = mocker.create_autospec(RPyCConnection) |
| 24 | + mock_connection.get_os_name.return_value = OSName.LINUX |
| 25 | + |
| 26 | + interface = LinuxNetworkInterface( |
| 27 | + connection=mock_connection, |
| 28 | + interface_info=LinuxInterfaceInfo(pci_address=pci_address, name=name), |
| 29 | + ) |
| 30 | + |
| 31 | + mock_owner = mocker.create_autospec(LinuxNetworkAdapterOwner) |
| 32 | + mock_owner._connection = mock_connection |
| 33 | + |
| 34 | + vlan = LinuxVLAN(connection=mock_connection, interface=interface) |
| 35 | + vlan.owner = mock_owner |
| 36 | + vlan._interface = lambda: interface |
| 37 | + |
| 38 | + return vlan |
| 39 | + |
| 40 | + def test_get_vlan_ids_multiple_vlans(self, vlan): |
| 41 | + vlan.owner._connection.execute_command.return_value = ConnectionCompletedProcess( |
| 42 | + return_code=0, |
| 43 | + args="command", |
| 44 | + stdout=dedent( |
| 45 | + """ |
| 46 | + eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 |
| 47 | + inet 10.91.218.28 netmask 255.255.252.0 broadcast 10.91.219.255 |
| 48 | + ether 52:5a:00:5b:da:1c txqueuelen 1000 (Ethernet) |
| 49 | + RX packets 6402 bytes 814401 (795.3 KiB) |
| 50 | + RX errors 0 dropped 0 overruns 0 frame 0 |
| 51 | + TX packets 3063 bytes 378535 (369.6 KiB) |
| 52 | + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 |
| 53 | +
|
| 54 | + eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 |
| 55 | + inet 1.1.10.1 netmask 255.0.0.0 broadcast 0.0.0.0 |
| 56 | + ether 00:15:5d:24:32:2e txqueuelen 1000 (Ethernet) |
| 57 | + RX packets 435 bytes 42015 (41.0 KiB) |
| 58 | + RX errors 0 dropped 0 overruns 0 frame 0 |
| 59 | + TX packets 18 bytes 1947 (1.9 KiB) |
| 60 | + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 |
| 61 | +
|
| 62 | + eth1.100: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 |
| 63 | + inet6 fe80::215:5dff:fe24:322e prefixlen 64 scopeid 0x20<link> |
| 64 | + ether 00:15:5d:24:32:2e txqueuelen 1000 (Ethernet) |
| 65 | + RX packets 0 bytes 0 (0.0 B) |
| 66 | + RX errors 0 dropped 0 overruns 0 frame 0 |
| 67 | + TX packets 1 bytes 90 (90.0 B) |
| 68 | + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0") |
| 69 | +
|
| 70 | + eth1.200: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 |
| 71 | + inet6 fe80::215:5dff:fe24:322e prefixlen 64 scopeid 0x20<link> |
| 72 | + ether 00:15:5d:24:32:2e txqueuelen 1000 (Ethernet) |
| 73 | + RX packets 0 bytes 0 (0.0 B) |
| 74 | + RX errors 0 dropped 0 overruns 0 frame 0 |
| 75 | + TX packets 1 bytes 90 (90.0 B) |
| 76 | + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0") |
| 77 | +
|
| 78 | + eth1.300: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 |
| 79 | + inet6 fe80::215:5dff:fe24:322e prefixlen 64 scopeid 0x20<link> |
| 80 | + ether 00:15:5d:24:32:2e txqueuelen 1000 (Ethernet) |
| 81 | + RX packets 0 bytes 0 (0.0 B) |
| 82 | + RX errors 0 dropped 0 overruns 0 frame 0 |
| 83 | + TX packets 1 bytes 90 (90.0 B) |
| 84 | + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0") |
| 85 | + """ |
| 86 | + ), |
| 87 | + stderr="", |
| 88 | + ) |
| 89 | + result = vlan.get_vlan_ids() |
| 90 | + vlan.owner._connection.execute_command.assert_called_once_with( |
| 91 | + "ifconfig", |
| 92 | + expected_return_codes={0}, |
| 93 | + ) |
| 94 | + assert result == [100, 200, 300] |
| 95 | + |
| 96 | + def test_get_vlan_ids_no_vlans(self, vlan): |
| 97 | + vlan.owner._connection.execute_command.return_value = ConnectionCompletedProcess( |
| 98 | + return_code=0, |
| 99 | + args="command", |
| 100 | + stdout=dedent( |
| 101 | + """ |
| 102 | + eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 |
| 103 | + inet 10.91.218.28 netmask 255.255.252.0 broadcast 10.91.219.255 |
| 104 | + ether 52:5a:00:5b:da:1c txqueuelen 1000 (Ethernet) |
| 105 | + RX packets 6402 bytes 814401 (795.3 KiB) |
| 106 | + RX errors 0 dropped 0 overruns 0 frame 0 |
| 107 | + TX packets 3063 bytes 378535 (369.6 KiB) |
| 108 | + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 |
| 109 | +
|
| 110 | + eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 |
| 111 | + inet 1.1.10.1 netmask 255.0.0.0 broadcast 0.0.0.0 |
| 112 | + ether 00:15:5d:24:32:2e txqueuelen 1000 (Ethernet) |
| 113 | + RX packets 435 bytes 42015 (41.0 KiB) |
| 114 | + RX errors 0 dropped 0 overruns 0 frame 0 |
| 115 | + TX packets 18 bytes 1947 (1.9 KiB) |
| 116 | + TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 |
| 117 | + """ |
| 118 | + ), |
| 119 | + stderr="", |
| 120 | + ) |
| 121 | + result = vlan.get_vlan_ids() |
| 122 | + assert result == [] |
| 123 | + |
| 124 | + def test_get_vlan_id_vlan_exist(self, vlan, mocker): |
| 125 | + mocker.patch.object(vlan, "get_vlan_ids", return_value=[100]) |
| 126 | + result = vlan.get_vlan_id() |
| 127 | + assert result == 100 |
| 128 | + |
| 129 | + def test_get_vlan_id_vlan_does_not_exist(self, vlan, mocker): |
| 130 | + mocker.patch.object(vlan, "get_vlan_ids", return_value=[]) |
| 131 | + result = vlan.get_vlan_id() |
| 132 | + assert result == 0 |
| 133 | + |
| 134 | + def test_add_vlan_success(self, vlan, mocker): |
| 135 | + vlan.owner._connection.execute_command.return_value = ConnectionCompletedProcess( |
| 136 | + return_code=0, args="command", stdout="", stderr="" |
| 137 | + ) |
| 138 | + mocker.patch.object(vlan, "get_vlan_ids", side_effect=[[], [100]]) |
| 139 | + |
| 140 | + vlan.add_vlan(100) |
| 141 | + |
| 142 | + expected_calls = [ |
| 143 | + call( |
| 144 | + "ip link add link eth1 name eth1.100 type vlan id 100", |
| 145 | + expected_return_codes={0}, |
| 146 | + ), |
| 147 | + call("ip link set dev eth1.100 up", expected_return_codes={0}), |
| 148 | + ] |
| 149 | + vlan.owner._connection.execute_command.assert_has_calls(expected_calls) |
| 150 | + |
| 151 | + def test_add_vlan_failure(self, vlan, mocker): |
| 152 | + vlan.owner._connection.execute_command.return_value = ConnectionCompletedProcess( |
| 153 | + return_code=1, args="command", stdout="", stderr="" |
| 154 | + ) |
| 155 | + vlan.add_vlan(100) |
| 156 | + |
| 157 | + def test_add_vlan_already_exists(self, vlan, mocker): |
| 158 | + mocker.patch.object(vlan, "get_vlan_ids", return_value=[100]) |
| 159 | + |
| 160 | + with pytest.raises(VlanAlreadyExistsException, match="VLAN 100 already exists on interface eth1"): |
| 161 | + vlan.add_vlan(100) |
| 162 | + |
| 163 | + def test_remove_vlan_success(self, vlan, mocker): |
| 164 | + mocker.patch.object(vlan, "get_vlan_id", return_value=100) |
| 165 | + vlan.owner._connection.execute_command.return_value = ConnectionCompletedProcess( |
| 166 | + return_code=0, args="command", stdout="", stderr="" |
| 167 | + ) |
| 168 | + vlan.remove_vlan() |
| 169 | + vlan.owner._connection.execute_command.assert_called_once_with( |
| 170 | + "ip link del link eth1 name eth1.100 type vlan id 100", |
| 171 | + expected_return_codes={0}, |
| 172 | + ) |
| 173 | + |
| 174 | + def test_remove_vlan_failure(self, vlan, mocker): |
| 175 | + vlan.owner._connection.execute_command.return_value = ConnectionCompletedProcess( |
| 176 | + return_code=1, args="command", stdout="", stderr="" |
| 177 | + ) |
| 178 | + vlan.remove_vlan(101) |
| 179 | + |
| 180 | + def test_remove_vlan_no_vlan_exists(self, vlan, mocker): |
| 181 | + mocker.patch.object(vlan, "get_vlan_id", return_value=0) |
| 182 | + with pytest.raises(VlanNotFoundException, match="No VLANs found on interface eth1"): |
| 183 | + vlan.remove_vlan() |
| 184 | + vlan.owner._connection.execute_command.assert_not_called() |
| 185 | + |
| 186 | + def test_remove_specified_vlan_success(self, vlan, mocker): |
| 187 | + vlan.owner._connection.execute_command.return_value = ConnectionCompletedProcess( |
| 188 | + return_code=0, args="command", stdout="", stderr="" |
| 189 | + ) |
| 190 | + vlan.remove_vlan(vlan_id=101) |
| 191 | + vlan.owner._connection.execute_command.assert_called_once_with( |
| 192 | + "ip link del link eth1 name eth1.101 type vlan id 101", |
| 193 | + expected_return_codes={0}, |
| 194 | + ) |
| 195 | + |
| 196 | + def test_remove_specified_vlan_failure(self, vlan, mocker): |
| 197 | + vlan.owner._connection.execute_command.return_value = ConnectionCompletedProcess( |
| 198 | + return_code=1, args="command", stdout="", stderr="" |
| 199 | + ) |
| 200 | + vlan.remove_vlan(vlan_id=101) |
0 commit comments