Skip to content

Commit abf98ae

Browse files
author
John L. Villalovos
committed
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
1 parent ddb78ec commit abf98ae

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

ironic_python_agent/agent.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import collections
1516
import os
1617
import random
1718
import select
@@ -48,6 +49,8 @@
4849
cfg.CONF.import_group('metrics', 'ironic_lib.metrics_utils')
4950
cfg.CONF.import_group('metrics_statsd', 'ironic_lib.metrics_statsd')
5051

52+
Host = collections.namedtuple('Host', ['hostname', 'port'])
53+
5154

5255
def _time():
5356
"""Wraps time.time() for simpler testing."""
@@ -222,7 +225,7 @@ def set_agent_advertise_addr(self):
222225
223226
:raises: LookupAgentIPError if an IP address could not be found
224227
"""
225-
if self.advertise_address[0] is not None:
228+
if self.advertise_address.hostname is not None:
226229
return
227230

228231
found_ip = None
@@ -247,8 +250,8 @@ def set_agent_advertise_addr(self):
247250
time.sleep(self.ip_lookup_sleep)
248251

249252
if found_ip:
250-
self.advertise_address = (found_ip,
251-
self.advertise_address[1])
253+
self.advertise_address = Host(hostname=found_ip,
254+
port=self.advertise_address.port)
252255
else:
253256
raise errors.LookupAgentIPError('Agent could not find a valid IP '
254257
'address.')
@@ -354,8 +357,8 @@ def run(self):
354357
setattr(cfg.CONF.metrics_statsd, opt, val)
355358

356359
wsgi = simple_server.make_server(
357-
self.listen_address[0],
358-
self.listen_address[1],
360+
self.listen_address.hostname,
361+
self.listen_address.port,
359362
self.api,
360363
server_class=simple_server.WSGIServer)
361364

ironic_python_agent/cmd/agent.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ def run():
3535
CONF.set_override('debug', ipa_debug)
3636
log.setup(CONF, 'ironic-python-agent')
3737
agent.IronicPythonAgent(CONF.api_url,
38-
(CONF.advertise_host, CONF.advertise_port),
39-
(CONF.listen_host, CONF.listen_port),
38+
agent.Host(hostname=CONF.advertise_host,
39+
port=CONF.advertise_port),
40+
agent.Host(hostname=CONF.listen_host,
41+
port=CONF.listen_port),
4042
CONF.ip_lookup_attempts,
4143
CONF.ip_lookup_sleep,
4244
CONF.network_interface,

ironic_python_agent/tests/functional/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def setUp(self):
3939

4040
self.agent = agent.IronicPythonAgent(
4141
api_url='http://127.0.0.1:6835',
42-
advertise_address='localhost',
43-
listen_address=('0.0.0.0', int(self.test_port)),
42+
advertise_address=agent.Host('localhost', 9999),
43+
listen_address=agent.Host('0.0.0.0', int(self.test_port)),
4444
ip_lookup_attempts=3,
4545
ip_lookup_sleep=10,
4646
network_interface=None,

ironic_python_agent/tests/unit/test_agent.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ def setUp(self):
137137

138138
self.agent = agent.IronicPythonAgent('https://fake_api.example.'
139139
'org:8081/',
140-
('203.0.113.1', 9990),
141-
('192.0.2.1', 9999),
140+
agent.Host('203.0.113.1', 9990),
141+
agent.Host('192.0.2.1', 9999),
142142
3,
143143
10,
144144
'eth0',
@@ -192,10 +192,10 @@ def test_run(self, wsgi_server_cls, mocked_dispatch, mocked_wait):
192192
}
193193
self.agent.run()
194194

195-
listen_addr = ('192.0.2.1', 9999)
195+
listen_addr = agent.Host('192.0.2.1', 9999)
196196
wsgi_server_cls.assert_called_once_with(
197-
listen_addr[0],
198-
listen_addr[1],
197+
listen_addr.hostname,
198+
listen_addr.port,
199199
self.agent.api,
200200
server_class=simple_server.WSGIServer)
201201
wsgi_server.serve_forever.assert_called_once_with()
@@ -232,10 +232,10 @@ def test_run_with_inspection(self, mocked_list_hardware, wsgi_server_cls,
232232
}
233233
self.agent.run()
234234

235-
listen_addr = ('192.0.2.1', 9999)
235+
listen_addr = agent.Host('192.0.2.1', 9999)
236236
wsgi_server_cls.assert_called_once_with(
237-
listen_addr[0],
238-
listen_addr[1],
237+
listen_addr.hostname,
238+
listen_addr.port,
239239
self.agent.api,
240240
server_class=simple_server.WSGIServer)
241241
wsgi_server.serve_forever.assert_called_once_with()
@@ -296,10 +296,10 @@ def test_run_with_sleep(self, mock_check_for_iscsi, mocked_list_hardware,
296296
}
297297
self.agent.run()
298298

299-
listen_addr = ('192.0.2.1', 9999)
299+
listen_addr = agent.Host('192.0.2.1', 9999)
300300
wsgi_server_cls.assert_called_once_with(
301-
listen_addr[0],
302-
listen_addr[1],
301+
listen_addr.hostname,
302+
listen_addr.port,
303303
self.agent.api,
304304
server_class=simple_server.WSGIServer)
305305
wsgi_server.serve_forever.assert_called_once_with()
@@ -378,8 +378,10 @@ def setUp(self):
378378
super(TestAgentStandalone, self).setUp()
379379
self.agent = agent.IronicPythonAgent('https://fake_api.example.'
380380
'org:8081/',
381-
('203.0.113.1', 9990),
382-
('192.0.2.1', 9999),
381+
agent.Host(hostname='203.0.113.1',
382+
port=9990),
383+
agent.Host(hostname='192.0.2.1',
384+
port=9999),
383385
3,
384386
10,
385387
'eth0',
@@ -406,10 +408,10 @@ def test_run(self, mocked_list_hardware, wsgi_server_cls):
406408
}
407409
self.agent.run()
408410

409-
listen_addr = ('192.0.2.1', 9999)
411+
listen_addr = agent.Host('192.0.2.1', 9999)
410412
wsgi_server_cls.assert_called_once_with(
411-
listen_addr[0],
412-
listen_addr[1],
413+
listen_addr.hostname,
414+
listen_addr.port,
413415
self.agent.api,
414416
server_class=simple_server.WSGIServer)
415417
wsgi_server.serve_forever.assert_called_once_with()
@@ -429,8 +431,8 @@ def setUp(self):
429431

430432
self.agent = agent.IronicPythonAgent(
431433
api_url='https://fake_api.example.org:8081/',
432-
advertise_address=(None, 9990),
433-
listen_address=('0.0.0.0', 9999),
434+
advertise_address=agent.Host(None, 9990),
435+
listen_address=agent.Host('0.0.0.0', 9999),
434436
ip_lookup_attempts=5,
435437
ip_lookup_sleep=10,
436438
network_interface=None,
@@ -440,7 +442,7 @@ def setUp(self):
440442
standalone=False)
441443

442444
def test_advertise_address_provided(self, mock_exec, mock_gethostbyname):
443-
self.agent.advertise_address = ('1.2.3.4', 9990)
445+
self.agent.advertise_address = agent.Host('1.2.3.4', 9990)
444446

445447
self.agent.set_agent_advertise_addr()
446448

@@ -457,7 +459,8 @@ def test_with_network_interface(self, mock_get_ipv4, mock_exec,
457459

458460
self.agent.set_agent_advertise_addr()
459461

460-
self.assertEqual(('1.2.3.4', 9990), self.agent.advertise_address)
462+
self.assertEqual(agent.Host('1.2.3.4', 9990),
463+
self.agent.advertise_address)
461464
mock_get_ipv4.assert_called_once_with(mock.ANY, 'em1')
462465
self.assertFalse(mock_exec.called)
463466
self.assertFalse(mock_gethostbyname.called)

0 commit comments

Comments
 (0)