Add unit tests

The patch adds unit tests for methods of api_discovery module.

Change-Id: Idc0f4b511e5d54e8a21ade6694e18add12c4e9ec
This commit is contained in:
Martin Kopec 2017-01-31 12:29:03 +00:00
parent 51e8a6d5b2
commit 088ec97bf0
7 changed files with 474 additions and 4 deletions

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright 2010-2011 OpenStack Foundation
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
# Copyright 2017 Red Hat, Inc.
# All Rights Reserved.
#
# 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

View File

@ -0,0 +1,165 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Red Hat, Inc.
# All Rights Reserved.
#
# 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.
from config_tempest import api_discovery as api
from config_tempest.tests import base
from fixtures import MonkeyPatch
import json
from mock import Mock
class BaseServiceTest(base.TestCase):
FAKE_TOKEN = "s6d5f45sdf4s564f4s6464sdfsd514"
FAKE_HEADERS = {
'Accept': 'application/json', 'X-Auth-Token': FAKE_TOKEN
}
FAKE_URL = "http://10.200.16.10:8774/"
FAKE_VERSIONS = (
{
"versions": [{
"status": "SUPPORTED",
"updated": "2011-01-21T11:33:21Z",
"links": [{
"href": "http://10.200.16.10:8774/v2 / ",
"rel": "self "
}],
"min_version": "",
"version": "",
"id": "v2.0",
"values": [
{"id": 'v3.8'}
]
}, {
"status": "CURRENT",
"updated": "2013-07-23T11:33:21Z",
"links": [{
"href": "http://10.200.16.10:8774/v2.1/",
"rel": "self"
}],
"min_version": "2.1",
"version": "2.41",
"id": "v2.1",
"values": [
{"id": 'v2.0'}
]
}]
}
)
FAKE_IDENTITY_VERSIONS = (
{
'versions': {
'values': [{
'status': 'stable',
'id': 'v3.8',
}, {
'status': 'deprecated',
'id': 'v2.0',
}]
}
}
)
FAKE_EXTENSIONS = (
{
"extensions": [{
"updated": "2014-12-03T00:00:00Z",
"name": "Multinic",
"namespace": "http://docs.openstack.org/compute/ext/fake_xml",
"alias": "NMN",
"description": "Multiple network support."
}, {
"updated": "2014-12-03T00:00:00Z",
"name": "DiskConfig",
"namespace": "http://docs.openstack.org/compute/ext/fake_xml",
"alias": "OS-DCF",
"description": "Disk Management Extension."
}]
}
)
FAKE_IDENTITY_EXTENSIONS = (
{
"extensions": {
'values': [{
'alias': 'OS-DCF',
'id': 'v3.8',
}, {
'alias': 'NMN',
'id': 'v2.0',
}]
}
}
)
FAKE_STORAGE_EXTENSIONS = (
{
"formpost": {},
"methods": ["GET", "HEAD", "PUT", "POST", "DELETE"],
"ratelimit": {
"account_ratelimit": 0.0,
"max_sleep_time_seconds": 60.0,
"container_ratelimits": []
},
"account_quotas": {},
"swift": {
"container_listing_limit": 10000,
"allow_account_management": True,
"max_container_name_length": 256
}
}
)
class FakeRequestResponse(object):
URL = 'http://docs.openstack.org/api/openstack-identity/3/ext/'
FAKE_V3_EXTENSIONS = (
{
'resources': {
URL + 'OS-INHERIT/1.0/rel/domain_user_'
+ 'role_inherited_to_projects': "",
URL + 'OS-SIMPLE-CERT/1.0/rel/ca_certificate': "",
URL + 'OS-EP-FILTER/1.0/rel/endpoint_group_to_'
+ 'project_association': "",
URL + 'OS-EP-FILTER/1.0/rel/project_endpoint': "",
URL + 'OS-OAUTH1/1.0/rel/user_access_token_roles': ""
}
}
)
def __init__(self):
self.content = json.dumps(self.FAKE_V3_EXTENSIONS)
def _fake_service_do_get_method(self, fake_data):
function2mock = 'config_tempest.api_discovery.Service.do_get'
do_get_output = json.dumps(fake_data)
mocked_do_get = Mock()
mocked_do_get.return_value = do_get_output
self.useFixture(MonkeyPatch(function2mock, mocked_do_get))
def _test_get_service_class(self, service, cls):
resp = api.get_service_class(service)
self.assertEqual(resp, cls)
def _get_extensions(self, service, expected_resp, fake_data):
self._fake_service_do_get_method(fake_data)
resp = service.get_extensions()
self.assertItemsEqual(resp, expected_resp)
def _test_deserialize_versions(self, service, expected_resp, fake_data):
resp = service.deserialize_versions(fake_data)
self.assertItemsEqual(resp, expected_resp)

View File

@ -0,0 +1,128 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Red Hat, Inc.
# All Rights Reserved.
#
# 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.
from config_tempest import api_discovery as api
from config_tempest.tests.base_service_test import BaseServiceTest
from fixtures import MonkeyPatch
from mock import Mock
class TestApiDiscoveryMethods(BaseServiceTest):
FAKE_AUTH_DATA = (
{
'serviceCatalog': [
{
'endpoints_links': [],
'endpoints': [{
'adminURL': 'http://172.16.52.151:8080/v1/AUTH_402',
'region': 'RegionOne',
'publicURL': 'http://172.16.52.151:8080/v1/AUTH_402',
'internalURL': 'http://172.16.52.151:8080/v1/AUTH_402',
'id': '22c221db6ffd4236a3fd054c60aa8fd6'
}],
'type': 'object-store',
'name': 'swift'
}
]
}
)
EXPECTED_RESP = (
{
'object-store':
{
'extensions': 'discovered extensions',
'url': 'http://172.16.52.151:8080/v1/AUTH_402',
'versions': []
}
}
)
FAKE_AUTH_DATA_V3 = (
{
'serviceCatalog': [
{
'endpoints_links': [],
'endpoints': [{
'adminURL': 'http://172.16.52.151:8774/v3/402',
'region': 'RegionOne',
'publicURL': 'http://172.16.52.151:8774/v3/402',
'internalURL': 'http://172.16.52.151:8774/v3/402',
'id': '01bbde4f9fb54d35badf0561a53b2bdb'
}],
'type': 'compute',
'name': 'nova'
}
]
}
)
EXPECTED_RESP_V3 = (
{
'compute':
{
'url': 'http://172.16.52.151:8774/v3/402',
'versions': 'discovered versions'
}
}
)
def setUp(self):
super(TestApiDiscoveryMethods, self).setUp()
class FakeAuthProvider(object):
def __init__(self, auth_url, auth_data):
self.auth_url = auth_url # 'http://172.16.52.151:5000/v2.0'
self.auth_data = auth_data
def get_auth(self):
token = 'AAAAABYkehvCCJOsO2GWGqBbxk0mhH7VulICOW'
return (token, self.auth_data)
def _test_discover(self, url, data, function2mock,
mock_ret_val, expected_resp):
provider = self.FakeAuthProvider(url, data)
mocked_function = Mock()
mocked_function.return_value = mock_ret_val
self.useFixture(MonkeyPatch(function2mock, mocked_function))
resp = api.discover(provider, "RegionOne")
self.assertEqual(resp, expected_resp)
def test_get_identity_v3_extensions(self):
expected_resp = ['OS-INHERIT', 'OS-OAUTH1',
'OS-SIMPLE-CERT', 'OS-EP-FILTER']
fake_resp = self.FakeRequestResponse()
mocked_requests = Mock()
mocked_requests.return_value = fake_resp
self.useFixture(MonkeyPatch('requests.get', mocked_requests))
resp = api.get_identity_v3_extensions(self.FAKE_URL + "v3")
self.assertItemsEqual(resp, expected_resp)
def test_discover_with_v2_url(self):
function2mock = ('config_tempest.api_discovery.'
'ObjectStorageService.get_extensions')
mock_ret_val = "discovered extensions"
self._test_discover(self.FAKE_URL + "v2.0", self.FAKE_AUTH_DATA,
function2mock, mock_ret_val, self.EXPECTED_RESP)
def test_discover_with_v3_url(self):
function2mock = ('config_tempest.api_discovery.'
'ComputeService.get_versions')
mock_ret_val = "discovered versions"
self._test_discover(self.FAKE_URL + "v3", self.FAKE_AUTH_DATA_V3,
function2mock, mock_ret_val, self.EXPECTED_RESP_V3)

View File

@ -0,0 +1,164 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Red Hat, Inc.
# All Rights Reserved.
#
# 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.
from config_tempest import api_discovery as api
from config_tempest.tests.base_service_test import BaseServiceTest
import mock
class TestService(BaseServiceTest):
def setUp(self):
super(TestService, self).setUp()
self.Service = api.Service("ServiceName",
self.FAKE_URL,
self.FAKE_TOKEN,
disable_ssl_validation=False)
def _mocked_do_get(self, mock_urllib3):
mock_http = mock_urllib3.PoolManager()
expected_resp = mock_http.request('GET',
self.FAKE_URL,
self.FAKE_HEADERS)
return expected_resp.data
@mock.patch('config_tempest.api_discovery.urllib3')
def test_do_get(self, mock_urllib3):
resp = self.Service.do_get(self.FAKE_URL)
expected_resp = self._mocked_do_get(mock_urllib3)
self.assertEqual(resp, expected_resp)
def test_service_headers(self):
self.assertEqual(self.Service.headers, self.FAKE_HEADERS)
class TestVersionedService(BaseServiceTest):
def setUp(self):
super(TestVersionedService, self).setUp()
self.Service = api.VersionedService("ServiceName",
self.FAKE_URL,
self.FAKE_TOKEN,
disable_ssl_validation=False)
def test_get_versions(self):
expected_resp = ['v2.0', 'v2.1']
self._fake_service_do_get_method(self.FAKE_VERSIONS)
resp = self.Service.get_versions()
self.assertItemsEqual(resp, expected_resp)
def test_deserialize_versions(self):
expected_resp = ['v2.0', 'v2.1']
self._test_deserialize_versions(self.Service,
expected_resp,
self.FAKE_VERSIONS)
class TestComputeService(BaseServiceTest):
def setUp(self):
super(TestComputeService, self).setUp()
self.Service = api.ComputeService("ServiceName",
self.FAKE_URL,
self.FAKE_TOKEN,
disable_ssl_validation=False)
def test_get_extensions(self):
expected_resp = ['NMN', 'OS-DCF']
self._get_extensions(self.Service, expected_resp, self.FAKE_EXTENSIONS)
def test_get_service_class(self):
self._test_get_service_class('compute', api.ComputeService)
class TestImageService(BaseServiceTest):
def setUp(self):
super(TestImageService, self).setUp()
def test_get_service_class(self):
self._test_get_service_class('image', api.ImageService)
class TestNetworkService(BaseServiceTest):
def setUp(self):
super(TestNetworkService, self).setUp()
self.Service = api.NetworkService("ServiceName",
self.FAKE_URL,
self.FAKE_TOKEN,
disable_ssl_validation=False)
def test_get_extensions(self):
expected_resp = ['NMN', 'OS-DCF']
self._get_extensions(self.Service, expected_resp, self.FAKE_EXTENSIONS)
def test_get_service_class(self):
self._test_get_service_class('network', api.NetworkService)
class TestVolumeService(BaseServiceTest):
def setUp(self):
super(TestVolumeService, self).setUp()
self.Service = api.VolumeService("ServiceName",
self.FAKE_URL,
self.FAKE_TOKEN,
disable_ssl_validation=False)
def test_get_extensions(self):
expected_resp = ['NMN', 'OS-DCF']
self._get_extensions(self.Service, expected_resp, self.FAKE_EXTENSIONS)
def test_get_service_class(self):
self._test_get_service_class('volume', api.VolumeService)
class TestIdentityService(BaseServiceTest):
def setUp(self):
super(TestIdentityService, self).setUp()
self.Service = api.IdentityService("ServiceName",
self.FAKE_URL,
self.FAKE_TOKEN,
disable_ssl_validation=False)
def test_get_extensions(self):
expected_resp = ['OS-DCF', 'NMN']
self._get_extensions(self.Service, expected_resp,
self.FAKE_IDENTITY_EXTENSIONS)
def test_deserialize_versions(self):
expected_resp = ['v3.8', 'v2.0']
self._test_deserialize_versions(self.Service,
expected_resp,
self.FAKE_IDENTITY_VERSIONS)
def test_get_service_class(self):
self._test_get_service_class('identity', api.IdentityService)
class TestObjectStorageService(BaseServiceTest):
def setUp(self):
super(TestObjectStorageService, self).setUp()
self.Service = api.ObjectStorageService("ServiceName",
self.FAKE_URL,
self.FAKE_TOKEN,
disable_ssl_validation=False)
def test_get_extensions(self):
expected_resp = ['formpost', 'ratelimit',
'methods', 'account_quotas']
self._get_extensions(self.Service, expected_resp,
self.FAKE_STORAGE_EXTENSIONS)
def test_get_service_class(self):
self._test_get_service_class('object-store',
api.ObjectStorageService)

View File

@ -0,0 +1,6 @@
---
prelude: >
Added new unit tests.
other:
- |
Added new unit tests for api discovery module.

View File

@ -5,3 +5,4 @@
pbr>=1.8 # Apache-2.0
tempest>=14.0.0 # Apache-2.0
requests>=2.10.0,!=2.12.2 # Apache-2.0
os-testr>=0.8.0 # Apache-2.0

10
tox.ini
View File

@ -7,10 +7,16 @@ skipsdist = True
usedevelop = True
install_command = pip install -c{env:UPPER_CONSTRAINTS_FILE:https://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt} {opts} {packages}
setenv =
OS_TEST_PATH=./config_tempest/tests
VIRTUAL_ENV={envdir}
PYTHONWARNINGS=default::DeprecationWarning
deps = -r{toxinidir}/test-requirements.txt
commands = python setup.py testr --slowest --testr-args='{posargs}'
whitelist_externals = *
deps =
-r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
commands =
find . -type f -name "*.pyc" -delete
ostestr {posargs}
[testenv:pep8]
commands = flake8 {posargs}