ganeti.mcpu_unittest.py 1.96 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/python
#

# Copyright (C) 2009 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""Script for unittesting the mcpu module"""


import unittest

from ganeti import mcpu
28
from ganeti import opcodes
29

30
31
import testutils

32

33
class TestLockAttemptTimeoutStrategy(unittest.TestCase):
34
  def testConstants(self):
35
    tpa = mcpu.LockAttemptTimeoutStrategy._TIMEOUT_PER_ATTEMPT
36
37
    self.assert_(len(tpa) > 10)
    self.assert_(sum(tpa) >= 150.0)
38
39

  def testSimple(self):
40
41
    strat = mcpu.LockAttemptTimeoutStrategy(_random_fn=lambda: 0.5,
                                            _time_fn=lambda: 0.0)
42
43

    prev = None
44
45
    for i in range(len(strat._TIMEOUT_PER_ATTEMPT)):
      timeout = strat.NextAttempt()
46
47
48
      self.assert_(timeout is not None)

      self.assert_(timeout <= 10.0)
49
      self.assert_(timeout >= 0.0)
50
51
52
53
      self.assert_(prev is None or timeout >= prev)

      prev = timeout

54
    for _ in range(10):
55
      self.assert_(strat.NextAttempt() is None)
56
57


58
59
60
61
62
63
64
65
66
class TestDispatchTable(unittest.TestCase):
  def test(self):
    for opcls in opcodes.OP_MAPPING.values():
      if opcls is opcodes.OpCode or opcls is opcodes.OpTestDummy:
        continue
      self.assert_(opcls in mcpu.Processor.DISPATCH_TABLE,
                   msg="%s missing handler class" % opcls)


67
if __name__ == "__main__":
68
  testutils.GanetiTestProgram()