Merge lp:~lamont/maas/bug-1614584 into lp:~maas-committers/maas/trunk

Proposed by LaMont Jones
Status: Merged
Approved by: LaMont Jones
Approved revision: 5591
Merged at revision: 5593
Proposed branch: lp:~lamont/maas/bug-1614584
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 73 lines (+25/-7)
3 files modified
src/maasserver/models/node.py (+13/-1)
src/maasserver/models/tests/test_node.py (+6/-1)
src/maasserver/models/tests/test_staticipaddress.py (+6/-5)
To merge this branch: bzr merge lp:~lamont/maas/bug-1614584
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+312648@code.launchpad.net

Commit message

Handle the case where gethostname() returns an FQDN.

Description of the change

Handle the case where gethostname() returns an FQDN.

To post a comment you must log in.
Revision history for this message
Blake Rouse (blake-rouse) wrote :

Looks good.

review: Approve
lp:~lamont/maas/bug-1614584 updated
5591. By LaMont Jones

Driveby: fix TestStaticIPAddressManagerMapping.test_get_hostname_ip_mapping_returns_fqdn_and_other.

5592. By LaMont Jones

Strip any domain from the remaining uses of gethostname().

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/models/node.py'
2--- src/maasserver/models/node.py 2016-12-02 17:41:31 +0000
3+++ src/maasserver/models/node.py 2016-12-07 12:47:26 +0000
4@@ -743,7 +743,19 @@
5 regiond has run on it. Create a region controller only; it can be
6 upgraded to a region+rack controller later if necessary.
7 """
8- return self.create(owner=get_worker_user(), hostname=gethostname())
9+ hostname = gethostname()
10+ # Bug#1614584: it is possible that gethostname() reurns the FQDN.
11+ # Split it up, and get the appropriate domain. If we wind up creating
12+ # one for it, we are not authoritative.
13+ # Just in case the default domain has not been created, let's create it
14+ # here, even if we subsequently overwrite it inside the if statement.
15+ domain = Domain.objects.get_default_domain()
16+ if hostname.find('.') > 0:
17+ hostname, domainname = hostname.split('.', 1)
18+ (domain, _) = Domain.objects.get_or_create(
19+ name=domainname, defaults={'authoritative': False})
20+ return self.create(
21+ owner=get_worker_user(), hostname=hostname, domain=domain)
22
23
24 def get_default_domain():
25
26=== modified file 'src/maasserver/models/tests/test_node.py'
27--- src/maasserver/models/tests/test_node.py 2016-12-02 17:41:31 +0000
28+++ src/maasserver/models/tests/test_node.py 2016-12-07 12:47:26 +0000
29@@ -622,7 +622,12 @@
30 super().setUp()
31 # Patch out gethostname and get_mac_addresses.
32 self.patch_autospec(node_module, "gethostname")
33- node_module.gethostname.return_value = factory.make_name("host")
34+ hostname = factory.make_name('host')
35+ # Bug#1614584: make sure that we handle the case where gethostname()
36+ # returns an FQDN, instead of a domainless hostname.
37+ if factory.pick_bool():
38+ hostname += ".%s" % factory.make_name('domain')
39+ node_module.gethostname.return_value = hostname
40 self.patch_autospec(node_module, "get_mac_addresses")
41 node_module.get_mac_addresses.return_value = []
42
43
44=== modified file 'src/maasserver/models/tests/test_staticipaddress.py'
45--- src/maasserver/models/tests/test_staticipaddress.py 2016-11-23 15:05:45 +0000
46+++ src/maasserver/models/tests/test_staticipaddress.py 2016-12-07 12:47:26 +0000
47@@ -424,20 +424,21 @@
48 alloc_type=IPADDRESS_TYPE.STICKY,
49 ip=factory.pick_ip_in_Subnet(subnet),
50 subnet=subnet, interface=boot_interface)
51- ifaces = node.interface_set.exclude(interface=boot_interface)
52+ iface2 = node.interface_set.exclude(id=boot_interface.id).first()
53 sip2 = factory.make_StaticIPAddress(
54 alloc_type=IPADDRESS_TYPE.STICKY,
55 ip=factory.pick_ip_in_Subnet(subnet),
56- subnet=subnet, interface=ifaces[0])
57+ subnet=subnet, interface=iface2)
58 mapping = StaticIPAddress.objects.get_hostname_ip_mapping(subnet)
59- self.assertEqual({
60+ expected = {
61 full_hostname:
62 HostnameIPMapping(
63 node.system_id, 30, {staticip.ip}, node.node_type),
64- "%s.%s" % (ifaces[0].name, full_hostname):
65+ "%s.%s" % (iface2.name, full_hostname):
66 HostnameIPMapping(
67 node.system_id, 30, {sip2.ip}, node.node_type),
68- }, mapping)
69+ }
70+ self.assertEqual(expected, mapping)
71
72 def make_mapping(self, node, raw_ttl=False):
73 if raw_ttl or node.address_ttl is not None: