Commit 7fbb1f65 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Make sure aliases written to /etc/hosts are unique.

Reviewed-by: iustinp
parent f7414041
......@@ -756,6 +756,9 @@ def SetEtcHostsEntry(file_name, ip, hostname, aliases):
"""Sets the name of an IP address and hostname in /etc/hosts.
"""
# Ensure aliases are unique
aliases = UniqueSequence([hostname] + aliases)[1:]
fd, tmpname = tempfile.mkstemp(dir=os.path.dirname(file_name))
try:
out = os.fdopen(fd, 'w')
......
......@@ -456,6 +456,15 @@ class TestEtcHosts(GanetiTestCase):
"127.0.0.1\tlocalhost\n"
"192.168.1.1\tmyhost.domain.tld myhost\n")
def testSettingDuplicateName(self):
SetEtcHostsEntry(self.tmpname, '1.2.3.4', 'myhost', ['myhost'])
self.assertFileContent(self.tmpname,
"# This is a test file for /etc/hosts\n"
"127.0.0.1\tlocalhost\n"
"192.168.1.1 router gw\n"
"1.2.3.4\tmyhost\n")
def testRemovingExistingHost(self):
RemoveEtcHostsEntry(self.tmpname, 'router')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment