Add unit tests for CheckAssignmentForSplitInstances
[ganeti-local] / test / ganeti.utils.retry_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2011 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.utils.retry"""
23
24 import unittest
25
26 from ganeti import constants
27 from ganeti import errors
28 from ganeti import utils
29
30 import testutils
31
32
33 class TestRetry(testutils.GanetiTestCase):
34   def setUp(self):
35     testutils.GanetiTestCase.setUp(self)
36     self.retries = 0
37
38   @staticmethod
39   def _RaiseRetryAgain():
40     raise utils.RetryAgain()
41
42   @staticmethod
43   def _RaiseRetryAgainWithArg(args):
44     raise utils.RetryAgain(*args)
45
46   def _WrongNestedLoop(self):
47     return utils.Retry(self._RaiseRetryAgain, 0.01, 0.02)
48
49   def _RetryAndSucceed(self, retries):
50     if self.retries < retries:
51       self.retries += 1
52       raise utils.RetryAgain()
53     else:
54       return True
55
56   def testRaiseTimeout(self):
57     self.failUnlessRaises(utils.RetryTimeout, utils.Retry,
58                           self._RaiseRetryAgain, 0.01, 0.02)
59     self.failUnlessRaises(utils.RetryTimeout, utils.Retry,
60                           self._RetryAndSucceed, 0.01, 0, args=[1])
61     self.failUnlessEqual(self.retries, 1)
62
63   def testComplete(self):
64     self.failUnlessEqual(utils.Retry(lambda: True, 0, 1), True)
65     self.failUnlessEqual(utils.Retry(self._RetryAndSucceed, 0, 1, args=[2]),
66                          True)
67     self.failUnlessEqual(self.retries, 2)
68
69   def testNestedLoop(self):
70     try:
71       self.failUnlessRaises(errors.ProgrammerError, utils.Retry,
72                             self._WrongNestedLoop, 0, 1)
73     except utils.RetryTimeout:
74       self.fail("Didn't detect inner loop's exception")
75
76   def testTimeoutArgument(self):
77     retry_arg="my_important_debugging_message"
78     try:
79       utils.Retry(self._RaiseRetryAgainWithArg, 0.01, 0.02, args=[[retry_arg]])
80     except utils.RetryTimeout, err:
81       self.failUnlessEqual(err.args, (retry_arg, ))
82     else:
83       self.fail("Expected timeout didn't happen")
84
85   def testRaiseInnerWithExc(self):
86     retry_arg="my_important_debugging_message"
87     try:
88       try:
89         utils.Retry(self._RaiseRetryAgainWithArg, 0.01, 0.02,
90                     args=[[errors.GenericError(retry_arg, retry_arg)]])
91       except utils.RetryTimeout, err:
92         err.RaiseInner()
93       else:
94         self.fail("Expected timeout didn't happen")
95     except errors.GenericError, err:
96       self.failUnlessEqual(err.args, (retry_arg, retry_arg))
97     else:
98       self.fail("Expected GenericError didn't happen")
99
100   def testRaiseInnerWithMsg(self):
101     retry_arg="my_important_debugging_message"
102     try:
103       try:
104         utils.Retry(self._RaiseRetryAgainWithArg, 0.01, 0.02,
105                     args=[[retry_arg, retry_arg]])
106       except utils.RetryTimeout, err:
107         err.RaiseInner()
108       else:
109         self.fail("Expected timeout didn't happen")
110     except utils.RetryTimeout, err:
111       self.failUnlessEqual(err.args, (retry_arg, retry_arg))
112     else:
113       self.fail("Expected RetryTimeout didn't happen")
114
115
116 if __name__ == "__main__":
117   testutils.GanetiTestProgram()