Merge lp:~mpontillo/maas/fix-dhcp-tests--bug-1699286--2.2 into lp:maas/2.2

Proposed by Mike Pontillo on 2017-06-20
Status: Merged
Approved by: Mike Pontillo on 2017-06-20
Approved revision: 6066
Merged at revision: 6065
Proposed branch: lp:~mpontillo/maas/fix-dhcp-tests--bug-1699286--2.2
Merge into: lp:maas/2.2
Diff against target: 72 lines (+17/-11)
1 file modified
src/provisioningserver/dhcp/tests/test_config.py (+17/-11)
To merge this branch: bzr merge lp:~mpontillo/maas/fix-dhcp-tests--bug-1699286--2.2
Reviewer Review Type Date Requested Status
Mike Pontillo (community) Approve on 2017-06-20
Review via email: mp+326031@code.launchpad.net

Commit message

Merge revision 6096 from trunk to fix DHCP NTP configuration tests.

Note that no changelog entry is necessary for this change; it is developer-facing only.

To post a comment you must log in.
Mike Pontillo (mpontillo) wrote :

Self-review backport.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/provisioningserver/dhcp/tests/test_config.py'
2--- src/provisioningserver/dhcp/tests/test_config.py 2017-05-23 14:58:09 +0000
3+++ src/provisioningserver/dhcp/tests/test_config.py 2017-06-20 21:00:33 +0000
4@@ -26,6 +26,7 @@
5 import netaddr
6 from provisioningserver.boot import BootMethodRegistry
7 from provisioningserver.dhcp import config
8+from provisioningserver.dhcp.config import _get_addresses
9 from provisioningserver.dhcp.testing.config import (
10 fix_shared_networks_failover,
11 make_failover_peer_config,
12@@ -33,6 +34,7 @@
13 make_host,
14 make_shared_network,
15 )
16+from provisioningserver.utils import flatten
17 import provisioningserver.utils.network as net_utils
18 from provisioningserver.utils.shell import select_c_utf8_locale
19 from testtools.content import (
20@@ -87,13 +89,24 @@
21 }
22
23
24+def _resolve(name):
25+ # Find the first address that `getaddrinfo` returns for any address
26+ # family, socket type, and protocol. This is like `gethostbyname` that
27+ # also finds names in `/etc/hosts` and works with IPv6.
28+ for *_, addr in socket.getaddrinfo(name, 0):
29+ return addr[0]
30+
31+
32 def read_aliases_from_etc_hosts():
33 """Read all the aliases (hostnames) from /etc/hosts."""
34 with open("/etc/hosts", "r", encoding="ascii") as hosts:
35 for line in map(str.strip, hosts):
36 if len(line) > 0 and not line.startswith("#"):
37 address, *aliases = line.split()
38- yield from aliases
39+ for alias in aliases:
40+ # Ensure each alias resolves as expected before returning.
41+ if _resolve(alias) == address:
42+ yield alias
43
44
45 # Names that will resolve without network activity.
46@@ -246,23 +259,16 @@
47 self.assertNotIn("dhcp6.name-servers", rendered) # IPv6
48 self.assertNotIn("domain-name-servers", rendered) # IPv4
49
50- def _resolve(self, name):
51- # Find the first address that `getaddrinfo` returns for any address
52- # family, socket type, and protocol. This is like `gethostbyname` that
53- # also finds names in `/etc/hosts` and works with IPv6.
54- for *_, addr in socket.getaddrinfo(name, 0):
55- return addr[0]
56-
57 def test__renders_ntp_servers_as_comma_separated_list(self):
58 params = make_sample_params(self, ipv6=self.ipv6)
59 rendered = config.get_config(self.template, **params)
60 validate_dhcpd_configuration(self, rendered, self.ipv6)
61- ntp_servers_expected = [
62- server if is_ip_address(server) else self._resolve(server)
63+ ntp_servers_expected = flatten([
64+ server if is_ip_address(server) else _get_addresses(server)
65 for network in params['shared_networks']
66 for subnet in network['subnets']
67 for server in subnet["ntp_servers"]
68- ]
69+ ])
70 ntp_servers_observed = [
71 server for server_line in re.findall(
72 r"\b(?:ntp-servers|dhcp6[.]sntp-servers)\s+(.+);", rendered)

Subscribers

People subscribed via source and target branches

to all changes: