4 # Copyright (C) 2010, 2012 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Script for testing ganeti.backend"""
28 from ganeti import errors
33 class TestErrors(testutils.GanetiTestCase):
34 def testGetErrorClass(self):
40 "NoErrorClassName": None,
41 "GenericError": errors.GenericError,
42 "ProgrammerError": errors.ProgrammerError,
45 for name, cls in tdata.items():
46 self.assert_(errors.GetErrorClass(name) is cls)
48 def testEncodeException(self):
49 self.assertEqualValues(errors.EncodeException(Exception("Foobar")),
50 ("Exception", ("Foobar", )))
51 err = errors.GenericError(True, 100, "foo", ["x", "y"])
52 self.assertEqualValues(errors.EncodeException(err),
53 ("GenericError", (True, 100, "foo", ["x", "y"])))
55 def testMaybeRaise(self):
56 testvals = [None, 1, 2, 3, "Hello World", (1, ), (1, 2, 3),
57 ("NoErrorClassName", []), ("NoErrorClassName", None),
58 ("GenericError", [1, 2, 3], None), ("GenericError", 1)]
59 # These shouldn't raise
63 self.assertRaises(errors.GenericError, errors.MaybeRaise,
64 ("GenericError", ["Hello"]))
65 # Check error encoding
67 src = errors.GenericError(i)
69 errors.MaybeRaise(errors.EncodeException(src))
70 except errors.GenericError, dst:
71 self.assertEqual(src.args, dst.args)
72 self.assertEqual(src.__class__, dst.__class__)
74 self.fail("Exception %s not raised" % repr(src))
76 def testGetEncodedError(self):
77 self.assertEqualValues(errors.GetEncodedError(["GenericError",
78 ("Hello", 123, "World")]),
79 (errors.GenericError, ("Hello", 123, "World")))
80 self.assertEqualValues(errors.GetEncodedError(["GenericError", []]),
81 (errors.GenericError, ()))
82 self.assertFalse(errors.GetEncodedError(["NoErrorClass",
83 ("Hello", 123, "World")]))
86 if __name__ == "__main__":
87 testutils.GanetiTestProgram()