From c32b908e03365b62162273c14e55a72037957f76 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Fri, 19 Feb 2010 17:55:52 +0100
Subject: [PATCH] Add unittests for ganeti.opcodes
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

According to β€œcoverage”, this covers 99% of the code.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 Makefile.am                     |  1 +
 test/ganeti.opcodes_unittest.py | 83 +++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+)
 create mode 100755 test/ganeti.opcodes_unittest.py

diff --git a/Makefile.am b/Makefile.am
index a0d87f7dc..3066d3742 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -327,6 +327,7 @@ dist_TESTS = \
 	test/ganeti.locking_unittest.py \
 	test/ganeti.mcpu_unittest.py \
 	test/ganeti.objects_unittest.py \
+	test/ganeti.opcodes_unittest.py \
 	test/ganeti.rapi.resources_unittest.py \
 	test/ganeti.serializer_unittest.py \
 	test/ganeti.ssh_unittest.py \
diff --git a/test/ganeti.opcodes_unittest.py b/test/ganeti.opcodes_unittest.py
new file mode 100755
index 000000000..6554f794b
--- /dev/null
+++ b/test/ganeti.opcodes_unittest.py
@@ -0,0 +1,83 @@
+#!/usr/bin/python
+#
+
+# Copyright (C) 2010 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
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+
+"""Script for testing ganeti.backend"""
+
+import os
+import sys
+import unittest
+
+from ganeti import utils
+from ganeti import opcodes
+
+import testutils
+
+
+class TestOpcodes(unittest.TestCase):
+  def test(self):
+    self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, None)
+    self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, "")
+    self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, {})
+    self.assertRaises(ValueError, opcodes.OpCode.LoadOpCode, {"OP_ID": ""})
+
+    for cls in opcodes.OP_MAPPING.values():
+      self.assert_(cls.OP_ID.startswith("OP_"))
+      self.assertEqual(cls.OP_ID, cls.OP_ID.upper())
+
+      self.assertRaises(TypeError, cls, unsupported_parameter="some value")
+
+      args = [
+        # No variables
+        {},
+
+        # Variables supported by all opcodes
+        {"dry_run": False, "debug_level": 0, },
+
+        # All variables
+        dict([(name, False) for name in cls._all_slots()])
+        ]
+
+      for i in args:
+        op = cls(**i)
+
+        self.assertEqual(op.OP_ID, cls.OP_ID)
+        self._checkSummary(op)
+
+        # Try a restore
+        state = op.__getstate__()
+        self.assert_(isinstance(state, dict))
+
+        restored = opcodes.OpCode.LoadOpCode(state)
+        self.assert_(isinstance(restored, cls))
+        self._checkSummary(restored)
+
+  def _checkSummary(self, op):
+    summary = op.Summary()
+
+    if hasattr(op, "OP_DSC_FIELD"):
+      self.assert_(("OP_%s" % summary).startswith("%s(" % op.OP_ID))
+      self.assert_(summary.endswith(")"))
+    else:
+      self.assertEqual("OP_%s" % summary, op.OP_ID)
+
+
+if __name__ == "__main__":
+  testutils.GanetiTestProgram()
-- 
GitLab