From: Michael Hanselmann Date: Thu, 13 Oct 2011 12:36:41 +0000 (+0200) Subject: constants: Verify exported names X-Git-Tag: v2.6.0beta1~806 X-Git-Url: https://code.grnet.gr/git/ganeti-local/commitdiff_plain/b8d51bb2b3b92caf8d6937a87e5863822876947f constants: Verify exported names 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 Reviewed-by: Iustin Pop --- diff --git a/lib/constants.py b/lib/constants.py index d30aeb7..79d0b2f 100644 --- a/lib/constants.py +++ b/lib/constants.py @@ -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 diff --git a/test/ganeti.constants_unittest.py b/test/ganeti.constants_unittest.py index 9aab10f..c0d834c 100755 --- a/test/ganeti.constants_unittest.py +++ b/test/ganeti.constants_unittest.py @@ -24,9 +24,11 @@ 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_]*$")