diff options
Diffstat (limited to 'os_net_config/tests')
-rw-r--r-- | os_net_config/tests/test_impl_eni.py | 17 | ||||
-rw-r--r-- | os_net_config/tests/test_impl_ifcfg.py | 87 | ||||
-rw-r--r-- | os_net_config/tests/test_objects.py | 53 | ||||
-rw-r--r-- | os_net_config/tests/test_utils.py | 24 |
4 files changed, 177 insertions, 4 deletions
diff --git a/os_net_config/tests/test_impl_eni.py b/os_net_config/tests/test_impl_eni.py index e445ed4..4911cb9 100644 --- a/os_net_config/tests/test_impl_eni.py +++ b/os_net_config/tests/test_impl_eni.py @@ -32,6 +32,12 @@ _V4_IFACE_STATIC_IP = _AUTO + """iface eth0 inet static netmask 255.255.255.0 """ +_IFACE_HOTPLUG = """allow-hotplug eth0 +iface eth0 inet static + address 192.168.1.2 + netmask 255.255.255.0 +""" + _V4_IFACE_STATIC_IP_MULTIPLE = (_V4_IFACE_STATIC_IP + _AUTO + """iface eth0 inet static address 10.0.0.2 @@ -117,8 +123,9 @@ class TestENINetConfig(base.TestCase): def get_route_config(self): return self.provider.routes[self.if_name] - def _default_interface(self, addr=[], rts=[]): - return objects.Interface(self.if_name, addresses=addr, routes=rts) + def _default_interface(self, addr=[], rts=[], hotplug=False): + return objects.Interface(self.if_name, addresses=addr, routes=rts, + hotplug=hotplug) def test_interface_no_ip(self): interface = self._default_interface() @@ -131,6 +138,12 @@ class TestENINetConfig(base.TestCase): self.provider.add_interface(interface) self.assertEqual(_V4_IFACE_STATIC_IP, self.get_interface_config()) + def test_add_interface_with_hotplug(self): + v4_addr = objects.Address('192.168.1.2/24') + interface = self._default_interface(addr=[v4_addr], hotplug=True) + self.provider.add_interface(interface) + self.assertEqual(_IFACE_HOTPLUG, self.get_interface_config()) + def test_add_interface_with_v4_multiple(self): v4_addresses = [objects.Address('192.168.1.2/24'), objects.Address('10.0.0.2/8')] diff --git a/os_net_config/tests/test_impl_ifcfg.py b/os_net_config/tests/test_impl_ifcfg.py index 3a5a7db..82ca116 100644 --- a/os_net_config/tests/test_impl_ifcfg.py +++ b/os_net_config/tests/test_impl_ifcfg.py @@ -20,6 +20,7 @@ import tempfile from oslo_concurrency import processutils from os_net_config import impl_ifcfg +from os_net_config import NetConfig from os_net_config import objects from os_net_config.tests import base from os_net_config import utils @@ -33,6 +34,23 @@ NM_CONTROLLED=no PEERDNS=no """ +_BASE_IFCFG_NETWORKMANAGER = """# This file is autogenerated by os-net-config +DEVICE=em1 +ONBOOT=yes +HOTPLUG=no +NM_CONTROLLED=yes +PEERDNS=no +""" + +_HOTPLUG = """# This file is autogenerated by os-net-config +DEVICE=em1 +ONBOOT=yes +HOTPLUG=yes +NM_CONTROLLED=no +PEERDNS=no +BOOTPROTO=none +""" + _NO_IP = _BASE_IFCFG + "BOOTPROTO=none\n" _V4_IFCFG = _BASE_IFCFG + """BOOTPROTO=static @@ -143,6 +161,19 @@ OVSBOOTPROTO=dhcp OVSDHCPINTERFACES="em1" """ +_NM_CONTROLLED_INTERFACE = _BASE_IFCFG_NETWORKMANAGER + """MASTER=bond1 +SLAVE=yes +BOOTPROTO=none +""" + +_NM_CONTROLLED_BOND = """# This file is autogenerated by os-net-config +DEVICE=bond1 +ONBOOT=yes +HOTPLUG=no +NM_CONTROLLED=yes +PEERDNS=no +""" + _OVS_BRIDGE_DHCP_STANDALONE = _OVS_BRIDGE_DHCP + \ "OVS_EXTRA=\"set bridge br-ctlplane fail_mode=standalone\"\n" @@ -386,6 +417,11 @@ class TestIfcfgNetConfig(base.TestCase): self.provider.add_interface(interface) self.assertEqual(_NO_IP, self.get_interface_config()) + def test_add_interface_with_hotplug(self): + interface = objects.Interface('em1', hotplug=True) + self.provider.add_interface(interface) + self.assertEqual(_HOTPLUG, self.get_interface_config()) + def test_add_base_interface_vlan(self): interface = objects.Interface('em1.120') self.provider.add_interface(interface) @@ -830,6 +866,20 @@ DNS2=5.6.7.8 """ self.assertEqual(em1_config, self.get_interface_config('em1')) + def test_nm_controlled(self): + interface1 = objects.Interface('em1', nm_controlled=True) + interface2 = objects.Interface('em2', nm_controlled=True) + bond = objects.LinuxBond('bond1', nm_controlled=True, + members=[interface1, interface2]) + self.provider.add_linux_bond(bond) + self.provider.add_interface(interface1) + self.provider.add_interface(interface2) + + ifcfg_data = self.get_interface_config('em1') + self.assertEqual(_NM_CONTROLLED_INTERFACE, ifcfg_data) + bond_data = self.get_linux_bond_config('bond1') + self.assertEqual(_NM_CONTROLLED_BOND, bond_data) + def test_network_ovs_dpdk_bridge_and_port(self): nic_mapping = {'nic1': 'eth0', 'nic2': 'eth1', 'nic3': 'eth2'} self.stubbed_mapped_nics = nic_mapping @@ -909,6 +959,7 @@ class TestIfcfgNetConfigApply(base.TestCase): def setUp(self): super(TestIfcfgNetConfigApply, self).setUp() self.temp_ifcfg_file = tempfile.NamedTemporaryFile() + self.temp_bond_file = tempfile.NamedTemporaryFile() self.temp_route_file = tempfile.NamedTemporaryFile() self.temp_route6_file = tempfile.NamedTemporaryFile() self.temp_bridge_file = tempfile.NamedTemporaryFile() @@ -921,6 +972,13 @@ class TestIfcfgNetConfigApply(base.TestCase): return self.temp_ifcfg_file.name self.stubs.Set(impl_ifcfg, 'ifcfg_config_path', test_ifcfg_path) + def test_remove_ifcfg_config(name): + ifcfg_file = self.temp_ifcfg_file.name + if os.path.exists(ifcfg_file): + os.remove(ifcfg_file) + self.stubs.Set(impl_ifcfg, 'remove_ifcfg_config', + test_remove_ifcfg_config) + def test_routes_path(name): return self.temp_route_file.name self.stubs.Set(impl_ifcfg, 'route_config_path', test_routes_path) @@ -1143,3 +1201,32 @@ class TestIfcfgNetConfigApply(base.TestCase): self.provider.apply(cleanup=True) self.assertTrue(os.path.exists(tmp_lo_file)) os.remove(tmp_lo_file) + + def test_ovs_restart_called(self): + interface = objects.Interface('em1') + dpdk_port = objects.OvsDpdkPort('dpdk0', members=[interface]) + execute_strings = [] + + def test_execute(*args, **kwargs): + execute_strings.append(args[1]) + pass + self.stubs.Set(NetConfig, 'execute', test_execute) + + self.provider.noop = True + self.provider.add_ovs_dpdk_port(dpdk_port) + self.provider.apply() + self.assertIn('Restart openvswitch', execute_strings) + + def test_ovs_restart_not_called(self): + interface = objects.Interface('em1') + execute_strings = [] + + def test_execute(*args, **kwargs): + execute_strings.append(args[1]) + pass + self.stubs.Set(NetConfig, 'execute', test_execute) + + self.provider.noop = True + self.provider.add_interface(interface) + self.provider.apply() + self.assertNotIn('Restart openvswitch', execute_strings) diff --git a/os_net_config/tests/test_objects.py b/os_net_config/tests/test_objects.py index ca2dd47..f5daf31 100644 --- a/os_net_config/tests/test_objects.py +++ b/os_net_config/tests/test_objects.py @@ -98,6 +98,27 @@ class TestInterface(base.TestCase): self.assertEqual("em1", interface.name) self.assertTrue(interface.use_dhcp) + def test_from_json_hotplug(self): + data = """{ +"type": "interface", +"name": "em1", +"hotplug": true +} +""" + interface = objects.object_from_json(json.loads(data)) + self.assertEqual("em1", interface.name) + self.assertTrue(interface.hotplug) + + def test_from_json_hotplug_off_by_default(self): + data = """{ +"type": "interface", +"name": "em1" +} +""" + interface = objects.object_from_json(json.loads(data)) + self.assertEqual("em1", interface.name) + self.assertFalse(interface.hotplug) + def test_from_json_defroute(self): data = '{"type": "interface", "name": "em1", "use_dhcp": true}' interface1 = objects.object_from_json(json.loads(data)) @@ -274,6 +295,34 @@ class TestBridge(base.TestCase): self.assertTrue(interface2.ovs_port) self.assertEqual("br-foo", interface2.bridge_name) + def test_from_json_ovs_extra(self): + data = """{ +"type": "ovs_bridge", +"name": "br-foo", +"ovs_extra": ["bar"], +"ovs_fail_mode": "standalone" +} +""" + bridge = objects.object_from_json(json.loads(data)) + self.assertTrue(2 == len(bridge.ovs_extra)) + self.assertEqual("bar", bridge.ovs_extra[0]) + self.assertEqual("set bridge br-foo fail_mode=standalone", + bridge.ovs_extra[1]) + + def test_from_json_ovs_extra_string(self): + data = """{ +"type": "ovs_bridge", +"name": "br-foo", +"ovs_extra": "bar", +"ovs_fail_mode": "standalone" +} +""" + bridge = objects.object_from_json(json.loads(data)) + self.assertTrue(2 == len(bridge.ovs_extra)) + self.assertEqual("bar", bridge.ovs_extra[0]) + self.assertEqual("set bridge br-foo fail_mode=standalone", + bridge.ovs_extra[1]) + class TestLinuxBridge(base.TestCase): @@ -407,7 +456,7 @@ class TestIvsInterface(base.TestCase): objects.IvsBridge.from_json, json.loads(data)) expected = 'IVS does not support bond interfaces.' - self.assertIn(expected, err) + self.assertIn(expected, six.text_type(err)) class TestNfvswitchBridge(base.TestCase): @@ -472,7 +521,7 @@ class TestNfvswitchInterface(base.TestCase): objects.NfvswitchBridge.from_json, json.loads(data)) expected = 'NFVSwitch does not support bond interfaces.' - self.assertIn(expected, err) + self.assertIn(expected, six.text_type(err)) class TestBond(base.TestCase): diff --git a/os_net_config/tests/test_utils.py b/os_net_config/tests/test_utils.py index b766384..1885cbb 100644 --- a/os_net_config/tests/test_utils.py +++ b/os_net_config/tests/test_utils.py @@ -217,3 +217,27 @@ class TestUtils(base.TestCase): def test_interface_mac_raises(self): self.assertRaises(IOError, utils.interface_mac, 'ens20f2p3') + + def test_is_active_nic_for_sriov_vf(self): + + tmpdir = tempfile.mkdtemp() + self.stubs.Set(utils, '_SYS_CLASS_NET', tmpdir) + + # SR-IOV PF = ens802f0 + # SR-IOV VF = enp129s2 + for nic in ['ens802f0', 'enp129s2']: + nic_path = os.path.join(tmpdir, nic) + os.makedirs(nic_path) + os.makedirs(os.path.join(nic_path, 'device')) + with open(os.path.join(nic_path, 'operstate'), 'w') as f: + f.write('up') + with open(os.path.join(nic_path, 'address'), 'w') as f: + f.write('1.2.3.4') + + nic_path = os.path.join(tmpdir, 'enp129s2', 'device', 'physfn') + os.makedirs(nic_path) + + self.assertEqual(utils._is_active_nic('ens802f0'), True) + self.assertEqual(utils._is_active_nic('enp129s2'), False) + + shutil.rmtree(tmpdir) |