verify-disks: Explicitely state nothing has to be done
[ganeti-local] / test / ganeti.errors_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2010, 2012 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 """Script for testing ganeti.backend"""
23
24 import os
25 import sys
26 import unittest
27
28 from ganeti import errors
29
30 import testutils
31
32
33 class TestErrors(testutils.GanetiTestCase):
34   def testGetErrorClass(self):
35     tdata = {
36       "": None,
37       ".": None,
38       "-": None,
39       "ECODE_INVAL": None,
40       "NoErrorClassName": None,
41       "GenericError": errors.GenericError,
42       "ProgrammerError": errors.ProgrammerError,
43       }
44
45     for name, cls in tdata.items():
46       self.assert_(errors.GetErrorClass(name) is cls)
47
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"])))
54
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
60     for i in testvals:
61       errors.MaybeRaise(i)
62
63     self.assertRaises(errors.GenericError, errors.MaybeRaise,
64                       ("GenericError", ["Hello"]))
65     # Check error encoding
66     for i in testvals:
67       src = errors.GenericError(i)
68       try:
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__)
73       else:
74         self.fail("Exception %s not raised" % repr(src))
75
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")]))
84
85
86 if __name__ == "__main__":
87   testutils.GanetiTestProgram()