From 47bb4bddb2cf54c5cf758128feb31cccd5719e1f Mon Sep 17 00:00:00 2001 From: Yanyan Hu Date: Thu, 17 Jul 2014 04:22:32 -0500 Subject: [PATCH] Using keystone discover to find V3 auth_url This patch uses keystone client discover to find correct V3 auth_url which is expected by heat collector. Change-Id: I2b8b5a768bdeb1cd1d20fab3b4234306c1429d6d Closes-Bug: #1341936 --- os_collect_config/keystone.py | 11 ++++++++++- os_collect_config/tests/test_collect.py | 10 ++++++++-- os_collect_config/tests/test_heat.py | 21 ++++++++++++++++++--- os_collect_config/tests/test_keystone.py | 24 ++++++++++++++++++++++-- test-requirements.txt | 1 + 5 files changed, 59 insertions(+), 8 deletions(-) diff --git a/os_collect_config/keystone.py b/os_collect_config/keystone.py index d79c3c2..302abb4 100644 --- a/os_collect_config/keystone.py +++ b/os_collect_config/keystone.py @@ -16,6 +16,7 @@ import hashlib import os from dogpile import cache +from keystoneclient import discover as ks_discover from keystoneclient import exceptions as ks_exc from keystoneclient.v3 import client as ks_keystoneclient from oslo.config import cfg @@ -50,11 +51,19 @@ class Keystone(object): Uses keystoneclient.v3 if unspecified. ''' self.keystoneclient = keystoneclient or ks_keystoneclient - self.auth_url = auth_url self.user_id = user_id self.password = password self.project_id = project_id self._client = None + try: + discover = ks_discover.Discover(auth_url=auth_url) + v3_auth_url = discover.url_for('3.0') + if v3_auth_url: + self.auth_url = v3_auth_url + else: + self.auth_url = auth_url + except ks_exc.ClientException: + self.auth_url = auth_url.replace('/v2.0', '/v3') if CONF.keystone.cache_dir: if not os.path.isdir(CONF.keystone.cache_dir): os.makedirs(CONF.keystone.cache_dir, mode=0o700) diff --git a/os_collect_config/tests/test_collect.py b/os_collect_config/tests/test_collect.py index c753e2d..e46a59e 100644 --- a/os_collect_config/tests/test_collect.py +++ b/os_collect_config/tests/test_collect.py @@ -17,11 +17,13 @@ import copy import extras import fixtures import json +import mock import os import signal import sys import tempfile +from keystoneclient import discover as ks_discover from oslo.config import cfg import testtools from testtools import matchers @@ -335,8 +337,12 @@ class TestCollectAll(testtools.TestCase): cfg.CONF.heat.stack_id = 'a/c482680f-7238-403d-8f76-36acf0c8e0aa' cfg.CONF.heat.resource_name = 'server' - def _call_collect_all( - self, store, collector_kwargs_map=None, collectors=None): + @mock.patch.object(ks_discover.Discover, '__init__') + @mock.patch.object(ks_discover.Discover, 'url_for') + def _call_collect_all(self, mock_url_for, mock___init__, store, + collector_kwargs_map=None, collectors=None): + mock___init__.return_value = None + mock_url_for.return_value = cfg.CONF.heat.auth_url if collector_kwargs_map is None: collector_kwargs_map = { 'ec2': {'requests_impl': test_ec2.FakeRequests}, diff --git a/os_collect_config/tests/test_heat.py b/os_collect_config/tests/test_heat.py index cc3fa42..feac846 100644 --- a/os_collect_config/tests/test_heat.py +++ b/os_collect_config/tests/test_heat.py @@ -12,7 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +import mock + import fixtures +from keystoneclient import discover as ks_discover from keystoneclient import exceptions as ks_exc from oslo.config import cfg import testtools @@ -126,7 +129,11 @@ class TestHeatBase(testtools.TestCase): class TestHeat(TestHeatBase): - def test_collect_heat(self): + @mock.patch.object(ks_discover.Discover, '__init__') + @mock.patch.object(ks_discover.Discover, 'url_for') + def test_collect_heat(self, mock_url_for, mock___init__): + mock___init__.return_value = None + mock_url_for.return_value = cfg.CONF.heat.auth_url heat_md = heat.Collector(keystoneclient=FakeKeystoneClient(self), heatclient=FakeHeatClient(self)).collect() self.assertThat(heat_md, matchers.IsInstance(list)) @@ -137,9 +144,17 @@ class TestHeat(TestHeatBase): self.assertIn(k, heat_md) self.assertEqual(heat_md[k], META_DATA[k]) - self.assertEqual('', self.log.output) + # FIXME(yanyanhu): Temporary hack to deal with possible log + # level setting for urllib3.connectionpool. + self.assertTrue( + self.log.output == '' or + self.log.output == 'Starting new HTTP connection (1): 127.0.0.1\n') - def test_collect_heat_fail(self): + @mock.patch.object(ks_discover.Discover, '__init__') + @mock.patch.object(ks_discover.Discover, 'url_for') + def test_collect_heat_fail(self, mock_url_for, mock___init__): + mock___init__.return_value = None + mock_url_for.return_value = cfg.CONF.heat.auth_url heat_collect = heat.Collector( keystoneclient=FakeFailKeystoneClient(self), heatclient=FakeHeatClient(self)) diff --git a/os_collect_config/tests/test_keystone.py b/os_collect_config/tests/test_keystone.py index e6d0dc1..f02c7fb 100644 --- a/os_collect_config/tests/test_keystone.py +++ b/os_collect_config/tests/test_keystone.py @@ -12,9 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +import mock import tempfile import fixtures +from keystoneclient import discover as ks_discover from keystoneclient import exceptions as ks_exc from oslo.config import cfg import testtools @@ -50,19 +52,37 @@ class KeystoneTest(testtools.TestCase): self.cachedir = tempfile.mkdtemp() cfg.CONF.set_override('cache_dir', self.cachedir, group='keystone') - def test_cache_is_created(self): + @mock.patch.object(ks_discover.Discover, '__init__') + @mock.patch.object(ks_discover.Discover, 'url_for') + def test_discover_fail(self, mock_url_for, mock___init__): + mock___init__.return_value = None + mock_url_for.side_effect = ks_exc.DiscoveryFailure() + ks = keystone.Keystone( + 'http://server.test:5000/v2.0', 'auser', 'apassword', 'aproject', + test_heat.FakeKeystoneClient(self)) + self.assertEqual(ks.auth_url, 'http://server.test:5000/v3') + + @mock.patch.object(ks_discover.Discover, '__init__') + @mock.patch.object(ks_discover.Discover, 'url_for') + def test_cache_is_created(self, mock_url_for, mock___init__): + mock___init__.return_value = None + mock_url_for.return_value = 'http://server.test:5000/' ks = keystone.Keystone( 'http://server.test:5000/', 'auser', 'apassword', 'aproject', test_heat.FakeKeystoneClient(self)) self.assertIsNotNone(ks.cache) - def _make_ks(self, client): + @mock.patch.object(ks_discover.Discover, '__init__') + @mock.patch.object(ks_discover.Discover, 'url_for') + def _make_ks(self, client, mock_url_for, mock___init__): class Configs(object): auth_url = 'http://server.test:5000/' user_id = 'auser' password = 'apassword' project_id = 'aproject' + mock___init__.return_value = None + mock_url_for.return_value = Configs.auth_url return keystone.Keystone( 'http://server.test:5000/', 'auser', 'apassword', 'aproject', client(self, Configs)) diff --git a/test-requirements.txt b/test-requirements.txt index 6b83c08..bda95b0 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -2,6 +2,7 @@ coverage>=3.6 discover fixtures>=0.3.14 hacking>=0.8.0,<0.9 +mock>=1.0 python-subunit>=0.0.18 sphinx>=1.1.2,!=1.2.0,<1.3 testrepository>=0.0.18