Merge "Skip test class unless a service exists"
This commit is contained in:
		| @@ -14,35 +14,32 @@ import os | ||||
| import time | ||||
| import unittest | ||||
|  | ||||
| from keystoneauth1 import exceptions as _exceptions | ||||
| from openstack import connection | ||||
|  | ||||
|  | ||||
| CLOUD_NAME = os.getenv('OS_CLOUD', 'test_cloud') | ||||
|  | ||||
|  | ||||
| def requires_service(**kwargs): | ||||
|     """Check whether a service is available for this test | ||||
|  | ||||
|     When the service exists, the test will be run as normal. | ||||
|     When the service does not exist, the test will be skipped. | ||||
| def service_exists(**kwargs): | ||||
|     """Decorator function to check whether a service exists | ||||
|  | ||||
|     Usage: | ||||
|     @requires_service(service_type="identity", version="v3") | ||||
|     def test_v3_auth(self): | ||||
|     @unittest.skipUnless(base.service_exists(service_type="metering"), | ||||
|                          "Metering service does not exist") | ||||
|     class TestMeter(base.BaseFunctionalTest): | ||||
|         ... | ||||
|  | ||||
|     :param kwargs: The kwargs needed to filter an endpoint. | ||||
|     :returns: The test result if the test is executed. | ||||
|     :raises: SkipTest, which is handled by the test runner. | ||||
|     :returns: True if the service exists, otherwise False. | ||||
|     """ | ||||
|     def wrap(method): | ||||
|         def check(self): | ||||
|             ep = self.conn.session.get_endpoint(**kwargs) | ||||
|             if ep is None: | ||||
|                 self.skip('Service endpoint not found.') | ||||
|             return method(self) | ||||
|         return check | ||||
|     return wrap | ||||
|     try: | ||||
|         conn = connection.from_config(cloud_name=CLOUD_NAME) | ||||
|         conn.session.get_endpoint(**kwargs) | ||||
|  | ||||
|         return True | ||||
|     except _exceptions.EndpointNotFound: | ||||
|         return False | ||||
|  | ||||
|  | ||||
| class BaseFunctionalTest(unittest.TestCase): | ||||
|   | ||||
| @@ -18,6 +18,8 @@ from openstack.tests.functional import base | ||||
|  | ||||
|  | ||||
| @unittest.skip("bug/1524468") | ||||
| @unittest.skipUnless(base.service_exists(service_type="metering"), | ||||
|                      "Metering service does not exist") | ||||
| class TestAlarm(base.BaseFunctionalTest): | ||||
|  | ||||
|     NAME = uuid.uuid4().hex | ||||
|   | ||||
| @@ -17,6 +17,8 @@ from openstack.tests.functional import base | ||||
|  | ||||
|  | ||||
| @unittest.skip("bug/1524468") | ||||
| @unittest.skipUnless(base.service_exists(service_type="metering"), | ||||
|                      "Metering service does not exist") | ||||
| class TestAlarmChange(base.BaseFunctionalTest): | ||||
|  | ||||
|     NAME = uuid.uuid4().hex | ||||
|   | ||||
| @@ -10,9 +10,13 @@ | ||||
| # License for the specific language governing permissions and limitations | ||||
| # under the License. | ||||
|  | ||||
| import unittest | ||||
|  | ||||
| from openstack.tests.functional import base | ||||
|  | ||||
|  | ||||
| @unittest.skipUnless(base.service_exists(service_type="metering"), | ||||
|                      "Metering service does not exist") | ||||
| class TestCapability(base.BaseFunctionalTest): | ||||
|  | ||||
|     def test_list(self): | ||||
|   | ||||
| @@ -10,11 +10,14 @@ | ||||
| # License for the specific language governing permissions and limitations | ||||
| # under the License. | ||||
|  | ||||
| import unittest | ||||
| import uuid | ||||
|  | ||||
| from openstack.tests.functional import base | ||||
|  | ||||
|  | ||||
| @unittest.skipUnless(base.service_exists(service_type="metering"), | ||||
|                      "Metering service does not exist") | ||||
| class TestMeter(base.BaseFunctionalTest): | ||||
|  | ||||
|     def test_list(self): | ||||
|   | ||||
| @@ -10,9 +10,13 @@ | ||||
| # License for the specific language governing permissions and limitations | ||||
| # under the License. | ||||
|  | ||||
| import unittest | ||||
|  | ||||
| from openstack.tests.functional import base | ||||
|  | ||||
|  | ||||
| @unittest.skipUnless(base.service_exists(service_type="metering"), | ||||
|                      "Metering service does not exist") | ||||
| class TestResource(base.BaseFunctionalTest): | ||||
|  | ||||
|     def test_list(self): | ||||
|   | ||||
| @@ -10,10 +10,14 @@ | ||||
| # License for the specific language governing permissions and limitations | ||||
| # under the License. | ||||
|  | ||||
| import unittest | ||||
|  | ||||
| from openstack.telemetry.v2 import sample | ||||
| from openstack.tests.functional import base | ||||
|  | ||||
|  | ||||
| @unittest.skipUnless(base.service_exists(service_type="metering"), | ||||
|                      "Metering service does not exist") | ||||
| class TestSample(base.BaseFunctionalTest): | ||||
|  | ||||
|     meter = None | ||||
|   | ||||
| @@ -10,9 +10,13 @@ | ||||
| # License for the specific language governing permissions and limitations | ||||
| # under the License. | ||||
|  | ||||
| import unittest | ||||
|  | ||||
| from openstack.tests.functional import base | ||||
|  | ||||
|  | ||||
| @unittest.skipUnless(base.service_exists(service_type="metering"), | ||||
|                      "Metering service does not exist") | ||||
| class TestStatistics(base.BaseFunctionalTest): | ||||
|  | ||||
|     def test_list(self): | ||||
|   | ||||
| @@ -13,37 +13,34 @@ | ||||
| import mock | ||||
| import unittest | ||||
|  | ||||
| from keystoneauth1 import exceptions as _exceptions | ||||
|  | ||||
| from openstack.tests.functional import base | ||||
|  | ||||
|  | ||||
| class Test_requires_service(unittest.TestCase): | ||||
| class TestServiceExists(unittest.TestCase): | ||||
|  | ||||
|     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.sot = mock.Mock() | ||||
|         self.sot.test_method = lambda *args: self.return_value | ||||
|     @mock.patch('openstack.connection.from_config') | ||||
|     def test_service_exists(self, mock_from_config): | ||||
|         mock_from_config.return_value = self.conn | ||||
|  | ||||
|         self.mock_skip = mock.Mock() | ||||
|         self.sot.skip = self.mock_skip | ||||
|         self.sess.get_endpoint = mock.Mock() | ||||
|  | ||||
|         self.get_endpoint = mock.Mock() | ||||
|         self.sot.conn.session.get_endpoint = self.get_endpoint | ||||
|         self.assertTrue(base.service_exists(**self.kwargs)) | ||||
|  | ||||
|     def _test(self, **kwargs): | ||||
|         decorated = base.requires_service(**kwargs)(self.sot.test_method) | ||||
|         return decorated(self.sot) | ||||
|     @mock.patch('openstack.connection.from_config') | ||||
|     def test_service_doesnt_exist(self, mock_from_config): | ||||
|         mock_from_config.return_value = self.conn | ||||
|  | ||||
|     def test_service_exists(self): | ||||
|         self.assertEqual(self.return_value, self._test(**self.kwargs)) | ||||
|         self.get_endpoint.assert_called_with(**self.kwargs) | ||||
|         self.sess.get_endpoint = mock.Mock( | ||||
|             side_effect=_exceptions.EndpointNotFound('')) | ||||
|  | ||||
|     def test_service_doesnt_exist(self): | ||||
|         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) | ||||
|         self.assertFalse(base.service_exists(**self.kwargs)) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Jenkins
					Jenkins