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}) 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.blah.blah.yahoo.com'}, dict(n_cfg))
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}) 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.blah.blah.yahoo.com"}, dict(n_cfg))
def test_write_hostname_sles(self): cfg = { 'hostname': 'blah.blah.blah.suse.com', } distro = self._fetch_distro('sles') paths = helpers.Paths({}) 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', contents.strip())
def test_write_hostname_sles(self, m_uses_systemd): cfg = { 'hostname': 'blah.blah.blah.suse.com', } distro = self._fetch_distro('sles') 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(distro.hostname_conf_fn) self.assertEqual('blah', contents.strip())
def test_write_hostname_sles(self, m_uses_systemd): cfg = { "hostname": "blah.blah.blah.suse.com", } distro = self._fetch_distro("sles") 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(distro.hostname_conf_fn) self.assertEqual("blah", contents.strip())
def test_write_hostname_debian(self): cfg = { 'hostname': 'blah.blah.blah.yahoo.com', } distro = self._fetch_distro('debian') 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', contents.strip())
def test_write_hostname_debian(self): cfg = { "hostname": "blah", "fqdn": "blah.blah.blah.yahoo.com", } distro = self._fetch_distro("debian") 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", contents.strip())
def test_write_hostname_sles(self): cfg = { 'hostname': 'blah.blah.blah.suse.com', } distro = self._fetch_distro('sles') 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, []) if not distro.uses_systemd(): contents = util.load_file(distro.hostname_conf_fn) self.assertEqual('blah', contents.strip())
def test_write_hostname_rhel(self): cfg = { 'hostname': 'blah.blah.blah.yahoo.com', } distro = self._fetch_distro('rhel') paths = helpers.Paths({}) 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") n_cfg = ConfigObj(StringIO(contents)) self.assertEquals({'HOSTNAME': 'blah.blah.blah.yahoo.com'}, dict(n_cfg))
def test_localhost_default_hostname(self, get_hostname): """ No hostname set. Default value returned is localhost, but we shouldn't write it in /etc/hostname """ distro = self._fetch_distro("debian") paths = helpers.Paths({"cloud_dir": self.tmp}) ds = DataSourceNone.DataSourceNone({}, None, paths) cc = cloud.Cloud(ds, paths, {}, distro, None) self.patchUtils(self.tmp) util.write_file("/etc/hostname", "") cc_set_hostname.handle("cc_set_hostname", {}, cc, LOG, []) contents = util.load_file("/etc/hostname") self.assertEqual("", contents.strip())
def _maybe_set_hostname(init, stage, retry_stage): """Call set-hostname if metadata, vendordata or userdata provides it. @param stage: String representing current stage in which we are running. @param retry_stage: String represented logs upon error setting hostname. """ cloud = init.cloudify() (hostname, _fqdn) = util.get_hostname_fqdn( init.cfg, cloud, metadata_only=True) if hostname: # meta-data or user-data hostname content try: cc_set_hostname.handle('set-hostname', init.cfg, cloud, LOG, None) except cc_set_hostname.SetHostnameError as e: LOG.debug( 'Failed setting hostname in %s stage. Will' ' retry in %s stage. Error: %s.', stage, retry_stage, str(e))
def test_write_hostname_rhel(self): cfg = { 'hostname': 'blah.blah.blah.yahoo.com', } distro = self._fetch_distro('rhel') 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, []) 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))
def test_localhost_user_given_hostname(self, get_hostname): """ User set hostname is localhost. We should write it in /etc/hostname """ distro = self._fetch_distro("debian") paths = helpers.Paths({"cloud_dir": self.tmp}) ds = DataSourceNone.DataSourceNone({}, None, paths) cc = cloud.Cloud(ds, paths, {}, distro, None) self.patchUtils(self.tmp) # user-provided localhost should not be ignored util.write_file("/etc/hostname", "") cc_set_hostname.handle("cc_set_hostname", {"hostname": "localhost"}, cc, LOG, []) contents = util.load_file("/etc/hostname") self.assertEqual("localhost", contents.strip())
def test_photon_hostname(self, m_subp): cfg1 = { "hostname": "photon", "prefer_fqdn_over_hostname": True, "fqdn": "test1.vmware.com", } cfg2 = { "hostname": "photon", "prefer_fqdn_over_hostname": False, "fqdn": "test2.vmware.com", } ds = None m_subp.return_value = (None, None) distro = self._fetch_distro("photon", cfg1) paths = helpers.Paths({"cloud_dir": self.tmp}) cc = cloud.Cloud(ds, paths, {}, distro, None) for c in [cfg1, cfg2]: cc_set_hostname.handle("cc_set_hostname", c, cc, LOG, []) print("\n", m_subp.call_args_list) if c["prefer_fqdn_over_hostname"]: assert [ mock.call( ["hostnamectl", "set-hostname", c["fqdn"]], capture=True, ) ] in m_subp.call_args_list assert [ mock.call( ["hostnamectl", "set-hostname", c["hostname"]], capture=True, ) ] not in m_subp.call_args_list else: assert [ mock.call( ["hostnamectl", "set-hostname", c["hostname"]], capture=True, ) ] in m_subp.call_args_list assert [ mock.call( ["hostnamectl", "set-hostname", c["fqdn"]], capture=True, ) ] not in m_subp.call_args_list
def test_error_on_distro_set_hostname_errors(self): """Raise SetHostnameError on exceptions from distro.set_hostname.""" distro = self._fetch_distro('debian') def set_hostname_error(hostname, fqdn): raise Exception("OOPS on: %s" % fqdn) distro.set_hostname = set_hostname_error paths = helpers.Paths({'cloud_dir': self.tmp}) ds = None cc = cloud.Cloud(ds, paths, {}, distro, None) self.patchUtils(self.tmp) with self.assertRaises(cc_set_hostname.SetHostnameError) as ctx_mgr: cc_set_hostname.handle( 'somename', {'hostname': 'hostname1.me.com'}, cc, LOG, []) self.assertEqual( 'Failed to set the hostname to hostname1.me.com (hostname1):' ' OOPS on: hostname1.me.com', str(ctx_mgr.exception))
def test_multiple_calls_skips_unchanged_hostname(self, get_hostname): """Only new hostname or fqdn values will generate a hostname call.""" distro = self._fetch_distro("debian") 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", {"hostname": "hostname1.me.com"}, cc, LOG, []) contents = util.load_file("/etc/hostname") self.assertEqual("hostname1", contents.strip()) cc_set_hostname.handle("cc_set_hostname", {"hostname": "hostname1.me.com"}, cc, LOG, []) self.assertIn( "DEBUG: No hostname changes. Skipping set-hostname\n", self.logs.getvalue(), ) cc_set_hostname.handle("cc_set_hostname", {"hostname": "hostname2.me.com"}, cc, LOG, []) contents = util.load_file("/etc/hostname") self.assertEqual("hostname2", contents.strip()) self.assertIn( "Non-persistently setting the system hostname to hostname2", self.logs.getvalue(), )
def test_photon_hostname(self, m_subp): cfg1 = { 'hostname': 'photon', 'prefer_fqdn_over_hostname': True, 'fqdn': 'test1.vmware.com', } cfg2 = { 'hostname': 'photon', 'prefer_fqdn_over_hostname': False, 'fqdn': 'test2.vmware.com', } ds = None m_subp.return_value = (None, None) distro = self._fetch_distro('photon', cfg1) paths = helpers.Paths({'cloud_dir': self.tmp}) cc = cloud.Cloud(ds, paths, {}, distro, None) for c in [cfg1, cfg2]: cc_set_hostname.handle('cc_set_hostname', c, cc, LOG, []) print("\n", m_subp.call_args_list) if c['prefer_fqdn_over_hostname']: assert [ mock.call(['hostnamectl', 'set-hostname', c['fqdn']], capture=True) ] in m_subp.call_args_list assert [ mock.call(['hostnamectl', 'set-hostname', c['hostname']], capture=True) ] not in m_subp.call_args_list else: assert [ mock.call(['hostnamectl', 'set-hostname', c['hostname']], capture=True) ] in m_subp.call_args_list assert [ mock.call(['hostnamectl', 'set-hostname', c['fqdn']], capture=True) ] not in m_subp.call_args_list
def test_photon_hostname(self, m_uses_systemd): cfg1 = { 'hostname': 'photon', 'prefer_fqdn_over_hostname': True, 'fqdn': 'test1.vmware.com', } cfg2 = { 'hostname': 'photon', 'prefer_fqdn_over_hostname': False, 'fqdn': 'test2.vmware.com', } ds = None distro = self._fetch_distro('photon', cfg1) paths = helpers.Paths({'cloud_dir': self.tmp}) cc = cloud.Cloud(ds, paths, {}, distro, None) self.patchUtils(self.tmp) for c in [cfg1, cfg2]: cc_set_hostname.handle('cc_set_hostname', c, cc, LOG, []) contents = util.load_file(distro.hostname_conf_fn, decode=True) if c['prefer_fqdn_over_hostname']: self.assertEqual(contents.strip(), c['fqdn']) else: self.assertEqual(contents.strip(), c['hostname'])
def test_multiple_calls_skips_unchanged_hostname(self): """Only new hostname or fqdn values will generate a hostname call.""" distro = self._fetch_distro('debian') 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', {'hostname': 'hostname1.me.com'}, cc, LOG, []) contents = util.load_file("/etc/hostname") self.assertEqual('hostname1', contents.strip()) cc_set_hostname.handle('cc_set_hostname', {'hostname': 'hostname1.me.com'}, cc, LOG, []) self.assertIn('DEBUG: No hostname changes. Skipping set-hostname\n', self.logs.getvalue()) cc_set_hostname.handle('cc_set_hostname', {'hostname': 'hostname2.me.com'}, cc, LOG, []) contents = util.load_file("/etc/hostname") self.assertEqual('hostname2', contents.strip()) self.assertIn( 'Non-persistently setting the system hostname to hostname2', self.logs.getvalue())
def test_multiple_calls_skips_unchanged_hostname(self): """Only new hostname or fqdn values will generate a hostname call.""" distro = self._fetch_distro('debian') 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', {'hostname': 'hostname1.me.com'}, cc, LOG, []) contents = util.load_file("/etc/hostname") self.assertEqual('hostname1', contents.strip()) cc_set_hostname.handle( 'cc_set_hostname', {'hostname': 'hostname1.me.com'}, cc, LOG, []) self.assertIn( 'DEBUG: No hostname changes. Skipping set-hostname\n', self.logs.getvalue()) cc_set_hostname.handle( 'cc_set_hostname', {'hostname': 'hostname2.me.com'}, cc, LOG, []) contents = util.load_file("/etc/hostname") self.assertEqual('hostname2', contents.strip()) self.assertIn( 'Non-persistently setting the system hostname to hostname2', self.logs.getvalue())