While ‘test1’ matches both ‘test1’ and ‘test1.example’, it has a full,
exact match and we should return it if that is the case.
---
Hmm, maybe commit to next instead?
lib/utils.py | 5 ++++-
test/ganeti.utils_unittest.py | 8 ++++++++
2 files changed, 12 insertions(+), 1 deletions(-)
diff --git a/lib/utils.py b/lib/utils.py
index 80e0d9a..c58c2cd 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 2893b8a..1e8761d 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"""
--
1.6.3.3