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