From 7fbb1f659cc9c596a02e386ac96c7bcfe2bce1a6 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Tue, 20 Nov 2007 09:18:27 +0000
Subject: [PATCH] Make sure aliases written to /etc/hosts are unique.

Reviewed-by: iustinp
---
 lib/utils.py                  | 3 +++
 test/ganeti.utils_unittest.py | 9 +++++++++
 2 files changed, 12 insertions(+)

diff --git a/lib/utils.py b/lib/utils.py
index f9f5f1b39..14540ff40 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 8ec8be070..020365626 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')
 
-- 
GitLab