root / test / py / ganeti.errors_unittest.py @ d01e51a5
History | View | Annotate | Download (2.9 kB)
1 | 3bd19c66 | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | 3bd19c66 | Michael Hanselmann | #
|
3 | 3bd19c66 | Michael Hanselmann | |
4 | 98dfcaff | Iustin Pop | # Copyright (C) 2010, 2012 Google Inc.
|
5 | 3bd19c66 | Michael Hanselmann | #
|
6 | 3bd19c66 | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | 3bd19c66 | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | 3bd19c66 | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | 3bd19c66 | Michael Hanselmann | # (at your option) any later version.
|
10 | 3bd19c66 | Michael Hanselmann | #
|
11 | 3bd19c66 | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | 3bd19c66 | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 3bd19c66 | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 3bd19c66 | Michael Hanselmann | # General Public License for more details.
|
15 | 3bd19c66 | Michael Hanselmann | #
|
16 | 3bd19c66 | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | 3bd19c66 | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | 3bd19c66 | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 3bd19c66 | Michael Hanselmann | # 02110-1301, USA.
|
20 | 3bd19c66 | Michael Hanselmann | |
21 | 3bd19c66 | Michael Hanselmann | |
22 | 3bd19c66 | Michael Hanselmann | """Script for testing ganeti.backend"""
|
23 | 3bd19c66 | Michael Hanselmann | |
24 | 3bd19c66 | Michael Hanselmann | import os |
25 | 3bd19c66 | Michael Hanselmann | import sys |
26 | 3bd19c66 | Michael Hanselmann | import unittest |
27 | 3bd19c66 | Michael Hanselmann | |
28 | 3bd19c66 | Michael Hanselmann | from ganeti import errors |
29 | 3bd19c66 | Michael Hanselmann | |
30 | 3bd19c66 | Michael Hanselmann | import testutils |
31 | 3bd19c66 | Michael Hanselmann | |
32 | 3bd19c66 | Michael Hanselmann | |
33 | 3bd19c66 | Michael Hanselmann | class TestErrors(testutils.GanetiTestCase): |
34 | 3bd19c66 | Michael Hanselmann | def testGetErrorClass(self): |
35 | 3bd19c66 | Michael Hanselmann | tdata = { |
36 | 3bd19c66 | Michael Hanselmann | "": None, |
37 | 3bd19c66 | Michael Hanselmann | ".": None, |
38 | 3bd19c66 | Michael Hanselmann | "-": None, |
39 | 3bd19c66 | Michael Hanselmann | "ECODE_INVAL": None, |
40 | 3bd19c66 | Michael Hanselmann | "NoErrorClassName": None, |
41 | 3bd19c66 | Michael Hanselmann | "GenericError": errors.GenericError,
|
42 | 3bd19c66 | Michael Hanselmann | "ProgrammerError": errors.ProgrammerError,
|
43 | 3bd19c66 | Michael Hanselmann | } |
44 | 3bd19c66 | Michael Hanselmann | |
45 | 3bd19c66 | Michael Hanselmann | for name, cls in tdata.items(): |
46 | 3bd19c66 | Michael Hanselmann | self.assert_(errors.GetErrorClass(name) is cls) |
47 | 3bd19c66 | Michael Hanselmann | |
48 | 3bd19c66 | Michael Hanselmann | def testEncodeException(self): |
49 | 3bd19c66 | Michael Hanselmann | self.assertEqualValues(errors.EncodeException(Exception("Foobar")), |
50 | 3bd19c66 | Michael Hanselmann | ("Exception", ("Foobar", ))) |
51 | 3bd19c66 | Michael Hanselmann | err = errors.GenericError(True, 100, "foo", ["x", "y"]) |
52 | 3bd19c66 | Michael Hanselmann | self.assertEqualValues(errors.EncodeException(err),
|
53 | 3bd19c66 | Michael Hanselmann | ("GenericError", (True, 100, "foo", ["x", "y"]))) |
54 | 3bd19c66 | Michael Hanselmann | |
55 | 3bd19c66 | Michael Hanselmann | def testMaybeRaise(self): |
56 | 98dfcaff | Iustin Pop | testvals = [None, 1, 2, 3, "Hello World", (1, ), (1, 2, 3), |
57 | 98dfcaff | Iustin Pop | ("NoErrorClassName", []), ("NoErrorClassName", None), |
58 | 98dfcaff | Iustin Pop | ("GenericError", [1, 2, 3], None), ("GenericError", 1)] |
59 | 3bd19c66 | Michael Hanselmann | # These shouldn't raise
|
60 | 98dfcaff | Iustin Pop | for i in testvals: |
61 | 3bd19c66 | Michael Hanselmann | errors.MaybeRaise(i) |
62 | 3bd19c66 | Michael Hanselmann | |
63 | 3bd19c66 | Michael Hanselmann | self.assertRaises(errors.GenericError, errors.MaybeRaise,
|
64 | 3bd19c66 | Michael Hanselmann | ("GenericError", ["Hello"])) |
65 | 98dfcaff | Iustin Pop | # Check error encoding
|
66 | 98dfcaff | Iustin Pop | for i in testvals: |
67 | 98dfcaff | Iustin Pop | src = errors.GenericError(i) |
68 | 98dfcaff | Iustin Pop | try:
|
69 | 98dfcaff | Iustin Pop | errors.MaybeRaise(errors.EncodeException(src)) |
70 | 98dfcaff | Iustin Pop | except errors.GenericError, dst:
|
71 | 98dfcaff | Iustin Pop | self.assertEqual(src.args, dst.args)
|
72 | 98dfcaff | Iustin Pop | self.assertEqual(src.__class__, dst.__class__)
|
73 | 98dfcaff | Iustin Pop | else:
|
74 | 98dfcaff | Iustin Pop | self.fail("Exception %s not raised" % repr(src)) |
75 | 3bd19c66 | Michael Hanselmann | |
76 | 84f790e6 | Michael Hanselmann | def testGetEncodedError(self): |
77 | 84f790e6 | Michael Hanselmann | self.assertEqualValues(errors.GetEncodedError(["GenericError", |
78 | 84f790e6 | Michael Hanselmann | ("Hello", 123, "World")]), |
79 | 84f790e6 | Michael Hanselmann | (errors.GenericError, ("Hello", 123, "World"))) |
80 | 84f790e6 | Michael Hanselmann | self.assertEqualValues(errors.GetEncodedError(["GenericError", []]), |
81 | 84f790e6 | Michael Hanselmann | (errors.GenericError, ())) |
82 | 84f790e6 | Michael Hanselmann | self.assertFalse(errors.GetEncodedError(["NoErrorClass", |
83 | 84f790e6 | Michael Hanselmann | ("Hello", 123, "World")])) |
84 | 84f790e6 | Michael Hanselmann | |
85 | 3bd19c66 | Michael Hanselmann | |
86 | 3bd19c66 | Michael Hanselmann | if __name__ == "__main__": |
87 | 3bd19c66 | Michael Hanselmann | testutils.GanetiTestProgram() |