diff --git a/lib/utils.py b/lib/utils.py index f9f5f1b39971f3ebd96a30e69ddf37e79c4aa49a..14540ff401b4681fde35fd8afef525da1481e645 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -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') diff --git a/test/ganeti.utils_unittest.py b/test/ganeti.utils_unittest.py index 8ec8be070422f1b0c744e3c9c66b8f01c0c6d3b1..02036562642c20fb4cfc6b3cd3610fecab444ac9 100755 --- a/test/ganeti.utils_unittest.py +++ b/test/ganeti.utils_unittest.py @@ -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')