Add "__repr__" method to class "Service"

The class "Service" doesn't have a "__repr__" method which results
in log outputs like:

    DEBUG [...] service = <nova.service.Service object at 0x56eb890>

This is not helpful when analyzing logs. Therefore this change adds a
"__repr__" method.

Change-Id: I49c8b7f0dde298de780808c988e4b50278eed665
This commit is contained in:
Markus Zoeller
2016-04-08 15:19:59 +02:00
parent e6aab744a3
commit e42ff5e5ca
2 changed files with 20 additions and 0 deletions

View File

@@ -110,6 +110,15 @@ class Service(service.Service):
self.conductor_api = conductor.API(use_local=db_allowed)
self.conductor_api.wait_until_ready(context.get_admin_context())
def __repr__(self):
return "<%(cls_name)s: host=%(host)s, binary=%(binary)s, " \
"manager_class_name=%(manager)s>" % {
'cls_name': self.__class__.__name__,
'host': self.host,
'binary': self.binary,
'manager': self.manager_class_name
}
def start(self):
verstr = version.version_string_with_package()
LOG.info(_LI('Starting %(topic)s node (version %(version)s)'),

View File

@@ -109,6 +109,17 @@ class ServiceTestCase(test.NoDBTestCase):
self.assertTrue(app)
def test_repr(self):
# Test if a Service object is correctly represented, for example in
# log files.
serv = service.Service(self.host,
self.binary,
self.topic,
'nova.tests.unit.test_service.FakeManager')
exp = "<Service: host=foo, binary=nova-fake, " \
"manager_class_name=nova.tests.unit.test_service.FakeManager>"
self.assertEqual(exp, repr(serv))
def _service_start_mocks(self):
self.mox.StubOutWithMock(objects.Service, 'create')
self.mox.StubOutWithMock(objects.Service, 'get_by_host_and_binary')