root / test / ganeti.mcpu_unittest.py @ 1a2eb2dc
History | View | Annotate | Download (5.2 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 | 07923a3c | Michael Hanselmann | import itertools |
27 | 407339d0 | Michael Hanselmann | |
28 | 407339d0 | Michael Hanselmann | from ganeti import mcpu |
29 | 0a31dda0 | Michael Hanselmann | from ganeti import opcodes |
30 | 07923a3c | Michael Hanselmann | from ganeti import cmdlib |
31 | 07923a3c | Michael Hanselmann | from ganeti import constants |
32 | d385a174 | Iustin Pop | from ganeti.constants import \ |
33 | d385a174 | Iustin Pop | LOCK_ATTEMPTS_TIMEOUT, \
|
34 | d385a174 | Iustin Pop | LOCK_ATTEMPTS_MAXWAIT, \
|
35 | d385a174 | Iustin Pop | LOCK_ATTEMPTS_MINWAIT
|
36 | 407339d0 | Michael Hanselmann | |
37 | 25231ec5 | Michael Hanselmann | import testutils |
38 | 25231ec5 | Michael Hanselmann | |
39 | 407339d0 | Michael Hanselmann | |
40 | c9c33a28 | Michael Hanselmann | REQ_BGL_WHITELIST = frozenset([
|
41 | c9c33a28 | Michael Hanselmann | opcodes.OpClusterActivateMasterIp, |
42 | c9c33a28 | Michael Hanselmann | opcodes.OpClusterDeactivateMasterIp, |
43 | c9c33a28 | Michael Hanselmann | opcodes.OpClusterDestroy, |
44 | c9c33a28 | Michael Hanselmann | opcodes.OpClusterPostInit, |
45 | c9c33a28 | Michael Hanselmann | opcodes.OpClusterRename, |
46 | c9c33a28 | Michael Hanselmann | opcodes.OpInstanceRename, |
47 | c9c33a28 | Michael Hanselmann | opcodes.OpNodeAdd, |
48 | c9c33a28 | Michael Hanselmann | opcodes.OpNodeRemove, |
49 | c9c33a28 | Michael Hanselmann | opcodes.OpTestAllocator, |
50 | c9c33a28 | Michael Hanselmann | ]) |
51 | c9c33a28 | Michael Hanselmann | |
52 | c9c33a28 | Michael Hanselmann | |
53 | e3200b18 | Michael Hanselmann | class TestLockAttemptTimeoutStrategy(unittest.TestCase): |
54 | 407339d0 | Michael Hanselmann | def testConstants(self): |
55 | a7770f03 | Michael Hanselmann | tpa = mcpu.LockAttemptTimeoutStrategy._TIMEOUT_PER_ATTEMPT |
56 | d385a174 | Iustin Pop | self.assert_(len(tpa) > LOCK_ATTEMPTS_TIMEOUT / LOCK_ATTEMPTS_MAXWAIT) |
57 | d385a174 | Iustin Pop | self.assert_(sum(tpa) >= LOCK_ATTEMPTS_TIMEOUT) |
58 | 407339d0 | Michael Hanselmann | |
59 | 0b04b188 | Michael Hanselmann | self.assertTrue(LOCK_ATTEMPTS_TIMEOUT >= 1800, |
60 | 0b04b188 | Michael Hanselmann | msg="Waiting less than half an hour per priority")
|
61 | 0b04b188 | Michael Hanselmann | self.assertTrue(LOCK_ATTEMPTS_TIMEOUT <= 3600, |
62 | 0b04b188 | Michael Hanselmann | msg="Waiting more than an hour per priority")
|
63 | 0b04b188 | Michael Hanselmann | |
64 | 407339d0 | Michael Hanselmann | def testSimple(self): |
65 | a7770f03 | Michael Hanselmann | strat = mcpu.LockAttemptTimeoutStrategy(_random_fn=lambda: 0.5, |
66 | a7770f03 | Michael Hanselmann | _time_fn=lambda: 0.0) |
67 | 407339d0 | Michael Hanselmann | |
68 | 407339d0 | Michael Hanselmann | prev = None
|
69 | a7770f03 | Michael Hanselmann | for i in range(len(strat._TIMEOUT_PER_ATTEMPT)): |
70 | a7770f03 | Michael Hanselmann | timeout = strat.NextAttempt() |
71 | 407339d0 | Michael Hanselmann | self.assert_(timeout is not None) |
72 | 407339d0 | Michael Hanselmann | |
73 | d385a174 | Iustin Pop | self.assert_(timeout <= LOCK_ATTEMPTS_MAXWAIT)
|
74 | d385a174 | Iustin Pop | self.assert_(timeout >= LOCK_ATTEMPTS_MINWAIT)
|
75 | 407339d0 | Michael Hanselmann | self.assert_(prev is None or timeout >= prev) |
76 | 407339d0 | Michael Hanselmann | |
77 | 407339d0 | Michael Hanselmann | prev = timeout |
78 | 407339d0 | Michael Hanselmann | |
79 | a6db1af2 | Michael Hanselmann | for _ in range(10): |
80 | a7770f03 | Michael Hanselmann | self.assert_(strat.NextAttempt() is None) |
81 | 407339d0 | Michael Hanselmann | |
82 | 407339d0 | Michael Hanselmann | |
83 | 0a31dda0 | Michael Hanselmann | class TestDispatchTable(unittest.TestCase): |
84 | 0a31dda0 | Michael Hanselmann | def test(self): |
85 | 0a31dda0 | Michael Hanselmann | for opcls in opcodes.OP_MAPPING.values(): |
86 | 687c10d9 | Iustin Pop | if not opcls.WITH_LU: |
87 | 0a31dda0 | Michael Hanselmann | continue
|
88 | 687c10d9 | Iustin Pop | self.assertTrue(opcls in mcpu.Processor.DISPATCH_TABLE, |
89 | 687c10d9 | Iustin Pop | msg="%s missing handler class" % opcls)
|
90 | 0a31dda0 | Michael Hanselmann | |
91 | c9c33a28 | Michael Hanselmann | # Check against BGL whitelist
|
92 | c9c33a28 | Michael Hanselmann | lucls = mcpu.Processor.DISPATCH_TABLE[opcls] |
93 | c9c33a28 | Michael Hanselmann | if lucls.REQ_BGL:
|
94 | c9c33a28 | Michael Hanselmann | self.assertTrue(opcls in REQ_BGL_WHITELIST, |
95 | c9c33a28 | Michael Hanselmann | msg=("%s not whitelisted for BGL" % opcls.OP_ID))
|
96 | c9c33a28 | Michael Hanselmann | else:
|
97 | c9c33a28 | Michael Hanselmann | self.assertFalse(opcls in REQ_BGL_WHITELIST, |
98 | c9c33a28 | Michael Hanselmann | msg=("%s whitelisted for BGL, but doesn't use it" %
|
99 | c9c33a28 | Michael Hanselmann | opcls.OP_ID)) |
100 | c9c33a28 | Michael Hanselmann | |
101 | 0a31dda0 | Michael Hanselmann | |
102 | 07923a3c | Michael Hanselmann | class TestProcessResult(unittest.TestCase): |
103 | 07923a3c | Michael Hanselmann | def setUp(self): |
104 | 07923a3c | Michael Hanselmann | self._submitted = []
|
105 | 07923a3c | Michael Hanselmann | self._count = itertools.count(200) |
106 | 07923a3c | Michael Hanselmann | |
107 | 07923a3c | Michael Hanselmann | def _Submit(self, jobs): |
108 | 07923a3c | Michael Hanselmann | job_ids = [self._count.next() for _ in jobs] |
109 | 07923a3c | Michael Hanselmann | self._submitted.extend(zip(job_ids, jobs)) |
110 | 07923a3c | Michael Hanselmann | return job_ids
|
111 | 07923a3c | Michael Hanselmann | |
112 | 07923a3c | Michael Hanselmann | def testNoJobs(self): |
113 | 07923a3c | Michael Hanselmann | for i in [object(), [], False, True, None, 1, 929, {}]: |
114 | 07923a3c | Michael Hanselmann | self.assertEqual(mcpu._ProcessResult(NotImplemented, NotImplemented, i), |
115 | 07923a3c | Michael Hanselmann | i) |
116 | 07923a3c | Michael Hanselmann | |
117 | 07923a3c | Michael Hanselmann | def testDefaults(self): |
118 | 07923a3c | Michael Hanselmann | src = opcodes.OpTestDummy() |
119 | 07923a3c | Michael Hanselmann | |
120 | 07923a3c | Michael Hanselmann | res = mcpu._ProcessResult(self._Submit, src, cmdlib.ResultWithJobs([[
|
121 | 07923a3c | Michael Hanselmann | opcodes.OpTestDelay(), |
122 | 07923a3c | Michael Hanselmann | opcodes.OpTestDelay(), |
123 | 07923a3c | Michael Hanselmann | ], [ |
124 | 07923a3c | Michael Hanselmann | opcodes.OpTestDelay(), |
125 | 07923a3c | Michael Hanselmann | ]])) |
126 | 07923a3c | Michael Hanselmann | |
127 | 07923a3c | Michael Hanselmann | self.assertEqual(res, {
|
128 | 07923a3c | Michael Hanselmann | constants.JOB_IDS_KEY: [200, 201], |
129 | 07923a3c | Michael Hanselmann | }) |
130 | 07923a3c | Michael Hanselmann | |
131 | 07923a3c | Michael Hanselmann | (_, (op1, op2)) = self._submitted.pop(0) |
132 | 07923a3c | Michael Hanselmann | (_, (op3, )) = self._submitted.pop(0) |
133 | 07923a3c | Michael Hanselmann | self.assertRaises(IndexError, self._submitted.pop) |
134 | 07923a3c | Michael Hanselmann | |
135 | 07923a3c | Michael Hanselmann | for op in [op1, op2, op3]: |
136 | 07923a3c | Michael Hanselmann | self.assertTrue("OP_TEST_DUMMY" in op.comment) |
137 | 07923a3c | Michael Hanselmann | self.assertFalse(hasattr(op, "priority")) |
138 | 07923a3c | Michael Hanselmann | self.assertFalse(hasattr(op, "debug_level")) |
139 | 07923a3c | Michael Hanselmann | |
140 | 07923a3c | Michael Hanselmann | def testParams(self): |
141 | 07923a3c | Michael Hanselmann | src = opcodes.OpTestDummy(priority=constants.OP_PRIO_HIGH, |
142 | 07923a3c | Michael Hanselmann | debug_level=3)
|
143 | 07923a3c | Michael Hanselmann | |
144 | 07923a3c | Michael Hanselmann | res = mcpu._ProcessResult(self._Submit, src, cmdlib.ResultWithJobs([[
|
145 | 07923a3c | Michael Hanselmann | opcodes.OpTestDelay(priority=constants.OP_PRIO_LOW), |
146 | 07923a3c | Michael Hanselmann | ], [ |
147 | 07923a3c | Michael Hanselmann | opcodes.OpTestDelay(comment="foobar", debug_level=10), |
148 | 07923a3c | Michael Hanselmann | ]], other=True, value=range(10))) |
149 | 07923a3c | Michael Hanselmann | |
150 | 07923a3c | Michael Hanselmann | self.assertEqual(res, {
|
151 | 07923a3c | Michael Hanselmann | constants.JOB_IDS_KEY: [200, 201], |
152 | 07923a3c | Michael Hanselmann | "other": True, |
153 | 07923a3c | Michael Hanselmann | "value": range(10), |
154 | 07923a3c | Michael Hanselmann | }) |
155 | 07923a3c | Michael Hanselmann | |
156 | 07923a3c | Michael Hanselmann | (_, (op1, )) = self._submitted.pop(0) |
157 | 07923a3c | Michael Hanselmann | (_, (op2, )) = self._submitted.pop(0) |
158 | 07923a3c | Michael Hanselmann | self.assertRaises(IndexError, self._submitted.pop) |
159 | 07923a3c | Michael Hanselmann | |
160 | 07923a3c | Michael Hanselmann | self.assertEqual(op1.priority, constants.OP_PRIO_LOW)
|
161 | 07923a3c | Michael Hanselmann | self.assertTrue("OP_TEST_DUMMY" in op1.comment) |
162 | 07923a3c | Michael Hanselmann | self.assertEqual(op1.debug_level, 3) |
163 | 07923a3c | Michael Hanselmann | |
164 | 07923a3c | Michael Hanselmann | self.assertEqual(op2.priority, constants.OP_PRIO_HIGH)
|
165 | 07923a3c | Michael Hanselmann | self.assertEqual(op2.comment, "foobar") |
166 | 07923a3c | Michael Hanselmann | self.assertEqual(op2.debug_level, 3) |
167 | 07923a3c | Michael Hanselmann | |
168 | 07923a3c | Michael Hanselmann | |
169 | 407339d0 | Michael Hanselmann | if __name__ == "__main__": |
170 | 25231ec5 | Michael Hanselmann | testutils.GanetiTestProgram() |