root / test / ganeti.mcpu_unittest.py @ 68959ca5
History | View | Annotate | Download (2.1 kB)
1 | 407339d0 | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | 407339d0 | Michael Hanselmann | #
|
3 | 407339d0 | Michael Hanselmann | |
4 | 687c10d9 | Iustin Pop | # Copyright (C) 2009, 2011 Google Inc.
|
5 | 407339d0 | Michael Hanselmann | #
|
6 | 407339d0 | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | 407339d0 | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | 407339d0 | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | 407339d0 | Michael Hanselmann | # (at your option) any later version.
|
10 | 407339d0 | Michael Hanselmann | #
|
11 | 407339d0 | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | 407339d0 | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 407339d0 | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 407339d0 | Michael Hanselmann | # General Public License for more details.
|
15 | 407339d0 | Michael Hanselmann | #
|
16 | 407339d0 | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | 407339d0 | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | 407339d0 | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 407339d0 | Michael Hanselmann | # 02110-1301, USA.
|
20 | 407339d0 | Michael Hanselmann | |
21 | 407339d0 | Michael Hanselmann | |
22 | 407339d0 | Michael Hanselmann | """Script for unittesting the mcpu module"""
|
23 | 407339d0 | Michael Hanselmann | |
24 | 407339d0 | Michael Hanselmann | |
25 | 407339d0 | Michael Hanselmann | import unittest |
26 | 407339d0 | Michael Hanselmann | |
27 | 407339d0 | Michael Hanselmann | from ganeti import mcpu |
28 | 0a31dda0 | Michael Hanselmann | from ganeti import opcodes |
29 | d385a174 | Iustin Pop | from ganeti.constants import \ |
30 | d385a174 | Iustin Pop | LOCK_ATTEMPTS_TIMEOUT, \
|
31 | d385a174 | Iustin Pop | LOCK_ATTEMPTS_MAXWAIT, \
|
32 | d385a174 | Iustin Pop | LOCK_ATTEMPTS_MINWAIT
|
33 | 407339d0 | Michael Hanselmann | |
34 | 25231ec5 | Michael Hanselmann | import testutils |
35 | 25231ec5 | Michael Hanselmann | |
36 | 407339d0 | Michael Hanselmann | |
37 | e3200b18 | Michael Hanselmann | class TestLockAttemptTimeoutStrategy(unittest.TestCase): |
38 | 407339d0 | Michael Hanselmann | def testConstants(self): |
39 | a7770f03 | Michael Hanselmann | tpa = mcpu.LockAttemptTimeoutStrategy._TIMEOUT_PER_ATTEMPT |
40 | d385a174 | Iustin Pop | self.assert_(len(tpa) > LOCK_ATTEMPTS_TIMEOUT / LOCK_ATTEMPTS_MAXWAIT) |
41 | d385a174 | Iustin Pop | self.assert_(sum(tpa) >= LOCK_ATTEMPTS_TIMEOUT) |
42 | 407339d0 | Michael Hanselmann | |
43 | 407339d0 | Michael Hanselmann | def testSimple(self): |
44 | a7770f03 | Michael Hanselmann | strat = mcpu.LockAttemptTimeoutStrategy(_random_fn=lambda: 0.5, |
45 | a7770f03 | Michael Hanselmann | _time_fn=lambda: 0.0) |
46 | 407339d0 | Michael Hanselmann | |
47 | 407339d0 | Michael Hanselmann | prev = None
|
48 | a7770f03 | Michael Hanselmann | for i in range(len(strat._TIMEOUT_PER_ATTEMPT)): |
49 | a7770f03 | Michael Hanselmann | timeout = strat.NextAttempt() |
50 | 407339d0 | Michael Hanselmann | self.assert_(timeout is not None) |
51 | 407339d0 | Michael Hanselmann | |
52 | d385a174 | Iustin Pop | self.assert_(timeout <= LOCK_ATTEMPTS_MAXWAIT)
|
53 | d385a174 | Iustin Pop | self.assert_(timeout >= LOCK_ATTEMPTS_MINWAIT)
|
54 | 407339d0 | Michael Hanselmann | self.assert_(prev is None or timeout >= prev) |
55 | 407339d0 | Michael Hanselmann | |
56 | 407339d0 | Michael Hanselmann | prev = timeout |
57 | 407339d0 | Michael Hanselmann | |
58 | a6db1af2 | Michael Hanselmann | for _ in range(10): |
59 | a7770f03 | Michael Hanselmann | self.assert_(strat.NextAttempt() is None) |
60 | 407339d0 | Michael Hanselmann | |
61 | 407339d0 | Michael Hanselmann | |
62 | 0a31dda0 | Michael Hanselmann | class TestDispatchTable(unittest.TestCase): |
63 | 0a31dda0 | Michael Hanselmann | def test(self): |
64 | 0a31dda0 | Michael Hanselmann | for opcls in opcodes.OP_MAPPING.values(): |
65 | 687c10d9 | Iustin Pop | if not opcls.WITH_LU: |
66 | 0a31dda0 | Michael Hanselmann | continue
|
67 | 687c10d9 | Iustin Pop | self.assertTrue(opcls in mcpu.Processor.DISPATCH_TABLE, |
68 | 687c10d9 | Iustin Pop | msg="%s missing handler class" % opcls)
|
69 | 0a31dda0 | Michael Hanselmann | |
70 | 0a31dda0 | Michael Hanselmann | |
71 | 407339d0 | Michael Hanselmann | if __name__ == "__main__": |
72 | 25231ec5 | Michael Hanselmann | testutils.GanetiTestProgram() |