Switch from unittest2 compat methods to Python 3.x methods

With the removal of Python 2.x we can remove the unittest2 compat
wrappers and switch to assertCountEqual instead of assertItemsEqual

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: Ied2227a482087f4a2dc4e2d9986f9b3b777aa821
This commit is contained in:
Dirk Mueller 2020-06-23 14:16:07 +02:00
parent f1ebc15dfc
commit 4d58c0bb3d
8 changed files with 19 additions and 19 deletions

View File

@ -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."""

View File

@ -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):

View File

@ -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)

View File

@ -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')

View File

@ -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))

View File

@ -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)

View File

@ -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)

View File

@ -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([