From b8d51bb2b3b92caf8d6937a87e5863822876947f Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 13 Oct 2011 14:36:41 +0200
Subject: [PATCH] constants: Verify exported names
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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                  |  3 +++
 test/ganeti.constants_unittest.py | 21 +++++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/lib/constants.py b/lib/constants.py
index d30aeb7d9..79d0b2fed 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 9aab10fa1..c0d834cce 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_]*$")
-- 
GitLab