Merge lp:~hopem/charm-helpers/lp1525845 into lp:charm-helpers

Proposed by Edward Hope-Morley
Status: Superseded
Proposed branch: lp:~hopem/charm-helpers/lp1525845
Merge into: lp:charm-helpers
Diff against target: 87 lines (+32/-19)
2 files modified
charmhelpers/contrib/network/ip.py (+21/-19)
tests/contrib/network/test_ip.py (+11/-0)
To merge this branch: bzr merge lp:~hopem/charm-helpers/lp1525845
Reviewer Review Type Date Requested Status
James Page Needs Fixing
Review via email: mp+280432@code.launchpad.net

This proposal has been superseded by a proposal from 2015-12-14.

To post a comment you must log in.
Revision history for this message
James Page (james-page) wrote :

Some units tests would be nice :-)

review: Needs Fixing
lp:~hopem/charm-helpers/lp1525845 updated
509. By Edward Hope-Morley

added unit tests

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'charmhelpers/contrib/network/ip.py'
--- charmhelpers/contrib/network/ip.py 2015-09-18 07:21:17 +0000
+++ charmhelpers/contrib/network/ip.py 2015-12-14 15:17:51 +0000
@@ -53,7 +53,7 @@
5353
5454
55def no_ip_found_error_out(network):55def no_ip_found_error_out(network):
56 errmsg = ("No IP address found in network: %s" % network)56 errmsg = ("No IP address found in network(s): %s" % network)
57 raise ValueError(errmsg)57 raise ValueError(errmsg)
5858
5959
@@ -61,7 +61,7 @@
61 """Get an IPv4 or IPv6 address within the network from the host.61 """Get an IPv4 or IPv6 address within the network from the host.
6262
63 :param network (str): CIDR presentation format. For example,63 :param network (str): CIDR presentation format. For example,
64 '192.168.1.0/24'.64 '192.168.1.0/24'. Supports multiple networks as a space-delimited list.
65 :param fallback (str): If no address is found, return fallback.65 :param fallback (str): If no address is found, return fallback.
66 :param fatal (boolean): If no address is found, fallback is not66 :param fatal (boolean): If no address is found, fallback is not
67 set and fatal is True then exit(1).67 set and fatal is True then exit(1).
@@ -75,24 +75,26 @@
75 else:75 else:
76 return None76 return None
7777
78 _validate_cidr(network)78 networks = network.split() or [network]
79 network = netaddr.IPNetwork(network)79 for network in networks:
80 for iface in netifaces.interfaces():80 _validate_cidr(network)
81 addresses = netifaces.ifaddresses(iface)81 network = netaddr.IPNetwork(network)
82 if network.version == 4 and netifaces.AF_INET in addresses:82 for iface in netifaces.interfaces():
83 addr = addresses[netifaces.AF_INET][0]['addr']83 addresses = netifaces.ifaddresses(iface)
84 netmask = addresses[netifaces.AF_INET][0]['netmask']84 if network.version == 4 and netifaces.AF_INET in addresses:
85 cidr = netaddr.IPNetwork("%s/%s" % (addr, netmask))85 addr = addresses[netifaces.AF_INET][0]['addr']
86 if cidr in network:86 netmask = addresses[netifaces.AF_INET][0]['netmask']
87 return str(cidr.ip)87 cidr = netaddr.IPNetwork("%s/%s" % (addr, netmask))
88 if cidr in network:
89 return str(cidr.ip)
8890
89 if network.version == 6 and netifaces.AF_INET6 in addresses:91 if network.version == 6 and netifaces.AF_INET6 in addresses:
90 for addr in addresses[netifaces.AF_INET6]:92 for addr in addresses[netifaces.AF_INET6]:
91 if not addr['addr'].startswith('fe80'):93 if not addr['addr'].startswith('fe80'):
92 cidr = netaddr.IPNetwork("%s/%s" % (addr['addr'],94 cidr = netaddr.IPNetwork("%s/%s" % (addr['addr'],
93 addr['netmask']))95 addr['netmask']))
94 if cidr in network:96 if cidr in network:
95 return str(cidr.ip)97 return str(cidr.ip)
9698
97 if fallback is not None:99 if fallback is not None:
98 return fallback100 return fallback
99101
=== modified file 'tests/contrib/network/test_ip.py'
--- tests/contrib/network/test_ip.py 2015-08-26 18:27:32 +0000
+++ tests/contrib/network/test_ip.py 2015-12-14 15:17:51 +0000
@@ -171,6 +171,17 @@
171 def test_get_address_in_network_ipv4(self):171 def test_get_address_in_network_ipv4(self):
172 self._test_get_address_in_network('192.168.1.55', '192.168.1.0/24')172 self._test_get_address_in_network('192.168.1.55', '192.168.1.0/24')
173173
174 def test_get_address_in_network_ipv4_multi(self):
175 # Assumes that there is an address configured on both but the first
176 # one is picked#
177 self._test_get_address_in_network('192.168.1.55',
178 '192.168.1.0/24 192.168.10.0/24')
179
180 def test_get_address_in_network_ipv4_multi2(self):
181 # Assumes that there is nothing configured on 192.168.11.0/24
182 self._test_get_address_in_network('192.168.10.58',
183 '192.168.11.0/24 192.168.10.0/24')
184
174 def test_get_address_in_network_ipv6(self):185 def test_get_address_in_network_ipv6(self):
175 self._test_get_address_in_network('2a01:348:2f4:0:685e:5748:ae62:209f',186 self._test_get_address_in_network('2a01:348:2f4:0:685e:5748:ae62:209f',
176 '2a01:348:2f4::/64')187 '2a01:348:2f4::/64')

Subscribers

People subscribed via source and target branches