constants: Verify exported names
authorMichael Hanselmann <hansmi@google.com>
Thu, 13 Oct 2011 12:36:41 +0000 (14:36 +0200)
committerMichael Hanselmann <hansmi@google.com>
Thu, 13 Oct 2011 14:40:05 +0000 (16:40 +0200)
The “constants” module is a bit special in the sense that we don't want
to export random stuff from it. This unittest checks the naming
convention and removes imported modules from the module's namespace.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

lib/constants.py
test/ganeti.constants_unittest.py

index d30aeb7..79d0b2f 100644 (file)
@@ -1703,3 +1703,6 @@ BLOCKDEV_DRIVER_MANUAL = "manual"
 HTOOLS = _autoconf.HTOOLS
 # The hail iallocator
 IALLOC_HAIL = "hail"
+
+# Do not re-export imported modules
+del re, _vcsversion, _autoconf
index 9aab10f..c0d834c 100755 (executable)
 
 import unittest
 import re
+import itertools
 
 from ganeti import constants
 from ganeti import locking
+from ganeti import utils
 
 import testutils
 
@@ -78,6 +80,25 @@ class TestConstants(unittest.TestCase):
     self.failUnless(constants.OP_PRIO_HIGH > constants.OP_PRIO_HIGHEST)
 
 
+class TestExportedNames(unittest.TestCase):
+  _VALID_NAME_RE = re.compile(r"^[A-Z][A-Z0-9_]+$")
+  _BUILTIN_NAME_RE = re.compile(r"^__\w+__$")
+  _EXCEPTIONS = frozenset([
+    "SplitVersion",
+    "BuildVersion",
+    ])
+
+  def test(self):
+    wrong = \
+      set(itertools.ifilterfalse(self._BUILTIN_NAME_RE.match,
+            itertools.ifilterfalse(self._VALID_NAME_RE.match,
+                                   dir(constants))))
+    wrong -= self._EXCEPTIONS
+    self.assertFalse(wrong,
+                     msg=("Invalid names exported from constants module: %s" %
+                          utils.CommaJoin(sorted(wrong))))
+
+
 class TestParameterNames(unittest.TestCase):
   """HV/BE parameter tests"""
   VALID_NAME = re.compile("^[a-zA-Z_][a-zA-Z0-9_]*$")