From dbc9602840ae001f8048c7e666818b66d4f2b6fe Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 30 Dec 2010 18:22:05 +0100
Subject: [PATCH] Improve tests for OP_ID
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

… by detecting duplicates.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/opcodes.py                  | 15 ++++++++++++---
 test/ganeti.opcodes_unittest.py |  6 ++++++
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/lib/opcodes.py b/lib/opcodes.py
index 44026fdb6..4211c717c 100644
--- a/lib/opcodes.py
+++ b/lib/opcodes.py
@@ -1234,6 +1234,15 @@ class OpTestDummy(OpCode):
     ]
 
 
-OP_MAPPING = dict([(v.OP_ID, v) for v in globals().values()
-                   if (isinstance(v, type) and issubclass(v, OpCode) and
-                       hasattr(v, "OP_ID"))])
+def _GetOpList():
+  """Returns list of all defined opcodes.
+
+  Does not eliminate duplicates by C{OP_ID}.
+
+  """
+  return [v for v in globals().values()
+          if (isinstance(v, type) and issubclass(v, OpCode) and
+              hasattr(v, "OP_ID"))]
+
+
+OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())
diff --git a/test/ganeti.opcodes_unittest.py b/test/ganeti.opcodes_unittest.py
index 5269fc846..6be101dd0 100755
--- a/test/ganeti.opcodes_unittest.py
+++ b/test/ganeti.opcodes_unittest.py
@@ -41,6 +41,7 @@ class TestOpcodes(unittest.TestCase):
 
     for cls in opcodes.OP_MAPPING.values():
       self.assert_(cls.OP_ID.startswith("OP_"))
+      self.assert_(len(cls.OP_ID) > 3)
       self.assertEqual(cls.OP_ID, cls.OP_ID.upper())
 
       self.assertRaises(TypeError, cls, unsupported_parameter="some value")
@@ -84,6 +85,11 @@ class TestOpcodes(unittest.TestCase):
     else:
       self.assertEqual("OP_%s" % summary, op.OP_ID)
 
+  def testOpId(self):
+    self.assertFalse(utils.FindDuplicates(cls.OP_ID
+                                          for cls in opcodes._GetOpList()))
+    self.assertEqual(len(opcodes._GetOpList()), len(opcodes.OP_MAPPING))
+
   def testParams(self):
     supported_by_all = set(["debug_level", "dry_run", "priority"])
 
-- 
GitLab