root / test / ganeti.utils_unittest.py @ 75d81fc8
History | View | Annotate | Download (10 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 | 1b045f5d | Balazs Lecz | class TestGetMounts(unittest.TestCase): |
65 | 1b045f5d | Balazs Lecz | """Test case for GetMounts()."""
|
66 | 1b045f5d | Balazs Lecz | |
67 | 1b045f5d | Balazs Lecz | TESTDATA = ( |
68 | 1b045f5d | Balazs Lecz | "rootfs / rootfs rw 0 0\n"
|
69 | 1b045f5d | Balazs Lecz | "none /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0\n"
|
70 | 1b045f5d | Balazs Lecz | "none /proc proc rw,nosuid,nodev,noexec,relatime 0 0\n")
|
71 | 1b045f5d | Balazs Lecz | |
72 | 1b045f5d | Balazs Lecz | def setUp(self): |
73 | 1b045f5d | Balazs Lecz | self.tmpfile = tempfile.NamedTemporaryFile()
|
74 | 1b045f5d | Balazs Lecz | utils.WriteFile(self.tmpfile.name, data=self.TESTDATA) |
75 | 1b045f5d | Balazs Lecz | |
76 | 1b045f5d | Balazs Lecz | def testGetMounts(self): |
77 | 1b045f5d | Balazs Lecz | self.assertEqual(utils.GetMounts(filename=self.tmpfile.name), |
78 | 1b045f5d | Balazs Lecz | [ |
79 | 1b045f5d | Balazs Lecz | ("rootfs", "/", "rootfs", "rw"), |
80 | 1b045f5d | Balazs Lecz | ("none", "/sys", "sysfs", "rw,nosuid,nodev,noexec,relatime"), |
81 | 1b045f5d | Balazs Lecz | ("none", "/proc", "proc", "rw,nosuid,nodev,noexec,relatime"), |
82 | 1b045f5d | Balazs Lecz | ]) |
83 | 1b045f5d | Balazs Lecz | |
84 | 59072e7e | Michael Hanselmann | |
85 | 7b4126b7 | Iustin Pop | class TestFirstFree(unittest.TestCase): |
86 | 7b4126b7 | Iustin Pop | """Test case for the FirstFree function"""
|
87 | 7b4126b7 | Iustin Pop | |
88 | 7b4126b7 | Iustin Pop | def test(self): |
89 | 7b4126b7 | Iustin Pop | """Test FirstFree"""
|
90 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([0, 1, 3]), 2) |
91 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([]), None) |
92 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([3, 4, 6]), 0) |
93 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([3, 4, 6], base=3), 5) |
94 | 7b4126b7 | Iustin Pop | self.failUnlessRaises(AssertionError, FirstFree, [0, 3, 4, 6], base=3) |
95 | f7414041 | Michael Hanselmann | |
96 | a87b4824 | Michael Hanselmann | |
97 | 739be818 | Michael Hanselmann | class TestTimeFunctions(unittest.TestCase): |
98 | 739be818 | Michael Hanselmann | """Test case for time functions"""
|
99 | 739be818 | Michael Hanselmann | |
100 | 739be818 | Michael Hanselmann | def runTest(self): |
101 | 739be818 | Michael Hanselmann | self.assertEqual(utils.SplitTime(1), (1, 0)) |
102 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(1.5), (1, 500000)) |
103 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(1218448917.4809151), (1218448917, 480915)) |
104 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.48012), (123, 480120)) |
105 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.9996), (123, 999600)) |
106 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.9995), (123, 999500)) |
107 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.9994), (123, 999400)) |
108 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.999999999), (123, 999999)) |
109 | 45bc5e4a | Michael Hanselmann | |
110 | 45bc5e4a | Michael Hanselmann | self.assertRaises(AssertionError, utils.SplitTime, -1) |
111 | 739be818 | Michael Hanselmann | |
112 | 739be818 | Michael Hanselmann | self.assertEqual(utils.MergeTime((1, 0)), 1.0) |
113 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.MergeTime((1, 500000)), 1.5) |
114 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.MergeTime((1218448917, 500000)), 1218448917.5) |
115 | 739be818 | Michael Hanselmann | |
116 | 4d4a651d | Michael Hanselmann | self.assertEqual(round(utils.MergeTime((1218448917, 481000)), 3), |
117 | 4d4a651d | Michael Hanselmann | 1218448917.481)
|
118 | 45bc5e4a | Michael Hanselmann | self.assertEqual(round(utils.MergeTime((1, 801000)), 3), 1.801) |
119 | 739be818 | Michael Hanselmann | |
120 | 739be818 | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (0, -1)) |
121 | 45bc5e4a | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (0, 1000000)) |
122 | 45bc5e4a | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (0, 9999999)) |
123 | 739be818 | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (-1, 0)) |
124 | 739be818 | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (-9999, 0)) |
125 | 739be818 | Michael Hanselmann | |
126 | 739be818 | Michael Hanselmann | |
127 | a2d2e1a7 | Iustin Pop | class FieldSetTestCase(unittest.TestCase): |
128 | a2d2e1a7 | Iustin Pop | """Test case for FieldSets"""
|
129 | a2d2e1a7 | Iustin Pop | |
130 | a2d2e1a7 | Iustin Pop | def testSimpleMatch(self): |
131 | a2d2e1a7 | Iustin Pop | f = utils.FieldSet("a", "b", "c", "def") |
132 | a2d2e1a7 | Iustin Pop | self.failUnless(f.Matches("a")) |
133 | a2d2e1a7 | Iustin Pop | self.failIf(f.Matches("d"), "Substring matched") |
134 | a2d2e1a7 | Iustin Pop | self.failIf(f.Matches("defghi"), "Prefix string matched") |
135 | a2d2e1a7 | Iustin Pop | self.failIf(f.NonMatching(["b", "c"])) |
136 | a2d2e1a7 | Iustin Pop | self.failIf(f.NonMatching(["a", "b", "c", "def"])) |
137 | a2d2e1a7 | Iustin Pop | self.failUnless(f.NonMatching(["a", "d"])) |
138 | a2d2e1a7 | Iustin Pop | |
139 | a2d2e1a7 | Iustin Pop | def testRegexMatch(self): |
140 | a2d2e1a7 | Iustin Pop | f = utils.FieldSet("a", "b([0-9]+)", "c") |
141 | a2d2e1a7 | Iustin Pop | self.failUnless(f.Matches("b1")) |
142 | a2d2e1a7 | Iustin Pop | self.failUnless(f.Matches("b99")) |
143 | a2d2e1a7 | Iustin Pop | self.failIf(f.Matches("b/1")) |
144 | a2d2e1a7 | Iustin Pop | self.failIf(f.NonMatching(["b12", "c"])) |
145 | a2d2e1a7 | Iustin Pop | self.failUnless(f.NonMatching(["a", "1"])) |
146 | a2d2e1a7 | Iustin Pop | |
147 | a5728081 | Guido Trotter | class TestForceDictType(unittest.TestCase): |
148 | a5728081 | Guido Trotter | """Test case for ForceDictType"""
|
149 | f40ae421 | Iustin Pop | KEY_TYPES = { |
150 | f40ae421 | Iustin Pop | "a": constants.VTYPE_INT,
|
151 | f40ae421 | Iustin Pop | "b": constants.VTYPE_BOOL,
|
152 | f40ae421 | Iustin Pop | "c": constants.VTYPE_STRING,
|
153 | f40ae421 | Iustin Pop | "d": constants.VTYPE_SIZE,
|
154 | f40ae421 | Iustin Pop | "e": constants.VTYPE_MAYBE_STRING,
|
155 | f40ae421 | Iustin Pop | } |
156 | a5728081 | Guido Trotter | |
157 | a5728081 | Guido Trotter | def _fdt(self, dict, allowed_values=None): |
158 | a5728081 | Guido Trotter | if allowed_values is None: |
159 | f40ae421 | Iustin Pop | utils.ForceDictType(dict, self.KEY_TYPES) |
160 | a5728081 | Guido Trotter | else:
|
161 | f40ae421 | Iustin Pop | utils.ForceDictType(dict, self.KEY_TYPES, allowed_values=allowed_values) |
162 | a5728081 | Guido Trotter | |
163 | a5728081 | Guido Trotter | return dict |
164 | a5728081 | Guido Trotter | |
165 | a5728081 | Guido Trotter | def testSimpleDict(self): |
166 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({}), {}) |
167 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'a': 1}), {'a': 1}) |
168 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'a': '1'}), {'a': 1}) |
169 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'a': 1, 'b': 1}), {'a':1, 'b': True}) |
170 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 1, 'c': 'foo'}), {'b': True, 'c': 'foo'}) |
171 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 1, 'c': False}), {'b': True, 'c': ''}) |
172 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'false'}), {'b': False}) |
173 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'False'}), {'b': False}) |
174 | f40ae421 | Iustin Pop | self.assertEqual(self._fdt({'b': False}), {'b': False}) |
175 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'true'}), {'b': True}) |
176 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'True'}), {'b': True}) |
177 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'d': '4'}), {'d': 4}) |
178 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'d': '4M'}), {'d': 4}) |
179 | 59525e1f | Michael Hanselmann | self.assertEqual(self._fdt({"e": None, }), {"e": None, }) |
180 | 59525e1f | Michael Hanselmann | self.assertEqual(self._fdt({"e": "Hello World", }), {"e": "Hello World", }) |
181 | 59525e1f | Michael Hanselmann | self.assertEqual(self._fdt({"e": False, }), {"e": '', }) |
182 | f40ae421 | Iustin Pop | self.assertEqual(self._fdt({"b": "hello", }, ["hello"]), {"b": "hello"}) |
183 | a5728081 | Guido Trotter | |
184 | a5728081 | Guido Trotter | def testErrors(self): |
185 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'a': 'astring'}) |
186 | f40ae421 | Iustin Pop | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"b": "hello"}) |
187 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'c': True}) |
188 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': 'astring'}) |
189 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': '4 L'}) |
190 | 59525e1f | Michael Hanselmann | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"e": object(), }) |
191 | 59525e1f | Michael Hanselmann | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"e": [], }) |
192 | f40ae421 | Iustin Pop | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"x": None, }) |
193 | f40ae421 | Iustin Pop | self.assertRaises(errors.TypeEnforcementError, self._fdt, []) |
194 | f40ae421 | Iustin Pop | self.assertRaises(errors.ProgrammerError, utils.ForceDictType,
|
195 | f40ae421 | Iustin Pop | {"b": "hello"}, {"b": "no-such-type"}) |
196 | a5728081 | Guido Trotter | |
197 | a2d2e1a7 | Iustin Pop | |
198 | 28f34048 | Michael Hanselmann | class TestValidateServiceName(unittest.TestCase): |
199 | 28f34048 | Michael Hanselmann | def testValid(self): |
200 | 28f34048 | Michael Hanselmann | testnames = [ |
201 | 28f34048 | Michael Hanselmann | 0, 1, 2, 3, 1024, 65000, 65534, 65535, |
202 | 28f34048 | Michael Hanselmann | "ganeti",
|
203 | 28f34048 | Michael Hanselmann | "gnt-masterd",
|
204 | 28f34048 | Michael Hanselmann | "HELLO_WORLD_SVC",
|
205 | 28f34048 | Michael Hanselmann | "hello.world.1",
|
206 | 28f34048 | Michael Hanselmann | "0", "80", "1111", "65535", |
207 | 28f34048 | Michael Hanselmann | ] |
208 | 28f34048 | Michael Hanselmann | |
209 | 28f34048 | Michael Hanselmann | for name in testnames: |
210 | 28f34048 | Michael Hanselmann | self.assertEqual(utils.ValidateServiceName(name), name)
|
211 | 28f34048 | Michael Hanselmann | |
212 | 28f34048 | Michael Hanselmann | def testInvalid(self): |
213 | 28f34048 | Michael Hanselmann | testnames = [ |
214 | 28f34048 | Michael Hanselmann | -15756, -1, 65536, 133428083, |
215 | 28f34048 | Michael Hanselmann | "", "Hello World!", "!", "'", "\"", "\t", "\n", "`", |
216 | 28f34048 | Michael Hanselmann | "-8546", "-1", "65536", |
217 | 28f34048 | Michael Hanselmann | (129 * "A"), |
218 | 28f34048 | Michael Hanselmann | ] |
219 | 28f34048 | Michael Hanselmann | |
220 | 28f34048 | Michael Hanselmann | for name in testnames: |
221 | a744b676 | Manuel Franceschini | self.assertRaises(errors.OpPrereqError, utils.ValidateServiceName, name)
|
222 | 28f34048 | Michael Hanselmann | |
223 | 28f34048 | Michael Hanselmann | |
224 | debed9ae | Michael Hanselmann | class TestReadLockedPidFile(unittest.TestCase): |
225 | debed9ae | Michael Hanselmann | def setUp(self): |
226 | debed9ae | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
227 | debed9ae | Michael Hanselmann | |
228 | debed9ae | Michael Hanselmann | def tearDown(self): |
229 | debed9ae | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
230 | debed9ae | Michael Hanselmann | |
231 | debed9ae | Michael Hanselmann | def testNonExistent(self): |
232 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "nonexist") |
233 | debed9ae | Michael Hanselmann | self.assert_(utils.ReadLockedPidFile(path) is None) |
234 | debed9ae | Michael Hanselmann | |
235 | debed9ae | Michael Hanselmann | def testUnlocked(self): |
236 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "pid") |
237 | debed9ae | Michael Hanselmann | utils.WriteFile(path, data="123")
|
238 | debed9ae | Michael Hanselmann | self.assert_(utils.ReadLockedPidFile(path) is None) |
239 | debed9ae | Michael Hanselmann | |
240 | debed9ae | Michael Hanselmann | def testLocked(self): |
241 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "pid") |
242 | debed9ae | Michael Hanselmann | utils.WriteFile(path, data="123")
|
243 | debed9ae | Michael Hanselmann | |
244 | debed9ae | Michael Hanselmann | fl = utils.FileLock.Open(path) |
245 | debed9ae | Michael Hanselmann | try:
|
246 | debed9ae | Michael Hanselmann | fl.Exclusive(blocking=True)
|
247 | debed9ae | Michael Hanselmann | |
248 | debed9ae | Michael Hanselmann | self.assertEqual(utils.ReadLockedPidFile(path), 123) |
249 | debed9ae | Michael Hanselmann | finally:
|
250 | debed9ae | Michael Hanselmann | fl.Close() |
251 | debed9ae | Michael Hanselmann | |
252 | debed9ae | Michael Hanselmann | self.assert_(utils.ReadLockedPidFile(path) is None) |
253 | debed9ae | Michael Hanselmann | |
254 | debed9ae | Michael Hanselmann | def testError(self): |
255 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "foobar", "pid") |
256 | 3865ca48 | Michael Hanselmann | utils.WriteFile(utils.PathJoin(self.tmpdir, "foobar"), data="") |
257 | debed9ae | Michael Hanselmann | # open(2) should return ENOTDIR
|
258 | debed9ae | Michael Hanselmann | self.assertRaises(EnvironmentError, utils.ReadLockedPidFile, path) |
259 | debed9ae | Michael Hanselmann | |
260 | debed9ae | Michael Hanselmann | |
261 | 691c81b7 | Michael Hanselmann | class TestFindMatch(unittest.TestCase): |
262 | 691c81b7 | Michael Hanselmann | def test(self): |
263 | 691c81b7 | Michael Hanselmann | data = { |
264 | 691c81b7 | Michael Hanselmann | "aaaa": "Four A", |
265 | 691c81b7 | Michael Hanselmann | "bb": {"Two B": True}, |
266 | 691c81b7 | Michael Hanselmann | re.compile(r"^x(foo|bar|bazX)([0-9]+)$"): (1, 2, 3), |
267 | 691c81b7 | Michael Hanselmann | } |
268 | 691c81b7 | Michael Hanselmann | |
269 | 691c81b7 | Michael Hanselmann | self.assertEqual(utils.FindMatch(data, "aaaa"), ("Four A", [])) |
270 | 691c81b7 | Michael Hanselmann | self.assertEqual(utils.FindMatch(data, "bb"), ({"Two B": True}, [])) |
271 | 691c81b7 | Michael Hanselmann | |
272 | 691c81b7 | Michael Hanselmann | for i in ["foo", "bar", "bazX"]: |
273 | 691c81b7 | Michael Hanselmann | for j in range(1, 100, 7): |
274 | 691c81b7 | Michael Hanselmann | self.assertEqual(utils.FindMatch(data, "x%s%s" % (i, j)), |
275 | 691c81b7 | Michael Hanselmann | ((1, 2, 3), [i, str(j)])) |
276 | 691c81b7 | Michael Hanselmann | |
277 | 691c81b7 | Michael Hanselmann | def testNoMatch(self): |
278 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch({}, "") is None) |
279 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch({}, "foo") is None) |
280 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch({}, 1234) is None) |
281 | 691c81b7 | Michael Hanselmann | |
282 | 691c81b7 | Michael Hanselmann | data = { |
283 | 691c81b7 | Michael Hanselmann | "X": "Hello World", |
284 | 691c81b7 | Michael Hanselmann | re.compile("^(something)$"): "Hello World", |
285 | 691c81b7 | Michael Hanselmann | } |
286 | 691c81b7 | Michael Hanselmann | |
287 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch(data, "") is None) |
288 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch(data, "Hello World") is None) |
289 | 691c81b7 | Michael Hanselmann | |
290 | 691c81b7 | Michael Hanselmann | |
291 | f40ae421 | Iustin Pop | class TestTryConvert(unittest.TestCase): |
292 | f40ae421 | Iustin Pop | def test(self): |
293 | f40ae421 | Iustin Pop | for src, fn, result in [ |
294 | f40ae421 | Iustin Pop | ("1", int, 1), |
295 | f40ae421 | Iustin Pop | ("a", int, "a"), |
296 | f40ae421 | Iustin Pop | ("", bool, False), |
297 | f40ae421 | Iustin Pop | ("a", bool, True), |
298 | f40ae421 | Iustin Pop | ]: |
299 | f40ae421 | Iustin Pop | self.assertEqual(utils.TryConvert(fn, src), result)
|
300 | f40ae421 | Iustin Pop | |
301 | f40ae421 | Iustin Pop | |
302 | a8083063 | Iustin Pop | if __name__ == '__main__': |
303 | 25231ec5 | Michael Hanselmann | testutils.GanetiTestProgram() |