From 3bd19c6670c2413aafcbb3adaa3a813177bddff3 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Wed, 10 Mar 2010 17:59:54 +0100
Subject: [PATCH] Add unittests for ganeti.errors

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

diff --git a/Makefile.am b/Makefile.am
index 3ad319b20..64b15e631 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -325,6 +325,7 @@ dist_TESTS = \
 	test/ganeti.confd_client_unittest.py \
 	test/ganeti.config_unittest.py \
 	test/ganeti.constants_unittest.py \
+	test/ganeti.errors_unittest.py \
 	test/ganeti.hooks_unittest.py \
 	test/ganeti.http_unittest.py \
 	test/ganeti.locking_unittest.py \
diff --git a/test/ganeti.errors_unittest.py b/test/ganeti.errors_unittest.py
new file mode 100755
index 000000000..d5a37b646
--- /dev/null
+++ b/test/ganeti.errors_unittest.py
@@ -0,0 +1,67 @@
+#!/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 errors
+
+import testutils
+
+
+class TestErrors(testutils.GanetiTestCase):
+  def testGetErrorClass(self):
+    tdata = {
+      "": None,
+      ".": None,
+      "-": None,
+      "ECODE_INVAL": None,
+      "NoErrorClassName": None,
+      "GenericError": errors.GenericError,
+      "ProgrammerError": errors.ProgrammerError,
+      }
+
+    for name, cls in tdata.items():
+      self.assert_(errors.GetErrorClass(name) is cls)
+
+  def testEncodeException(self):
+    self.assertEqualValues(errors.EncodeException(Exception("Foobar")),
+                           ("Exception", ("Foobar", )))
+    err = errors.GenericError(True, 100, "foo", ["x", "y"])
+    self.assertEqualValues(errors.EncodeException(err),
+                           ("GenericError", (True, 100, "foo", ["x", "y"])))
+
+  def testMaybeRaise(self):
+    # These shouldn't raise
+    for i in [None, 1, 2, 3, "Hello World", (1, ), (1, 2, 3),
+              ("NoErrorClassName", []), ("NoErrorClassName", None),
+              ("GenericError", [1, 2, 3], None), ("GenericError", 1)]:
+      errors.MaybeRaise(i)
+
+    self.assertRaises(errors.GenericError, errors.MaybeRaise,
+                      ("GenericError", ["Hello"]))
+
+
+if __name__ == "__main__":
+  testutils.GanetiTestProgram()
-- 
GitLab