summaryrefslogtreecommitdiff
path: root/tests/unittests/test_handler
diff options
context:
space:
mode:
authorhamalq <81582959+hamalq@users.noreply.github.com>2021-04-15 16:45:12 -0700
committerGitHub <noreply@github.com>2021-04-15 18:45:12 -0500
commit45db197cfc7e3488baae7dc1053c45da070248f6 (patch)
tree91b0148c026c619e6f7f9f50064e6d07c1f47fac /tests/unittests/test_handler
parent0d90596b56db5d306125ead08c571fc8d44d528e (diff)
downloadvyos-cloud-init-45db197cfc7e3488baae7dc1053c45da070248f6.tar.gz
vyos-cloud-init-45db197cfc7e3488baae7dc1053c45da070248f6.zip
add prefer_fqdn_over_hostname config option (#859)
the above option allows the user to control the behavior of a distro hostname selection if both short hostname and FQDN are supplied. If `prefer_fqdn_over_hostname` is true the FQDN will be selected as hostname; if false the hostname will be selected LP: #1921004
Diffstat (limited to 'tests/unittests/test_handler')
-rw-r--r--tests/unittests/test_handler/test_handler_set_hostname.py69
1 files changed, 55 insertions, 14 deletions
diff --git a/tests/unittests/test_handler/test_handler_set_hostname.py b/tests/unittests/test_handler/test_handler_set_hostname.py
index 58abf51a..73641b70 100644
--- a/tests/unittests/test_handler/test_handler_set_hostname.py
+++ b/tests/unittests/test_handler/test_handler_set_hostname.py
@@ -15,6 +15,7 @@ import os
import shutil
import tempfile
from io import BytesIO
+from unittest import mock
LOG = logging.getLogger(__name__)
@@ -29,14 +30,53 @@ class TestHostname(t_help.FilesystemMockingTestCase):
util.ensure_dir(os.path.join(self.tmp, 'data'))
self.addCleanup(shutil.rmtree, self.tmp)
- def _fetch_distro(self, kind):
+ def _fetch_distro(self, kind, conf=None):
cls = distros.fetch(kind)
paths = helpers.Paths({'cloud_dir': self.tmp})
- return cls(kind, {}, paths)
+ conf = {} if conf is None else conf
+ return cls(kind, conf, paths)
- def test_write_hostname_rhel(self):
+ def test_debian_write_hostname_prefer_fqdn(self):
cfg = {
- 'hostname': 'blah.blah.blah.yahoo.com',
+ 'hostname': 'blah',
+ 'prefer_fqdn_over_hostname': True,
+ 'fqdn': 'blah.yahoo.com',
+ }
+ distro = self._fetch_distro('debian', cfg)
+ paths = helpers.Paths({'cloud_dir': self.tmp})
+ ds = None
+ cc = cloud.Cloud(ds, paths, {}, distro, None)
+ self.patchUtils(self.tmp)
+ cc_set_hostname.handle('cc_set_hostname',
+ cfg, cc, LOG, [])
+ contents = util.load_file("/etc/hostname")
+ self.assertEqual('blah.yahoo.com', contents.strip())
+
+ @mock.patch('cloudinit.distros.Distro.uses_systemd', return_value=False)
+ def test_rhel_write_hostname_prefer_hostname(self, m_uses_systemd):
+ cfg = {
+ 'hostname': 'blah',
+ 'prefer_fqdn_over_hostname': False,
+ 'fqdn': 'blah.yahoo.com',
+ }
+ distro = self._fetch_distro('rhel', cfg)
+ paths = helpers.Paths({'cloud_dir': self.tmp})
+ ds = None
+ cc = cloud.Cloud(ds, paths, {}, distro, None)
+ self.patchUtils(self.tmp)
+ cc_set_hostname.handle('cc_set_hostname',
+ cfg, cc, LOG, [])
+ contents = util.load_file("/etc/sysconfig/network", decode=False)
+ n_cfg = ConfigObj(BytesIO(contents))
+ self.assertEqual(
+ {'HOSTNAME': 'blah'},
+ dict(n_cfg))
+
+ @mock.patch('cloudinit.distros.Distro.uses_systemd', return_value=False)
+ def test_write_hostname_rhel(self, m_uses_systemd):
+ cfg = {
+ 'hostname': 'blah',
+ 'fqdn': 'blah.blah.blah.yahoo.com'
}
distro = self._fetch_distro('rhel')
paths = helpers.Paths({'cloud_dir': self.tmp})
@@ -45,15 +85,16 @@ class TestHostname(t_help.FilesystemMockingTestCase):
self.patchUtils(self.tmp)
cc_set_hostname.handle('cc_set_hostname',
cfg, cc, LOG, [])
- if not distro.uses_systemd():
- contents = util.load_file("/etc/sysconfig/network", decode=False)
- n_cfg = ConfigObj(BytesIO(contents))
- self.assertEqual({'HOSTNAME': 'blah.blah.blah.yahoo.com'},
- dict(n_cfg))
+ contents = util.load_file("/etc/sysconfig/network", decode=False)
+ n_cfg = ConfigObj(BytesIO(contents))
+ self.assertEqual(
+ {'HOSTNAME': 'blah.blah.blah.yahoo.com'},
+ dict(n_cfg))
def test_write_hostname_debian(self):
cfg = {
- 'hostname': 'blah.blah.blah.yahoo.com',
+ 'hostname': 'blah',
+ 'fqdn': 'blah.blah.blah.yahoo.com',
}
distro = self._fetch_distro('debian')
paths = helpers.Paths({'cloud_dir': self.tmp})
@@ -65,7 +106,8 @@ class TestHostname(t_help.FilesystemMockingTestCase):
contents = util.load_file("/etc/hostname")
self.assertEqual('blah', contents.strip())
- def test_write_hostname_sles(self):
+ @mock.patch('cloudinit.distros.Distro.uses_systemd', return_value=False)
+ def test_write_hostname_sles(self, m_uses_systemd):
cfg = {
'hostname': 'blah.blah.blah.suse.com',
}
@@ -75,9 +117,8 @@ class TestHostname(t_help.FilesystemMockingTestCase):
cc = cloud.Cloud(ds, paths, {}, distro, None)
self.patchUtils(self.tmp)
cc_set_hostname.handle('cc_set_hostname', cfg, cc, LOG, [])
- if not distro.uses_systemd():
- contents = util.load_file(distro.hostname_conf_fn)
- self.assertEqual('blah', contents.strip())
+ contents = util.load_file(distro.hostname_conf_fn)
+ self.assertEqual('blah', contents.strip())
def test_multiple_calls_skips_unchanged_hostname(self):
"""Only new hostname or fqdn values will generate a hostname call."""