From 6297e3aadc0c27af04d44528a5b2b5c32ef3e086 Mon Sep 17 00:00:00 2001 From: gugug Date: Sun, 12 Jul 2020 11:26:40 +0800 Subject: [PATCH] Replace assertItemsEqual with assertCountEqual assertItemsEqual was removed from Python's unittest.TestCase in Python 3.3 [1][2]. We have been able to use them since then, because testtools required unittest2, which still included it. With testtools removing Python 2.7 support [3][4], we will lose support for assertItemsEqual, so we should switch to use assertCountEqual. [1] - https://bugs.python.org/issue17866 [2] - https://hg.python.org/cpython/rev/d9921cb6e3cd [3] - testing-cabal/testtools#286 [4] - testing-cabal/testtools#277 Change-Id: I251289104dc92fc48bdb6c4de8dc08fb81695ff2 --- nova/tests/functional/db/test_aggregate.py | 6 +++--- .../functional/libvirt/test_numa_live_migration.py | 6 +++--- nova/tests/functional/test_servers_provider_tree.py | 12 ++++++------ nova/tests/unit/compute/test_compute_api.py | 2 +- nova/tests/unit/network/test_network_info.py | 2 +- nova/tests/unit/network/test_neutron.py | 2 +- nova/tests/unit/objects/test_request_spec.py | 4 ++-- nova/tests/unit/scheduler/test_request_filter.py | 4 ++-- 8 files changed, 19 insertions(+), 19 deletions(-) diff --git a/nova/tests/functional/db/test_aggregate.py b/nova/tests/functional/db/test_aggregate.py index 9300baa678d2..ed0adfbc634f 100644 --- a/nova/tests/functional/db/test_aggregate.py +++ b/nova/tests/functional/db/test_aggregate.py @@ -587,7 +587,7 @@ class AggregateObjectTestCase(test.TestCase): value='required') self.assertEqual(2, len(aggs)) - self.assertItemsEqual([2, 3], [a.id for a in aggs]) + self.assertCountEqual([2, 3], [a.id for a in aggs]) def test_matching_aggregates_multiple_keys(self): """All matching aggregates for multiple keys.""" @@ -641,7 +641,7 @@ class AggregateObjectTestCase(test.TestCase): 'trait:', value='required') self.assertEqual(2, len(aggs)) - self.assertItemsEqual([2, 5], [a.id for a in aggs]) + self.assertCountEqual([2, 5], [a.id for a in aggs]) def test_get_non_matching_by_metadata_keys_empty_keys(self): """Test aggregates non matching by metadata with empty keys.""" @@ -669,7 +669,7 @@ class AggregateObjectTestCase(test.TestCase): self.context, [], 'trait:', value='required') self.assertEqual(5, len(aggs)) - self.assertItemsEqual([1, 2, 3, 4, 5], [a.id for a in aggs]) + self.assertCountEqual([1, 2, 3, 4, 5], [a.id for a in aggs]) def test_get_non_matching_by_metadata_keys_empty_key_prefix(self): """Test aggregates non matching by metadata with empty key_prefix.""" diff --git a/nova/tests/functional/libvirt/test_numa_live_migration.py b/nova/tests/functional/libvirt/test_numa_live_migration.py index 27071b7b9a52..427ba0edbe6e 100644 --- a/nova/tests/functional/libvirt/test_numa_live_migration.py +++ b/nova/tests/functional/libvirt/test_numa_live_migration.py @@ -87,16 +87,16 @@ class NUMALiveMigrationBase(base.ServersTestBase, ctxt, uuid) self.assertEqual(1, len(topology.cells)) # NOTE(artom) DictOfIntegersField has strings as keys, need to convert - self.assertItemsEqual([str(cpu) for cpu in instance_cpus], + self.assertCountEqual([str(cpu) for cpu in instance_cpus], topology.cells[0].cpu_pinning_raw.keys()) - self.assertItemsEqual(host_cpus, + self.assertCountEqual(host_cpus, topology.cells[0].cpu_pinning_raw.values()) def _assert_host_consumed_cpus(self, host, cpus): ctxt = context.get_admin_context() topology = objects.NUMATopology.obj_from_db_obj( objects.ComputeNode.get_by_nodename(ctxt, host).numa_topology) - self.assertItemsEqual(cpus, topology.cells[0].pinned_cpus) + self.assertCountEqual(cpus, topology.cells[0].pinned_cpus) class NUMALiveMigrationPositiveBase(NUMALiveMigrationBase): diff --git a/nova/tests/functional/test_servers_provider_tree.py b/nova/tests/functional/test_servers_provider_tree.py index 60ebf0350ebb..2105a182dbdd 100644 --- a/nova/tests/functional/test_servers_provider_tree.py +++ b/nova/tests/functional/test_servers_provider_tree.py @@ -85,7 +85,7 @@ class ProviderTreeTests(integrated_helpers.ProviderUsageBaseTestCase): self.expected_fake_driver_capability_traits.union( # The COMPUTE_NODE trait is always added [os_traits.COMPUTE_NODE])) - self.assertItemsEqual(self.expected_compute_node_traits, + self.assertCountEqual(self.expected_compute_node_traits, self._get_provider_traits(self.host_uuid)) def _run_update_available_resource(self, startup): @@ -151,7 +151,7 @@ class ProviderTreeTests(integrated_helpers.ProviderUsageBaseTestCase): self.assertIn('CUSTOM_BANDWIDTH', self._get_all_resource_classes()) self.assertIn('CUSTOM_GOLD', self._get_all_traits()) self.assertEqual(inv, self._get_provider_inventory(self.host_uuid)) - self.assertItemsEqual( + self.assertCountEqual( traits.union(self.expected_compute_node_traits), self._get_provider_traits(self.host_uuid) ) @@ -373,7 +373,7 @@ class ProviderTreeTests(integrated_helpers.ProviderUsageBaseTestCase): self._get_provider_inventory(uuids.pf2_2)['SRIOV_NET_VF']['total']) # Compute don't have any extra traits - self.assertItemsEqual(self.expected_compute_node_traits, + self.assertCountEqual(self.expected_compute_node_traits, self._get_provider_traits(self.host_uuid)) # NUMAs don't have any traits @@ -605,7 +605,7 @@ class TraitsTrackingTests(integrated_helpers.ProviderUsageBaseTestCase): ptree_traits + [os_traits.COMPUTE_NET_ATTACH_INTERFACE, os_traits.COMPUTE_NODE] ) - self.assertItemsEqual(expected_traits, + self.assertCountEqual(expected_traits, self._get_provider_traits(rp_uuid)) global_traits = self._get_all_traits() # CUSTOM_FOO is now a registered trait because the virt driver @@ -617,7 +617,7 @@ class TraitsTrackingTests(integrated_helpers.ProviderUsageBaseTestCase): expected_traits.remove(custom_trait) expected_traits.remove(os_traits.COMPUTE_NET_ATTACH_INTERFACE) self._set_provider_traits(rp_uuid, list(expected_traits)) - self.assertItemsEqual(expected_traits, + self.assertCountEqual(expected_traits, self._get_provider_traits(rp_uuid)) # The above trait deletions are simulations of an out-of-band @@ -637,7 +637,7 @@ class TraitsTrackingTests(integrated_helpers.ProviderUsageBaseTestCase): # placement. self._run_periodics() - self.assertItemsEqual(expected_traits, + self.assertCountEqual(expected_traits, self._get_provider_traits(rp_uuid)) global_traits = self._get_all_traits() self.assertIn(custom_trait, global_traits) diff --git a/nova/tests/unit/compute/test_compute_api.py b/nova/tests/unit/compute/test_compute_api.py index 6992f313224c..da4ac19ce652 100644 --- a/nova/tests/unit/compute/test_compute_api.py +++ b/nova/tests/unit/compute/test_compute_api.py @@ -6913,7 +6913,7 @@ class ComputeAPIUnitTestCase(_ComputeAPIUnitTestMixIn, test.NoDBTestCase): # and only for the non-default security group name. scget.assert_called_once_with(self.context, 'fake-security-group') # Assert we translated the non-default secgroup name to uuid. - self.assertItemsEqual(['default', uuids.secgroup_uuid], + self.assertCountEqual(['default', uuids.secgroup_uuid], security_groups) @mock.patch('nova.compute.api.API._record_action_start') diff --git a/nova/tests/unit/network/test_network_info.py b/nova/tests/unit/network/test_network_info.py index 9ce228295faf..07c4b562588f 100644 --- a/nova/tests/unit/network/test_network_info.py +++ b/nova/tests/unit/network/test_network_info.py @@ -872,7 +872,7 @@ iface eth1 inet static diff_host = objects.Migration(source_compute='fake-host1', dest_compute='fake-host2') # Same-host migrations will have all events be plug-time. - self.assertItemsEqual( + self.assertCountEqual( [('network-vif-plugged', uuids.normal_vif), ('network-vif-plugged', uuids.hybrid_vif)], network_info.get_plug_time_events(same_host)) diff --git a/nova/tests/unit/network/test_neutron.py b/nova/tests/unit/network/test_neutron.py index c64a1de50ea8..c3a3a0493051 100644 --- a/nova/tests/unit/network/test_neutron.py +++ b/nova/tests/unit/network/test_neutron.py @@ -5845,7 +5845,7 @@ class TestAPI(TestAPIBase): [None, None, None, None, uuids.trusted_port], [pci_req.requester_id for pci_req in pci_requests.requests]) - self.assertItemsEqual( + self.assertCountEqual( ['physnet1', 'physnet2', 'physnet3', 'physnet4'], network_metadata.physnets) self.assertTrue(network_metadata.tunneled) diff --git a/nova/tests/unit/objects/test_request_spec.py b/nova/tests/unit/objects/test_request_spec.py index 0e6286f2d6d5..17a28a915041 100644 --- a/nova/tests/unit/objects/test_request_spec.py +++ b/nova/tests/unit/objects/test_request_spec.py @@ -1005,7 +1005,7 @@ class TestRequestGroupObject(test.NoDBTestCase): self.assertIn('requester_id', primitive) self.assertIn('provider_uuids', primitive) self.assertIn('required_traits', primitive) - self.assertItemsEqual( + self.assertCountEqual( primitive['forbidden_aggregates'], set(['agg3', 'agg4'])) primitive = req_obj.obj_to_primitive( target_version='1.2', @@ -1078,7 +1078,7 @@ class TestDestinationObject(test.NoDBTestCase): obj_primitive = data(obj.obj_to_primitive(target_version='1.4', version_manifest=manifest)) self.assertIn('forbidden_aggregates', obj_primitive) - self.assertItemsEqual(obj_primitive['forbidden_aggregates'], + self.assertCountEqual(obj_primitive['forbidden_aggregates'], set(['agg3', 'agg4'])) self.assertIn('aggregates', obj_primitive) diff --git a/nova/tests/unit/scheduler/test_request_filter.py b/nova/tests/unit/scheduler/test_request_filter.py index e1f17cd9cd0b..e03d0c470b51 100644 --- a/nova/tests/unit/scheduler/test_request_filter.py +++ b/nova/tests/unit/scheduler/test_request_filter.py @@ -131,7 +131,7 @@ class TestRequestFilter(test.NoDBTestCase): reqspec = objects.RequestSpec(flavor=fake_flavor, image=fake_image) result = request_filter.isolate_aggregates(self.context, reqspec) self.assertTrue(result) - self.assertItemsEqual( + self.assertCountEqual( set([uuids.agg1, uuids.agg2, uuids.agg4]), reqspec.requested_destination.forbidden_aggregates) mock_getnotmd.assert_called_once_with( @@ -322,7 +322,7 @@ class TestRequestFilter(test.NoDBTestCase): ','.join(sorted([uuids.agg4])), ','.join(sorted( reqspec.requested_destination.aggregates[1].split(',')))) - self.assertItemsEqual( + self.assertCountEqual( set([uuids.agg1, uuids.agg2, uuids.agg3]), reqspec.requested_destination.forbidden_aggregates) mock_getmd.assert_has_calls([