From c6476838cc7d94982f3d6aea88926679cf40d839 Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Tue, 3 Nov 2015 07:31:54 -0800 Subject: [PATCH] Make string representation of DictModel generic The previous version was specific to a port dict and would fail if str() was called on other models. This iterates over and logs all values of the dict instead so it works under any dictionary. Change-Id: I630b39338e4e67080e100a9f32c9b6e341a93472 Closes-Bug: #1512753 --- neutron/agent/linux/dhcp.py | 3 ++- neutron/tests/unit/agent/linux/test_dhcp.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/neutron/agent/linux/dhcp.py b/neutron/agent/linux/dhcp.py index df79225a5..3781e1417 100644 --- a/neutron/agent/linux/dhcp.py +++ b/neutron/agent/linux/dhcp.py @@ -97,7 +97,8 @@ class DictModel(dict): del self[name] def __str__(self): - return "id=%s, network_id=%s" % (self.id, self.network_id) + pairs = ['%s=%s' % (k, v) for k, v in self.items()] + return ', '.join(sorted(pairs)) class NetModel(DictModel): diff --git a/neutron/tests/unit/agent/linux/test_dhcp.py b/neutron/tests/unit/agent/linux/test_dhcp.py index ac264c308..3a00ee866 100644 --- a/neutron/tests/unit/agent/linux/test_dhcp.py +++ b/neutron/tests/unit/agent/linux/test_dhcp.py @@ -2104,3 +2104,14 @@ class TestDeviceManager(TestConfBase): mgr.driver.init_l3.assert_called_with('ns-XXX', ['192.168.0.6/24'], namespace='qdhcp-ns') + + +class TestDictModel(base.BaseTestCase): + + def test_string_representation_port(self): + port = dhcp.DictModel({'id': 'id', 'network_id': 'net_id'}) + self.assertEqual('id=id, network_id=net_id', str(port)) + + def test_string_representation_network(self): + net = dhcp.DictModel({'id': 'id', 'name': 'myname'}) + self.assertEqual('id=id, name=myname', str(net)) -- 2.45.2