From 687c10d90dd630cda0b8ae6092fe92326fbfaf8b Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Mon, 10 Jan 2011 11:51:35 +0100
Subject: [PATCH] Generalize the OpCode-should-be-in-mcpu test

Currently, the unittest TestDispatchTable in mcpu unittest does a
hard-coded approach to test whether an opcode should be included or
not in the mcpu.Processor dispatch table. This is not flexible, so we
replace it with two changes:

- first, we do not return the base OpCode in opcodes.OP_MAPPING;
  rationale being that we shouldn't need to serialise or deserialise
  this opcode during normal operation
- second, we add a new attribute WITH_LU (other name suggestions
  welcome) to the base opcode set to True, and we explicitly set it to
  False in OpTestDummy, thus automatic skipping of such LUs in the
  unittest (and in other places)

To correct for the new behaviour, the tests in the opcode unittests
are updated to include OpCode explicitly.

Note: we also replace assert_ with assertTrue in the affected unittest
(as assert_ is deprecated in 2.7).

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

diff --git a/lib/opcodes.py b/lib/opcodes.py
index e15716625..4512d7b12 100644
--- a/lib/opcodes.py
+++ b/lib/opcodes.py
@@ -1,7 +1,7 @@
 #
 #
 
-# Copyright (C) 2006, 2007, 2008, 2009, 2010 Google Inc.
+# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -289,12 +289,15 @@ class OpCode(BaseOpCode):
                       method for details).
   @cvar OP_PARAMS: List of opcode attributes, the default values they should
                    get if not already defined, and types they must match.
+  @cvar WITH_LU: Boolean that specifies whether this should be included in
+      mcpu's dispatch table
   @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
                  the check steps
   @ivar priority: Opcode priority for queue
 
   """
   OP_ID = "OP_ABSTRACT"
+  WITH_LU = True
   OP_PARAMS = [
     ("dry_run", None, ht.TMaybeBool),
     ("debug_level", None, ht.TOr(ht.TNone, ht.TPositiveInt)),
@@ -1271,6 +1274,7 @@ class OpTestDummy(OpCode):
     ("messages", ht.NoDefault, ht.NoType),
     ("fail", ht.NoDefault, ht.NoType),
     ]
+  WITH_LU = False
 
 
 def _GetOpList():
@@ -1281,7 +1285,7 @@ def _GetOpList():
   """
   return [v for v in globals().values()
           if (isinstance(v, type) and issubclass(v, OpCode) and
-              hasattr(v, "OP_ID"))]
+              hasattr(v, "OP_ID") and v is not OpCode)]
 
 
 OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())
diff --git a/test/ganeti.mcpu_unittest.py b/test/ganeti.mcpu_unittest.py
index 9f5687068..aeca84d05 100755
--- a/test/ganeti.mcpu_unittest.py
+++ b/test/ganeti.mcpu_unittest.py
@@ -1,7 +1,7 @@
 #!/usr/bin/python
 #
 
-# Copyright (C) 2009 Google Inc.
+# Copyright (C) 2009, 2011 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -58,10 +58,10 @@ class TestLockAttemptTimeoutStrategy(unittest.TestCase):
 class TestDispatchTable(unittest.TestCase):
   def test(self):
     for opcls in opcodes.OP_MAPPING.values():
-      if opcls is opcodes.OpCode or opcls is opcodes.OpTestDummy:
+      if not opcls.WITH_LU:
         continue
-      self.assert_(opcls in mcpu.Processor.DISPATCH_TABLE,
-                   msg="%s missing handler class" % opcls)
+      self.assertTrue(opcls in mcpu.Processor.DISPATCH_TABLE,
+                      msg="%s missing handler class" % opcls)
 
 
 if __name__ == "__main__":
diff --git a/test/ganeti.opcodes_unittest.py b/test/ganeti.opcodes_unittest.py
index ff52381f9..bb77ca211 100755
--- a/test/ganeti.opcodes_unittest.py
+++ b/test/ganeti.opcodes_unittest.py
@@ -1,7 +1,7 @@
 #!/usr/bin/python
 #
 
-# Copyright (C) 2010 Google Inc.
+# Copyright (C) 2010, 2011 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -123,10 +123,10 @@ class TestOpcodes(unittest.TestCase):
   def testParams(self):
     supported_by_all = set(["debug_level", "dry_run", "priority"])
 
-    self.assert_(opcodes.BaseOpCode not in opcodes.OP_MAPPING.values())
-    self.assert_(opcodes.OpCode in opcodes.OP_MAPPING.values())
+    self.assertTrue(opcodes.BaseOpCode not in opcodes.OP_MAPPING.values())
+    self.assertTrue(opcodes.OpCode not in opcodes.OP_MAPPING.values())
 
-    for cls in opcodes.OP_MAPPING.values():
+    for cls in opcodes.OP_MAPPING.values() + [opcodes.OpCode]:
       all_slots = cls._all_slots()
 
       self.assertEqual(len(set(all_slots) & supported_by_all), 3,
-- 
GitLab