summaryrefslogtreecommitdiff
path: root/tests/unittests/test_handler/test_handler_set_hostname.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unittests/test_handler/test_handler_set_hostname.py')
-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."""