Merge "Skip test class unless a service exists"

This commit is contained in:
Jenkins
2016-01-15 16:19:06 +00:00
committed by Gerrit Code Review
9 changed files with 55 additions and 38 deletions

View File

@@ -14,35 +14,32 @@ import os
import time import time
import unittest import unittest
from keystoneauth1 import exceptions as _exceptions
from openstack import connection from openstack import connection
CLOUD_NAME = os.getenv('OS_CLOUD', 'test_cloud') CLOUD_NAME = os.getenv('OS_CLOUD', 'test_cloud')
def requires_service(**kwargs): def service_exists(**kwargs):
"""Check whether a service is available for this test """Decorator function to check whether a service exists
When the service exists, the test will be run as normal.
When the service does not exist, the test will be skipped.
Usage: Usage:
@requires_service(service_type="identity", version="v3") @unittest.skipUnless(base.service_exists(service_type="metering"),
def test_v3_auth(self): "Metering service does not exist")
class TestMeter(base.BaseFunctionalTest):
... ...
:param kwargs: The kwargs needed to filter an endpoint. :param kwargs: The kwargs needed to filter an endpoint.
:returns: The test result if the test is executed. :returns: True if the service exists, otherwise False.
:raises: SkipTest, which is handled by the test runner.
""" """
def wrap(method): try:
def check(self): conn = connection.from_config(cloud_name=CLOUD_NAME)
ep = self.conn.session.get_endpoint(**kwargs) conn.session.get_endpoint(**kwargs)
if ep is None:
self.skip('Service endpoint not found.') return True
return method(self) except _exceptions.EndpointNotFound:
return check return False
return wrap
class BaseFunctionalTest(unittest.TestCase): class BaseFunctionalTest(unittest.TestCase):

View File

@@ -18,6 +18,8 @@ from openstack.tests.functional import base
@unittest.skip("bug/1524468") @unittest.skip("bug/1524468")
@unittest.skipUnless(base.service_exists(service_type="metering"),
"Metering service does not exist")
class TestAlarm(base.BaseFunctionalTest): class TestAlarm(base.BaseFunctionalTest):
NAME = uuid.uuid4().hex NAME = uuid.uuid4().hex

View File

@@ -17,6 +17,8 @@ from openstack.tests.functional import base
@unittest.skip("bug/1524468") @unittest.skip("bug/1524468")
@unittest.skipUnless(base.service_exists(service_type="metering"),
"Metering service does not exist")
class TestAlarmChange(base.BaseFunctionalTest): class TestAlarmChange(base.BaseFunctionalTest):
NAME = uuid.uuid4().hex NAME = uuid.uuid4().hex

View File

@@ -10,9 +10,13 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import unittest
from openstack.tests.functional import base from openstack.tests.functional import base
@unittest.skipUnless(base.service_exists(service_type="metering"),
"Metering service does not exist")
class TestCapability(base.BaseFunctionalTest): class TestCapability(base.BaseFunctionalTest):
def test_list(self): def test_list(self):

View File

@@ -10,11 +10,14 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import unittest
import uuid import uuid
from openstack.tests.functional import base from openstack.tests.functional import base
@unittest.skipUnless(base.service_exists(service_type="metering"),
"Metering service does not exist")
class TestMeter(base.BaseFunctionalTest): class TestMeter(base.BaseFunctionalTest):
def test_list(self): def test_list(self):

View File

@@ -10,9 +10,13 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import unittest
from openstack.tests.functional import base from openstack.tests.functional import base
@unittest.skipUnless(base.service_exists(service_type="metering"),
"Metering service does not exist")
class TestResource(base.BaseFunctionalTest): class TestResource(base.BaseFunctionalTest):
def test_list(self): def test_list(self):

View File

@@ -10,10 +10,14 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import unittest
from openstack.telemetry.v2 import sample from openstack.telemetry.v2 import sample
from openstack.tests.functional import base from openstack.tests.functional import base
@unittest.skipUnless(base.service_exists(service_type="metering"),
"Metering service does not exist")
class TestSample(base.BaseFunctionalTest): class TestSample(base.BaseFunctionalTest):
meter = None meter = None

View File

@@ -10,9 +10,13 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import unittest
from openstack.tests.functional import base from openstack.tests.functional import base
@unittest.skipUnless(base.service_exists(service_type="metering"),
"Metering service does not exist")
class TestStatistics(base.BaseFunctionalTest): class TestStatistics(base.BaseFunctionalTest):
def test_list(self): def test_list(self):

View File

@@ -13,37 +13,34 @@
import mock import mock
import unittest import unittest
from keystoneauth1 import exceptions as _exceptions
from openstack.tests.functional import base from openstack.tests.functional import base
class Test_requires_service(unittest.TestCase): class TestServiceExists(unittest.TestCase):
def setUp(self): def setUp(self):
super(Test_requires_service, self).setUp() super(TestServiceExists, self).setUp()
self.return_value = 1 self.conn = mock.Mock()
self.sess = mock.Mock()
self.conn.session = self.sess
self.kwargs = {"service_type": "identity", "version": "v3"} self.kwargs = {"service_type": "identity", "version": "v3"}
self.sot = mock.Mock() @mock.patch('openstack.connection.from_config')
self.sot.test_method = lambda *args: self.return_value def test_service_exists(self, mock_from_config):
mock_from_config.return_value = self.conn
self.mock_skip = mock.Mock() self.sess.get_endpoint = mock.Mock()
self.sot.skip = self.mock_skip
self.get_endpoint = mock.Mock() self.assertTrue(base.service_exists(**self.kwargs))
self.sot.conn.session.get_endpoint = self.get_endpoint
def _test(self, **kwargs): @mock.patch('openstack.connection.from_config')
decorated = base.requires_service(**kwargs)(self.sot.test_method) def test_service_doesnt_exist(self, mock_from_config):
return decorated(self.sot) mock_from_config.return_value = self.conn
def test_service_exists(self): self.sess.get_endpoint = mock.Mock(
self.assertEqual(self.return_value, self._test(**self.kwargs)) side_effect=_exceptions.EndpointNotFound(''))
self.get_endpoint.assert_called_with(**self.kwargs)
def test_service_doesnt_exist(self): self.assertFalse(base.service_exists(**self.kwargs))
self.get_endpoint.return_value = None
self._test(**self.kwargs)
self.get_endpoint.assert_called_with(**self.kwargs)
self.assertEqual(self.mock_skip.call_count, 1)