From 68e5bfdbf561272cfcff3e0774c5d011f342c02a Mon Sep 17 00:00:00 2001 From: Charles Short Date: Thu, 8 Mar 2018 14:55:28 -0500 Subject: [PATCH] Remove mox usage Introduce ''stub_out'' method to base test class We would like to fully remove mox from the test tree. Even for tests that don't use mox's validation, many of them are using the symbol patching with self.stubs.Set. We can do the same thing with the monkeypatch fixture instead. This introduces self.stub_out to os_net_config/tests/base.py and modifies the self.stubs.Set usage to self.stub_out. We also remove the mox dependency. Change-Id: Ib2fba5ecbbca5526bce2a4405704efb69ab7b5f0 Signed-off-by: Charles Short --- os_net_config/tests/base.py | 21 +++-- os_net_config/tests/test_cli.py | 13 +-- os_net_config/tests/test_impl_eni.py | 17 ++-- os_net_config/tests/test_impl_ifcfg.py | 123 +++++++++++++------------ os_net_config/tests/test_objects.py | 52 ++++++----- os_net_config/tests/test_utils.py | 71 +++++++------- test-requirements.txt | 1 - 7 files changed, 160 insertions(+), 138 deletions(-) diff --git a/os_net_config/tests/base.py b/os_net_config/tests/base.py index b97b6b14..3a643b01 100644 --- a/os_net_config/tests/base.py +++ b/os_net_config/tests/base.py @@ -18,11 +18,8 @@ import os import fixtures -import stubout import testtools -from os_net_config import objects - _TRUE_VALUES = ('True', 'true', '1', 'yes') @@ -35,13 +32,13 @@ class TestCase(testtools.TestCase): """Run before each test method to initialize test environment.""" super(TestCase, self).setUp() - self.stubs = stubout.StubOutForTesting() self.stubbed_mapped_nics = {} def dummy_mapped_nics(nic_mapping=None): return self.stubbed_mapped_nics if self.stub_mapped_nics: - self.stubs.Set(objects, 'mapped_nics', dummy_mapped_nics) + self.stub_out('os_net_config.objects.mapped_nics', + dummy_mapped_nics) test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) try: @@ -64,7 +61,17 @@ class TestCase(testtools.TestCase): self.log_fixture = self.useFixture(fixtures.FakeLogger()) + def stub_out(self, old, new): + """Replace a function for the duration of the test. + + Use the monkey patch fixture to replace a function for the + duration of a test. Useful when you want to provide fake + methods instead of mocks during testing. + + This should be used instead of set.stubs.Set (which is based + on mox) going forward. + """ + self.useFixture(fixtures.MonkeyPatch(old, new)) + def tearDown(self): - self.stubs.UnsetAll() - self.stubs.SmartUnsetAll() super(TestCase, self).tearDown() diff --git a/os_net_config/tests/test_cli.py b/os_net_config/tests/test_cli.py index b48fd32e..06084dc8 100644 --- a/os_net_config/tests/test_cli.py +++ b/os_net_config/tests/test_cli.py @@ -20,10 +20,7 @@ import yaml import os_net_config from os_net_config import cli -from os_net_config import impl_ifcfg -from os_net_config import objects from os_net_config.tests import base -from os_net_config import utils import six @@ -187,7 +184,7 @@ class TestCli(base.TestCase): # this fake implementation returns no changes return {} - self.stubs.Set(impl_ifcfg, 'IfcfgNetConfig', TestImpl) + self.stub_out('os_net_config.impl_ifcfg.IfcfgNetConfig', TestImpl) stdout_yaml, stderr = self.run_cli('ARG0 --provider=ifcfg --noop ' '--exit-on-validation-errors ' '-c %s --detailed-exit-codes' @@ -273,7 +270,7 @@ class TestCli(base.TestCase): def dummy_mapped_nics(nic_mapping=None): return nic_mapping - self.stubs.Set(objects, 'mapped_nics', dummy_mapped_nics) + self.stub_out('os_net_config.objects.mapped_nics', dummy_mapped_nics) stdout, stderr = self.run_cli('ARG0 --interfaces ' '--exit-on-validation-errors ' @@ -290,7 +287,7 @@ class TestCli(base.TestCase): def dummy_mapped_nics(nic_mapping=None): return nic_mapping - self.stubs.Set(objects, 'mapped_nics', dummy_mapped_nics) + self.stub_out('os_net_config.objects.mapped_nics', dummy_mapped_nics) stdout, stderr = self.run_cli('ARG0 --interfaces em2 em3 ' '--exit-on-validation-errors ' @@ -326,8 +323,8 @@ class TestCli(base.TestCase): def test_contrail_vrouter_dpdk_noop_output(self): cvi_yaml = os.path.join(SAMPLE_BASE, 'contrail_vrouter_dpdk.yaml') cvi_json = os.path.join(SAMPLE_BASE, 'contrail_vrouter_dpdk.json') - self.stubs.Set(utils, 'get_stored_pci_address', - self.stub_get_stored_pci_address) + self.stub_out('os_net_config.utils.get_stored_pci_address', + self.stub_get_stored_pci_address) stdout_yaml, stderr = self.run_cli('ARG0 --provider=ifcfg --noop ' '--exit-on-validation-errors ' '--debug ' diff --git a/os_net_config/tests/test_impl_eni.py b/os_net_config/tests/test_impl_eni.py index 902cc027..2be2c999 100644 --- a/os_net_config/tests/test_impl_eni.py +++ b/os_net_config/tests/test_impl_eni.py @@ -234,7 +234,7 @@ class TestENINetConfig(base.TestCase): def test_interface_mac(name): return "a1:b2:c3:d4:e5" - self.stubs.Set(utils, 'interface_mac', test_interface_mac) + self.stub_out('os_net_config.utils.interface_mac', test_interface_mac) interface = objects.Interface(self.if_name, primary=True) bridge = objects.OvsBridge('br0', use_dhcp=True, @@ -249,7 +249,7 @@ class TestENINetConfig(base.TestCase): def test_interface_mac(name): return "a1:b2:c3:d4:e5" - self.stubs.Set(utils, 'interface_mac', test_interface_mac) + self.stub_out('os_net_config.utils.interface_mac', test_interface_mac) interface = objects.Interface(self.if_name, primary=True) ovs_extra = "br-set-external-id br0 bridge-id br0" @@ -266,7 +266,7 @@ class TestENINetConfig(base.TestCase): def test_interface_mac(name): return "a1:b2:c3:d4:e5" - self.stubs.Set(utils, 'interface_mac', test_interface_mac) + self.stub_out('os_net_config.utils.interface_mac', test_interface_mac) interface = objects.Interface(self.if_name, primary=True) ovs_extra = "br-set-external-id {name} bridge-id {name}" @@ -308,14 +308,15 @@ class TestENINetConfigApply(base.TestCase): def test_config_path(prefix): return self.temp_config_file.name - self.stubs.Set(impl_eni, '_network_config_path', test_config_path) + self.stub_out( + 'os_net_config.impl_eni._network_config_path', test_config_path) def test_execute(*args, **kwargs): if args[0] == '/sbin/ifup': self.ifup_interface_names.append(args[1]) pass - self.stubs.Set(processutils, 'execute', test_execute) + self.stub_out('oslo_concurrency.processutils.execute', test_execute) self.provider = impl_eni.ENINetConfig() @@ -370,7 +371,8 @@ class TestENINetConfigApply(base.TestCase): str(kwargs)) def test_interface_failure(self): - self.stubs.Set(processutils, 'execute', self._failed_execute) + self.stub_out('oslo_concurrency.processutils.execute', + self._failed_execute) v4_addr = objects.Address('192.168.1.2/24') interface = objects.Interface('em1', addresses=[v4_addr]) self.provider.add_interface(interface) @@ -380,7 +382,8 @@ class TestENINetConfigApply(base.TestCase): self.assertEqual(1, len(self.provider.errors)) def test_interface_failure_multiple(self): - self.stubs.Set(processutils, 'execute', self._failed_execute) + self.stub_out('oslo_concurrency.processutils.execute', + self._failed_execute) v4_addr = objects.Address('192.168.1.2/24') interface = objects.Interface('em1', addresses=[v4_addr]) v4_addr2 = objects.Address('192.168.2.2/24') diff --git a/os_net_config/tests/test_impl_ifcfg.py b/os_net_config/tests/test_impl_ifcfg.py index 1d9bdbd2..f9a46ed2 100644 --- a/os_net_config/tests/test_impl_ifcfg.py +++ b/os_net_config/tests/test_impl_ifcfg.py @@ -21,7 +21,6 @@ from oslo_concurrency import processutils import os_net_config 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 @@ -565,7 +564,7 @@ class TestIfcfgNetConfig(base.TestCase): def test_interface_mac(name): macs = {'em1': 'a1:b2:c3:d4:e5'} return macs[name] - self.stubs.Set(utils, 'interface_mac', test_interface_mac) + self.stub_out('os_net_config.utils.interface_mac', test_interface_mac) nic_mapping = {'nic1': 'em1'} self.stubbed_mapped_nics = nic_mapping @@ -709,7 +708,7 @@ class TestIfcfgNetConfig(base.TestCase): def test_network_ovs_bridge_with_dhcp_primary_interface(self): def test_interface_mac(name): return "a1:b2:c3:d4:e5" - self.stubs.Set(utils, 'interface_mac', test_interface_mac) + self.stub_out('os_net_config.utils.interface_mac', test_interface_mac) interface = objects.Interface('em1', primary=True) bridge = objects.OvsBridge('br-ctlplane', use_dhcp=True, @@ -723,7 +722,7 @@ class TestIfcfgNetConfig(base.TestCase): def test_network_ovs_bridge_with_dhcp_primary_interface_with_extra(self): def test_interface_mac(name): return "a1:b2:c3:d4:e5" - self.stubs.Set(utils, 'interface_mac', test_interface_mac) + self.stub_out('os_net_config.utils.interface_mac', test_interface_mac) interface = objects.Interface('em1', primary=True) ovs_extra = "br-set-external-id br-ctlplane bridge-id br-ctlplane" @@ -739,7 +738,7 @@ class TestIfcfgNetConfig(base.TestCase): def test_network_ovs_bridge_with_dhcp_primary_interface_with_format(self): def test_interface_mac(name): return "a1:b2:c3:d4:e5" - self.stubs.Set(utils, 'interface_mac', test_interface_mac) + self.stub_out('os_net_config.utils.interface_mac', test_interface_mac) interface = objects.Interface('em1', primary=True) ovs_extra = "br-set-external-id {name} bridge-id {name}" @@ -824,8 +823,8 @@ class TestIfcfgNetConfig(base.TestCase): def test_add_contrail_vrouter_dpdk_interface(self): addresses = [objects.Address('10.0.0.30/24')] interface1 = objects.Interface('em3') - self.stubs.Set(utils, 'get_stored_pci_address', - self.stub_get_stored_pci_address) + self.stub_out('os_net_config.utils.get_stored_pci_address', + self.stub_get_stored_pci_address) cvi = objects.ContrailVrouterDpdk('vhost0', addresses=addresses, members=[interface1]) self.provider.noop = True @@ -838,8 +837,8 @@ class TestIfcfgNetConfig(base.TestCase): def test_add_contrail_vrouter_dpdk_interface_cust_driver(self): addresses = [objects.Address('10.0.0.30/24')] interface1 = objects.Interface('em3') - self.stubs.Set(utils, 'get_stored_pci_address', - self.stub_get_stored_pci_address) + self.stub_out('os_net_config.utils.get_stored_pci_address', + self.stub_get_stored_pci_address) cvi = objects.ContrailVrouterDpdk('vhost0', addresses=addresses, members=[interface1], driver='vfio') self.provider.noop = True @@ -854,8 +853,8 @@ class TestIfcfgNetConfig(base.TestCase): self.stubbed_mapped_nics = nic_mapping addresses = [objects.Address('10.0.0.30/24')] interface1 = objects.Interface('nic1') - self.stubs.Set(utils, 'get_stored_pci_address', - self.stub_get_stored_pci_address) + self.stub_out('os_net_config.utils.get_stored_pci_address', + self.stub_get_stored_pci_address) cvi = objects.ContrailVrouterDpdk('vhost0', addresses=addresses, members=[interface1]) self.provider.noop = True @@ -869,8 +868,8 @@ class TestIfcfgNetConfig(base.TestCase): addresses = [objects.Address('10.0.0.30/24')] interface1 = objects.Interface('em3') interface2 = objects.Interface('em1') - self.stubs.Set(utils, 'get_stored_pci_address', - self.stub_get_stored_pci_address) + self.stub_out('os_net_config.utils.get_stored_pci_address', + self.stub_get_stored_pci_address) cvi = objects.ContrailVrouterDpdk('vhost0', addresses=addresses, members=[interface1, interface2], bond_mode="2", @@ -889,8 +888,8 @@ class TestIfcfgNetConfig(base.TestCase): addresses = [objects.Address('10.0.0.30/24')] interface1 = objects.Interface('nic1') interface2 = objects.Interface('nic2') - self.stubs.Set(utils, 'get_stored_pci_address', - self.stub_get_stored_pci_address) + self.stub_out('os_net_config.utils.get_stored_pci_address', + self.stub_get_stored_pci_address) cvi = objects.ContrailVrouterDpdk('vhost0', addresses=addresses, members=[interface1, interface2], bond_mode="2", @@ -1106,8 +1105,8 @@ DNS2=5.6.7.8 self.assertEqual(device, 'eth2') self.assertEqual(vfid, 7) return 'eth2_7' - self.stubs.Set(utils, 'get_vf_devname', - test_get_vf_devname) + self.stub_out('os_net_config.utils.get_vf_devname', + test_get_vf_devname) self.provider.add_sriov_vf(vf) vf_config = """# This file is autogenerated by os-net-config DEVICE=eth2_7 @@ -1130,8 +1129,8 @@ NETMASK=255.255.255.0 def test_update_sriov_pf_map(name, numvfs, noop): self.assertEqual(name, 'eth2') self.assertEqual(numvfs, 10) - self.stubs.Set(utils, 'update_sriov_pf_map', - test_update_sriov_pf_map) + self.stub_out('os_net_config.utils.update_sriov_pf_map', + test_update_sriov_pf_map) self.provider.add_sriov_pf(pf) pf_config = """# This file is autogenerated by os-net-config DEVICE=eth2 @@ -1154,10 +1153,10 @@ BOOTPROTO=none def test_bind_dpdk_interfaces(ifname, driver, noop): self.assertEqual(ifname, 'eth2') self.assertEqual(driver, 'vfio-pci') - self.stubs.Set(utils, 'bind_dpdk_interfaces', - test_bind_dpdk_interfaces) - self.stubs.Set(utils, 'get_stored_pci_address', - self.stub_get_stored_pci_address) + self.stub_out('os_net_config.utils.bind_dpdk_interfaces', + test_bind_dpdk_interfaces) + self.stub_out('os_net_config.utils.get_stored_pci_address', + self.stub_get_stored_pci_address) self.provider.add_ovs_dpdk_port(dpdk_port) self.provider.add_ovs_user_bridge(bridge) @@ -1197,10 +1196,10 @@ OVS_EXTRA="set Interface $DEVICE options:dpdk-devargs=0000:00:09.0" def test_bind_dpdk_interfaces(ifname, driver, noop): self.assertEqual(ifname, 'eth2') self.assertEqual(driver, 'vfio-pci') - self.stubs.Set(utils, 'bind_dpdk_interfaces', - test_bind_dpdk_interfaces) - self.stubs.Set(utils, 'get_stored_pci_address', - self.stub_get_stored_pci_address) + self.stub_out('os_net_config.utils.bind_dpdk_interfaces', + test_bind_dpdk_interfaces) + self.stub_out('os_net_config.utils.get_stored_pci_address', + self.stub_get_stored_pci_address) self.provider.add_ovs_dpdk_port(dpdk_port) self.provider.add_ovs_user_bridge(bridge) @@ -1246,10 +1245,10 @@ OVS_EXTRA="set Interface $DEVICE options:dpdk-devargs=0000:00:09.0 \ def test_bind_dpdk_interfaces(ifname, driver, noop): self.assertIn(ifname, ['eth1', 'eth2']) self.assertEqual(driver, 'vfio-pci') - self.stubs.Set(utils, 'bind_dpdk_interfaces', - test_bind_dpdk_interfaces) - self.stubs.Set(utils, 'get_stored_pci_address', - self.stub_get_stored_pci_address) + self.stub_out('os_net_config.utils.bind_dpdk_interfaces', + test_bind_dpdk_interfaces) + self.stub_out('os_net_config.utils.get_stored_pci_address', + self.stub_get_stored_pci_address) self.provider.add_ovs_dpdk_bond(bond) self.provider.add_ovs_user_bridge(bridge) @@ -1285,10 +1284,10 @@ OVS_EXTRA="set Interface dpdk0 options:dpdk-devargs=0000:00:08.0 \ def test_bind_dpdk_interfaces(ifname, driver, noop): self.assertIn(ifname, ['eth1', 'eth2']) self.assertEqual(driver, 'vfio-pci') - self.stubs.Set(utils, 'bind_dpdk_interfaces', - test_bind_dpdk_interfaces) - self.stubs.Set(utils, 'get_stored_pci_address', - self.stub_get_stored_pci_address) + self.stub_out('os_net_config.utils.bind_dpdk_interfaces', + test_bind_dpdk_interfaces) + self.stub_out('os_net_config.utils.get_stored_pci_address', + self.stub_get_stored_pci_address) self.provider.add_ovs_dpdk_bond(bond) self.provider.add_ovs_user_bridge(bridge) @@ -1327,10 +1326,10 @@ OVS_EXTRA="set Interface dpdk0 options:dpdk-devargs=0000:00:08.0 \ def test_bind_dpdk_interfaces(ifname, driver, noop): self.assertIn(ifname, ['eth1', 'eth2']) self.assertEqual(driver, 'vfio-pci') - self.stubs.Set(utils, 'bind_dpdk_interfaces', - test_bind_dpdk_interfaces) - self.stubs.Set(utils, 'get_stored_pci_address', - self.stub_get_stored_pci_address) + self.stub_out('os_net_config.utils.bind_dpdk_interfaces', + test_bind_dpdk_interfaces) + self.stub_out('os_net_config.utils.get_stored_pci_address', + self.stub_get_stored_pci_address) self.provider.add_ovs_dpdk_bond(bond) self.provider.add_ovs_user_bridge(bridge) @@ -1369,10 +1368,10 @@ OVS_EXTRA="set Interface dpdk0 options:dpdk-devargs=0000:00:08.0 \ def test_bind_dpdk_interfaces(ifname, driver, noop): self.assertIn(ifname, ['eth1', 'eth2']) self.assertEqual(driver, 'vfio-pci') - self.stubs.Set(utils, 'bind_dpdk_interfaces', - test_bind_dpdk_interfaces) - self.stubs.Set(utils, 'get_stored_pci_address', - self.stub_get_stored_pci_address) + self.stub_out('os_net_config.utils.bind_dpdk_interfaces', + test_bind_dpdk_interfaces) + self.stub_out('os_net_config.utils.get_stored_pci_address', + self.stub_get_stored_pci_address) self.provider.add_ovs_dpdk_bond(bond) self.provider.add_ovs_user_bridge(bridge) @@ -1416,35 +1415,40 @@ class TestIfcfgNetConfigApply(base.TestCase): def test_ifcfg_path(name): return self.temp_ifcfg_file.name - self.stubs.Set(impl_ifcfg, 'ifcfg_config_path', test_ifcfg_path) + self.stub_out( + 'os_net_config.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) + self.stub_out('os_net_config.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) + self.stub_out( + 'os_net_config.impl_ifcfg.route_config_path', test_routes_path) def test_routes6_path(name): return self.temp_route6_file.name - self.stubs.Set(impl_ifcfg, 'route6_config_path', test_routes6_path) + self.stub_out( + 'os_net_config.impl_ifcfg.route6_config_path', test_routes6_path) def test_bridge_path(name): return self.temp_bridge_file.name - self.stubs.Set(impl_ifcfg, 'bridge_config_path', test_bridge_path) + self.stub_out( + 'os_net_config.impl_ifcfg.bridge_config_path', test_bridge_path) def test_cleanup_pattern(): return self.temp_cleanup_file.name - self.stubs.Set(impl_ifcfg, 'cleanup_pattern', test_cleanup_pattern) + self.stub_out('os_net_config.impl_ifcfg.cleanup_pattern', + test_cleanup_pattern) def test_stop_dhclient_process(interface): self.stop_dhclient_interfaces.append(interface) - self.stubs.Set(impl_ifcfg, 'stop_dhclient_process', - test_stop_dhclient_process) + self.stub_out('os_net_config.impl_ifcfg.stop_dhclient_process', + test_stop_dhclient_process) def test_execute(*args, **kwargs): if args[0] == '/sbin/ifup': @@ -1452,7 +1456,7 @@ class TestIfcfgNetConfigApply(base.TestCase): elif args[0] == '/bin/ovs-appctl': self.ovs_appctl_cmds.append(' '.join(args)) pass - self.stubs.Set(processutils, 'execute', test_execute) + self.stub_out('oslo_concurrency.processutils.execute', test_execute) self.provider = impl_ifcfg.IfcfgNetConfig() @@ -1642,7 +1646,8 @@ class TestIfcfgNetConfigApply(base.TestCase): def test_cleanup_pattern(): return '%s-*' % self.temp_cleanup_file.name - self.stubs.Set(impl_ifcfg, 'cleanup_pattern', test_cleanup_pattern) + self.stub_out('os_net_config.impl_ifcfg.cleanup_pattern', + test_cleanup_pattern) self.provider.apply(cleanup=True) self.assertTrue(os.path.exists(tmp_lo_file)) @@ -1655,7 +1660,7 @@ class TestIfcfgNetConfigApply(base.TestCase): def test_execute(*args, **kwargs): execute_strings.append(args[1]) - self.stubs.Set(NetConfig, 'execute', test_execute) + self.stub_out('os_net_config.NetConfig.execute', test_execute) self.provider.noop = True self.provider.add_ovs_dpdk_port(dpdk_port) @@ -1668,7 +1673,7 @@ class TestIfcfgNetConfigApply(base.TestCase): def test_execute(*args, **kwargs): execute_strings.append(args[1]) - self.stubs.Set(NetConfig, 'execute', test_execute) + self.stub_out('os_net_config.NetConfig.execute', test_execute) self.provider.noop = True self.provider.add_interface(interface) @@ -1682,7 +1687,8 @@ class TestIfcfgNetConfigApply(base.TestCase): str(kwargs)) def test_interface_failure(self): - self.stubs.Set(processutils, 'execute', self._failed_execute) + self.stub_out('oslo_concurrency.processutils.execute', + self._failed_execute) v4_addr = objects.Address('192.168.1.2/24') interface = objects.Interface('em1', addresses=[v4_addr]) self.provider.add_interface(interface) @@ -1692,7 +1698,8 @@ class TestIfcfgNetConfigApply(base.TestCase): self.assertEqual(1, len(self.provider.errors)) def test_interface_failure_multiple(self): - self.stubs.Set(processutils, 'execute', self._failed_execute) + self.stub_out('oslo_concurrency.processutils.execute', + self._failed_execute) v4_addr = objects.Address('192.168.1.2/24') interface = objects.Interface('em1', addresses=[v4_addr]) v4_addr2 = objects.Address('192.168.2.2/24') diff --git a/os_net_config/tests/test_objects.py b/os_net_config/tests/test_objects.py index edcabf25..04b07272 100644 --- a/os_net_config/tests/test_objects.py +++ b/os_net_config/tests/test_objects.py @@ -19,7 +19,6 @@ import six from os_net_config import objects from os_net_config.tests import base -from os_net_config import utils class TestRoute(base.TestCase): @@ -238,7 +237,7 @@ class TestInterface(base.TestCase): def test_from_json_dhcp_nic1(self): def dummy_mapped_nics(nic_mapping=None): return {"nic1": "em3"} - self.stubs.Set(objects, 'mapped_nics', dummy_mapped_nics) + self.stub_out('os_net_config.objects.mapped_nics', dummy_mapped_nics) data = '{"type": "interface", "name": "nic1", "use_dhcp": true}' interface = objects.object_from_json(json.loads(data)) @@ -290,7 +289,7 @@ class TestVlan(base.TestCase): def test_from_json_dhcp_nic1(self): def dummy_mapped_nics(nic_mapping=None): return {"nic1": "em4"} - self.stubs.Set(objects, 'mapped_nics', dummy_mapped_nics) + self.stub_out('os_net_config.objects.mapped_nics', dummy_mapped_nics) data = '{"type": "vlan", "device": "nic1", "vlan_id": 16,' \ '"use_dhcp": true}' @@ -324,7 +323,7 @@ class TestBridge(base.TestCase): def test_from_json_dhcp_with_nic1(self): def dummy_mapped_nics(nic_mapping=None): return {"nic1": "em5"} - self.stubs.Set(objects, 'mapped_nics', dummy_mapped_nics) + self.stub_out('os_net_config.objects.mapped_nics', dummy_mapped_nics) data = """{ "type": "ovs_bridge", @@ -429,7 +428,7 @@ class TestLinuxBridge(base.TestCase): def test_from_json_dhcp_with_nic1(self): def dummy_mapped_nics(nic_mapping=None): return {"nic1": "em5"} - self.stubs.Set(objects, 'mapped_nics', dummy_mapped_nics) + self.stub_out('os_net_config.objects.mapped_nics', dummy_mapped_nics) data = """{ "type": "linux_bridge", @@ -636,7 +635,7 @@ class TestBond(base.TestCase): def dummy_mapped_nics(nic_mapping=None): return {"nic1": "em1", "nic2": "em2"} - self.stubs.Set(objects, 'mapped_nics', dummy_mapped_nics) + self.stub_out('os_net_config.objects.mapped_nics', dummy_mapped_nics) data = """{ "type": "ovs_bond", @@ -665,13 +664,14 @@ class TestBond(base.TestCase): def _stub_active_nics(self, nics): def dummy_ordered_active_nics(): return nics - self.stubs.Set(utils, 'ordered_active_nics', dummy_ordered_active_nics) + self.stub_out('os_net_config.utils.ordered_active_nics', + dummy_ordered_active_nics) def _stub_available_nics(self, nics): def dummy_ordered_available_nics(): return nics - self.stubs.Set(utils, 'ordered_available_nics', - dummy_ordered_available_nics) + self.stub_out('os_net_config.utils.ordered_available_nics', + dummy_ordered_available_nics) class TestLinuxTeam(base.TestCase): @@ -727,7 +727,7 @@ class TestLinuxBond(base.TestCase): def dummy_mapped_nics(nic_mapping=None): return {"nic1": "em1", "nic2": "em2"} - self.stubs.Set(objects, 'mapped_nics', dummy_mapped_nics) + self.stub_out('os_net_config.objects.mapped_nics', dummy_mapped_nics) data = """{ "type": "ovs_bond", @@ -915,7 +915,7 @@ class TestIbInterface(base.TestCase): def test_from_json_dhcp_nic1(self): def dummy_mapped_nics(nic_mapping=None): return {"nic1": "ib0"} - self.stubs.Set(objects, 'mapped_nics', dummy_mapped_nics) + self.stub_out('os_net_config.objects.mapped_nics', dummy_mapped_nics) data = '{"type": "ib_interface", "name": "nic1", "use_dhcp": true}' ib_interface = objects.object_from_json(json.loads(data)) @@ -964,13 +964,14 @@ class TestNicMapping(base.TestCase): def _stub_active_nics(self, nics): def dummy_ordered_active_nics(): return nics - self.stubs.Set(utils, 'ordered_active_nics', dummy_ordered_active_nics) + self.stub_out('os_net_config.utils.ordered_active_nics', + dummy_ordered_active_nics) def _stub_available_nics(self, nics): def dummy_ordered_available_nics(): return nics - self.stubs.Set(utils, 'ordered_available_nics', - dummy_ordered_available_nics) + self.stub_out('os_net_config.utils.ordered_available_nics', + dummy_ordered_available_nics) def test_mapped_nics_default(self): self._stub_active_nics(['em1', 'em2']) @@ -1034,7 +1035,7 @@ class TestNicMapping(base.TestCase): mac_map = {'em1': '12:34:56:78:9a:bc', 'em2': '12:34:56:de:f0:12'} return mac_map[name] - self.stubs.Set(utils, 'interface_mac', dummy_interface_mac) + self.stub_out('os_net_config.utils.interface_mac', dummy_interface_mac) self._stub_active_nics(['em1', 'em2']) self._stub_available_nics(['em1', 'em2']) mapping = {'nic1': '12:34:56:de:f0:12', 'nic2': '12:34:56:78:9a:bc'} @@ -1047,7 +1048,7 @@ class TestNicMapping(base.TestCase): 'em2': '12:34:56:de:f0:12'} return mac_map[name] - self.stubs.Set(utils, 'interface_mac', dummy_interface_mac) + self.stub_out('os_net_config.utils.interface_mac', dummy_interface_mac) self._stub_active_nics(['em1', 'em2']) self._stub_available_nics(['em1', 'em2']) mapping = {'nic1': '12:34:56:de:f0:12', 'nic2': 'aa:bb:cc:dd:ee:ff'} @@ -1067,7 +1068,7 @@ class TestNicMapping(base.TestCase): elif nic == 'nic1': return False - self.stubs.Set(utils, 'is_active_nic', dummy_is_active_nic) + self.stub_out('os_net_config.utils.is_active_nic', dummy_is_active_nic) self._stub_available_nics(['em1', 'em2']) mapping = {'nic1': 'em1', 'em1': 'em2'} err = self.assertRaises(objects.InvalidConfigException, @@ -1077,13 +1078,13 @@ class TestNicMapping(base.TestCase): def test_mapped_nics_mapping_inactive_name_as_alias(self): def dummy_is_active_nic(nic): - return False + return False def dummy_is_real_nic(nic): - return True + return True - self.stubs.Set(utils, 'is_active_nic', dummy_is_active_nic) - self.stubs.Set(utils, 'is_real_nic', dummy_is_real_nic) + self.stub_out('os_net_config.utils.is_active_nic', dummy_is_active_nic) + self.stub_out('os_net_config.utils.is_real_nic', dummy_is_real_nic) self._stub_active_nics([]) self._stub_available_nics(['em1', 'em2']) mapping = {'em2': 'em1', 'nic1': 'em2'} @@ -1200,7 +1201,7 @@ class TestSriovPF(base.TestCase): def test_from_json_numvfs_nic1(self): def dummy_mapped_nics(nic_mapping=None): return {"nic1": "em4"} - self.stubs.Set(objects, 'mapped_nics', dummy_mapped_nics) + self.stub_out('os_net_config.objects.mapped_nics', dummy_mapped_nics) data = '{"type": "sriov_pf", "name": "nic1", "numvfs": 16,' \ '"use_dhcp": false}' @@ -1233,7 +1234,7 @@ class TestSriovVF(base.TestCase): def test_from_json_vfid_nic1(self): def dummy_mapped_nics(nic_mapping=None): return {"nic1": "em4"} - self.stubs.Set(objects, 'mapped_nics', dummy_mapped_nics) + self.stub_out('os_net_config.objects.mapped_nics', dummy_mapped_nics) data = '{"type": "sriov_vf", "device": "nic1", "vfid": 16,' \ '"use_dhcp": false}' @@ -1252,7 +1253,8 @@ class TestOvsDpdkBond(base.TestCase): def _stub_active_nics(self, nics): def dummy_ordered_active_nics(): return nics - self.stubs.Set(utils, 'ordered_active_nics', dummy_ordered_active_nics) + self.stub_out('os_net_config.utils.ordered_active_nics', + dummy_ordered_active_nics) def test_from_json_dhcp(self): self._stub_active_nics(['eth0', 'eth1', 'eth2']) @@ -1300,6 +1302,7 @@ class TestOvsDpdkBond(base.TestCase): class TestVppInterface(base.TestCase): + def test_vpp_interface_from_json(self): data = """{ "type": "vpp_interface", @@ -1316,6 +1319,7 @@ class TestVppInterface(base.TestCase): class TestVppBond(base.TestCase): + def test_vpp_interface_from_json(self): data = """{ "type": "vpp_bond", diff --git a/os_net_config/tests/test_utils.py b/os_net_config/tests/test_utils.py index 7b47b814..29811f79 100644 --- a/os_net_config/tests/test_utils.py +++ b/os_net_config/tests/test_utils.py @@ -93,11 +93,12 @@ class TestUtils(base.TestCase): def test_ordered_active_nics(self): tmpdir = tempfile.mkdtemp() - self.stubs.Set(utils, '_SYS_CLASS_NET', tmpdir) + self.stub_out('os_net_config.utils._SYS_CLASS_NET', tmpdir) def test_is_available_nic(interface_name, check_active): return True - self.stubs.Set(utils, '_is_available_nic', test_is_available_nic) + self.stub_out('os_net_config.utils._is_available_nic', + test_is_available_nic) for nic in ['a1', 'em1', 'em2', 'eth2', 'z1', 'enp8s0', 'enp10s0', 'enp1s0f0']: @@ -138,7 +139,7 @@ class TestUtils(base.TestCase): def test_get_vf_devname_net_dir_not_found(self): tmpdir = tempfile.mkdtemp() - self.stubs.Set(utils, '_SYS_CLASS_NET', tmpdir) + self.stub_out('os_net_config.utils._SYS_CLASS_NET', tmpdir) self.assertRaises(utils.SriovVfNotFoundException, utils.get_vf_devname, "eth1", 1, False) @@ -146,7 +147,7 @@ class TestUtils(base.TestCase): def test_get_vf_devname_vf_dir_not_found(self): tmpdir = tempfile.mkdtemp() - self.stubs.Set(utils, '_SYS_CLASS_NET', tmpdir) + self.stub_out('os_net_config.utils._SYS_CLASS_NET', tmpdir) vf_path = os.path.join(utils._SYS_CLASS_NET, 'eth1/device/virtfn1/net') os.makedirs(vf_path) @@ -157,7 +158,7 @@ class TestUtils(base.TestCase): def test_get_vf_devname_vf_dir_found(self): tmpdir = tempfile.mkdtemp() - self.stubs.Set(utils, '_SYS_CLASS_NET', tmpdir) + self.stub_out('os_net_config.utils._SYS_CLASS_NET', tmpdir) vf_path = os.path.join(utils._SYS_CLASS_NET, 'eth1/device/virtfn1/net/eth1_1') @@ -171,7 +172,7 @@ class TestUtils(base.TestCase): if 'ethtool' in name: out = _PCI_OUTPUT return out, None - self.stubs.Set(processutils, 'execute', test_execute) + self.stub_out('oslo_concurrency.processutils.execute', test_execute) pci = utils.get_pci_address('nic2', False) self.assertEqual('0000:00:19.0', pci) @@ -179,7 +180,7 @@ class TestUtils(base.TestCase): def test_execute(name, dummy1, dummy2=None, dummy3=None): if 'ethtool' in name: raise processutils.ProcessExecutionError - self.stubs.Set(processutils, 'execute', test_execute) + self.stub_out('oslo_concurrency.processutils.execute', test_execute) pci = utils.get_pci_address('nic2', False) self.assertEqual(None, pci) @@ -187,7 +188,7 @@ class TestUtils(base.TestCase): def test_execute(name, dummy1, dummy2=None, dummy3=None): if 'ethtool' in name: return None, 'Error' - self.stubs.Set(processutils, 'execute', test_execute) + self.stub_out('oslo_concurrency.processutils.execute', test_execute) pci = utils.get_pci_address('nic2', False) self.assertEqual(None, pci) @@ -197,7 +198,7 @@ class TestUtils(base.TestCase): 'mac_address': '01:02:03:04:05:06', 'driver': 'vfio-pci'}] - self.stubs.Set(utils, '_get_dpdk_map', test_get_dpdk_map) + self.stub_out('os_net_config.utils._get_dpdk_map', test_get_dpdk_map) pci = utils.get_stored_pci_address('eth1', False) self.assertEqual('0000:00:09.0', pci) @@ -205,7 +206,7 @@ class TestUtils(base.TestCase): def test_get_dpdk_map(): return [] - self.stubs.Set(utils, '_get_dpdk_map', test_get_dpdk_map) + self.stub_out('os_net_config.utils._get_dpdk_map', test_get_dpdk_map) pci = utils.get_stored_pci_address('eth1', False) self.assertEqual(None, pci) @@ -245,9 +246,9 @@ class TestUtils(base.TestCase): def test_get_dpdk_mac_address(name): return '01:02:03:04:05:06' - self.stubs.Set(processutils, 'execute', test_execute) - self.stubs.Set(utils, '_get_dpdk_mac_address', - test_get_dpdk_mac_address) + self.stub_out('oslo_concurrency.processutils.execute', test_execute) + self.stub_out('os_net_config.utils._get_dpdk_mac_address', + test_get_dpdk_mac_address) try: utils.bind_dpdk_interfaces('nic2', 'vfio-pci', False) except utils.OvsDpdkBindException: @@ -263,9 +264,9 @@ class TestUtils(base.TestCase): def test_get_dpdk_mac_address(name): return '01:02:03:04:05:06' - self.stubs.Set(processutils, 'execute', test_execute) - self.stubs.Set(utils, '_get_dpdk_mac_address', - test_get_dpdk_mac_address) + self.stub_out('oslo_concurrency.processutils.execute', test_execute) + self.stub_out('os_net_config.utils._get_dpdk_mac_address', + test_get_dpdk_mac_address) self.assertRaises(utils.OvsDpdkBindException, utils.bind_dpdk_interfaces, 'eth1', 'vfio-pci', @@ -286,10 +287,10 @@ class TestUtils(base.TestCase): 'mac_address': '01:02:03:04:05:06', 'driver': 'vfio-pci'}] - self.stubs.Set(utils, '_get_dpdk_map', test_get_dpdk_map) - self.stubs.Set(processutils, 'execute', test_execute) - self.stubs.Set(utils, '_get_dpdk_mac_address', - test_get_dpdk_mac_address) + self.stub_out('os_net_config.utils._get_dpdk_map', test_get_dpdk_map) + self.stub_out('oslo_concurrency.processutils.execute', test_execute) + self.stub_out('os_net_config.utils_get_dpdk_mac_address', + test_get_dpdk_mac_address) try: utils.bind_dpdk_interfaces('eth1', 'vfio-pci', False) except utils.OvsDpdkBindException: @@ -310,10 +311,12 @@ class TestUtils(base.TestCase): 'mac_address': '01:02:03:04:05:06', 'driver': 'vfio-pci'}] - self.stubs.Set(utils, '_get_dpdk_map', test_get_dpdk_map) - self.stubs.Set(processutils, 'execute', test_execute) - self.stubs.Set(utils, '_get_dpdk_mac_address', - test_get_dpdk_mac_address) + self.stub_out('os_net_config.utils_get_dpdk_map', + test_get_dpdk_map) + self.stub_out('oslo_concurrency.processutils.execute', + test_execute) + self.stub_out('os_net_config.utils._get_dpdk_mac_address', + test_get_dpdk_mac_address) self.assertRaises(utils.OvsDpdkBindException, utils.bind_dpdk_interfaces, 'eth2', 'vfio-pci', @@ -367,11 +370,12 @@ class TestUtils(base.TestCase): def test_ordered_active_nics_with_dpdk_mapping(self): tmpdir = tempfile.mkdtemp() - self.stubs.Set(utils, '_SYS_CLASS_NET', tmpdir) + self.stub_out('os_net_config.utils._SYS_CLASS_NET', tmpdir) def test_is_available_nic(interface_name, check_active): return True - self.stubs.Set(utils, '_is_available_nic', test_is_available_nic) + self.stub_out('os_net_config.utils._is_available_nic', + test_is_available_nic) for nic in ['a1', 'em1', 'em2', 'eth2', 'z1', 'enp8s0', 'enp10s0', 'enp1s0f0']: @@ -404,7 +408,7 @@ class TestUtils(base.TestCase): def test_is_active_nic_for_sriov_vf(self): tmpdir = tempfile.mkdtemp() - self.stubs.Set(utils, '_SYS_CLASS_NET', tmpdir) + self.stub_out('os_net_config.utils._SYS_CLASS_NET', tmpdir) # SR-IOV PF = ens802f0 # SR-IOV VF = enp129s2 @@ -432,7 +436,8 @@ class TestUtils(base.TestCase): if 'vppctl' in name: return _VPPCTL_OUTPUT, None - self.stubs.Set(processutils, 'execute', test_execute) + self.stub_out('oslo_concurrency.processutils.execute', + test_execute) int_info = utils._get_vpp_interface('0000:00:09.0') self.assertIsNotNone(int_info) @@ -456,7 +461,7 @@ class TestUtils(base.TestCase): if 'vppctl' in name: return _VPPBOND_OUTPUT, None - self.stubs.Set(processutils, 'execute', test_execute) + self.stub_out('oslo_concurrency.processutils.execute', test_execute) bond_info = utils._get_vpp_bond(['1', '2']) self.assertIsNotNone(bond_info) self.assertEqual('BondEthernet0', bond_info['name']) @@ -548,17 +553,17 @@ dpdk { 'mac_address': '01:02:03:04:05:06', 'driver': 'vfio-pci'}] - self.stubs.Set(utils, '_get_dpdk_map', test_get_dpdk_map) + self.stub_out('os_net_config.utils._get_dpdk_map', test_get_dpdk_map) def test_execute(name, *args, **kwargs): return None, None - self.stubs.Set(processutils, 'execute', test_execute) + self.stub_out('oslo_concurrency.processutils.execute', test_execute) def test_get_vpp_interface(pci_dev, tries, timeout): return {'name': 'GigabitEthernet0/9/0', 'index': '1'} - self.stubs.Set(utils, '_get_vpp_interface', - test_get_vpp_interface) + self.stub_out('os_net_config.utils._get_vpp_interface', + test_get_vpp_interface) int1 = objects.VppInterface('eth1', options="vlan-strip-offload off") int1.pci_dev = '0000:00:09.0' diff --git a/test-requirements.txt b/test-requirements.txt index fb7184cd..e98b16e4 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -12,6 +12,5 @@ testrepository>=0.0.18 # Apache-2.0/BSD testscenarios>=0.4 # Apache-2.0/BSD testtools>=1.4.0 # MIT mock>=2.0 # BSD -mox>=0.5.3 # Apache-2.0 openstackdocstheme>=1.18.1 # Apache-2.0 reno>=2.5.0 # Apache-2.0