Fix intermittent error in unit tests

After the introduction of unit tests for the _neutron_exceptions
context, unit tests fail randomly with the following error:

    AttributeError: <class 'shade.OpenStackCloud'> does not have the
    attribute 'mock_me'

This error is due to the wrong location of test__neutron_excetpions
tests (i.e. the TestShadeOperator class rather than the TestShade
class).
Moreover, as pointed out on IRC we can get rid of "fake" methods to test
 the  _neutron_exception context, just using the existing code.

Change-Id: I2cfdcfce606272fbdbdf8a463db895d3d5a4a1e3
This commit is contained in:
Davide Guerri
2015-06-20 23:23:48 +01:00
parent 32f920e26f
commit 9db26ef1e2

View File

@@ -26,25 +26,10 @@ from shade.tests import fakes
from shade.tests.unit import base
def mock_me(self):
pass
def test_exception(self):
with self._neutron_exceptions("This is actually a test"):
self.mock_me()
class TestShade(base.TestCase):
def setUp(self):
super(TestShade, self).setUp()
# Inject test_exception and mock_me, used to test
# _neutron_exceptions context
shade.OpenStackCloud.mock_me = mock_me
shade.OpenStackCloud.test_exception = test_exception
self.cloud = shade.openstack_cloud()
def test_openstack_cloud(self):
@@ -297,6 +282,34 @@ class TestShade(base.TestCase):
flavor2 = self.cloud.get_flavor(1)
self.assertEquals(vanilla, flavor2)
def test__neutron_exceptions_resource_not_found(self):
with mock.patch.object(
shade._tasks, 'NetworkList',
side_effect=n_exc.NotFound()):
self.assertRaises(exc.OpenStackCloudResourceNotFound,
self.cloud.list_networks)
def test__neutron_exceptions_url_not_found(self):
with mock.patch.object(
shade._tasks, 'NetworkList',
side_effect=n_exc.NeutronClientException(status_code=404)):
self.assertRaises(exc.OpenStackCloudURINotFound,
self.cloud.list_networks)
def test__neutron_exceptions_neutron_client_generic(self):
with mock.patch.object(
shade._tasks, 'NetworkList',
side_effect=n_exc.NeutronClientException()):
self.assertRaises(exc.OpenStackCloudException,
self.cloud.list_networks)
def test__neutron_exceptions_generic(self):
with mock.patch.object(
shade._tasks, 'NetworkList',
side_effect=Exception()):
self.assertRaises(exc.OpenStackCloudException,
self.cloud.list_networks)
class TestShadeOperator(base.TestCase):
@@ -842,36 +855,3 @@ class TestShadeOperator(base.TestCase):
def test_has_service_yes(self, session_mock):
session_mock.get_endpoint.return_value = 'http://fake.url'
self.assertTrue(self.cloud.has_service("image"))
@mock.patch.object(shade.OpenStackCloud, 'mock_me')
def test__neutron_exceptions_resource_not_found(
self, mock_mock_me):
mock_mock_me.side_effect = n_exc.NotFound()
self.assertRaises(exc.OpenStackCloudResourceNotFound,
self.cloud.test_exception)
@mock.patch.object(shade.OpenStackCloud, 'mock_me')
def test__neutron_exceptions_url_not_found(
self, mock_mock_me):
mock_mock_me.side_effect = n_exc.NeutronClientException(
status_code=404)
self.assertRaises(exc.OpenStackCloudURINotFound,
self.cloud.test_exception)
@mock.patch.object(shade.OpenStackCloud, 'mock_me')
def test__neutron_exceptions_neutron_client_generic(
self, mock_mock_me):
mock_mock_me.side_effect = n_exc.NeutronClientException()
self.assertRaises(exc.OpenStackCloudException,
self.cloud.test_exception)
@mock.patch.object(shade.OpenStackCloud, 'mock_me')
def test__neutron_exceptions_generic(
self, mock_mock_me):
mock_mock_me.side_effect = Exception()
self.assertRaises(exc.OpenStackCloudException,
self.cloud.test_exception)