From 5ac9cde5918e6e440a9fff92306b6a1df1f3040c Mon Sep 17 00:00:00 2001 From: Eric Fried Date: Mon, 31 Jul 2017 16:50:05 -0500 Subject: [PATCH] Add EndpointData.__str__ for debugging It is useful to be able to dump the contents of an EndpointData for debugging purposes. This change adds a __str__ method that joins up all the public attributes/properties. Change-Id: Ib8985f0fa48a613ab8fca7faffbdf60c19c7cd22 --- keystoneauth1/discover.py | 10 ++++++++++ keystoneauth1/tests/unit/test_discovery.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/keystoneauth1/discover.py b/keystoneauth1/discover.py index 8003d018..9555b15a 100644 --- a/keystoneauth1/discover.py +++ b/keystoneauth1/discover.py @@ -735,6 +735,16 @@ class EndpointData(object): new_data._saved_project_id = self._saved_project_id return new_data + def __str__(self): + """Produce a string like EndpointData{key=val, ...}, for debugging.""" + str_attrs = ( + 'api_version', 'catalog_url', 'endpoint_id', 'interface', + 'major_version', 'max_microversion', 'min_microversion', + 'next_min_version', 'not_before', 'raw_endpoint', 'region_name', + 'service_id', 'service_name', 'service_type', 'service_url', 'url') + return "%s{%s}" % (self.__class__.__name__, ', '.join( + ["%s=%s" % (attr, getattr(self, attr)) for attr in str_attrs])) + @property def url(self): return self.service_url or self.catalog_url diff --git a/keystoneauth1/tests/unit/test_discovery.py b/keystoneauth1/tests/unit/test_discovery.py index ec5a15c4..4bab1108 100644 --- a/keystoneauth1/tests/unit/test_discovery.py +++ b/keystoneauth1/tests/unit/test_discovery.py @@ -949,3 +949,20 @@ class EndpointDataTests(utils.TestCase): mock_get_disc.assert_has_calls( [mock.call('sess', url, cache='cache', authenticated=False) for url in ('url1', 'url2', 'url3')]) + + def test_endpoint_data_str(self): + """Validate EndpointData.__str__.""" + # Populate a few fields to make sure they come through. + epd = discover.EndpointData(catalog_url='abc', service_type='123', + api_version=(2, 3)) + exp = ( + 'EndpointData{api_version=(2, 3), catalog_url=abc,' + ' endpoint_id=None, interface=None, major_version=None,' + ' max_microversion=None, min_microversion=None,' + ' next_min_version=None, not_before=None, raw_endpoint=None,' + ' region_name=None, service_id=None, service_name=None,' + ' service_type=123, service_url=None, url=abc}') + # Works with str() + self.assertEqual(exp, str(epd)) + # Works with implicit stringification + self.assertEqual(exp, "%s" % epd)