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
This commit is contained in:
Monty Taylor 2016-03-19 08:40:17 -05:00
parent d725978313
commit e6b60d99c8
11 changed files with 67 additions and 85 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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