Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.mcpu_unittest.py @ 3b877f08

History | View | Annotate | Download (1.9 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 407339d0 Michael Hanselmann
30 25231ec5 Michael Hanselmann
import testutils
31 25231ec5 Michael Hanselmann
32 407339d0 Michael Hanselmann
33 e3200b18 Michael Hanselmann
class TestLockAttemptTimeoutStrategy(unittest.TestCase):
34 407339d0 Michael Hanselmann
  def testConstants(self):
35 a7770f03 Michael Hanselmann
    tpa = mcpu.LockAttemptTimeoutStrategy._TIMEOUT_PER_ATTEMPT
36 e3200b18 Michael Hanselmann
    self.assert_(len(tpa) > 10)
37 e3200b18 Michael Hanselmann
    self.assert_(sum(tpa) >= 150.0)
38 407339d0 Michael Hanselmann
39 407339d0 Michael Hanselmann
  def testSimple(self):
40 a7770f03 Michael Hanselmann
    strat = mcpu.LockAttemptTimeoutStrategy(_random_fn=lambda: 0.5,
41 a7770f03 Michael Hanselmann
                                            _time_fn=lambda: 0.0)
42 407339d0 Michael Hanselmann
43 407339d0 Michael Hanselmann
    prev = None
44 a7770f03 Michael Hanselmann
    for i in range(len(strat._TIMEOUT_PER_ATTEMPT)):
45 a7770f03 Michael Hanselmann
      timeout = strat.NextAttempt()
46 407339d0 Michael Hanselmann
      self.assert_(timeout is not None)
47 407339d0 Michael Hanselmann
48 407339d0 Michael Hanselmann
      self.assert_(timeout <= 10.0)
49 a6db1af2 Michael Hanselmann
      self.assert_(timeout >= 0.0)
50 407339d0 Michael Hanselmann
      self.assert_(prev is None or timeout >= prev)
51 407339d0 Michael Hanselmann
52 407339d0 Michael Hanselmann
      prev = timeout
53 407339d0 Michael Hanselmann
54 a6db1af2 Michael Hanselmann
    for _ in range(10):
55 a7770f03 Michael Hanselmann
      self.assert_(strat.NextAttempt() is None)
56 407339d0 Michael Hanselmann
57 407339d0 Michael Hanselmann
58 0a31dda0 Michael Hanselmann
class TestDispatchTable(unittest.TestCase):
59 0a31dda0 Michael Hanselmann
  def test(self):
60 0a31dda0 Michael Hanselmann
    for opcls in opcodes.OP_MAPPING.values():
61 687c10d9 Iustin Pop
      if not opcls.WITH_LU:
62 0a31dda0 Michael Hanselmann
        continue
63 687c10d9 Iustin Pop
      self.assertTrue(opcls in mcpu.Processor.DISPATCH_TABLE,
64 687c10d9 Iustin Pop
                      msg="%s missing handler class" % opcls)
65 0a31dda0 Michael Hanselmann
66 0a31dda0 Michael Hanselmann
67 407339d0 Michael Hanselmann
if __name__ == "__main__":
68 25231ec5 Michael Hanselmann
  testutils.GanetiTestProgram()