Merge "Decorator for functional tests to check services"

This commit is contained in:
Jenkins
2015-05-28 17:34:15 +00:00
committed by Gerrit Code Review
2 changed files with 86 additions and 0 deletions

View File

@@ -15,11 +15,43 @@ import unittest
import os_client_config
from openstack.auth import service_filter
from openstack import connection
from openstack import exceptions
from openstack import profile
from openstack import utils
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.
Usage:
@requires_service(service_type="identity", version="v3")
def test_v3_auth(self):
...
:param kwargs: The kwargs needed to create a
:class:`~openstack.auth.service_filter.ServiceFilter`.
:returns: The test result if the test is executed.
:raises: SkipTest, which is handled by the test runner.
"""
def wrap(method):
def check(self):
try:
self.conn.authenticator.get_endpoint(
self.conn.transport,
service_filter.ServiceFilter(**kwargs))
return method(self)
except exceptions.EndpointNotFound as exc:
self.skip(exc.message)
return check
return wrap
class BaseFunctionalTest(unittest.TestCase):
@classmethod
def setUpClass(cls):

View File

@@ -0,0 +1,54 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import unittest
import mock
from openstack import exceptions
from openstack.tests.functional import base
class Test_requires_service(unittest.TestCase):
def setUp(self):
super(Test_requires_service, self).setUp()
self.return_value = 1
self.kwargs = {"service_type": "identity", "version": "v3"}
self.sot = mock.Mock()
self.sot.test_method = lambda *args: self.return_value
self.mock_skip = mock.Mock()
self.sot.skip = self.mock_skip
self.get_endpoint = mock.Mock()
self.sot.conn.authenticator.get_endpoint = self.get_endpoint
def _test(self, **kwargs):
decorated = base.requires_service(**kwargs)(self.sot.test_method)
return decorated(self.sot)
@mock.patch("openstack.auth.service_filter.ServiceFilter")
def test_service_exists(self, mock_filter):
self.assertEqual(self.return_value, self._test(**self.kwargs))
mock_filter.assert_called_with(**self.kwargs)
@mock.patch("openstack.auth.service_filter.ServiceFilter")
def test_service_doesnt_exist(self, mock_filter):
exc = exceptions.EndpointNotFound
self.sot.conn.authenticator.get_endpoint.side_effect = exc
self._test(**self.kwargs)
mock_filter.assert_called_with(**self.kwargs)
self.assertEqual(self.mock_skip.call_count, 1)