From 65031b1a46b4721fc9b10fede0a9828ea509b053 Mon Sep 17 00:00:00 2001 From: Andrey Kurilin Date: Mon, 13 May 2019 17:18:15 -0700 Subject: [PATCH] Switch to latest Rally release * adopt osclients.Clients.create_from_env method. It re-used code from rally framework repo which was recently deleted. * adopt error handling of new ks errors Change-Id: I9e20c0e69426d07dd682f596f7b75b2f284d141c --- rally_openstack/osclients.py | 20 +++++++----- tests/functional/test_cli_deployment.py | 23 ++++++-------- tests/unit/test_osclients.py | 41 +++++++++++++++++++++++++ upper-constraints.txt | 4 +-- 4 files changed, 65 insertions(+), 23 deletions(-) diff --git a/rally_openstack/osclients.py b/rally_openstack/osclients.py index 7438f51f..e25f5050 100644 --- a/rally_openstack/osclients.py +++ b/rally_openstack/osclients.py @@ -16,7 +16,6 @@ import abc import os -from rally.cli import envutils from rally.common import cfg from rally.common import logging from rally.common.plugin import plugin @@ -49,14 +48,15 @@ class AuthenticationFailed(exceptions.AuthenticationFailed): from keystoneauth1 import exceptions as ks_exc - if isinstance(error, ks_exc.ConnectionError): + if isinstance(error, (ks_exc.ConnectionError, + ks_exc.DiscoveryFailure)): # this type of errors is general for all users no need to include # username, project name. The original error message should be # self-sufficient self.msg_fmt = self.msg_fmt_2 message = error.message - if message.startswith("Unable to establish connection to"): - # this message contains too much info. + if (message.startswith("Unable to establish connection to") or + isinstance(error, ks_exc.DiscoveryFailure)): if "Max retries exceeded with url" in message: if "HTTPConnectionPool" in message: splitter = ": HTTPConnectionPool" @@ -900,17 +900,23 @@ class Clients(object): @classmethod def create_from_env(cls): - creds = envutils.get_creds_from_env_vars() from rally_openstack import credential + from rally_openstack.platforms import existing + + spec = existing.OpenStack.create_spec_from_sys_environ(os.environ) + if not spec["available"]: + raise ValueError(spec["message"]) + + creds = spec["spec"] oscred = credential.OpenStackCredential( auth_url=creds["auth_url"], username=creds["admin"]["username"], password=creds["admin"]["password"], - tenant_name=creds["admin"]["tenant_name"], + tenant_name=creds["admin"].get( + "tenant_name", creds["admin"].get("project_name")), endpoint_type=creds["endpoint_type"], user_domain_name=creds["admin"].get("user_domain_name"), project_domain_name=creds["admin"].get("project_domain_name"), - endpoint=creds["endpoint"], region_name=creds["region_name"], https_cacert=creds["https_cacert"], https_insecure=creds["https_insecure"]) diff --git a/tests/functional/test_cli_deployment.py b/tests/functional/test_cli_deployment.py index 2aec84b0..edb4cdb6 100644 --- a/tests/functional/test_cli_deployment.py +++ b/tests/functional/test_cli_deployment.py @@ -14,7 +14,8 @@ # under the License. import re -import unittest + +import testtools from tests.functional import utils @@ -27,7 +28,7 @@ TEST_ENV = { } -class DeploymentTestCase(unittest.TestCase): +class DeploymentTestCase(testtools.TestCase): def test_create_fromenv_list_show(self): # NOTE(andreykurilin): `rally deployment create --fromenv` is @@ -80,18 +81,12 @@ class DeploymentTestCase(unittest.TestCase): "--filename %s" % file.filename) self.assertIn("t_create_file_debug", rally("deployment list")) self.assertEqual(config, rally("deployment config", getjson=True)) - self.assertRaises(utils.RallyCliError, rally, "deployment check") - - try: - rally("--debug deployment check") - except utils.RallyCliError as e: - self.assertIn( - "AuthenticationFailed: Unable to establish connection to " - "%s" % TEST_ENV["OS_AUTH_URL"], - str(e)) - else: - self.fail("rally deployment fails to raise error for wrong" - " authentication info") + e = self.assertRaises(utils.RallyCliError, rally, + "--debug deployment check") + self.assertIn( + "AuthenticationFailed: Could not find versioned identity " + "endpoints when attempting to authenticate.", + e.output) def test_use(self): rally = utils.Rally() diff --git a/tests/unit/test_osclients.py b/tests/unit/test_osclients.py index 4ef20985..a31dc259 100644 --- a/tests/unit/test_osclients.py +++ b/tests/unit/test_osclients.py @@ -1063,3 +1063,44 @@ class OSClientsTestCase(test.TestCase): } mock_barbican.client.Client.assert_called_once_with(**kw) self.assertEqual(fake_barbican, self.clients.cache["barbican"]) + + +class AuthenticationFailedTestCase(test.TestCase): + def test_init(self): + from keystoneauth1 import exceptions as ks_exc + + actual_exc = ks_exc.ConnectionError("Something") + exc = osclients.AuthenticationFailed( + error=actual_exc, url="https://example.com", username="user", + project="project") + # only original exc should be used + self.assertEqual("Something", exc.format_message()) + + actual_exc = Exception("Something") + exc = osclients.AuthenticationFailed( + error=actual_exc, url="https://example.com", username="user", + project="project") + # additional info should be added + self.assertEqual("Failed to authenticate to https://example.com for " + "user 'user' in project 'project': " + "[Exception] Something", exc.format_message()) + + # check cutting message + actual_exc = ks_exc.DiscoveryFailure( + "Could not find versioned identity endpoints when attempting to " + "authenticate. Please check that your auth_url is correct. " + "Unable to establish connection to https://example.com: " + "HTTPConnectionPool(host='example.com', port=80): Max retries " + "exceeded with url: / (Caused by NewConnectionError('" + ": " + "Failed to establish a new connection: [Errno -2] Name or service" + " not known',))") + exc = osclients.AuthenticationFailed( + error=actual_exc, url="https://example.com", username="user", + project="project") + # original message should be simplified + self.assertEqual( + "Could not find versioned identity endpoints when attempting to " + "authenticate. Please check that your auth_url is correct. " + "Unable to establish connection to https://example.com", + exc.format_message()) diff --git a/upper-constraints.txt b/upper-constraints.txt index 49c4e58f..20cb409a 100644 --- a/upper-constraints.txt +++ b/upper-constraints.txt @@ -86,7 +86,7 @@ pyOpenSSL===18.0.0 pyparsing===2.2.2 pyperclip===1.7.0 Python===2.7.15rc1 -python-barbicanclient===4.5.2 +python-barbicanclient===4.5.2 python-ceilometerclient===2.9.0 python-cinderclient===4.0.1 python-dateutil===2.7.3 @@ -115,7 +115,7 @@ python-watcherclient===2.1.0 python-zaqarclient===1.10.0 pytz===2018.5 PyYAML===3.13 -rally===1.4.1 +rally===1.5.1 requests===2.21.0 requests-oauthlib===1.0.0 requestsexceptions===1.4.0