From 0106126829aa52e9cd53a4d5ff8d31069001d1ac Mon Sep 17 00:00:00 2001 From: Andrey Kurilin Date: Thu, 28 Apr 2016 12:15:30 +0300 Subject: [PATCH] [Services] Add base class Service Implements base class for Service helpers Co-Authored-By: Alexander Maretskiy Co-Authored-By: Andrey Kurilin Blueprint: refactor-scenarios-utils Change-Id: Ieb83a781f49e011d072315a4a5f1dcf251478087 --- rally/plugins/openstack/service.py | 35 ++++++++++++ tests/unit/plugins/openstack/test_service.py | 57 ++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 rally/plugins/openstack/service.py create mode 100644 tests/unit/plugins/openstack/test_service.py diff --git a/rally/plugins/openstack/service.py b/rally/plugins/openstack/service.py new file mode 100644 index 00000000..dd2ab5a9 --- /dev/null +++ b/rally/plugins/openstack/service.py @@ -0,0 +1,35 @@ +# +# 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 rally import consts +from rally.task import service as base_service + + +service = base_service.service +compat_layer = base_service.compat_layer +Service = base_service.Service + + +class UnifiedOpenStackService(base_service.UnifiedService): + def discover_impl(self): + impl_cls, impls = super(UnifiedOpenStackService, self).discover_impl() + if not impl_cls: + # Nova-network is not listed in keystone catalog and we can not + # assume that it is enabled if neutron is missed. Since such + # discovery needs an external call, it is done only if needed. + for impl in impls: + o = impl._meta_get("impl") + if (o._meta_get("name") == consts.Service.NOVA_NET and + impl.is_applicable(self._clients)): + return impl, impls + return impl_cls, impls diff --git a/tests/unit/plugins/openstack/test_service.py b/tests/unit/plugins/openstack/test_service.py new file mode 100644 index 00000000..811741df --- /dev/null +++ b/tests/unit/plugins/openstack/test_service.py @@ -0,0 +1,57 @@ +# +# 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. + +import mock + +from rally.plugins.openstack import service +from tests.unit import test + + +class DiscoverTestCase(test.TestCase): + def test_discover_network_impl_based_on_service(self): + + class SomeService(service.UnifiedOpenStackService): + pass + + @service.service("nova-network", "network", version="1", + client_name="nova") + class NovaNetService(service.Service): + pass + + @service.compat_layer(NovaNetService) + class UnifiedNovaNetService(SomeService): + @classmethod + def is_applicable(cls, clients): + return True + + @service.service("neutron", "network", version="2") + class NeutronV2Service(service.Service): + pass + + @service.compat_layer(NeutronV2Service) + class UnifiedNeutronV2Service(SomeService): + pass + + clients = mock.MagicMock() + clients.nova.choose_version.return_value = "1" + clients.neutron.choose_version.return_value = "2" + + clients.services.return_value = {} + self.assertIsInstance(SomeService(clients)._impl, + UnifiedNovaNetService) + + clients.nova.return_value.services.list.reset_mock() + + clients.services.return_value = {"network": "neutron"} + self.assertIsInstance(SomeService(clients)._impl, + UnifiedNeutronV2Service)