add test framework for rest api tests

this patch include:
fake pecan app for api tests
4 test cases:
  bad url
  setup
  get_root
  get_v1_root

Story: 2007082
Task: 38158
Change-Id: I3f933b43aede43f25abca3327a9638a6fa88914c
Signed-off-by: SidneyAn <ran1.an@intel.com>
This commit is contained in:
SidneyAn 2020-01-17 13:56:12 +08:00 committed by Ran An
parent b1dfa14f0b
commit d73ba87457
7 changed files with 186 additions and 2 deletions

View File

@ -0,0 +1,14 @@
# Copyright 2020 Intel Corporation.
# 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.

View File

@ -0,0 +1,79 @@
# Copyright 2020 Intel Corporation.
# 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.
"""Base classes for API tests."""
from oslo_config import cfg
import pecan
import pecan.testing
from fm.tests import base
from fm.common import context as fm_context
PATH_PREFIX = '/v1'
class FunctionalTest(base.TestCase):
"""Used for functional tests of Pecan controllers where you need to
test your literal application and its integration with the
framework.
"""
SOURCE_DATA = {'test_source': {'somekey': '666'}}
def setUp(self):
super(FunctionalTest, self).setUp()
self.context = fm_context.RequestContext(is_admin=True)
self.app = self._make_app()
def _make_app(self):
cfg.CONF.set_override("debug", True)
self.config = {
'app': {
'root': 'fm.api.controllers.root.RootController',
'modules': ['fm.api'],
'acl_public_routes': ['/', '/v1'],
},
}
return pecan.testing.load_test_app(self.config)
def tearDown(self):
super(FunctionalTest, self).tearDown()
pecan.set_config({}, overwrite=True)
def get_json(self, path, expect_errors=False, headers=None,
extra_environ=None, q=[], path_prefix=PATH_PREFIX, **params):
full_path = path_prefix + path
query_params = {'q.field': [],
'q.value': [],
'q.op': [],
}
for query in q:
for name in ['field', 'op', 'value']:
query_params['q.%s' % name].append(query.get(name, ''))
all_params = {}
all_params.update(params)
if q:
all_params.update(query_params)
response = self.app.get(full_path,
params=all_params,
headers=headers,
extra_environ=extra_environ,
expect_errors=expect_errors)
if not expect_errors:
response = response.json
return response

View File

@ -0,0 +1,30 @@
# Copyright 2020 Intel Corporation.
# 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 fm.tests.api import base
class TestBase(base.FunctionalTest):
def test_api_setup(self):
pass
def test_bad_uri(self):
response = self.get_json('/bad/path',
expect_errors=True,
headers={"Accept": "application/json"})
self.assertEqual(response.status_int, 404)
self.assertEqual(response.content_type, "application/json")
self.assertTrue(response.json['error_message'])

View File

@ -0,0 +1,40 @@
# Copyright 2013 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.
#
# Copyright 2020 Intel Corporation.
#
from fm.tests.api import base
class TestRoot(base.FunctionalTest):
def test_get_root(self):
data = self.get_json('/', path_prefix='')
self.assertEqual(data['default_version']['id'], 'v1')
# Check fields are not empty
[self.assertNotIn(f, ['', []]) for f in data.keys()]
class TestV1Root(base.FunctionalTest):
def test_get_v1_root(self):
data = self.get_json('/')
self.assertEqual(data['id'], 'v1')
# Check fields are not empty
[self.assertNotIn(f, ['', []]) for f in data.keys()]
# Check if the resources are present
self.assertIn({'type': 'application/vnd.openstack.fm.v1+json',
'base': 'application/json'}, data['media_types'])

View File

@ -19,7 +19,10 @@ Allows overriding of config for use of fakes, and some black magic for
inline callbacks. inline callbacks.
""" """
import sys
import fixtures import fixtures
import mock
import testtools import testtools
from oslo_config import cfg from oslo_config import cfg
@ -27,6 +30,8 @@ from oslo_log import log as logging
CONF = cfg.CONF CONF = cfg.CONF
sys.modules['fm_core'] = mock.Mock()
class TestCase(testtools.TestCase): class TestCase(testtools.TestCase):
"""Test case base class for all unit tests.""" """Test case base class for all unit tests."""

View File

@ -9,5 +9,18 @@ stestr
mock mock
cython cython
oslo.log oslo.log
oslo.concurrency oslo.i18n # Apache-2.0
oslo.config>=3.7.0 # Apache-2.0
oslo.concurrency>=3.7.1 # Apache-2.0
oslo.db>=4.1.0 # Apache-2.0
oslo.service>=1.10.0 # Apache-2.0
oslo.utils>=3.5.0 # Apache-2.0
oslo.serialization>=1.10.0,!=2.19.1 # Apache-2.0
oslo_policy
oslo_versionedobjects
python-keystoneclient>=3.8.0 # Apache-2.0
keystonemiddleware>=4.12.0 # Apache-2.0
pecan>=1.0.0
WSME>=0.5b2
httplib2
keyring <= 18.0.1

View File

@ -12,6 +12,9 @@ setenv = VIRTUAL_ENV={envdir}
OS_TEST_TIMEOUT=60 OS_TEST_TIMEOUT=60
deps = -r{toxinidir}/test-requirements.txt deps = -r{toxinidir}/test-requirements.txt
-e{[tox]stxdir}/config/tsconfig/tsconfig -e{[tox]stxdir}/config/tsconfig/tsconfig
-e{[tox]stxdir}/config/sysinv/cgts-client/cgts-client
-e{[tox]stxdir}/fault/fm-api
-e{[tox]stxdir}/fault/fm-rest-api/fm
[testenv:venv] [testenv:venv]
basepython = python3 basepython = python3