root / test / ganeti.utils_unittest.py @ b5f2ab80
History | View | Annotate | Download (10.6 kB)
1 | a8083063 | Iustin Pop | #!/usr/bin/python
|
---|---|---|---|
2 | a8083063 | Iustin Pop | #
|
3 | a8083063 | Iustin Pop | |
4 | 7831fc5f | Michael Hanselmann | # Copyright (C) 2006, 2007, 2010, 2011 Google Inc.
|
5 | a8083063 | Iustin Pop | #
|
6 | a8083063 | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | a8083063 | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | a8083063 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | a8083063 | Iustin Pop | # (at your option) any later version.
|
10 | a8083063 | Iustin Pop | #
|
11 | a8083063 | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | a8083063 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | a8083063 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | a8083063 | Iustin Pop | # General Public License for more details.
|
15 | a8083063 | Iustin Pop | #
|
16 | a8083063 | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | a8083063 | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | a8083063 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | a8083063 | Iustin Pop | # 02110-1301, USA.
|
20 | a8083063 | Iustin Pop | |
21 | a8083063 | Iustin Pop | |
22 | a8083063 | Iustin Pop | """Script for unittesting the utils module"""
|
23 | a8083063 | Iustin Pop | |
24 | a744b676 | Manuel Franceschini | import errno |
25 | a744b676 | Manuel Franceschini | import fcntl |
26 | a744b676 | Manuel Franceschini | import glob |
27 | a8083063 | Iustin Pop | import os |
28 | a8083063 | Iustin Pop | import os.path |
29 | a744b676 | Manuel Franceschini | import re |
30 | a744b676 | Manuel Franceschini | import shutil |
31 | 740c5aab | Guido Trotter | import signal |
32 | 2c30e9d7 | Alexander Schreiber | import socket |
33 | a744b676 | Manuel Franceschini | import stat |
34 | a744b676 | Manuel Franceschini | import tempfile |
35 | a744b676 | Manuel Franceschini | import time |
36 | a744b676 | Manuel Franceschini | import unittest |
37 | 27e46076 | Michael Hanselmann | import warnings |
38 | 153533f3 | Michael Hanselmann | import random |
39 | 153533f3 | Michael Hanselmann | import operator |
40 | a8083063 | Iustin Pop | |
41 | c9c4f19e | Michael Hanselmann | import testutils |
42 | 16abfbc2 | Alexander Schreiber | from ganeti import constants |
43 | 716a32cb | Guido Trotter | from ganeti import compat |
44 | 59072e7e | Michael Hanselmann | from ganeti import utils |
45 | a5728081 | Guido Trotter | from ganeti import errors |
46 | 3865ca48 | Michael Hanselmann | from ganeti.utils import RunCmd, \ |
47 | 3865ca48 | Michael Hanselmann | FirstFree, \
|
48 | 17b97ab3 | Michael Hanselmann | RunParts
|
49 | a8083063 | Iustin Pop | |
50 | d9f311d7 | Iustin Pop | |
51 | 31155d60 | Balazs Lecz | class TestParseCpuMask(unittest.TestCase): |
52 | 31155d60 | Balazs Lecz | """Test case for the ParseCpuMask function."""
|
53 | 31155d60 | Balazs Lecz | |
54 | 31155d60 | Balazs Lecz | def testWellFormed(self): |
55 | 31155d60 | Balazs Lecz | self.assertEqual(utils.ParseCpuMask(""), []) |
56 | 31155d60 | Balazs Lecz | self.assertEqual(utils.ParseCpuMask("1"), [1]) |
57 | 31155d60 | Balazs Lecz | self.assertEqual(utils.ParseCpuMask("0-2,4,5-5"), [0,1,2,4,5]) |
58 | 31155d60 | Balazs Lecz | |
59 | 31155d60 | Balazs Lecz | def testInvalidInput(self): |
60 | f40ae421 | Iustin Pop | for data in ["garbage", "0,", "0-1-2", "2-1", "1-a"]: |
61 | f40ae421 | Iustin Pop | self.assertRaises(errors.ParseError, utils.ParseCpuMask, data)
|
62 | f40ae421 | Iustin Pop | |
63 | 31155d60 | Balazs Lecz | |
64 | 538ca33a | Tsachy Shacham | class TestParseMultiCpuMask(unittest.TestCase): |
65 | 538ca33a | Tsachy Shacham | """Test case for the ParseMultiCpuMask function."""
|
66 | 538ca33a | Tsachy Shacham | |
67 | 538ca33a | Tsachy Shacham | def testWellFormed(self): |
68 | 538ca33a | Tsachy Shacham | self.assertEqual(utils.ParseMultiCpuMask(""), []) |
69 | 538ca33a | Tsachy Shacham | self.assertEqual(utils.ParseMultiCpuMask("1"), [[1]]) |
70 | 538ca33a | Tsachy Shacham | self.assertEqual(utils.ParseMultiCpuMask("0-2,4,5-5"), [[0, 1, 2, 4, 5]]) |
71 | 538ca33a | Tsachy Shacham | self.assertEqual(utils.ParseMultiCpuMask("all"), [[-1]]) |
72 | 538ca33a | Tsachy Shacham | self.assertEqual(utils.ParseMultiCpuMask("0-2:all:4,6-8"), |
73 | 538ca33a | Tsachy Shacham | [[0, 1, 2], [-1], [4, 6, 7, 8]]) |
74 | 538ca33a | Tsachy Shacham | |
75 | 538ca33a | Tsachy Shacham | def testInvalidInput(self): |
76 | 538ca33a | Tsachy Shacham | for data in ["garbage", "0,", "0-1-2", "2-1", "1-a", "all-all"]: |
77 | 538ca33a | Tsachy Shacham | self.assertRaises(errors.ParseError, utils.ParseCpuMask, data)
|
78 | 538ca33a | Tsachy Shacham | |
79 | 538ca33a | Tsachy Shacham | |
80 | 1b045f5d | Balazs Lecz | class TestGetMounts(unittest.TestCase): |
81 | 1b045f5d | Balazs Lecz | """Test case for GetMounts()."""
|
82 | 1b045f5d | Balazs Lecz | |
83 | 1b045f5d | Balazs Lecz | TESTDATA = ( |
84 | 1b045f5d | Balazs Lecz | "rootfs / rootfs rw 0 0\n"
|
85 | 1b045f5d | Balazs Lecz | "none /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0\n"
|
86 | 1b045f5d | Balazs Lecz | "none /proc proc rw,nosuid,nodev,noexec,relatime 0 0\n")
|
87 | 1b045f5d | Balazs Lecz | |
88 | 1b045f5d | Balazs Lecz | def setUp(self): |
89 | 1b045f5d | Balazs Lecz | self.tmpfile = tempfile.NamedTemporaryFile()
|
90 | 1b045f5d | Balazs Lecz | utils.WriteFile(self.tmpfile.name, data=self.TESTDATA) |
91 | 1b045f5d | Balazs Lecz | |
92 | 1b045f5d | Balazs Lecz | def testGetMounts(self): |
93 | 1b045f5d | Balazs Lecz | self.assertEqual(utils.GetMounts(filename=self.tmpfile.name), |
94 | 1b045f5d | Balazs Lecz | [ |
95 | 1b045f5d | Balazs Lecz | ("rootfs", "/", "rootfs", "rw"), |
96 | 1b045f5d | Balazs Lecz | ("none", "/sys", "sysfs", "rw,nosuid,nodev,noexec,relatime"), |
97 | 1b045f5d | Balazs Lecz | ("none", "/proc", "proc", "rw,nosuid,nodev,noexec,relatime"), |
98 | 1b045f5d | Balazs Lecz | ]) |
99 | 1b045f5d | Balazs Lecz | |
100 | 59072e7e | Michael Hanselmann | |
101 | 7b4126b7 | Iustin Pop | class TestFirstFree(unittest.TestCase): |
102 | 7b4126b7 | Iustin Pop | """Test case for the FirstFree function"""
|
103 | 7b4126b7 | Iustin Pop | |
104 | 7b4126b7 | Iustin Pop | def test(self): |
105 | 7b4126b7 | Iustin Pop | """Test FirstFree"""
|
106 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([0, 1, 3]), 2) |
107 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([]), None) |
108 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([3, 4, 6]), 0) |
109 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([3, 4, 6], base=3), 5) |
110 | 7b4126b7 | Iustin Pop | self.failUnlessRaises(AssertionError, FirstFree, [0, 3, 4, 6], base=3) |
111 | f7414041 | Michael Hanselmann | |
112 | a87b4824 | Michael Hanselmann | |
113 | 739be818 | Michael Hanselmann | class TestTimeFunctions(unittest.TestCase): |
114 | 739be818 | Michael Hanselmann | """Test case for time functions"""
|
115 | 739be818 | Michael Hanselmann | |
116 | 739be818 | Michael Hanselmann | def runTest(self): |
117 | 739be818 | Michael Hanselmann | self.assertEqual(utils.SplitTime(1), (1, 0)) |
118 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(1.5), (1, 500000)) |
119 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(1218448917.4809151), (1218448917, 480915)) |
120 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.48012), (123, 480120)) |
121 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.9996), (123, 999600)) |
122 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.9995), (123, 999500)) |
123 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.9994), (123, 999400)) |
124 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.999999999), (123, 999999)) |
125 | 45bc5e4a | Michael Hanselmann | |
126 | 45bc5e4a | Michael Hanselmann | self.assertRaises(AssertionError, utils.SplitTime, -1) |
127 | 739be818 | Michael Hanselmann | |
128 | 739be818 | Michael Hanselmann | self.assertEqual(utils.MergeTime((1, 0)), 1.0) |
129 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.MergeTime((1, 500000)), 1.5) |
130 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.MergeTime((1218448917, 500000)), 1218448917.5) |
131 | 739be818 | Michael Hanselmann | |
132 | 4d4a651d | Michael Hanselmann | self.assertEqual(round(utils.MergeTime((1218448917, 481000)), 3), |
133 | 4d4a651d | Michael Hanselmann | 1218448917.481)
|
134 | 45bc5e4a | Michael Hanselmann | self.assertEqual(round(utils.MergeTime((1, 801000)), 3), 1.801) |
135 | 739be818 | Michael Hanselmann | |
136 | 739be818 | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (0, -1)) |
137 | 45bc5e4a | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (0, 1000000)) |
138 | 45bc5e4a | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (0, 9999999)) |
139 | 739be818 | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (-1, 0)) |
140 | 739be818 | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (-9999, 0)) |
141 | 739be818 | Michael Hanselmann | |
142 | 739be818 | Michael Hanselmann | |
143 | a2d2e1a7 | Iustin Pop | class FieldSetTestCase(unittest.TestCase): |
144 | a2d2e1a7 | Iustin Pop | """Test case for FieldSets"""
|
145 | a2d2e1a7 | Iustin Pop | |
146 | a2d2e1a7 | Iustin Pop | def testSimpleMatch(self): |
147 | a2d2e1a7 | Iustin Pop | f = utils.FieldSet("a", "b", "c", "def") |
148 | a2d2e1a7 | Iustin Pop | self.failUnless(f.Matches("a")) |
149 | a2d2e1a7 | Iustin Pop | self.failIf(f.Matches("d"), "Substring matched") |
150 | a2d2e1a7 | Iustin Pop | self.failIf(f.Matches("defghi"), "Prefix string matched") |
151 | a2d2e1a7 | Iustin Pop | self.failIf(f.NonMatching(["b", "c"])) |
152 | a2d2e1a7 | Iustin Pop | self.failIf(f.NonMatching(["a", "b", "c", "def"])) |
153 | a2d2e1a7 | Iustin Pop | self.failUnless(f.NonMatching(["a", "d"])) |
154 | a2d2e1a7 | Iustin Pop | |
155 | a2d2e1a7 | Iustin Pop | def testRegexMatch(self): |
156 | a2d2e1a7 | Iustin Pop | f = utils.FieldSet("a", "b([0-9]+)", "c") |
157 | a2d2e1a7 | Iustin Pop | self.failUnless(f.Matches("b1")) |
158 | a2d2e1a7 | Iustin Pop | self.failUnless(f.Matches("b99")) |
159 | a2d2e1a7 | Iustin Pop | self.failIf(f.Matches("b/1")) |
160 | a2d2e1a7 | Iustin Pop | self.failIf(f.NonMatching(["b12", "c"])) |
161 | a2d2e1a7 | Iustin Pop | self.failUnless(f.NonMatching(["a", "1"])) |
162 | a2d2e1a7 | Iustin Pop | |
163 | a5728081 | Guido Trotter | class TestForceDictType(unittest.TestCase): |
164 | a5728081 | Guido Trotter | """Test case for ForceDictType"""
|
165 | f40ae421 | Iustin Pop | KEY_TYPES = { |
166 | f40ae421 | Iustin Pop | "a": constants.VTYPE_INT,
|
167 | f40ae421 | Iustin Pop | "b": constants.VTYPE_BOOL,
|
168 | f40ae421 | Iustin Pop | "c": constants.VTYPE_STRING,
|
169 | f40ae421 | Iustin Pop | "d": constants.VTYPE_SIZE,
|
170 | f40ae421 | Iustin Pop | "e": constants.VTYPE_MAYBE_STRING,
|
171 | f40ae421 | Iustin Pop | } |
172 | a5728081 | Guido Trotter | |
173 | a5728081 | Guido Trotter | def _fdt(self, dict, allowed_values=None): |
174 | a5728081 | Guido Trotter | if allowed_values is None: |
175 | f40ae421 | Iustin Pop | utils.ForceDictType(dict, self.KEY_TYPES) |
176 | a5728081 | Guido Trotter | else:
|
177 | f40ae421 | Iustin Pop | utils.ForceDictType(dict, self.KEY_TYPES, allowed_values=allowed_values) |
178 | a5728081 | Guido Trotter | |
179 | a5728081 | Guido Trotter | return dict |
180 | a5728081 | Guido Trotter | |
181 | a5728081 | Guido Trotter | def testSimpleDict(self): |
182 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({}), {}) |
183 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'a': 1}), {'a': 1}) |
184 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'a': '1'}), {'a': 1}) |
185 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'a': 1, 'b': 1}), {'a':1, 'b': True}) |
186 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 1, 'c': 'foo'}), {'b': True, 'c': 'foo'}) |
187 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 1, 'c': False}), {'b': True, 'c': ''}) |
188 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'false'}), {'b': False}) |
189 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'False'}), {'b': False}) |
190 | f40ae421 | Iustin Pop | self.assertEqual(self._fdt({'b': False}), {'b': False}) |
191 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'true'}), {'b': True}) |
192 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'True'}), {'b': True}) |
193 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'d': '4'}), {'d': 4}) |
194 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'d': '4M'}), {'d': 4}) |
195 | 59525e1f | Michael Hanselmann | self.assertEqual(self._fdt({"e": None, }), {"e": None, }) |
196 | 59525e1f | Michael Hanselmann | self.assertEqual(self._fdt({"e": "Hello World", }), {"e": "Hello World", }) |
197 | 59525e1f | Michael Hanselmann | self.assertEqual(self._fdt({"e": False, }), {"e": '', }) |
198 | f40ae421 | Iustin Pop | self.assertEqual(self._fdt({"b": "hello", }, ["hello"]), {"b": "hello"}) |
199 | a5728081 | Guido Trotter | |
200 | a5728081 | Guido Trotter | def testErrors(self): |
201 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'a': 'astring'}) |
202 | f40ae421 | Iustin Pop | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"b": "hello"}) |
203 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'c': True}) |
204 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': 'astring'}) |
205 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': '4 L'}) |
206 | 59525e1f | Michael Hanselmann | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"e": object(), }) |
207 | 59525e1f | Michael Hanselmann | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"e": [], }) |
208 | f40ae421 | Iustin Pop | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"x": None, }) |
209 | f40ae421 | Iustin Pop | self.assertRaises(errors.TypeEnforcementError, self._fdt, []) |
210 | f40ae421 | Iustin Pop | self.assertRaises(errors.ProgrammerError, utils.ForceDictType,
|
211 | f40ae421 | Iustin Pop | {"b": "hello"}, {"b": "no-such-type"}) |
212 | a5728081 | Guido Trotter | |
213 | a2d2e1a7 | Iustin Pop | |
214 | 28f34048 | Michael Hanselmann | class TestValidateServiceName(unittest.TestCase): |
215 | 28f34048 | Michael Hanselmann | def testValid(self): |
216 | 28f34048 | Michael Hanselmann | testnames = [ |
217 | 28f34048 | Michael Hanselmann | 0, 1, 2, 3, 1024, 65000, 65534, 65535, |
218 | 28f34048 | Michael Hanselmann | "ganeti",
|
219 | 28f34048 | Michael Hanselmann | "gnt-masterd",
|
220 | 28f34048 | Michael Hanselmann | "HELLO_WORLD_SVC",
|
221 | 28f34048 | Michael Hanselmann | "hello.world.1",
|
222 | 28f34048 | Michael Hanselmann | "0", "80", "1111", "65535", |
223 | 28f34048 | Michael Hanselmann | ] |
224 | 28f34048 | Michael Hanselmann | |
225 | 28f34048 | Michael Hanselmann | for name in testnames: |
226 | 28f34048 | Michael Hanselmann | self.assertEqual(utils.ValidateServiceName(name), name)
|
227 | 28f34048 | Michael Hanselmann | |
228 | 28f34048 | Michael Hanselmann | def testInvalid(self): |
229 | 28f34048 | Michael Hanselmann | testnames = [ |
230 | 28f34048 | Michael Hanselmann | -15756, -1, 65536, 133428083, |
231 | 28f34048 | Michael Hanselmann | "", "Hello World!", "!", "'", "\"", "\t", "\n", "`", |
232 | 28f34048 | Michael Hanselmann | "-8546", "-1", "65536", |
233 | 28f34048 | Michael Hanselmann | (129 * "A"), |
234 | 28f34048 | Michael Hanselmann | ] |
235 | 28f34048 | Michael Hanselmann | |
236 | 28f34048 | Michael Hanselmann | for name in testnames: |
237 | a744b676 | Manuel Franceschini | self.assertRaises(errors.OpPrereqError, utils.ValidateServiceName, name)
|
238 | 28f34048 | Michael Hanselmann | |
239 | 28f34048 | Michael Hanselmann | |
240 | debed9ae | Michael Hanselmann | class TestReadLockedPidFile(unittest.TestCase): |
241 | debed9ae | Michael Hanselmann | def setUp(self): |
242 | debed9ae | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
243 | debed9ae | Michael Hanselmann | |
244 | debed9ae | Michael Hanselmann | def tearDown(self): |
245 | debed9ae | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
246 | debed9ae | Michael Hanselmann | |
247 | debed9ae | Michael Hanselmann | def testNonExistent(self): |
248 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "nonexist") |
249 | debed9ae | Michael Hanselmann | self.assert_(utils.ReadLockedPidFile(path) is None) |
250 | debed9ae | Michael Hanselmann | |
251 | debed9ae | Michael Hanselmann | def testUnlocked(self): |
252 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "pid") |
253 | debed9ae | Michael Hanselmann | utils.WriteFile(path, data="123")
|
254 | debed9ae | Michael Hanselmann | self.assert_(utils.ReadLockedPidFile(path) is None) |
255 | debed9ae | Michael Hanselmann | |
256 | debed9ae | Michael Hanselmann | def testLocked(self): |
257 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "pid") |
258 | debed9ae | Michael Hanselmann | utils.WriteFile(path, data="123")
|
259 | debed9ae | Michael Hanselmann | |
260 | debed9ae | Michael Hanselmann | fl = utils.FileLock.Open(path) |
261 | debed9ae | Michael Hanselmann | try:
|
262 | debed9ae | Michael Hanselmann | fl.Exclusive(blocking=True)
|
263 | debed9ae | Michael Hanselmann | |
264 | debed9ae | Michael Hanselmann | self.assertEqual(utils.ReadLockedPidFile(path), 123) |
265 | debed9ae | Michael Hanselmann | finally:
|
266 | debed9ae | Michael Hanselmann | fl.Close() |
267 | debed9ae | Michael Hanselmann | |
268 | debed9ae | Michael Hanselmann | self.assert_(utils.ReadLockedPidFile(path) is None) |
269 | debed9ae | Michael Hanselmann | |
270 | debed9ae | Michael Hanselmann | def testError(self): |
271 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "foobar", "pid") |
272 | 3865ca48 | Michael Hanselmann | utils.WriteFile(utils.PathJoin(self.tmpdir, "foobar"), data="") |
273 | debed9ae | Michael Hanselmann | # open(2) should return ENOTDIR
|
274 | debed9ae | Michael Hanselmann | self.assertRaises(EnvironmentError, utils.ReadLockedPidFile, path) |
275 | debed9ae | Michael Hanselmann | |
276 | debed9ae | Michael Hanselmann | |
277 | 691c81b7 | Michael Hanselmann | class TestFindMatch(unittest.TestCase): |
278 | 691c81b7 | Michael Hanselmann | def test(self): |
279 | 691c81b7 | Michael Hanselmann | data = { |
280 | 691c81b7 | Michael Hanselmann | "aaaa": "Four A", |
281 | 691c81b7 | Michael Hanselmann | "bb": {"Two B": True}, |
282 | 691c81b7 | Michael Hanselmann | re.compile(r"^x(foo|bar|bazX)([0-9]+)$"): (1, 2, 3), |
283 | 691c81b7 | Michael Hanselmann | } |
284 | 691c81b7 | Michael Hanselmann | |
285 | 691c81b7 | Michael Hanselmann | self.assertEqual(utils.FindMatch(data, "aaaa"), ("Four A", [])) |
286 | 691c81b7 | Michael Hanselmann | self.assertEqual(utils.FindMatch(data, "bb"), ({"Two B": True}, [])) |
287 | 691c81b7 | Michael Hanselmann | |
288 | 691c81b7 | Michael Hanselmann | for i in ["foo", "bar", "bazX"]: |
289 | 691c81b7 | Michael Hanselmann | for j in range(1, 100, 7): |
290 | 691c81b7 | Michael Hanselmann | self.assertEqual(utils.FindMatch(data, "x%s%s" % (i, j)), |
291 | 691c81b7 | Michael Hanselmann | ((1, 2, 3), [i, str(j)])) |
292 | 691c81b7 | Michael Hanselmann | |
293 | 691c81b7 | Michael Hanselmann | def testNoMatch(self): |
294 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch({}, "") is None) |
295 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch({}, "foo") is None) |
296 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch({}, 1234) is None) |
297 | 691c81b7 | Michael Hanselmann | |
298 | 691c81b7 | Michael Hanselmann | data = { |
299 | 691c81b7 | Michael Hanselmann | "X": "Hello World", |
300 | 691c81b7 | Michael Hanselmann | re.compile("^(something)$"): "Hello World", |
301 | 691c81b7 | Michael Hanselmann | } |
302 | 691c81b7 | Michael Hanselmann | |
303 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch(data, "") is None) |
304 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch(data, "Hello World") is None) |
305 | 691c81b7 | Michael Hanselmann | |
306 | 691c81b7 | Michael Hanselmann | |
307 | f40ae421 | Iustin Pop | class TestTryConvert(unittest.TestCase): |
308 | f40ae421 | Iustin Pop | def test(self): |
309 | f40ae421 | Iustin Pop | for src, fn, result in [ |
310 | f40ae421 | Iustin Pop | ("1", int, 1), |
311 | f40ae421 | Iustin Pop | ("a", int, "a"), |
312 | f40ae421 | Iustin Pop | ("", bool, False), |
313 | f40ae421 | Iustin Pop | ("a", bool, True), |
314 | f40ae421 | Iustin Pop | ]: |
315 | f40ae421 | Iustin Pop | self.assertEqual(utils.TryConvert(fn, src), result)
|
316 | f40ae421 | Iustin Pop | |
317 | f40ae421 | Iustin Pop | |
318 | a8083063 | Iustin Pop | if __name__ == '__main__': |
319 | 25231ec5 | Michael Hanselmann | testutils.GanetiTestProgram() |