From e6b60d99c8ab381f2abe0ed66af4510eff3c910f Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Sat, 19 Mar 2016 08:40:17 -0500 Subject: [PATCH] Refactor unit tests to construct cloud in base There is no need to construct a cloud individually in each unit test. Further more, we can now just use the enable-inner-exception interface introduced a few patches ago. Change-Id: Ia45a47ec243c917ab05b5a2f95c449b9e8d1da68 --- shade/tests/unit/base.py | 41 +++++++++++++++++++-- shade/tests/unit/test_caching.py | 47 ++++++++---------------- shade/tests/unit/test_domain_params.py | 4 -- shade/tests/unit/test_image.py | 3 +- shade/tests/unit/test_keypair.py | 4 -- shade/tests/unit/test_meta.py | 33 +++++++---------- shade/tests/unit/test_network.py | 4 -- shade/tests/unit/test_security_groups.py | 4 -- shade/tests/unit/test_shade.py | 4 -- shade/tests/unit/test_stack.py | 4 -- shade/tests/unit/test_volume.py | 4 -- 11 files changed, 67 insertions(+), 85 deletions(-) diff --git a/shade/tests/unit/base.py b/shade/tests/unit/base.py index 0461a62ce..d9d6012c4 100644 --- a/shade/tests/unit/base.py +++ b/shade/tests/unit/base.py @@ -18,7 +18,11 @@ import time import fixtures +import os_client_config as occ +import tempfile +import yaml +import shade.openstackcloud from shade.tests import base @@ -26,6 +30,23 @@ class TestCase(base.TestCase): """Test case base class for all unit tests.""" + CLOUD_CONFIG = { + 'clouds': + { + '_test_cloud_': + { + 'auth': + { + 'auth_url': 'http://198.51.100.1:35357/v2.0', + 'username': '_test_user_', + 'password': '_test_pass_', + 'project_name': '_test_project_', + }, + 'region_name': '_test_region_', + }, + }, + } + def setUp(self): """Run before each test method to initialize test environment.""" @@ -40,7 +61,19 @@ class TestCase(base.TestCase): self.sleep_fixture = self.useFixture(fixtures.MonkeyPatch( 'time.sleep', _nosleep)) - # Getting the inner exceptions in the test log is super useful - self.useFixture(fixtures.MonkeyPatch( - 'shade.exc.OpenStackCloudException.log_inner_exceptions', - True)) + + # Isolate os-client-config from test environment + config = tempfile.NamedTemporaryFile(delete=False) + config.write(bytes(yaml.dump(self.CLOUD_CONFIG).encode('utf-8'))) + config.close() + vendor = tempfile.NamedTemporaryFile(delete=False) + vendor.write(b'{}') + vendor.close() + + self.config = occ.OpenStackConfig( + config_files=[config.name], + vendor_files=[vendor.name]) + self.cloud_config = self.config.get_one_cloud(cloud='_test_cloud_') + self.cloud = shade.OpenStackCloud( + cloud_config=self.cloud_config, + log_inner_exceptions=True) diff --git a/shade/tests/unit/test_caching.py b/shade/tests/unit/test_caching.py index 85585f456..76ae9cf22 100644 --- a/shade/tests/unit/test_caching.py +++ b/shade/tests/unit/test_caching.py @@ -18,7 +18,6 @@ import mock import os_client_config as occ import testtools import warlock -import yaml import shade.openstackcloud from shade import _utils @@ -98,7 +97,7 @@ _TASK_SCHEMA = dict( class TestMemoryCache(base.TestCase): - CACHING_CONFIG = { + CLOUD_CONFIG = { 'cache': { 'max_age': 90, @@ -109,7 +108,7 @@ class TestMemoryCache(base.TestCase): }, 'clouds': { - '_cache_test_': + '_test_cloud_': { 'auth': { @@ -123,22 +122,6 @@ class TestMemoryCache(base.TestCase): }, } - def setUp(self): - super(TestMemoryCache, self).setUp() - - # Isolate os-client-config from test environment - config = tempfile.NamedTemporaryFile(delete=False) - config.write(bytes(yaml.dump(self.CACHING_CONFIG).encode('utf-8'))) - config.close() - vendor = tempfile.NamedTemporaryFile(delete=False) - vendor.write(b'{}') - vendor.close() - - self.cloud_config = occ.OpenStackConfig(config_files=[config.name], - vendor_files=[vendor.name]) - self.cloud = shade.openstack_cloud(cloud='_cache_test_', - config=self.cloud_config) - def test_openstack_cloud(self): self.assertIsInstance(self.cloud, shade.OpenStackCloud) @@ -523,9 +506,20 @@ class TestMemoryCache(base.TestCase): class TestBogusAuth(base.TestCase): - CONFIG = { + CLOUD_CONFIG = { 'clouds': { + '_test_cloud_': + { + 'auth': + { + 'auth_url': 'http://198.51.100.1:35357/v2.0', + 'username': '_test_user_', + 'password': '_test_pass_', + 'project_name': '_test_project_', + }, + 'region_name': '_test_region_', + }, '_bogus_test_': { 'auth_type': 'bogus', @@ -544,18 +538,7 @@ class TestBogusAuth(base.TestCase): def setUp(self): super(TestBogusAuth, self).setUp() - # Isolate os-client-config from test environment - config = tempfile.NamedTemporaryFile(delete=False) - config.write(bytes(yaml.dump(self.CONFIG).encode('utf-8'))) - config.close() - vendor = tempfile.NamedTemporaryFile(delete=False) - vendor.write(b'{}') - vendor.close() - - self.cloud_config = occ.OpenStackConfig(config_files=[config.name], - vendor_files=[vendor.name]) - def test_get_auth_bogus(self): with testtools.ExpectedException(exc.OpenStackCloudException): shade.openstack_cloud( - cloud='_bogus_test_', config=self.cloud_config) + cloud='_bogus_test_', config=self.config) diff --git a/shade/tests/unit/test_domain_params.py b/shade/tests/unit/test_domain_params.py index f91483b22..628fe9f3c 100644 --- a/shade/tests/unit/test_domain_params.py +++ b/shade/tests/unit/test_domain_params.py @@ -24,10 +24,6 @@ from shade.tests.unit import base class TestDomainParams(base.TestCase): - def setUp(self): - super(TestDomainParams, self).setUp() - self.cloud = shade.openstack_cloud(validate=False) - @mock.patch.object(occ.cloud_config.CloudConfig, 'get_api_version') @mock.patch.object(shade.OpenStackCloud, 'get_project') def test_identity_params_v3(self, mock_get_project, mock_api_version): diff --git a/shade/tests/unit/test_image.py b/shade/tests/unit/test_image.py index 6d35070d8..f93ad76ed 100644 --- a/shade/tests/unit/test_image.py +++ b/shade/tests/unit/test_image.py @@ -20,14 +20,13 @@ import six import shade from shade import exc -from shade.tests import base +from shade.tests.unit import base class TestImage(base.TestCase): def setUp(self): super(TestImage, self).setUp() - self.cloud = shade.openstack_cloud(validate=False) self.image_id = str(uuid.uuid4()) self.fake_search_return = [{ u'image_state': u'available', diff --git a/shade/tests/unit/test_keypair.py b/shade/tests/unit/test_keypair.py index 86a7d398c..2d8698554 100644 --- a/shade/tests/unit/test_keypair.py +++ b/shade/tests/unit/test_keypair.py @@ -25,10 +25,6 @@ from shade.tests.unit import base class TestKeypair(base.TestCase): - def setUp(self): - super(TestKeypair, self).setUp() - self.cloud = shade.openstack_cloud(validate=False) - @patch.object(shade.OpenStackCloud, 'nova_client') def test_create_keypair(self, mock_nova): keyname = 'my_keyname' diff --git a/shade/tests/unit/test_meta.py b/shade/tests/unit/test_meta.py index faf06e462..adf5aad8d 100644 --- a/shade/tests/unit/test_meta.py +++ b/shade/tests/unit/test_meta.py @@ -13,7 +13,6 @@ # limitations under the License. import mock -import testtools import warlock from neutronclient.common import exceptions as neutron_exceptions @@ -22,6 +21,7 @@ import shade from shade import _utils from shade import meta from shade.tests import fakes +from shade.tests.unit import base PRIVATE_V4 = '198.51.100.3' PUBLIC_V4 = '192.0.2.99' @@ -80,7 +80,7 @@ class FakeServer(object): accessIPv6 = '' -class TestMeta(testtools.TestCase): +class TestMeta(base.TestCase): def test_find_nova_addresses_key_name(self): # Note 198.51.100.0/24 is TEST-NET-2 from rfc5737 addrs = {'public': [{'addr': '198.51.100.1', 'version': 4}], @@ -122,9 +122,10 @@ class TestMeta(testtools.TestCase): def test_get_server_ip(self): srv = meta.obj_to_dict(FakeServer()) - cloud = shade.openstack_cloud(validate=False) - self.assertEqual(PRIVATE_V4, meta.get_server_private_ip(srv, cloud)) - self.assertEqual(PUBLIC_V4, meta.get_server_external_ipv4(cloud, srv)) + self.assertEqual( + PRIVATE_V4, meta.get_server_private_ip(srv, self.cloud)) + self.assertEqual( + PUBLIC_V4, meta.get_server_external_ipv4(self.cloud, srv)) @mock.patch.object(shade.OpenStackCloud, 'has_service') @mock.patch.object(shade.OpenStackCloud, 'search_networks') @@ -145,9 +146,9 @@ class TestMeta(testtools.TestCase): 'addr': PUBLIC_V4, 'version': 4}]} )) - cloud = shade.openstack_cloud(validate=False) - self.assertEqual(PRIVATE_V4, meta.get_server_private_ip(srv, cloud)) + self.assertEqual( + PRIVATE_V4, meta.get_server_private_ip(srv, self.cloud)) mock_has_service.assert_called_with('network') mock_search_networks.assert_called_with( filters={'router:external': False} @@ -176,8 +177,7 @@ class TestMeta(testtools.TestCase): }, ] - cloud = shade.openstack_cloud(validate=False) - srv = cloud.get_openstack_vars(meta.obj_to_dict(fakes.FakeServer( + srv = self.cloud.get_openstack_vars(meta.obj_to_dict(fakes.FakeServer( id='test-id', name='test-name', status='ACTIVE', flavor={u'id': u'1'}, image={ @@ -216,8 +216,7 @@ class TestMeta(testtools.TestCase): 'addr': PUBLIC_V4, 'version': 4}]}, )) - ip = meta.get_server_external_ipv4( - cloud=shade.openstack_cloud(validate=False), server=srv) + ip = meta.get_server_external_ipv4(cloud=self.cloud, server=srv) self.assertEqual(PUBLIC_V4, ip) @@ -225,8 +224,7 @@ class TestMeta(testtools.TestCase): srv = meta.obj_to_dict(fakes.FakeServer( id='test-id', name='test-name', status='ACTIVE', accessIPv4=PUBLIC_V4)) - ip = meta.get_server_external_ipv4( - cloud=shade.openstack_cloud(validate=False), server=srv) + ip = meta.get_server_external_ipv4(cloud=self.cloud, server=srv) self.assertEqual(PUBLIC_V4, ip) @@ -254,8 +252,7 @@ class TestMeta(testtools.TestCase): srv = meta.obj_to_dict(fakes.FakeServer( id='test-id', name='test-name', status='ACTIVE')) - ip = meta.get_server_external_ipv4( - cloud=shade.openstack_cloud(validate=False), server=srv) + ip = meta.get_server_external_ipv4(cloud=self.cloud, server=srv) self.assertEqual(PUBLIC_V4, ip) self.assertTrue(mock_get_server_ip.called) @@ -269,8 +266,7 @@ class TestMeta(testtools.TestCase): srv = meta.obj_to_dict(fakes.FakeServer( id='test-id', name='test-name', status='ACTIVE', addresses={'public': [{'addr': PUBLIC_V4, 'version': 4}]})) - ip = meta.get_server_external_ipv4( - cloud=shade.openstack_cloud(validate=False), server=srv) + ip = meta.get_server_external_ipv4(cloud=self.cloud, server=srv) self.assertEqual(PUBLIC_V4, ip) @@ -285,8 +281,7 @@ class TestMeta(testtools.TestCase): srv = meta.obj_to_dict(fakes.FakeServer( id='test-id', name='test-name', status='ACTIVE', addresses={'test-net': [{'addr': PRIVATE_V4}]})) - ip = meta.get_server_external_ipv4( - cloud=shade.openstack_cloud(validate=False), server=srv) + ip = meta.get_server_external_ipv4(cloud=self.cloud, server=srv) self.assertIsNone(ip) self.assertTrue(mock_get_server_ip.called) diff --git a/shade/tests/unit/test_network.py b/shade/tests/unit/test_network.py index 4f20396b6..ee007c91f 100644 --- a/shade/tests/unit/test_network.py +++ b/shade/tests/unit/test_network.py @@ -19,10 +19,6 @@ from shade.tests.unit import base class TestNetwork(base.TestCase): - def setUp(self): - super(TestNetwork, self).setUp() - self.cloud = shade.openstack_cloud(validate=False) - @mock.patch.object(shade.OpenStackCloud, 'neutron_client') def test_create_network(self, mock_neutron): self.cloud.create_network("netname") diff --git a/shade/tests/unit/test_security_groups.py b/shade/tests/unit/test_security_groups.py index 1c1ab6b9d..4012c3206 100644 --- a/shade/tests/unit/test_security_groups.py +++ b/shade/tests/unit/test_security_groups.py @@ -55,10 +55,6 @@ nova_grp_dict = meta.obj_to_dict(nova_grp_obj) class TestSecurityGroups(base.TestCase): - def setUp(self): - super(TestSecurityGroups, self).setUp() - self.cloud = shade.openstack_cloud(validate=False) - @mock.patch.object(shade.OpenStackCloud, 'neutron_client') @mock.patch.object(shade.OpenStackCloud, 'nova_client') def test_list_security_groups_neutron(self, mock_nova, mock_neutron): diff --git a/shade/tests/unit/test_shade.py b/shade/tests/unit/test_shade.py index f66b82976..0ed6ba9f1 100644 --- a/shade/tests/unit/test_shade.py +++ b/shade/tests/unit/test_shade.py @@ -41,10 +41,6 @@ RANGE_DATA = [ class TestShade(base.TestCase): - def setUp(self): - super(TestShade, self).setUp() - self.cloud = shade.openstack_cloud(validate=False) - def test_openstack_cloud(self): self.assertIsInstance(self.cloud, shade.OpenStackCloud) diff --git a/shade/tests/unit/test_stack.py b/shade/tests/unit/test_stack.py index 28820429f..b2b40bc0c 100644 --- a/shade/tests/unit/test_stack.py +++ b/shade/tests/unit/test_stack.py @@ -26,10 +26,6 @@ from shade.tests.unit import base class TestStack(base.TestCase): - def setUp(self): - super(TestStack, self).setUp() - self.cloud = shade.openstack_cloud(validate=False) - @mock.patch.object(shade.OpenStackCloud, 'heat_client') def test_list_stacks(self, mock_heat): fake_stacks = [ diff --git a/shade/tests/unit/test_volume.py b/shade/tests/unit/test_volume.py index f53f284c3..01cb0c316 100644 --- a/shade/tests/unit/test_volume.py +++ b/shade/tests/unit/test_volume.py @@ -22,10 +22,6 @@ from shade.tests.unit import base class TestVolume(base.TestCase): - def setUp(self): - super(TestVolume, self).setUp() - self.cloud = shade.openstack_cloud(validate=False) - @mock.patch.object(shade.OpenStackCloud, 'nova_client') def test_attach_volume(self, mock_nova): server = dict(id='server001')