Use namedtuple to improve code readability

Use the namedtuple class to improve code readability by creating a Host
class with namedtuple to store the 'hostname' and 'port'

Replace foo[0] with foo.hostname, and foo[1] with foo.port to make code
more readable.

Change-Id: Ie2b5f9cf89e7ccbbcf0a2573dab6f6c5d14c018b
This commit is contained in:
John L. Villalovos
2016-08-28 18:39:52 -07:00
parent ddb78ec4b3
commit abf98ae84a
4 changed files with 37 additions and 29 deletions

View File

@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import collections
import os import os
import random import random
import select import select
@@ -48,6 +49,8 @@ NETWORK_WAIT_RETRY = 5
cfg.CONF.import_group('metrics', 'ironic_lib.metrics_utils') cfg.CONF.import_group('metrics', 'ironic_lib.metrics_utils')
cfg.CONF.import_group('metrics_statsd', 'ironic_lib.metrics_statsd') cfg.CONF.import_group('metrics_statsd', 'ironic_lib.metrics_statsd')
Host = collections.namedtuple('Host', ['hostname', 'port'])
def _time(): def _time():
"""Wraps time.time() for simpler testing.""" """Wraps time.time() for simpler testing."""
@@ -222,7 +225,7 @@ class IronicPythonAgent(base.ExecuteCommandMixin):
:raises: LookupAgentIPError if an IP address could not be found :raises: LookupAgentIPError if an IP address could not be found
""" """
if self.advertise_address[0] is not None: if self.advertise_address.hostname is not None:
return return
found_ip = None found_ip = None
@@ -247,8 +250,8 @@ class IronicPythonAgent(base.ExecuteCommandMixin):
time.sleep(self.ip_lookup_sleep) time.sleep(self.ip_lookup_sleep)
if found_ip: if found_ip:
self.advertise_address = (found_ip, self.advertise_address = Host(hostname=found_ip,
self.advertise_address[1]) port=self.advertise_address.port)
else: else:
raise errors.LookupAgentIPError('Agent could not find a valid IP ' raise errors.LookupAgentIPError('Agent could not find a valid IP '
'address.') 'address.')
@@ -354,8 +357,8 @@ class IronicPythonAgent(base.ExecuteCommandMixin):
setattr(cfg.CONF.metrics_statsd, opt, val) setattr(cfg.CONF.metrics_statsd, opt, val)
wsgi = simple_server.make_server( wsgi = simple_server.make_server(
self.listen_address[0], self.listen_address.hostname,
self.listen_address[1], self.listen_address.port,
self.api, self.api,
server_class=simple_server.WSGIServer) server_class=simple_server.WSGIServer)

View File

@@ -35,8 +35,10 @@ def run():
CONF.set_override('debug', ipa_debug) CONF.set_override('debug', ipa_debug)
log.setup(CONF, 'ironic-python-agent') log.setup(CONF, 'ironic-python-agent')
agent.IronicPythonAgent(CONF.api_url, agent.IronicPythonAgent(CONF.api_url,
(CONF.advertise_host, CONF.advertise_port), agent.Host(hostname=CONF.advertise_host,
(CONF.listen_host, CONF.listen_port), port=CONF.advertise_port),
agent.Host(hostname=CONF.listen_host,
port=CONF.listen_port),
CONF.ip_lookup_attempts, CONF.ip_lookup_attempts,
CONF.ip_lookup_sleep, CONF.ip_lookup_sleep,
CONF.network_interface, CONF.network_interface,

View File

@@ -39,8 +39,8 @@ class FunctionalBase(test_base.BaseTestCase):
self.agent = agent.IronicPythonAgent( self.agent = agent.IronicPythonAgent(
api_url='http://127.0.0.1:6835', api_url='http://127.0.0.1:6835',
advertise_address='localhost', advertise_address=agent.Host('localhost', 9999),
listen_address=('0.0.0.0', int(self.test_port)), listen_address=agent.Host('0.0.0.0', int(self.test_port)),
ip_lookup_attempts=3, ip_lookup_attempts=3,
ip_lookup_sleep=10, ip_lookup_sleep=10,
network_interface=None, network_interface=None,

View File

@@ -137,8 +137,8 @@ class TestBaseAgent(test_base.BaseTestCase):
self.agent = agent.IronicPythonAgent('https://fake_api.example.' self.agent = agent.IronicPythonAgent('https://fake_api.example.'
'org:8081/', 'org:8081/',
('203.0.113.1', 9990), agent.Host('203.0.113.1', 9990),
('192.0.2.1', 9999), agent.Host('192.0.2.1', 9999),
3, 3,
10, 10,
'eth0', 'eth0',
@@ -192,10 +192,10 @@ class TestBaseAgent(test_base.BaseTestCase):
} }
self.agent.run() self.agent.run()
listen_addr = ('192.0.2.1', 9999) listen_addr = agent.Host('192.0.2.1', 9999)
wsgi_server_cls.assert_called_once_with( wsgi_server_cls.assert_called_once_with(
listen_addr[0], listen_addr.hostname,
listen_addr[1], listen_addr.port,
self.agent.api, self.agent.api,
server_class=simple_server.WSGIServer) server_class=simple_server.WSGIServer)
wsgi_server.serve_forever.assert_called_once_with() wsgi_server.serve_forever.assert_called_once_with()
@@ -232,10 +232,10 @@ class TestBaseAgent(test_base.BaseTestCase):
} }
self.agent.run() self.agent.run()
listen_addr = ('192.0.2.1', 9999) listen_addr = agent.Host('192.0.2.1', 9999)
wsgi_server_cls.assert_called_once_with( wsgi_server_cls.assert_called_once_with(
listen_addr[0], listen_addr.hostname,
listen_addr[1], listen_addr.port,
self.agent.api, self.agent.api,
server_class=simple_server.WSGIServer) server_class=simple_server.WSGIServer)
wsgi_server.serve_forever.assert_called_once_with() wsgi_server.serve_forever.assert_called_once_with()
@@ -296,10 +296,10 @@ class TestBaseAgent(test_base.BaseTestCase):
} }
self.agent.run() self.agent.run()
listen_addr = ('192.0.2.1', 9999) listen_addr = agent.Host('192.0.2.1', 9999)
wsgi_server_cls.assert_called_once_with( wsgi_server_cls.assert_called_once_with(
listen_addr[0], listen_addr.hostname,
listen_addr[1], listen_addr.port,
self.agent.api, self.agent.api,
server_class=simple_server.WSGIServer) server_class=simple_server.WSGIServer)
wsgi_server.serve_forever.assert_called_once_with() wsgi_server.serve_forever.assert_called_once_with()
@@ -378,8 +378,10 @@ class TestAgentStandalone(test_base.BaseTestCase):
super(TestAgentStandalone, self).setUp() super(TestAgentStandalone, self).setUp()
self.agent = agent.IronicPythonAgent('https://fake_api.example.' self.agent = agent.IronicPythonAgent('https://fake_api.example.'
'org:8081/', 'org:8081/',
('203.0.113.1', 9990), agent.Host(hostname='203.0.113.1',
('192.0.2.1', 9999), port=9990),
agent.Host(hostname='192.0.2.1',
port=9999),
3, 3,
10, 10,
'eth0', 'eth0',
@@ -406,10 +408,10 @@ class TestAgentStandalone(test_base.BaseTestCase):
} }
self.agent.run() self.agent.run()
listen_addr = ('192.0.2.1', 9999) listen_addr = agent.Host('192.0.2.1', 9999)
wsgi_server_cls.assert_called_once_with( wsgi_server_cls.assert_called_once_with(
listen_addr[0], listen_addr.hostname,
listen_addr[1], listen_addr.port,
self.agent.api, self.agent.api,
server_class=simple_server.WSGIServer) server_class=simple_server.WSGIServer)
wsgi_server.serve_forever.assert_called_once_with() wsgi_server.serve_forever.assert_called_once_with()
@@ -429,8 +431,8 @@ class TestAdvertiseAddress(test_base.BaseTestCase):
self.agent = agent.IronicPythonAgent( self.agent = agent.IronicPythonAgent(
api_url='https://fake_api.example.org:8081/', api_url='https://fake_api.example.org:8081/',
advertise_address=(None, 9990), advertise_address=agent.Host(None, 9990),
listen_address=('0.0.0.0', 9999), listen_address=agent.Host('0.0.0.0', 9999),
ip_lookup_attempts=5, ip_lookup_attempts=5,
ip_lookup_sleep=10, ip_lookup_sleep=10,
network_interface=None, network_interface=None,
@@ -440,7 +442,7 @@ class TestAdvertiseAddress(test_base.BaseTestCase):
standalone=False) standalone=False)
def test_advertise_address_provided(self, mock_exec, mock_gethostbyname): def test_advertise_address_provided(self, mock_exec, mock_gethostbyname):
self.agent.advertise_address = ('1.2.3.4', 9990) self.agent.advertise_address = agent.Host('1.2.3.4', 9990)
self.agent.set_agent_advertise_addr() self.agent.set_agent_advertise_addr()
@@ -457,7 +459,8 @@ class TestAdvertiseAddress(test_base.BaseTestCase):
self.agent.set_agent_advertise_addr() self.agent.set_agent_advertise_addr()
self.assertEqual(('1.2.3.4', 9990), self.agent.advertise_address) self.assertEqual(agent.Host('1.2.3.4', 9990),
self.agent.advertise_address)
mock_get_ipv4.assert_called_once_with(mock.ANY, 'em1') mock_get_ipv4.assert_called_once_with(mock.ANY, 'em1')
self.assertFalse(mock_exec.called) self.assertFalse(mock_exec.called)
self.assertFalse(mock_gethostbyname.called) self.assertFalse(mock_gethostbyname.called)