From d73ba874571bdaeba24f856fb785749954a23c55 Mon Sep 17 00:00:00 2001 From: SidneyAn Date: Fri, 17 Jan 2020 13:56:12 +0800 Subject: [PATCH] add test framework for rest api tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- fm-rest-api/fm/fm/tests/api/__init__.py | 14 +++++ fm-rest-api/fm/fm/tests/api/base.py | 79 ++++++++++++++++++++++++ fm-rest-api/fm/fm/tests/api/test_base.py | 30 +++++++++ fm-rest-api/fm/fm/tests/api/test_root.py | 40 ++++++++++++ fm-rest-api/fm/fm/tests/base.py | 5 ++ fm-rest-api/fm/test-requirements.txt | 17 ++++- fm-rest-api/fm/tox.ini | 3 + 7 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 fm-rest-api/fm/fm/tests/api/__init__.py create mode 100644 fm-rest-api/fm/fm/tests/api/base.py create mode 100644 fm-rest-api/fm/fm/tests/api/test_base.py create mode 100644 fm-rest-api/fm/fm/tests/api/test_root.py diff --git a/fm-rest-api/fm/fm/tests/api/__init__.py b/fm-rest-api/fm/fm/tests/api/__init__.py new file mode 100644 index 00000000..70618e64 --- /dev/null +++ b/fm-rest-api/fm/fm/tests/api/__init__.py @@ -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. diff --git a/fm-rest-api/fm/fm/tests/api/base.py b/fm-rest-api/fm/fm/tests/api/base.py new file mode 100644 index 00000000..d8506099 --- /dev/null +++ b/fm-rest-api/fm/fm/tests/api/base.py @@ -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 diff --git a/fm-rest-api/fm/fm/tests/api/test_base.py b/fm-rest-api/fm/fm/tests/api/test_base.py new file mode 100644 index 00000000..179ea53a --- /dev/null +++ b/fm-rest-api/fm/fm/tests/api/test_base.py @@ -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']) diff --git a/fm-rest-api/fm/fm/tests/api/test_root.py b/fm-rest-api/fm/fm/tests/api/test_root.py new file mode 100644 index 00000000..802e41ae --- /dev/null +++ b/fm-rest-api/fm/fm/tests/api/test_root.py @@ -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']) diff --git a/fm-rest-api/fm/fm/tests/base.py b/fm-rest-api/fm/fm/tests/base.py index 9a8d4678..3f5e7e71 100644 --- a/fm-rest-api/fm/fm/tests/base.py +++ b/fm-rest-api/fm/fm/tests/base.py @@ -19,7 +19,10 @@ Allows overriding of config for use of fakes, and some black magic for inline callbacks. """ +import sys + import fixtures +import mock import testtools from oslo_config import cfg @@ -27,6 +30,8 @@ from oslo_log import log as logging CONF = cfg.CONF +sys.modules['fm_core'] = mock.Mock() + class TestCase(testtools.TestCase): """Test case base class for all unit tests.""" diff --git a/fm-rest-api/fm/test-requirements.txt b/fm-rest-api/fm/test-requirements.txt index 02dc0100..d5e38037 100644 --- a/fm-rest-api/fm/test-requirements.txt +++ b/fm-rest-api/fm/test-requirements.txt @@ -9,5 +9,18 @@ stestr mock cython 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 diff --git a/fm-rest-api/fm/tox.ini b/fm-rest-api/fm/tox.ini index 8de4cc65..f883d69e 100644 --- a/fm-rest-api/fm/tox.ini +++ b/fm-rest-api/fm/tox.ini @@ -12,6 +12,9 @@ setenv = VIRTUAL_ENV={envdir} OS_TEST_TIMEOUT=60 deps = -r{toxinidir}/test-requirements.txt -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] basepython = python3