Expose uses_systemd as a distro function

Without this change the tests are currently failing on rhel7
since a location where a hostname file is written no longer
exists at that location when systemd is active. To avoid this
allow the test to inspect if the distro has systemd enabled
and avoid testing the file when systemd is being used so the
test passes.

We likely need to figure out a better way to test features 
that no longer exist as files but exist as commands with
systemd in general.
This commit is contained in:
Joshua Harlow 2014-10-17 12:32:41 -07:00
parent 457d941d61
commit 0cca19229b
2 changed files with 11 additions and 10 deletions

View File

@ -98,7 +98,7 @@ class Distro(distros.Distro):
rhel_util.update_sysconfig_file(self.network_conf_fn, net_cfg) rhel_util.update_sysconfig_file(self.network_conf_fn, net_cfg)
return dev_names return dev_names
def _dist_uses_systemd(self): def uses_systemd(self):
# Fedora 18 and RHEL 7 were the first adopters in their series # Fedora 18 and RHEL 7 were the first adopters in their series
(dist, vers) = util.system_info()['dist'][:2] (dist, vers) = util.system_info()['dist'][:2]
major = (int)(vers.split('.')[0]) major = (int)(vers.split('.')[0])
@ -106,7 +106,7 @@ class Distro(distros.Distro):
or (dist.startswith('Fedora') and major >= 18)) or (dist.startswith('Fedora') and major >= 18))
def apply_locale(self, locale, out_fn=None): def apply_locale(self, locale, out_fn=None):
if self._dist_uses_systemd(): if self.uses_systemd():
if not out_fn: if not out_fn:
out_fn = self.systemd_locale_conf_fn out_fn = self.systemd_locale_conf_fn
out_fn = self.systemd_locale_conf_fn out_fn = self.systemd_locale_conf_fn
@ -119,7 +119,7 @@ class Distro(distros.Distro):
rhel_util.update_sysconfig_file(out_fn, locale_cfg) rhel_util.update_sysconfig_file(out_fn, locale_cfg)
def _write_hostname(self, hostname, out_fn): def _write_hostname(self, hostname, out_fn):
if self._dist_uses_systemd(): if self.uses_systemd():
util.subp(['hostnamectl', 'set-hostname', str(hostname)]) util.subp(['hostnamectl', 'set-hostname', str(hostname)])
else: else:
host_cfg = { host_cfg = {
@ -135,14 +135,14 @@ class Distro(distros.Distro):
return hostname return hostname
def _read_system_hostname(self): def _read_system_hostname(self):
if self._dist_uses_systemd(): if self.uses_systemd():
host_fn = self.systemd_hostname_conf_fn host_fn = self.systemd_hostname_conf_fn
else: else:
host_fn = self.hostname_conf_fn host_fn = self.hostname_conf_fn
return (host_fn, self._read_hostname(host_fn)) return (host_fn, self._read_hostname(host_fn))
def _read_hostname(self, filename, default=None): def _read_hostname(self, filename, default=None):
if self._dist_uses_systemd(): if self.uses_systemd():
(out, _err) = util.subp(['hostname']) (out, _err) = util.subp(['hostname'])
if len(out): if len(out):
return out return out
@ -163,7 +163,7 @@ class Distro(distros.Distro):
def set_timezone(self, tz): def set_timezone(self, tz):
tz_file = self._find_tz_file(tz) tz_file = self._find_tz_file(tz)
if self._dist_uses_systemd(): if self.uses_systemd():
# Currently, timedatectl complains if invoked during startup # Currently, timedatectl complains if invoked during startup
# so for compatibility, create the link manually. # so for compatibility, create the link manually.
util.del_file(self.tz_local_fn) util.del_file(self.tz_local_fn)

View File

@ -37,10 +37,11 @@ class TestHostname(t_help.FilesystemMockingTestCase):
self.patchUtils(self.tmp) self.patchUtils(self.tmp)
cc_set_hostname.handle('cc_set_hostname', cc_set_hostname.handle('cc_set_hostname',
cfg, cc, LOG, []) cfg, cc, LOG, [])
contents = util.load_file("/etc/sysconfig/network") if not distro.uses_systemd():
n_cfg = ConfigObj(StringIO(contents)) contents = util.load_file("/etc/sysconfig/network")
self.assertEquals({'HOSTNAME': 'blah.blah.blah.yahoo.com'}, n_cfg = ConfigObj(StringIO(contents))
dict(n_cfg)) self.assertEquals({'HOSTNAME': 'blah.blah.blah.yahoo.com'},
dict(n_cfg))
def test_write_hostname_debian(self): def test_write_hostname_debian(self):
cfg = { cfg = {