From 2585047ffceadeb79864008c81f3ff0f07a24d5f Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Tue, 11 Sep 2018 11:39:17 -0500 Subject: [PATCH] Protect against endpoint_data not existing It's possible in get_api_major_version that the endpoint in question is not found at all. In that case, we are documented to return None, but what we do instead is throw an exception trying to get data off of the None object. Change-Id: I06ad497854f4e95a1a2a4a93241b244fc476b139 --- keystoneauth1/plugin.py | 4 +++- keystoneauth1/tests/unit/test_discovery.py | 23 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/keystoneauth1/plugin.py b/keystoneauth1/plugin.py index 74970af5..f27a9e1a 100644 --- a/keystoneauth1/plugin.py +++ b/keystoneauth1/plugin.py @@ -149,7 +149,9 @@ class BaseAuthPlugin(object): endpoint_data = self.get_endpoint_data( session, endpoint_override=endpoint_override, discover_versions=False, **kwargs) - return endpoint_data.api_version + if endpoint_data: + return endpoint_data.api_version + return None def get_endpoint(self, session, **kwargs): """Return an endpoint for the client. diff --git a/keystoneauth1/tests/unit/test_discovery.py b/keystoneauth1/tests/unit/test_discovery.py index b0cdd9c0..241afe55 100644 --- a/keystoneauth1/tests/unit/test_discovery.py +++ b/keystoneauth1/tests/unit/test_discovery.py @@ -560,6 +560,29 @@ class VersionDataTests(utils.TestCase): self.assertTrue(mock.called_once) + def test_version_data_legacy_ironic_no_override(self): + """Validate detection of legacy Ironic microversion ranges.""" + ironic_url = 'https://bare-metal.example.com/v1/' + self.requests_mock.get( + ironic_url, status_code=200, + json={ + 'id': 'v1', + 'links': [{ + "href": ironic_url, + "rel": "self"}]}, + headers={ + 'X-OpenStack-Ironic-API-Minimum-Version': '1.3', + 'X-OpenStack-Ironic-API-Maximum-Version': '1.21', + }) + + plugin = noauth.NoAuth() + a = adapter.Adapter( + self.session, + auth=plugin, + service_type='baremetal') + + self.assertIsNone(a.get_api_major_version()) + def test_version_data_ironic_microversions(self): """Validate detection of Ironic microversion ranges.""" ironic_url = 'https://bare-metal.example.com/v1/'