From a78e1599cf219a4e2a0fd30969f7ed4ca0eed6b3 Mon Sep 17 00:00:00 2001 From: Zhongyue Luo Date: Wed, 25 Sep 2013 10:24:32 +0800 Subject: [PATCH] Utilizes assertIn Using assertTrue and the 'in' operator to test if an element is in a sequence is too python2.4. Our unit testing framework supports assertIn which was created for these types of tests. Fixes bug #1230028 Change-Id: I8a9147fa9982644067ff45c73e116d88845e47e3 --- .../tests/unit/_test_extension_portbindings.py | 2 +- neutron/tests/unit/bigswitch/test_router_db.py | 4 ++-- neutron/tests/unit/nec/test_nec_agent.py | 2 +- neutron/tests/unit/nicira/test_networkgw.py | 8 ++++---- .../tests/unit/openvswitch/test_ovs_tunnel.py | 6 +++--- neutron/tests/unit/test_db_plugin.py | 6 +++--- neutron/tests/unit/test_extensions.py | 16 ++++++++-------- neutron/tests/unit/test_servicetype.py | 2 +- 8 files changed, 23 insertions(+), 23 deletions(-) diff --git a/neutron/tests/unit/_test_extension_portbindings.py b/neutron/tests/unit/_test_extension_portbindings.py index 3995fb9118..85df2f7cac 100644 --- a/neutron/tests/unit/_test_extension_portbindings.py +++ b/neutron/tests/unit/_test_extension_portbindings.py @@ -44,7 +44,7 @@ class PortBindingsTestCase(test_db_plugin.NeutronDbPluginV2TestCase): self.HAS_PORT_FILTER) def _check_response_no_portbindings(self, port): - self.assertTrue('status' in port) + self.assertIn('status', port) self.assertNotIn(portbindings.VIF_TYPE, port) self.assertNotIn(portbindings.CAPABILITIES, port) diff --git a/neutron/tests/unit/bigswitch/test_router_db.py b/neutron/tests/unit/bigswitch/test_router_db.py index ae655bafa6..827bbdc123 100644 --- a/neutron/tests/unit/bigswitch/test_router_db.py +++ b/neutron/tests/unit/bigswitch/test_router_db.py @@ -258,7 +258,7 @@ class RouterDBTestCase(test_l3_plugin.L3NatDBIntTestCase): r1_id = r1['router']['id'] body = self._router_interface_action('add', r_id, s_id, None) - self.assertTrue('port_id' in body) + self.assertIn('port_id', body) r_port_id = body['port_id'] body = self._show('ports', r_port_id) self.assertEqual(body['port']['device_id'], r_id) @@ -267,7 +267,7 @@ class RouterDBTestCase(test_l3_plugin.L3NatDBIntTestCase): s1_id = s1['subnet']['id'] body = self._router_interface_action('add', r1_id, s1_id, None) - self.assertTrue('port_id' in body) + self.assertIn('port_id', body) r1_port_id = body['port_id'] body = self._show('ports', r1_port_id) self.assertEqual(body['port']['device_id'], r1_id) diff --git a/neutron/tests/unit/nec/test_nec_agent.py b/neutron/tests/unit/nec/test_nec_agent.py index 4c9689ec62..341eaf7749 100644 --- a/neutron/tests/unit/nec/test_nec_agent.py +++ b/neutron/tests/unit/nec/test_nec_agent.py @@ -230,7 +230,7 @@ class TestNecAgent(TestNecAgentBase): dummy_vif = ovs_lib.VifPort('port1', '1', 'id-1', 'mac-1', None) self.state_rpc_api.assert_called_once_with('q-plugin') - self.assertTrue('start_flag' in self.agent.agent_state) + self.assertIn('start_flag', self.agent.agent_state) self._check_report_state([], 0, fail_mode, first=True) self._check_report_state([dummy_vif] * 2, 2, fail_mode) diff --git a/neutron/tests/unit/nicira/test_networkgw.py b/neutron/tests/unit/nicira/test_networkgw.py index 5a4259e5eb..e8fcd32a0f 100644 --- a/neutron/tests/unit/nicira/test_networkgw.py +++ b/neutron/tests/unit/nicira/test_networkgw.py @@ -112,7 +112,7 @@ class NetworkGatewayExtensionTestCase(base.BaseTestCase): instance.create_network_gateway.assert_called_with( mock.ANY, network_gateway=data) self.assertEqual(res.status_int, exc.HTTPCreated.code) - self.assertTrue(self._resource in res.json) + self.assertIn(self._resource, res.json) nw_gw = res.json[self._resource] self.assertEqual(nw_gw['id'], nw_gw_id) @@ -152,7 +152,7 @@ class NetworkGatewayExtensionTestCase(base.BaseTestCase): instance.update_network_gateway.assert_called_with( mock.ANY, nw_gw_id, network_gateway=data) self.assertEqual(res.status_int, exc.HTTPOk.code) - self.assertTrue(self._resource in res.json) + self.assertIn(self._resource, res.json) nw_gw = res.json[self._resource] self.assertEqual(nw_gw['id'], nw_gw_id) self.assertEqual(nw_gw['name'], nw_gw_name) @@ -310,11 +310,11 @@ class NetworkGatewayDbTestCase(test_db_plugin.NeutronDbPluginV2TestCase): net['network']['id'], segmentation_type, segmentation_id) - self.assertTrue('connection_info' in body) + self.assertIn('connection_info', body) connection_info = body['connection_info'] for attr in ('network_id', 'port_id', 'network_gateway_id'): - self.assertTrue(attr in connection_info) + self.assertIn(attr, connection_info) # fetch port and confirm device_id gw_port_id = connection_info['port_id'] port_body = self._show('ports', gw_port_id) diff --git a/neutron/tests/unit/openvswitch/test_ovs_tunnel.py b/neutron/tests/unit/openvswitch/test_ovs_tunnel.py index 896913fa4e..e3b306cbcb 100644 --- a/neutron/tests/unit/openvswitch/test_ovs_tunnel.py +++ b/neutron/tests/unit/openvswitch/test_ovs_tunnel.py @@ -301,7 +301,7 @@ class TunnelTest(base.BaseTestCase): a.available_local_vlans = set() a.local_vlan_map[NET_UUID] = LVM a.reclaim_local_vlan(NET_UUID) - self.assertTrue(LVM.vlan in a.available_local_vlans) + self.assertIn(LVM.vlan, a.available_local_vlans) self.mox.VerifyAll() def test_reclaim_local_vlan_flat(self): @@ -324,7 +324,7 @@ class TunnelTest(base.BaseTestCase): a.available_local_vlans = set() a.local_vlan_map[NET_UUID] = LVM_FLAT a.reclaim_local_vlan(NET_UUID) - self.assertTrue(LVM_FLAT.vlan in a.available_local_vlans) + self.assertIn(LVM_FLAT.vlan, a.available_local_vlans) self.mox.VerifyAll() def test_reclaim_local_vlan_vlan(self): @@ -347,7 +347,7 @@ class TunnelTest(base.BaseTestCase): a.available_local_vlans = set() a.local_vlan_map[NET_UUID] = LVM_VLAN a.reclaim_local_vlan(NET_UUID) - self.assertTrue(LVM_VLAN.vlan in a.available_local_vlans) + self.assertIn(LVM_VLAN.vlan, a.available_local_vlans) self.mox.VerifyAll() def test_port_bound(self): diff --git a/neutron/tests/unit/test_db_plugin.py b/neutron/tests/unit/test_db_plugin.py index 002f283886..6b9ed3c17c 100644 --- a/neutron/tests/unit/test_db_plugin.py +++ b/neutron/tests/unit/test_db_plugin.py @@ -788,7 +788,7 @@ class TestPortsV2(NeutronDbPluginV2TestCase): with self.port(name='myname') as port: for k, v in keys: self.assertEqual(port['port'][k], v) - self.assertTrue('mac_address' in port['port']) + self.assertIn('mac_address', port['port']) ips = port['port']['fixed_ips'] self.assertEqual(len(ips), 1) self.assertEqual(ips[0]['ip_address'], '10.0.0.2') @@ -827,7 +827,7 @@ class TestPortsV2(NeutronDbPluginV2TestCase): port = self.deserialize(self.fmt, port_res) for k, v in keys: self.assertEqual(port['port'][k], v) - self.assertTrue('mac_address' in port['port']) + self.assertIn('mac_address', port['port']) self._delete('ports', port['port']['id']) def test_create_port_public_network_with_ip(self): @@ -845,7 +845,7 @@ class TestPortsV2(NeutronDbPluginV2TestCase): port = self.deserialize(self.fmt, port_res) for k, v in keys: self.assertEqual(port['port'][k], v) - self.assertTrue('mac_address' in port['port']) + self.assertIn('mac_address', port['port']) self._delete('ports', port['port']['id']) def test_create_ports_bulk_native(self): diff --git a/neutron/tests/unit/test_extensions.py b/neutron/tests/unit/test_extensions.py index f9e1610675..b2ae1ec3b5 100644 --- a/neutron/tests/unit/test_extensions.py +++ b/neutron/tests/unit/test_extensions.py @@ -115,7 +115,7 @@ class ResourceExtensionTest(base.BaseTestCase): # Shouldn't be reached self.assertTrue(False) except webtest.AppError as e: - self.assertTrue('501' in e.message) + self.assertIn('501', e.message) def test_resource_can_be_added_as_extension(self): res_ext = extensions.ResourceExtension( @@ -438,7 +438,7 @@ class ExtensionManagerTest(base.BaseTestCase): ext_mgr.add_extension(InvalidExtension()) ext_mgr.add_extension(ext_stubs.StubExtension("valid_extension")) - self.assertTrue('valid_extension' in ext_mgr.extensions) + self.assertIn('valid_extension', ext_mgr.extensions) self.assertNotIn('invalid_extension', ext_mgr.extensions) @@ -453,9 +453,9 @@ class PluginAwareExtensionManagerTest(base.BaseTestCase): ext_mgr.add_extension(ext_stubs.StubExtension("e2")) ext_mgr.add_extension(ext_stubs.StubExtension("e3")) - self.assertTrue("e1" in ext_mgr.extensions) + self.assertIn("e1", ext_mgr.extensions) self.assertNotIn("e2", ext_mgr.extensions) - self.assertTrue("e3" in ext_mgr.extensions) + self.assertIn("e3", ext_mgr.extensions) def test_extensions_are_not_loaded_for_plugins_unaware_of_extensions(self): class ExtensionUnawarePlugin(object): @@ -498,7 +498,7 @@ class PluginAwareExtensionManagerTest(base.BaseTestCase): ext_mgr.add_extension( ext_stubs.ExtensionExpectingPluginInterface("supported_extension")) - self.assertTrue("supported_extension" in ext_mgr.extensions) + self.assertIn("supported_extension", ext_mgr.extensions) def test_extensions_expecting_neutron_plugin_interface_are_loaded(self): class ExtensionForQuamtumPluginInterface(ext_stubs.StubExtension): @@ -512,7 +512,7 @@ class PluginAwareExtensionManagerTest(base.BaseTestCase): ext_mgr = extensions.PluginAwareExtensionManager('', plugin_info) ext_mgr.add_extension(ExtensionForQuamtumPluginInterface("e1")) - self.assertTrue("e1" in ext_mgr.extensions) + self.assertIn("e1", ext_mgr.extensions) def test_extensions_without_need_for__plugin_interface_are_loaded(self): class ExtensionWithNoNeedForPluginInterface(ext_stubs.StubExtension): @@ -528,7 +528,7 @@ class PluginAwareExtensionManagerTest(base.BaseTestCase): ext_mgr = extensions.PluginAwareExtensionManager('', plugin_info) ext_mgr.add_extension(ExtensionWithNoNeedForPluginInterface("e1")) - self.assertTrue("e1" in ext_mgr.extensions) + self.assertIn("e1", ext_mgr.extensions) def test_extension_loaded_for_non_core_plugin(self): class NonCorePluginExtenstion(ext_stubs.StubExtension): @@ -540,7 +540,7 @@ class PluginAwareExtensionManagerTest(base.BaseTestCase): ext_mgr = extensions.PluginAwareExtensionManager('', plugin_info) ext_mgr.add_extension(NonCorePluginExtenstion("e1")) - self.assertTrue("e1" in ext_mgr.extensions) + self.assertIn("e1", ext_mgr.extensions) class ExtensionControllerTest(testlib_api.WebTestCase): diff --git a/neutron/tests/unit/test_servicetype.py b/neutron/tests/unit/test_servicetype.py index 69b0e1d967..fde78cc7b9 100644 --- a/neutron/tests/unit/test_servicetype.py +++ b/neutron/tests/unit/test_servicetype.py @@ -238,7 +238,7 @@ class ServiceTypeManagerExtTestCase(ServiceTypeExtensionTestCaseBase): res = self._list_service_providers() self.assertEqual(res.status_int, webexc.HTTPOk.code) data = self.deserialize(res) - self.assertTrue('service_providers' in data) + self.assertIn('service_providers', data) self.assertEqual(len(data['service_providers']), 2)