Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dynamic ipv6 address check & DNS tests #878

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions charmhelpers/contrib/network/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ def get_ipv6_addr(iface=None, inc_aliases=False, fatal=True, exc_list=None,
key_scope_link_local = re.compile("^fe80::..(.+)%(.+)")
m = re.match(key_scope_link_local, addr)
if m:
eui_64_mac = m.group(1)
iface = m.group(2)
else:
global_addrs.append(addr)
Expand All @@ -404,8 +403,7 @@ def get_ipv6_addr(iface=None, inc_aliases=False, fatal=True, exc_list=None,
# Return the first valid address we find
for addr in global_addrs:
if m.group(1) == addr:
if not dynamic_only or \
m.group(1).endswith(eui_64_mac):
if not dynamic_only or 'dynamic' in line:
addrs.append(addr)

if addrs:
Expand Down Expand Up @@ -467,7 +465,9 @@ def ns_query(address):

try:
answers = dns.resolver.query(address, rtype)
except (dns.resolver.NXDOMAIN, dns.resolver.NoNameservers):
# If we hit a non-authoritative recursor it's going
# to raise a dns.resolver.NoAnswer exception
except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN, dns.resolver.NoNameservers):
return None

if answers:
Expand Down
35 changes: 31 additions & 4 deletions tests/contrib/network/test_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def test_get_ipv6_addr(self, _interfaces, _ifaddresses, mock_check_out,
mock_get_iface_from_addr):
mock_get_iface_from_addr.return_value = 'eth0'
mock_check_out.return_value = \
b"inet6 2a01:348:2f4:0:685e:5748:ae62:209f/64 scope global dynamic"
b"inet6 2a01:348:2f4:0:685e:5748:ae62:209f/64 scope global"
_interfaces.return_value = DUMMY_ADDRESSES.keys()
_ifaddresses.side_effect = DUMMY_ADDRESSES.__getitem__
result = net_ip.get_ipv6_addr(dynamic_only=False)
Expand All @@ -355,8 +355,7 @@ def test_get_ipv6_addr(self, _interfaces, _ifaddresses, mock_check_out,
@patch('charmhelpers.contrib.network.ip.subprocess.check_output')
@patch.object(netifaces, 'ifaddresses')
@patch.object(netifaces, 'interfaces')
def test_get_ipv6_addr_global_dynamic(self, _interfaces, _ifaddresses,
mock_check_out,
def test_get_ipv6_addr_global_dynamic(self, _interfaces, _ifaddresses, mock_check_out,
mock_get_iface_from_addr):
mock_get_iface_from_addr.return_value = 'eth0'
mock_check_out.return_value = \
Expand All @@ -366,6 +365,34 @@ def test_get_ipv6_addr_global_dynamic(self, _interfaces, _ifaddresses,
result = net_ip.get_ipv6_addr(dynamic_only=False)
self.assertEqual(['2a01:348:2f4:0:685e:5748:ae62:209f'], result)

@patch('charmhelpers.contrib.network.ip.get_iface_from_addr')
@patch('charmhelpers.contrib.network.ip.subprocess.check_output')
@patch.object(netifaces, 'ifaddresses')
@patch.object(netifaces, 'interfaces')
def test_get_ipv6_addr_global_dynamic_only(self, _interfaces, _ifaddresses, mock_check_out,
mock_get_iface_from_addr):
mock_get_iface_from_addr.return_value = 'eth0'
mock_check_out.return_value = \
(b"inet6 2a01:348:2f4:0:685e:5748:ae62:209f/64 scope global\n" +
b"inet6 2a01:348:2f4:0:685e:5748:ae62:209f/64 scope global dynamic")
_interfaces.return_value = DUMMY_ADDRESSES.keys()
_ifaddresses.side_effect = DUMMY_ADDRESSES.__getitem__
result = net_ip.get_ipv6_addr(dynamic_only=True)
self.assertEqual(['2a01:348:2f4:0:685e:5748:ae62:209f'], result)

@patch('charmhelpers.contrib.network.ip.get_iface_from_addr')
@patch('charmhelpers.contrib.network.ip.subprocess.check_output')
@patch.object(netifaces, 'ifaddresses')
@patch.object(netifaces, 'interfaces')
def test_get_ipv6_addr_no_dynamic_addresses(self, _interfaces, _ifaddresses, mock_check_out,
mock_get_iface_from_addr):
mock_get_iface_from_addr.return_value = 'eth0'
mock_check_out.return_value = \
b"inet6 2a01:348:2f4:0:685e:5748:ae62:209f/64 scope global"
_interfaces.return_value = DUMMY_ADDRESSES.keys()
_ifaddresses.side_effect = DUMMY_ADDRESSES.__getitem__
self.assertRaises(Exception, net_ip.get_ipv6_addr, dynamic_only=True)

@patch.object(netifaces, 'interfaces')
def test_get_ipv6_addr_invalid_nic(self, _interfaces):
_interfaces.return_value = DUMMY_ADDRESSES.keys()
Expand Down Expand Up @@ -709,7 +736,7 @@ def test_ns_query_lookup_fail(self, apt_install):
self.assertEquals(nsq, None)

@patch('charmhelpers.contrib.network.ip.apt_install')
def test_ns_query_loopup_fail_real_implementation(self, apt_install):
def test_ns_query_lookup_fail_real_implementation(self, apt_install):
self.assertEqual(net_ip.ns_query('nonexistant'), None)
apt_install.assert_not_called()

Expand Down
Loading