root / test / py / ganeti.utils_unittest.py @ 14933c17
History | View | Annotate | Download (12.7 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 | 8c114acd | Michael Hanselmann | self.assertEqual(self._fdt({"a": 1}), {"a": 1}) |
184 | 8c114acd | Michael Hanselmann | self.assertEqual(self._fdt({"a": "1"}), {"a": 1}) |
185 | 8c114acd | Michael Hanselmann | self.assertEqual(self._fdt({"a": 1, "b": 1}), {"a":1, "b": True}) |
186 | 8c114acd | Michael Hanselmann | self.assertEqual(self._fdt({"b": 1, "c": "foo"}), {"b": True, "c": "foo"}) |
187 | 8c114acd | Michael Hanselmann | self.assertEqual(self._fdt({"b": 1, "c": False}), {"b": True, "c": ""}) |
188 | 8c114acd | Michael Hanselmann | self.assertEqual(self._fdt({"b": "false"}), {"b": False}) |
189 | 8c114acd | Michael Hanselmann | self.assertEqual(self._fdt({"b": "False"}), {"b": False}) |
190 | 8c114acd | Michael Hanselmann | self.assertEqual(self._fdt({"b": False}), {"b": False}) |
191 | 8c114acd | Michael Hanselmann | self.assertEqual(self._fdt({"b": "true"}), {"b": True}) |
192 | 8c114acd | Michael Hanselmann | self.assertEqual(self._fdt({"b": "True"}), {"b": True}) |
193 | 8c114acd | Michael Hanselmann | self.assertEqual(self._fdt({"d": "4"}), {"d": 4}) |
194 | 8c114acd | Michael Hanselmann | self.assertEqual(self._fdt({"d": "4M"}), {"d": 4}) |
195 | 7eed4433 | Michele Tartara | self.assertEqual(self._fdt({"e": constants.VALUE_HS_NOTHING, }), {"e": |
196 | 7eed4433 | Michele Tartara | constants.VALUE_HS_NOTHING, }) |
197 | 59525e1f | Michael Hanselmann | self.assertEqual(self._fdt({"e": "Hello World", }), {"e": "Hello World", }) |
198 | 8c114acd | Michael Hanselmann | self.assertEqual(self._fdt({"e": False, }), {"e": "", }) |
199 | f40ae421 | Iustin Pop | self.assertEqual(self._fdt({"b": "hello", }, ["hello"]), {"b": "hello"}) |
200 | a5728081 | Guido Trotter | |
201 | a5728081 | Guido Trotter | def testErrors(self): |
202 | 8c114acd | Michael Hanselmann | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"a": "astring"}) |
203 | f40ae421 | Iustin Pop | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"b": "hello"}) |
204 | 8c114acd | Michael Hanselmann | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"c": True}) |
205 | 8c114acd | Michael Hanselmann | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"d": "astring"}) |
206 | 8c114acd | Michael Hanselmann | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"d": "4 L"}) |
207 | 59525e1f | Michael Hanselmann | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"e": object(), }) |
208 | 59525e1f | Michael Hanselmann | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"e": [], }) |
209 | f40ae421 | Iustin Pop | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"x": None, }) |
210 | f40ae421 | Iustin Pop | self.assertRaises(errors.TypeEnforcementError, self._fdt, []) |
211 | f40ae421 | Iustin Pop | self.assertRaises(errors.ProgrammerError, utils.ForceDictType,
|
212 | f40ae421 | Iustin Pop | {"b": "hello"}, {"b": "no-such-type"}) |
213 | a5728081 | Guido Trotter | |
214 | a2d2e1a7 | Iustin Pop | |
215 | 28f34048 | Michael Hanselmann | class TestValidateServiceName(unittest.TestCase): |
216 | 28f34048 | Michael Hanselmann | def testValid(self): |
217 | 28f34048 | Michael Hanselmann | testnames = [ |
218 | 28f34048 | Michael Hanselmann | 0, 1, 2, 3, 1024, 65000, 65534, 65535, |
219 | 28f34048 | Michael Hanselmann | "ganeti",
|
220 | 28f34048 | Michael Hanselmann | "gnt-masterd",
|
221 | 28f34048 | Michael Hanselmann | "HELLO_WORLD_SVC",
|
222 | 28f34048 | Michael Hanselmann | "hello.world.1",
|
223 | 28f34048 | Michael Hanselmann | "0", "80", "1111", "65535", |
224 | 28f34048 | Michael Hanselmann | ] |
225 | 28f34048 | Michael Hanselmann | |
226 | 28f34048 | Michael Hanselmann | for name in testnames: |
227 | 28f34048 | Michael Hanselmann | self.assertEqual(utils.ValidateServiceName(name), name)
|
228 | 28f34048 | Michael Hanselmann | |
229 | 28f34048 | Michael Hanselmann | def testInvalid(self): |
230 | 28f34048 | Michael Hanselmann | testnames = [ |
231 | 28f34048 | Michael Hanselmann | -15756, -1, 65536, 133428083, |
232 | 28f34048 | Michael Hanselmann | "", "Hello World!", "!", "'", "\"", "\t", "\n", "`", |
233 | 28f34048 | Michael Hanselmann | "-8546", "-1", "65536", |
234 | 28f34048 | Michael Hanselmann | (129 * "A"), |
235 | 28f34048 | Michael Hanselmann | ] |
236 | 28f34048 | Michael Hanselmann | |
237 | 28f34048 | Michael Hanselmann | for name in testnames: |
238 | a744b676 | Manuel Franceschini | self.assertRaises(errors.OpPrereqError, utils.ValidateServiceName, name)
|
239 | 28f34048 | Michael Hanselmann | |
240 | 28f34048 | Michael Hanselmann | |
241 | debed9ae | Michael Hanselmann | class TestReadLockedPidFile(unittest.TestCase): |
242 | debed9ae | Michael Hanselmann | def setUp(self): |
243 | debed9ae | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
244 | debed9ae | Michael Hanselmann | |
245 | debed9ae | Michael Hanselmann | def tearDown(self): |
246 | debed9ae | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
247 | debed9ae | Michael Hanselmann | |
248 | debed9ae | Michael Hanselmann | def testNonExistent(self): |
249 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "nonexist") |
250 | debed9ae | Michael Hanselmann | self.assert_(utils.ReadLockedPidFile(path) is None) |
251 | debed9ae | Michael Hanselmann | |
252 | debed9ae | Michael Hanselmann | def testUnlocked(self): |
253 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "pid") |
254 | debed9ae | Michael Hanselmann | utils.WriteFile(path, data="123")
|
255 | debed9ae | Michael Hanselmann | self.assert_(utils.ReadLockedPidFile(path) is None) |
256 | debed9ae | Michael Hanselmann | |
257 | debed9ae | Michael Hanselmann | def testLocked(self): |
258 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "pid") |
259 | debed9ae | Michael Hanselmann | utils.WriteFile(path, data="123")
|
260 | debed9ae | Michael Hanselmann | |
261 | debed9ae | Michael Hanselmann | fl = utils.FileLock.Open(path) |
262 | debed9ae | Michael Hanselmann | try:
|
263 | debed9ae | Michael Hanselmann | fl.Exclusive(blocking=True)
|
264 | debed9ae | Michael Hanselmann | |
265 | debed9ae | Michael Hanselmann | self.assertEqual(utils.ReadLockedPidFile(path), 123) |
266 | debed9ae | Michael Hanselmann | finally:
|
267 | debed9ae | Michael Hanselmann | fl.Close() |
268 | debed9ae | Michael Hanselmann | |
269 | debed9ae | Michael Hanselmann | self.assert_(utils.ReadLockedPidFile(path) is None) |
270 | debed9ae | Michael Hanselmann | |
271 | debed9ae | Michael Hanselmann | def testError(self): |
272 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "foobar", "pid") |
273 | 3865ca48 | Michael Hanselmann | utils.WriteFile(utils.PathJoin(self.tmpdir, "foobar"), data="") |
274 | debed9ae | Michael Hanselmann | # open(2) should return ENOTDIR
|
275 | debed9ae | Michael Hanselmann | self.assertRaises(EnvironmentError, utils.ReadLockedPidFile, path) |
276 | debed9ae | Michael Hanselmann | |
277 | debed9ae | Michael Hanselmann | |
278 | 691c81b7 | Michael Hanselmann | class TestFindMatch(unittest.TestCase): |
279 | 691c81b7 | Michael Hanselmann | def test(self): |
280 | 691c81b7 | Michael Hanselmann | data = { |
281 | 691c81b7 | Michael Hanselmann | "aaaa": "Four A", |
282 | 691c81b7 | Michael Hanselmann | "bb": {"Two B": True}, |
283 | 691c81b7 | Michael Hanselmann | re.compile(r"^x(foo|bar|bazX)([0-9]+)$"): (1, 2, 3), |
284 | 691c81b7 | Michael Hanselmann | } |
285 | 691c81b7 | Michael Hanselmann | |
286 | 691c81b7 | Michael Hanselmann | self.assertEqual(utils.FindMatch(data, "aaaa"), ("Four A", [])) |
287 | 691c81b7 | Michael Hanselmann | self.assertEqual(utils.FindMatch(data, "bb"), ({"Two B": True}, [])) |
288 | 691c81b7 | Michael Hanselmann | |
289 | 691c81b7 | Michael Hanselmann | for i in ["foo", "bar", "bazX"]: |
290 | 691c81b7 | Michael Hanselmann | for j in range(1, 100, 7): |
291 | 691c81b7 | Michael Hanselmann | self.assertEqual(utils.FindMatch(data, "x%s%s" % (i, j)), |
292 | 691c81b7 | Michael Hanselmann | ((1, 2, 3), [i, str(j)])) |
293 | 691c81b7 | Michael Hanselmann | |
294 | 691c81b7 | Michael Hanselmann | def testNoMatch(self): |
295 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch({}, "") is None) |
296 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch({}, "foo") is None) |
297 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch({}, 1234) is None) |
298 | 691c81b7 | Michael Hanselmann | |
299 | 691c81b7 | Michael Hanselmann | data = { |
300 | 691c81b7 | Michael Hanselmann | "X": "Hello World", |
301 | 691c81b7 | Michael Hanselmann | re.compile("^(something)$"): "Hello World", |
302 | 691c81b7 | Michael Hanselmann | } |
303 | 691c81b7 | Michael Hanselmann | |
304 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch(data, "") is None) |
305 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch(data, "Hello World") is None) |
306 | 691c81b7 | Michael Hanselmann | |
307 | 691c81b7 | Michael Hanselmann | |
308 | f40ae421 | Iustin Pop | class TestTryConvert(unittest.TestCase): |
309 | f40ae421 | Iustin Pop | def test(self): |
310 | f40ae421 | Iustin Pop | for src, fn, result in [ |
311 | f40ae421 | Iustin Pop | ("1", int, 1), |
312 | f40ae421 | Iustin Pop | ("a", int, "a"), |
313 | f40ae421 | Iustin Pop | ("", bool, False), |
314 | f40ae421 | Iustin Pop | ("a", bool, True), |
315 | f40ae421 | Iustin Pop | ]: |
316 | f40ae421 | Iustin Pop | self.assertEqual(utils.TryConvert(fn, src), result)
|
317 | f40ae421 | Iustin Pop | |
318 | f40ae421 | Iustin Pop | |
319 | 32be86da | René Nussbaumer | class TestVerifyDictOptions(unittest.TestCase): |
320 | 32be86da | René Nussbaumer | def setUp(self): |
321 | 32be86da | René Nussbaumer | self.defaults = {
|
322 | 32be86da | René Nussbaumer | "first_key": "foobar", |
323 | 32be86da | René Nussbaumer | "foobar": {
|
324 | 32be86da | René Nussbaumer | "key1": "value2", |
325 | 32be86da | René Nussbaumer | "key2": "value1", |
326 | 32be86da | René Nussbaumer | }, |
327 | 32be86da | René Nussbaumer | "another_key": "another_value", |
328 | 32be86da | René Nussbaumer | } |
329 | 32be86da | René Nussbaumer | |
330 | 32be86da | René Nussbaumer | def test(self): |
331 | 32be86da | René Nussbaumer | some_keys = { |
332 | 32be86da | René Nussbaumer | "first_key": "blubb", |
333 | 32be86da | René Nussbaumer | "foobar": {
|
334 | 32be86da | René Nussbaumer | "key2": "foo", |
335 | 32be86da | René Nussbaumer | }, |
336 | 32be86da | René Nussbaumer | } |
337 | 32be86da | René Nussbaumer | utils.VerifyDictOptions(some_keys, self.defaults)
|
338 | 32be86da | René Nussbaumer | |
339 | 32be86da | René Nussbaumer | def testInvalid(self): |
340 | 32be86da | René Nussbaumer | some_keys = { |
341 | 32be86da | René Nussbaumer | "invalid_key": "blubb", |
342 | 32be86da | René Nussbaumer | "foobar": {
|
343 | 32be86da | René Nussbaumer | "key2": "foo", |
344 | 32be86da | René Nussbaumer | }, |
345 | 32be86da | René Nussbaumer | } |
346 | 32be86da | René Nussbaumer | self.assertRaises(errors.OpPrereqError, utils.VerifyDictOptions,
|
347 | 32be86da | René Nussbaumer | some_keys, self.defaults)
|
348 | 32be86da | René Nussbaumer | |
349 | 32be86da | René Nussbaumer | def testNestedInvalid(self): |
350 | 32be86da | René Nussbaumer | some_keys = { |
351 | 32be86da | René Nussbaumer | "foobar": {
|
352 | 32be86da | René Nussbaumer | "key2": "foo", |
353 | 32be86da | René Nussbaumer | "key3": "blibb" |
354 | 32be86da | René Nussbaumer | }, |
355 | 32be86da | René Nussbaumer | } |
356 | 32be86da | René Nussbaumer | self.assertRaises(errors.OpPrereqError, utils.VerifyDictOptions,
|
357 | 32be86da | René Nussbaumer | some_keys, self.defaults)
|
358 | 32be86da | René Nussbaumer | |
359 | 32be86da | René Nussbaumer | def testMultiInvalid(self): |
360 | 32be86da | René Nussbaumer | some_keys = { |
361 | 32be86da | René Nussbaumer | "foobar": {
|
362 | 32be86da | René Nussbaumer | "key1": "value3", |
363 | 32be86da | René Nussbaumer | "key6": "Right here", |
364 | 32be86da | René Nussbaumer | }, |
365 | 32be86da | René Nussbaumer | "invalid_with_sub": {
|
366 | 32be86da | René Nussbaumer | "sub1": "value3", |
367 | 32be86da | René Nussbaumer | }, |
368 | 32be86da | René Nussbaumer | } |
369 | 32be86da | René Nussbaumer | self.assertRaises(errors.OpPrereqError, utils.VerifyDictOptions,
|
370 | 32be86da | René Nussbaumer | some_keys, self.defaults)
|
371 | 32be86da | René Nussbaumer | |
372 | 32be86da | René Nussbaumer | |
373 | 651cc3e2 | Christos Stavrakakis | class TestValidateDeviceNames(unittest.TestCase): |
374 | 651cc3e2 | Christos Stavrakakis | def testEmpty(self): |
375 | 651cc3e2 | Christos Stavrakakis | utils.ValidateDeviceNames("NIC", [])
|
376 | 651cc3e2 | Christos Stavrakakis | utils.ValidateDeviceNames("disk", [])
|
377 | 651cc3e2 | Christos Stavrakakis | |
378 | 651cc3e2 | Christos Stavrakakis | def testNoName(self): |
379 | 651cc3e2 | Christos Stavrakakis | nics = [{}, {}] |
380 | 651cc3e2 | Christos Stavrakakis | utils.ValidateDeviceNames("NIC", nics)
|
381 | 651cc3e2 | Christos Stavrakakis | |
382 | 651cc3e2 | Christos Stavrakakis | def testInvalidName(self): |
383 | 651cc3e2 | Christos Stavrakakis | self.assertRaises(errors.OpPrereqError, utils.ValidateDeviceNames,
|
384 | 651cc3e2 | Christos Stavrakakis | "disk", [{constants.IDISK_NAME: "42"}]) |
385 | 651cc3e2 | Christos Stavrakakis | self.assertRaises(errors.OpPrereqError, utils.ValidateDeviceNames,
|
386 | 651cc3e2 | Christos Stavrakakis | "NIC", [{constants.INIC_NAME: "42"}]) |
387 | 651cc3e2 | Christos Stavrakakis | |
388 | 651cc3e2 | Christos Stavrakakis | def testUsedName(self): |
389 | 651cc3e2 | Christos Stavrakakis | disks = [{constants.IDISK_NAME: "name1"}, {constants.IDISK_NAME: "name1"}] |
390 | 651cc3e2 | Christos Stavrakakis | self.assertRaises(errors.OpPrereqError, utils.ValidateDeviceNames,
|
391 | 651cc3e2 | Christos Stavrakakis | "disk", disks)
|
392 | 651cc3e2 | Christos Stavrakakis | |
393 | 651cc3e2 | Christos Stavrakakis | |
394 | 2f96c43c | Michael Hanselmann | if __name__ == "__main__": |
395 | 25231ec5 | Michael Hanselmann | testutils.GanetiTestProgram() |