From 3a541d90a21f521de6186c07d028f5b0371c79df Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Tue, 22 Sep 2009 15:02:41 +0200
Subject: [PATCH] Fix utils.MatchNameComponent for full matches
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

While β€˜test1’ matches both β€˜test1’ and β€˜test1.example’, it has a full,
exact match and we should return it if that is the case.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/utils.py                  | 5 ++++-
 test/ganeti.utils_unittest.py | 8 ++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/lib/utils.py b/lib/utils.py
index 80e0d9a4d..c58c2cd27 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -497,7 +497,8 @@ def MatchNameComponent(key, name_list):
   this list, I{'test1'} as well as I{'test1.example'} will match, but
   not I{'test1.ex'}. A multiple match will be considered as no match
   at all (e.g. I{'test1'} against C{['test1.example.com',
-  'test1.example.org']}).
+  'test1.example.org']}), except when the key fully matches an entry
+  (e.g. I{'test1'} against C{['test1', 'test1.example.com']}).
 
   @type key: str
   @param key: the name to be searched
@@ -509,6 +510,8 @@ def MatchNameComponent(key, name_list):
       otherwise the element from the list which matches
 
   """
+  if key in name_list:
+    return key
   mo = re.compile("^%s(\..*)?$" % re.escape(key))
   names_filtered = [name for name in name_list if mo.match(name) is not None]
   if len(names_filtered) != 1:
diff --git a/test/ganeti.utils_unittest.py b/test/ganeti.utils_unittest.py
index 2893b8a31..1e8761d1f 100755
--- a/test/ganeti.utils_unittest.py
+++ b/test/ganeti.utils_unittest.py
@@ -328,6 +328,14 @@ class TestMatchNameComponent(unittest.TestCase):
     for key in "test1", "test1.example":
       self.failUnlessEqual(MatchNameComponent(key, mlist), None)
 
+  def testFullMatch(self):
+    """Test that a full match is returned correctly"""
+    key1 = "test1"
+    key2 = "test1.example"
+    mlist = [key2, key2 + ".com"]
+    self.failUnlessEqual(MatchNameComponent(key1, mlist), None)
+    self.failUnlessEqual(MatchNameComponent(key2, mlist), key2)
+
 
 class TestFormatUnit(unittest.TestCase):
   """Test case for the FormatUnit function"""
-- 
GitLab