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

Proposed by Mike Pontillo
Status: Merged
Approved by: Mike Pontillo
Approved revision: no longer in the source branch.
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
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.
Revision history for this message
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
=== modified file 'src/provisioningserver/dhcp/tests/test_config.py'
--- src/provisioningserver/dhcp/tests/test_config.py 2017-05-23 14:58:09 +0000
+++ src/provisioningserver/dhcp/tests/test_config.py 2017-06-20 21:00:33 +0000
@@ -26,6 +26,7 @@
26import netaddr26import netaddr
27from provisioningserver.boot import BootMethodRegistry27from provisioningserver.boot import BootMethodRegistry
28from provisioningserver.dhcp import config28from provisioningserver.dhcp import config
29from provisioningserver.dhcp.config import _get_addresses
29from provisioningserver.dhcp.testing.config import (30from provisioningserver.dhcp.testing.config import (
30 fix_shared_networks_failover,31 fix_shared_networks_failover,
31 make_failover_peer_config,32 make_failover_peer_config,
@@ -33,6 +34,7 @@
33 make_host,34 make_host,
34 make_shared_network,35 make_shared_network,
35)36)
37from provisioningserver.utils import flatten
36import provisioningserver.utils.network as net_utils38import provisioningserver.utils.network as net_utils
37from provisioningserver.utils.shell import select_c_utf8_locale39from provisioningserver.utils.shell import select_c_utf8_locale
38from testtools.content import (40from testtools.content import (
@@ -87,13 +89,24 @@
87 }89 }
8890
8991
92def _resolve(name):
93 # Find the first address that `getaddrinfo` returns for any address
94 # family, socket type, and protocol. This is like `gethostbyname` that
95 # also finds names in `/etc/hosts` and works with IPv6.
96 for *_, addr in socket.getaddrinfo(name, 0):
97 return addr[0]
98
99
90def read_aliases_from_etc_hosts():100def read_aliases_from_etc_hosts():
91 """Read all the aliases (hostnames) from /etc/hosts."""101 """Read all the aliases (hostnames) from /etc/hosts."""
92 with open("/etc/hosts", "r", encoding="ascii") as hosts:102 with open("/etc/hosts", "r", encoding="ascii") as hosts:
93 for line in map(str.strip, hosts):103 for line in map(str.strip, hosts):
94 if len(line) > 0 and not line.startswith("#"):104 if len(line) > 0 and not line.startswith("#"):
95 address, *aliases = line.split()105 address, *aliases = line.split()
96 yield from aliases106 for alias in aliases:
107 # Ensure each alias resolves as expected before returning.
108 if _resolve(alias) == address:
109 yield alias
97110
98111
99# Names that will resolve without network activity.112# Names that will resolve without network activity.
@@ -246,23 +259,16 @@
246 self.assertNotIn("dhcp6.name-servers", rendered) # IPv6259 self.assertNotIn("dhcp6.name-servers", rendered) # IPv6
247 self.assertNotIn("domain-name-servers", rendered) # IPv4260 self.assertNotIn("domain-name-servers", rendered) # IPv4
248261
249 def _resolve(self, name):
250 # Find the first address that `getaddrinfo` returns for any address
251 # family, socket type, and protocol. This is like `gethostbyname` that
252 # also finds names in `/etc/hosts` and works with IPv6.
253 for *_, addr in socket.getaddrinfo(name, 0):
254 return addr[0]
255
256 def test__renders_ntp_servers_as_comma_separated_list(self):262 def test__renders_ntp_servers_as_comma_separated_list(self):
257 params = make_sample_params(self, ipv6=self.ipv6)263 params = make_sample_params(self, ipv6=self.ipv6)
258 rendered = config.get_config(self.template, **params)264 rendered = config.get_config(self.template, **params)
259 validate_dhcpd_configuration(self, rendered, self.ipv6)265 validate_dhcpd_configuration(self, rendered, self.ipv6)
260 ntp_servers_expected = [266 ntp_servers_expected = flatten([
261 server if is_ip_address(server) else self._resolve(server)267 server if is_ip_address(server) else _get_addresses(server)
262 for network in params['shared_networks']268 for network in params['shared_networks']
263 for subnet in network['subnets']269 for subnet in network['subnets']
264 for server in subnet["ntp_servers"]270 for server in subnet["ntp_servers"]
265 ]271 ])
266 ntp_servers_observed = [272 ntp_servers_observed = [
267 server for server_line in re.findall(273 server for server_line in re.findall(
268 r"\b(?:ntp-servers|dhcp6[.]sntp-servers)\s+(.+);", rendered)274 r"\b(?:ntp-servers|dhcp6[.]sntp-servers)\s+(.+);", rendered)

Subscribers

People subscribed via source and target branches

to all changes: