From 3eba5b396929d3441154f9fd628289a2b9c170e5 Mon Sep 17 00:00:00 2001 From: Surojit Pathak Date: Mon, 5 Oct 2015 19:46:53 +0000 Subject: [PATCH] Functional tests for magnum service Adding functional tests for 'magnum service-list'. This ensures end-to-end working of CLI/API/DB/RPC/CONDUCTOR for this feature. Change-Id: I0bbf45a799cca232484ed7f3652b14692e47daf2 Closes-bug: #1501002 --- .../api/v1/clients/magnum_service_client.py | 45 +++++++++++++++++ .../api/v1/models/magnum_service_model.py | 30 ++++++++++++ .../functional/api/v1/test_magnum_service.py | 49 +++++++++++++++++++ magnum/tests/functional/common/client.py | 13 +++++ 4 files changed, 137 insertions(+) create mode 100644 magnum/tests/functional/api/v1/clients/magnum_service_client.py create mode 100644 magnum/tests/functional/api/v1/models/magnum_service_model.py create mode 100644 magnum/tests/functional/api/v1/test_magnum_service.py diff --git a/magnum/tests/functional/api/v1/clients/magnum_service_client.py b/magnum/tests/functional/api/v1/clients/magnum_service_client.py new file mode 100644 index 0000000000..05645601be --- /dev/null +++ b/magnum/tests/functional/api/v1/clients/magnum_service_client.py @@ -0,0 +1,45 @@ +# 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 magnum.tests.functional.api.v1.models import magnum_service_model +from magnum.tests.functional.common import client + + +class MagnumServiceClient(client.ClientMixin): + """Encapsulates REST calls and maps JSON to/from models""" + + @classmethod + def magnum_service_uri(cls, filters=None): + """Construct magnum services uri with optional filters + + :param filters: Optional k:v dict that's converted to url query + :returns: url string + """ + + url = "/mservices" + if filters: + url = cls.add_filters(url, filters) + return url + + def magnum_service_list(self, filters=None, **kwargs): + """Makes GET /mservices request and returns MagnumServiceCollection + + Abstracts REST call to return all magnum services. + + :param filters: Optional k:v dict that's converted to url query + :returns: response object and MagnumServiceCollection object + """ + + resp, body = self.client.get(self.magnum_service_uri(filters), + **kwargs) + return self.deserialize(resp, body, + magnum_service_model.MagnumServiceCollection) diff --git a/magnum/tests/functional/api/v1/models/magnum_service_model.py b/magnum/tests/functional/api/v1/models/magnum_service_model.py new file mode 100644 index 0000000000..94a2319644 --- /dev/null +++ b/magnum/tests/functional/api/v1/models/magnum_service_model.py @@ -0,0 +1,30 @@ +# 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 magnum.tests.functional.common import models + + +class MagnumServiceData(models.BaseModel): + """Data that encapsulates magnum_service attributes""" + pass + + +class MagnumServiceEntity(models.EntityModel): + """Entity Model that represents a single instance of MagnumServiceData""" + ENTITY_NAME = 'mservice' + MODEL_TYPE = MagnumServiceData + + +class MagnumServiceCollection(models.CollectionModel): + """Collection Model that represents a list of MagnumServiceData objects""" + COLLECTION_NAME = 'mservicelists' + MODEL_TYPE = MagnumServiceData diff --git a/magnum/tests/functional/api/v1/test_magnum_service.py b/magnum/tests/functional/api/v1/test_magnum_service.py new file mode 100644 index 0000000000..20b7da8ff7 --- /dev/null +++ b/magnum/tests/functional/api/v1/test_magnum_service.py @@ -0,0 +1,49 @@ +# 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 tempest_lib import exceptions +import testtools +import unittest + +from magnum.tests.functional.api.v1.clients import magnum_service_client as cli +from magnum.tests.functional.common import base + + +class MagnumServiceTest(base.BaseMagnumTest): + + """Tests for magnum-service .""" + + # NOTE(suro-patz): The following test fails in lieu of non-admin user + # available for functional-test-gate. + # For now, I will keep the test, but skipped. + @unittest.skip("Requires non-admin user in functional-test-gate") + @testtools.testcase.attr('negative') + def test_magnum_service_list_needs_admin(self): + # Ensure that policy enforcement does not allow 'default' user + client = cli.MagnumServiceClient.as_user('default') + self.assertRaises(exceptions.ServerFault, client.magnum_service_list) + + @testtools.testcase.attr('positive') + def test_magnum_service_list(self): + # get json object + client = cli.MagnumServiceClient.as_user('admin') + resp, msvcs = client.magnum_service_list() + self.assertEqual(resp.status, 200) + # Note(suro-patz): Following code assumes that we have only + # one service, magnum-conductor enabled, as of now. + self.assertEqual(len(msvcs.mservices), 1) + mcond_svc = msvcs.mservices[0] + self.assertEqual(mcond_svc['id'], 1) + self.assertEqual(mcond_svc['state'], 'up') + self.assertEqual(mcond_svc['binary'], 'magnum-conductor') + self.assertGreater(mcond_svc['report_count'], 0) diff --git a/magnum/tests/functional/common/client.py b/magnum/tests/functional/common/client.py index 98c959ba67..23bbac9760 100644 --- a/magnum/tests/functional/common/client.py +++ b/magnum/tests/functional/common/client.py @@ -49,6 +49,18 @@ class MagnumClient(BaseMagnumClient): ) +class AdminMagnumClient(BaseMagnumClient): + """Responsible for setting up auth provider for admin user""" + + def get_auth_provider(self): + mgr = manager.Manager() + return mgr.get_auth_provider( + username=config.Config.admin_user, + password=config.Config.admin_passwd, + tenant_name=config.Config.admin_tenant + ) + + class ClientMixin(object): """Responsible for mapping setting up common client use cases: @@ -62,6 +74,7 @@ class ClientMixin(object): def get_clients(cls): return { 'default': MagnumClient(), + 'admin': AdminMagnumClient(), } def __init__(self, client):