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:
parent
ddb78ec4b3
commit
abf98ae84a
ironic_python_agent
@ -12,6 +12,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import collections
|
||||
import os
|
||||
import random
|
||||
import select
|
||||
@ -48,6 +49,8 @@ NETWORK_WAIT_RETRY = 5
|
||||
cfg.CONF.import_group('metrics', 'ironic_lib.metrics_utils')
|
||||
cfg.CONF.import_group('metrics_statsd', 'ironic_lib.metrics_statsd')
|
||||
|
||||
Host = collections.namedtuple('Host', ['hostname', 'port'])
|
||||
|
||||
|
||||
def _time():
|
||||
"""Wraps time.time() for simpler testing."""
|
||||
@ -222,7 +225,7 @@ class IronicPythonAgent(base.ExecuteCommandMixin):
|
||||
|
||||
: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
|
||||
|
||||
found_ip = None
|
||||
@ -247,8 +250,8 @@ class IronicPythonAgent(base.ExecuteCommandMixin):
|
||||
time.sleep(self.ip_lookup_sleep)
|
||||
|
||||
if found_ip:
|
||||
self.advertise_address = (found_ip,
|
||||
self.advertise_address[1])
|
||||
self.advertise_address = Host(hostname=found_ip,
|
||||
port=self.advertise_address.port)
|
||||
else:
|
||||
raise errors.LookupAgentIPError('Agent could not find a valid IP '
|
||||
'address.')
|
||||
@ -354,8 +357,8 @@ class IronicPythonAgent(base.ExecuteCommandMixin):
|
||||
setattr(cfg.CONF.metrics_statsd, opt, val)
|
||||
|
||||
wsgi = simple_server.make_server(
|
||||
self.listen_address[0],
|
||||
self.listen_address[1],
|
||||
self.listen_address.hostname,
|
||||
self.listen_address.port,
|
||||
self.api,
|
||||
server_class=simple_server.WSGIServer)
|
||||
|
||||
|
@ -35,8 +35,10 @@ def run():
|
||||
CONF.set_override('debug', ipa_debug)
|
||||
log.setup(CONF, 'ironic-python-agent')
|
||||
agent.IronicPythonAgent(CONF.api_url,
|
||||
(CONF.advertise_host, CONF.advertise_port),
|
||||
(CONF.listen_host, CONF.listen_port),
|
||||
agent.Host(hostname=CONF.advertise_host,
|
||||
port=CONF.advertise_port),
|
||||
agent.Host(hostname=CONF.listen_host,
|
||||
port=CONF.listen_port),
|
||||
CONF.ip_lookup_attempts,
|
||||
CONF.ip_lookup_sleep,
|
||||
CONF.network_interface,
|
||||
|
@ -39,8 +39,8 @@ class FunctionalBase(test_base.BaseTestCase):
|
||||
|
||||
self.agent = agent.IronicPythonAgent(
|
||||
api_url='http://127.0.0.1:6835',
|
||||
advertise_address='localhost',
|
||||
listen_address=('0.0.0.0', int(self.test_port)),
|
||||
advertise_address=agent.Host('localhost', 9999),
|
||||
listen_address=agent.Host('0.0.0.0', int(self.test_port)),
|
||||
ip_lookup_attempts=3,
|
||||
ip_lookup_sleep=10,
|
||||
network_interface=None,
|
||||
|
@ -137,8 +137,8 @@ class TestBaseAgent(test_base.BaseTestCase):
|
||||
|
||||
self.agent = agent.IronicPythonAgent('https://fake_api.example.'
|
||||
'org:8081/',
|
||||
('203.0.113.1', 9990),
|
||||
('192.0.2.1', 9999),
|
||||
agent.Host('203.0.113.1', 9990),
|
||||
agent.Host('192.0.2.1', 9999),
|
||||
3,
|
||||
10,
|
||||
'eth0',
|
||||
@ -192,10 +192,10 @@ class TestBaseAgent(test_base.BaseTestCase):
|
||||
}
|
||||
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(
|
||||
listen_addr[0],
|
||||
listen_addr[1],
|
||||
listen_addr.hostname,
|
||||
listen_addr.port,
|
||||
self.agent.api,
|
||||
server_class=simple_server.WSGIServer)
|
||||
wsgi_server.serve_forever.assert_called_once_with()
|
||||
@ -232,10 +232,10 @@ class TestBaseAgent(test_base.BaseTestCase):
|
||||
}
|
||||
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(
|
||||
listen_addr[0],
|
||||
listen_addr[1],
|
||||
listen_addr.hostname,
|
||||
listen_addr.port,
|
||||
self.agent.api,
|
||||
server_class=simple_server.WSGIServer)
|
||||
wsgi_server.serve_forever.assert_called_once_with()
|
||||
@ -296,10 +296,10 @@ class TestBaseAgent(test_base.BaseTestCase):
|
||||
}
|
||||
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(
|
||||
listen_addr[0],
|
||||
listen_addr[1],
|
||||
listen_addr.hostname,
|
||||
listen_addr.port,
|
||||
self.agent.api,
|
||||
server_class=simple_server.WSGIServer)
|
||||
wsgi_server.serve_forever.assert_called_once_with()
|
||||
@ -378,8 +378,10 @@ class TestAgentStandalone(test_base.BaseTestCase):
|
||||
super(TestAgentStandalone, self).setUp()
|
||||
self.agent = agent.IronicPythonAgent('https://fake_api.example.'
|
||||
'org:8081/',
|
||||
('203.0.113.1', 9990),
|
||||
('192.0.2.1', 9999),
|
||||
agent.Host(hostname='203.0.113.1',
|
||||
port=9990),
|
||||
agent.Host(hostname='192.0.2.1',
|
||||
port=9999),
|
||||
3,
|
||||
10,
|
||||
'eth0',
|
||||
@ -406,10 +408,10 @@ class TestAgentStandalone(test_base.BaseTestCase):
|
||||
}
|
||||
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(
|
||||
listen_addr[0],
|
||||
listen_addr[1],
|
||||
listen_addr.hostname,
|
||||
listen_addr.port,
|
||||
self.agent.api,
|
||||
server_class=simple_server.WSGIServer)
|
||||
wsgi_server.serve_forever.assert_called_once_with()
|
||||
@ -429,8 +431,8 @@ class TestAdvertiseAddress(test_base.BaseTestCase):
|
||||
|
||||
self.agent = agent.IronicPythonAgent(
|
||||
api_url='https://fake_api.example.org:8081/',
|
||||
advertise_address=(None, 9990),
|
||||
listen_address=('0.0.0.0', 9999),
|
||||
advertise_address=agent.Host(None, 9990),
|
||||
listen_address=agent.Host('0.0.0.0', 9999),
|
||||
ip_lookup_attempts=5,
|
||||
ip_lookup_sleep=10,
|
||||
network_interface=None,
|
||||
@ -440,7 +442,7 @@ class TestAdvertiseAddress(test_base.BaseTestCase):
|
||||
standalone=False)
|
||||
|
||||
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()
|
||||
|
||||
@ -457,7 +459,8 @@ class TestAdvertiseAddress(test_base.BaseTestCase):
|
||||
|
||||
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')
|
||||
self.assertFalse(mock_exec.called)
|
||||
self.assertFalse(mock_gethostbyname.called)
|
||||
|
Loading…
x
Reference in New Issue
Block a user