Handle unsetting '[DEFAULT] dhcp_domain'

Fix a long-standing issue whereby setting 'dhcp_domain' to 'None' would
result in a hostname of '${hostname}None' instead of '${hostname}'.

Change-Id: Ic9aa74f5344ba469b61a87de1ebd27e6f49c3318
Closes-Bug: #1824813
This commit is contained in:
Stephen Finucane 2019-04-15 14:34:55 +01:00 committed by Matt Riedemann
parent 03322bb517
commit 97549a2c41
4 changed files with 25 additions and 7 deletions

View File

@ -528,9 +528,10 @@ class InstanceMetadata(object):
return self._check_version(required, requested, OPENSTACK_VERSIONS)
def _get_hostname(self):
return "%s%s%s" % (self.instance.hostname,
'.' if CONF.dhcp_domain else '',
CONF.dhcp_domain)
if CONF.dhcp_domain:
return '.'.join([self.instance.hostname, CONF.dhcp_domain])
return self.instance.hostname
def lookup(self, path):
if path == "" or path[0] != "/":

View File

@ -59,7 +59,7 @@ class MetadataTest(test.TestCase):
projects=[])
instance = objects.Instance(ctxt, flavor=flavor, vcpus=1,
memory_mb=256, root_gb=0, ephemeral_gb=0,
project_id='fake')
project_id='fake', hostname='test')
instance.create()
# NOTE(mikal): We could create a network and a fixed IP here, but it
@ -100,6 +100,9 @@ class MetadataTest(test.TestCase):
url = '%sopenstack/latest/meta_data.json' % self.md_url
res = requests.request('GET', url, timeout=5)
self.assertEqual(200, res.status_code)
j = jsonutils.loads(res.text)
self.assertIn('hostname', j)
self.assertEqual('test.novalocal', j['hostname'])
def test_lookup_external_service(self):
self.flags(

View File

@ -85,7 +85,7 @@ def fake_inst_obj(context):
vcpus=1,
fixed_ips=[],
root_device_name='/dev/sda1',
hostname='test.novadomain',
hostname='test',
display_name='my_displayname',
metadata={},
device_metadata=fake_metadata_objects(),
@ -310,11 +310,19 @@ class MetadataTestCase(test.TestCase):
self.flags(use_neutron=True)
self._test_security_groups()
def test_local_hostname_fqdn(self):
def test_local_hostname(self):
self.flags(dhcp_domain=None)
md = fake_InstanceMetadata(self, self.instance.obj_clone())
data = md.get_ec2_metadata(version='2009-04-04')
self.assertEqual(data['meta-data']['local-hostname'],
"%s.%s" % (self.instance['hostname'], CONF.dhcp_domain))
self.instance['hostname'])
def test_local_hostname_fqdn(self):
self.flags(dhcp_domain='fakedomain')
md = fake_InstanceMetadata(self, self.instance.obj_clone())
data = md.get_ec2_metadata(version='2009-04-04')
self.assertEqual('%s.fakedomain' % self.instance['hostname'],
data['meta-data']['local-hostname'])
def test_format_instance_mapping(self):
# Make sure that _format_instance_mappings works.

View File

@ -0,0 +1,6 @@
---
fixes:
- |
Unsetting '[DEFAULT] dhcp_domain' will now correctly result in the metadata
service/config drive providing an instance hostname of '${hostname}' instead
of '${hostname}None', as was previously seen.