From 90030192a4f346f96fd94651d6435d0e6d76b98d Mon Sep 17 00:00:00 2001 From: jiaopengju Date: Fri, 9 Nov 2018 10:38:43 +0800 Subject: [PATCH] Fix getting services from keystone error Now, karbor's fullstack is broken, it is because the getting services operation in operationengine is broken. Change-Id: Ibbb717f9a5ed78e0f2d56499a41ab312a47a5837 --- karbor/common/karbor_keystone_plugin.py | 8 +++- karbor/tests/unit/common/__init__.py | 0 .../common/test_karbor_keystone_plugin.py | 48 +++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 karbor/tests/unit/common/__init__.py create mode 100644 karbor/tests/unit/common/test_karbor_keystone_plugin.py diff --git a/karbor/common/karbor_keystone_plugin.py b/karbor/common/karbor_keystone_plugin.py index 0fac6654..f579a3be 100644 --- a/karbor/common/karbor_keystone_plugin.py +++ b/karbor/common/karbor_keystone_plugin.py @@ -89,17 +89,21 @@ class KarborKeystonePlugin(object): def get_service_endpoint(self, service_name, service_type, region_id, interface='public'): + if self._auth_uri and self._auth_uri.endswith('/'): + base_url = self._auth_uri[:-1] + else: + base_url = self._auth_uri try: service = self.client.services.list( name=service_name, service_type=service_type, - base_url=self.auth_uri) + base_url=base_url) endpoint = self.client.endpoints.list( service=service[0], interface=interface, region_id=region_id, - base_url=self.auth_uri) + base_url=base_url) return endpoint[0].url if endpoint else None diff --git a/karbor/tests/unit/common/__init__.py b/karbor/tests/unit/common/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/karbor/tests/unit/common/test_karbor_keystone_plugin.py b/karbor/tests/unit/common/test_karbor_keystone_plugin.py new file mode 100644 index 00000000..cdeffbd3 --- /dev/null +++ b/karbor/tests/unit/common/test_karbor_keystone_plugin.py @@ -0,0 +1,48 @@ +# 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 karbor.common import karbor_keystone_plugin +from karbor.tests import base + + +class KarborKeystonePluginTest(base.TestCase): + + def setUp(self): + super(KarborKeystonePluginTest, self).setUp() + self.kc_plugin = karbor_keystone_plugin.KarborKeystonePlugin() + self.kc_plugin.client.services.list = mock.MagicMock() + self.kc_plugin.client.endpoints.list = mock.MagicMock() + self.kc_plugin.client.services.list.return_value = ( + 'http://192.168.1.2:8799') + + def test_get_service_endpoint_with_slash_end(self): + self.kc_plugin._auth_uri = 'http://192.168.1.1/identity/v3/' + self.kc_plugin.get_service_endpoint( + 'karbor', 'data-protect', 'fake_region_id', 'public') + self.kc_plugin.client.services.list.assert_called_once_with( + name='karbor', + service_type='data-protect', + base_url='http://192.168.1.1/identity/v3' + ) + + def test_get_service_endpoint_with_no_slash_end(self): + self.kc_plugin._auth_uri = 'http://192.168.1.1/identity/v3' + self.kc_plugin.get_service_endpoint( + 'karbor', 'data-protect', 'fake_region_id', 'public') + self.kc_plugin.client.services.list.assert_called_once_with( + name='karbor', + service_type='data-protect', + base_url='http://192.168.1.1/identity/v3' + )