Added IP4_RE and IP6_RE to ganeti.constants and the relative tests.
Signed-off-by: Andrea Spadaccini <[email protected]>
---
lib/constants.py | 9 ++++++++
test/ganeti.constants_unittest.py | 39 +++++++++++++++++++++++++++++++++++++
2 files changed, 48 insertions(+), 0 deletions(-)
diff --git a/lib/constants.py b/lib/constants.py
index c0b8c3b..8b55677 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -554,6 +554,15 @@ IP6_ADDRESS_LOCALHOST = "::1"
IP6_ADDRESS_ANY = "::"
IP4_VERSION = 4
IP6_VERSION = 6
+VALID_IP_VERSIONS = frozenset([IP4_VERSION, IP6_VERSION])
+
+# Regexes used to find IP addresses
+IP4_RE_TEXT = "([0-9]{1,3}\.){3}[0-9]{1,3}"
+IP4_RE = re.compile(IP4_RE_TEXT)
+IP6_RE = re.compile("([a-f0-9]{0,4}:){2,6}%s|" # IPv6-embedded IPv4 address
+ "([a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}" # IPv6 address
+ % IP4_RE_TEXT, re.IGNORECASE)
+
TCP_PING_TIMEOUT = 10
GANETI_RUNAS = "root"
DEFAULT_VG = "xenvg"
diff --git a/test/ganeti.constants_unittest.py
b/test/ganeti.constants_unittest.py
index 9aab10f..637e8da 100755
--- a/test/ganeti.constants_unittest.py
+++ b/test/ganeti.constants_unittest.py
@@ -95,6 +95,45 @@ class TestParameterNames(unittest.TestCase):
(kind, key))
+class TestIpRegex(unittest.TestCase):
+ """Test the IPv4 and IPv6 address search regular expressions"""
+
+ def _checkMatch(self, match, expected):
+ return match and match.group() == expected
+
+ def testIp4(self):
+ valid_addresses = [constants.IP4_ADDRESS_ANY,
+ constants.IP4_ADDRESS_LOCALHOST,
+ "4.4.4.4",
+ "192.168.0.1",
+ ]
+ for addr in valid_addresses:
+ self.failUnless(self._checkMatch(constants.IP4_RE.search(addr), addr))
+
+ def testIp6(self):
+ valid_addresses = [constants.IP6_ADDRESS_ANY,
+ constants.IP6_ADDRESS_LOCALHOST,
+ "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
+ "2001:db8:85a3:0:0:8a2e:370:7334",
+ "0:0:0:0:0:0:0:1",
+ "0:0:0:0:0:0:0:0",
+ "::ffff:c000:280",
+ "0:0:0:0:0:FFFF:222.1.41.90", # IPv4-compatible IPv6
+ "::FFFF:222.1.41.90",
+ "0:0:0:0:0:0:101.45.75.219", # IPv4-mapped IPv6
+ "::101.45.75.219",
+ ]
+ for addr in valid_addresses:
+ self.failUnless(self._checkMatch(constants.IP6_RE.search(addr), addr))
+
+ # Check that IPv6 regex does not validate IPv4 addresses (should be
+ # checked due to the possibility to embed IPv4 addresses in IPv6.
+ invalid_addresses = ["127.0.0.1",
+ ]
+ for addr in invalid_addresses:
+ self.failIf(constants.IP6_RE.search(addr))
+
+
class TestConfdConstants(unittest.TestCase):
"""Test the confd constants"""
--
1.7.3.1