def test_del(self): eh = hosts.HostsConf(BASE_ETC) eh.add_entry('127.0.0.0', 'blah') self.assertEquals(eh.get_entry('127.0.0.0'), [['blah']]) eh.del_entries('127.0.0.0') self.assertEquals(eh.get_entry('127.0.0.0'), [])
def test_add(self): eh = hosts.HostsConf(BASE_ETC) eh.add_entry('127.0.0.0', 'blah') self.assertEquals(eh.get_entry('127.0.0.0'), [['blah']]) eh.add_entry('127.0.0.3', 'blah', 'blah2', 'blah3') self.assertEquals(eh.get_entry('127.0.0.3'), [['blah', 'blah2', 'blah3']])
def test_del(self): eh = hosts.HostsConf(BASE_ETC) eh.add_entry("127.0.0.0", "blah") self.assertEqual(eh.get_entry("127.0.0.0"), [["blah"]]) eh.del_entries("127.0.0.0") self.assertEqual(eh.get_entry("127.0.0.0"), [])
def test_add(self): eh = hosts.HostsConf(BASE_ETC) eh.add_entry("127.0.0.0", "blah") self.assertEqual(eh.get_entry("127.0.0.0"), [["blah"]]) eh.add_entry("127.0.0.3", "blah", "blah2", "blah3") self.assertEqual(eh.get_entry("127.0.0.3"), [["blah", "blah2", "blah3"]])
def test_parse(self): eh = hosts.HostsConf(BASE_ETC) self.assertEquals(eh.get_entry('127.0.0.1'), [['localhost']]) self.assertEquals(eh.get_entry('192.168.1.10'), [['foo.mydomain.org', 'foo'], ['bar.mydomain.org', 'bar']]) eh = str(eh) self.assertTrue(eh.startswith('# Example'))
def test_parse(self): eh = hosts.HostsConf(BASE_ETC) self.assertEqual(eh.get_entry("127.0.0.1"), [["localhost"]]) self.assertEqual( eh.get_entry("192.168.1.10"), [["foo.mydomain.org", "foo"], ["bar.mydomain.org", "bar"]], ) eh = str(eh) self.assertTrue(eh.startswith("# Example"))
def update_etc_hosts(self, hostname, fqdn): header = '' if os.path.exists(self.hosts_fn): eh = hosts.HostsConf(util.load_file(self.hosts_fn)) else: eh = hosts.HostsConf('') header = util.make_header(base="added") local_ip = self._get_localhost_ip() prev_info = eh.get_entry(local_ip) need_change = False if not prev_info: eh.add_entry(local_ip, fqdn, hostname) need_change = True else: need_change = True for entry in prev_info: entry_fqdn = None entry_aliases = [] if len(entry) >= 1: entry_fqdn = entry[0] if len(entry) >= 2: entry_aliases = entry[1:] if entry_fqdn is not None and entry_fqdn == fqdn: if hostname in entry_aliases: # Exists already, leave it be need_change = False if need_change: # Doesn't exist, add that entry in... new_entries = list(prev_info) new_entries.append([fqdn, hostname]) eh.del_entries(local_ip) for entry in new_entries: if len(entry) == 1: eh.add_entry(local_ip, entry[0]) elif len(entry) >= 2: eh.add_entry(local_ip, *entry) if need_change: contents = StringIO() if header: contents.write("%s\n" % (header)) contents.write("%s\n" % (eh)) util.write_file(self.hosts_fn, contents.getvalue(), mode=0o644)