root / test / ganeti.utils_unittest.py @ 74aa2478
History | View | Annotate | Download (32.1 kB)
1 | a8083063 | Iustin Pop | #!/usr/bin/python
|
---|---|---|---|
2 | a8083063 | Iustin Pop | #
|
3 | a8083063 | Iustin Pop | |
4 | a8083063 | Iustin Pop | # Copyright (C) 2006, 2007 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 | a8083063 | Iustin Pop | import unittest |
25 | a8083063 | Iustin Pop | import os |
26 | a8083063 | Iustin Pop | import time |
27 | a8083063 | Iustin Pop | import tempfile |
28 | a8083063 | Iustin Pop | import os.path |
29 | 320b4e2d | Alexander Schreiber | import os |
30 | a8083063 | Iustin Pop | import md5 |
31 | 740c5aab | Guido Trotter | import signal |
32 | 2c30e9d7 | Alexander Schreiber | import socket |
33 | eedbda4b | Michael Hanselmann | import shutil |
34 | 59072e7e | Michael Hanselmann | import re |
35 | 09352fa4 | Iustin Pop | import select |
36 | a8083063 | Iustin Pop | |
37 | a8083063 | Iustin Pop | import ganeti |
38 | c9c4f19e | Michael Hanselmann | import testutils |
39 | 16abfbc2 | Alexander Schreiber | from ganeti import constants |
40 | 59072e7e | Michael Hanselmann | from ganeti import utils |
41 | a5728081 | Guido Trotter | from ganeti import errors |
42 | e5392d79 | Iustin Pop | from ganeti.utils import IsProcessAlive, RunCmd, \ |
43 | a8083063 | Iustin Pop | RemoveFile, CheckDict, MatchNameComponent, FormatUnit, \ |
44 | a8083063 | Iustin Pop | ParseUnit, AddAuthorizedKey, RemoveAuthorizedKey, \ |
45 | 899d2a81 | Michael Hanselmann | ShellQuote, ShellQuoteArgs, TcpPing, ListVisibleFiles, \ |
46 | f65f63ef | Iustin Pop | SetEtcHostsEntry, RemoveEtcHostsEntry, FirstFree, OwnIpAddress, \ |
47 | a5728081 | Guido Trotter | TailFile, ForceDictType |
48 | f65f63ef | Iustin Pop | |
49 | b2a1f511 | Iustin Pop | from ganeti.errors import LockError, UnitParseError, GenericError, \ |
50 | b2a1f511 | Iustin Pop | ProgrammerError
|
51 | a8083063 | Iustin Pop | |
52 | d9f311d7 | Iustin Pop | |
53 | a8083063 | Iustin Pop | class TestIsProcessAlive(unittest.TestCase): |
54 | a8083063 | Iustin Pop | """Testing case for IsProcessAlive"""
|
55 | 740c5aab | Guido Trotter | |
56 | a8083063 | Iustin Pop | def testExists(self): |
57 | a8083063 | Iustin Pop | mypid = os.getpid() |
58 | a8083063 | Iustin Pop | self.assert_(IsProcessAlive(mypid),
|
59 | a8083063 | Iustin Pop | "can't find myself running")
|
60 | a8083063 | Iustin Pop | |
61 | a8083063 | Iustin Pop | def testNotExisting(self): |
62 | 09352fa4 | Iustin Pop | pid_non_existing = os.fork() |
63 | 09352fa4 | Iustin Pop | if pid_non_existing == 0: |
64 | 09352fa4 | Iustin Pop | os._exit(0)
|
65 | 09352fa4 | Iustin Pop | elif pid_non_existing < 0: |
66 | 09352fa4 | Iustin Pop | raise SystemError("can't fork") |
67 | 09352fa4 | Iustin Pop | os.waitpid(pid_non_existing, 0)
|
68 | 09352fa4 | Iustin Pop | self.assert_(not IsProcessAlive(pid_non_existing), |
69 | 09352fa4 | Iustin Pop | "nonexisting process detected")
|
70 | a8083063 | Iustin Pop | |
71 | d9f311d7 | Iustin Pop | |
72 | af99afa6 | Guido Trotter | class TestPidFileFunctions(unittest.TestCase): |
73 | d9f311d7 | Iustin Pop | """Tests for WritePidFile, RemovePidFile and ReadPidFile"""
|
74 | af99afa6 | Guido Trotter | |
75 | af99afa6 | Guido Trotter | def setUp(self): |
76 | af99afa6 | Guido Trotter | self.dir = tempfile.mkdtemp()
|
77 | af99afa6 | Guido Trotter | self.f_dpn = lambda name: os.path.join(self.dir, "%s.pid" % name) |
78 | 53beffbb | Iustin Pop | utils.DaemonPidFileName = self.f_dpn
|
79 | af99afa6 | Guido Trotter | |
80 | af99afa6 | Guido Trotter | def testPidFileFunctions(self): |
81 | d9f311d7 | Iustin Pop | pid_file = self.f_dpn('test') |
82 | af99afa6 | Guido Trotter | utils.WritePidFile('test')
|
83 | d9f311d7 | Iustin Pop | self.failUnless(os.path.exists(pid_file),
|
84 | d9f311d7 | Iustin Pop | "PID file should have been created")
|
85 | d9f311d7 | Iustin Pop | read_pid = utils.ReadPidFile(pid_file) |
86 | d9f311d7 | Iustin Pop | self.failUnlessEqual(read_pid, os.getpid())
|
87 | d9f311d7 | Iustin Pop | self.failUnless(utils.IsProcessAlive(read_pid))
|
88 | d9f311d7 | Iustin Pop | self.failUnlessRaises(GenericError, utils.WritePidFile, 'test') |
89 | d9f311d7 | Iustin Pop | utils.RemovePidFile('test')
|
90 | d9f311d7 | Iustin Pop | self.failIf(os.path.exists(pid_file),
|
91 | d9f311d7 | Iustin Pop | "PID file should not exist anymore")
|
92 | d9f311d7 | Iustin Pop | self.failUnlessEqual(utils.ReadPidFile(pid_file), 0, |
93 | d9f311d7 | Iustin Pop | "ReadPidFile should return 0 for missing pid file")
|
94 | d9f311d7 | Iustin Pop | fh = open(pid_file, "w") |
95 | d9f311d7 | Iustin Pop | fh.write("blah\n")
|
96 | d9f311d7 | Iustin Pop | fh.close() |
97 | d9f311d7 | Iustin Pop | self.failUnlessEqual(utils.ReadPidFile(pid_file), 0, |
98 | d9f311d7 | Iustin Pop | "ReadPidFile should return 0 for invalid pid file")
|
99 | af99afa6 | Guido Trotter | utils.RemovePidFile('test')
|
100 | d9f311d7 | Iustin Pop | self.failIf(os.path.exists(pid_file),
|
101 | d9f311d7 | Iustin Pop | "PID file should not exist anymore")
|
102 | af99afa6 | Guido Trotter | |
103 | b2a1f511 | Iustin Pop | def testKill(self): |
104 | b2a1f511 | Iustin Pop | pid_file = self.f_dpn('child') |
105 | b2a1f511 | Iustin Pop | r_fd, w_fd = os.pipe() |
106 | b2a1f511 | Iustin Pop | new_pid = os.fork() |
107 | b2a1f511 | Iustin Pop | if new_pid == 0: #child |
108 | b2a1f511 | Iustin Pop | utils.WritePidFile('child')
|
109 | b2a1f511 | Iustin Pop | os.write(w_fd, 'a')
|
110 | b2a1f511 | Iustin Pop | signal.pause() |
111 | b2a1f511 | Iustin Pop | os._exit(0)
|
112 | b2a1f511 | Iustin Pop | return
|
113 | b2a1f511 | Iustin Pop | # else we are in the parent
|
114 | b2a1f511 | Iustin Pop | # wait until the child has written the pid file
|
115 | b2a1f511 | Iustin Pop | os.read(r_fd, 1)
|
116 | b2a1f511 | Iustin Pop | read_pid = utils.ReadPidFile(pid_file) |
117 | b2a1f511 | Iustin Pop | self.failUnlessEqual(read_pid, new_pid)
|
118 | b2a1f511 | Iustin Pop | self.failUnless(utils.IsProcessAlive(new_pid))
|
119 | ff5251bc | Iustin Pop | utils.KillProcess(new_pid, waitpid=True)
|
120 | b2a1f511 | Iustin Pop | self.failIf(utils.IsProcessAlive(new_pid))
|
121 | b2a1f511 | Iustin Pop | utils.RemovePidFile('child')
|
122 | b2a1f511 | Iustin Pop | self.failUnlessRaises(ProgrammerError, utils.KillProcess, 0) |
123 | b2a1f511 | Iustin Pop | |
124 | af99afa6 | Guido Trotter | def tearDown(self): |
125 | d9f311d7 | Iustin Pop | for name in os.listdir(self.dir): |
126 | d9f311d7 | Iustin Pop | os.unlink(os.path.join(self.dir, name))
|
127 | af99afa6 | Guido Trotter | os.rmdir(self.dir)
|
128 | af99afa6 | Guido Trotter | |
129 | a8083063 | Iustin Pop | |
130 | 36117c2b | Iustin Pop | class TestRunCmd(testutils.GanetiTestCase): |
131 | a8083063 | Iustin Pop | """Testing case for the RunCmd function"""
|
132 | a8083063 | Iustin Pop | |
133 | a8083063 | Iustin Pop | def setUp(self): |
134 | 51596eb2 | Iustin Pop | testutils.GanetiTestCase.setUp(self)
|
135 | a8083063 | Iustin Pop | self.magic = time.ctime() + " ganeti test" |
136 | 51596eb2 | Iustin Pop | self.fname = self._CreateTempFile() |
137 | a8083063 | Iustin Pop | |
138 | a8083063 | Iustin Pop | def testOk(self): |
139 | 31ee599c | Michael Hanselmann | """Test successful exit code"""
|
140 | a8083063 | Iustin Pop | result = RunCmd("/bin/sh -c 'exit 0'")
|
141 | a8083063 | Iustin Pop | self.assertEqual(result.exit_code, 0) |
142 | 36117c2b | Iustin Pop | self.assertEqual(result.output, "") |
143 | a8083063 | Iustin Pop | |
144 | a8083063 | Iustin Pop | def testFail(self): |
145 | a8083063 | Iustin Pop | """Test fail exit code"""
|
146 | a8083063 | Iustin Pop | result = RunCmd("/bin/sh -c 'exit 1'")
|
147 | a8083063 | Iustin Pop | self.assertEqual(result.exit_code, 1) |
148 | 36117c2b | Iustin Pop | self.assertEqual(result.output, "") |
149 | a8083063 | Iustin Pop | |
150 | a8083063 | Iustin Pop | def testStdout(self): |
151 | a8083063 | Iustin Pop | """Test standard output"""
|
152 | a8083063 | Iustin Pop | cmd = 'echo -n "%s"' % self.magic |
153 | a8083063 | Iustin Pop | result = RunCmd("/bin/sh -c '%s'" % cmd)
|
154 | a8083063 | Iustin Pop | self.assertEqual(result.stdout, self.magic) |
155 | 36117c2b | Iustin Pop | result = RunCmd("/bin/sh -c '%s'" % cmd, output=self.fname) |
156 | 36117c2b | Iustin Pop | self.assertEqual(result.output, "") |
157 | 36117c2b | Iustin Pop | self.assertFileContent(self.fname, self.magic) |
158 | a8083063 | Iustin Pop | |
159 | a8083063 | Iustin Pop | def testStderr(self): |
160 | a8083063 | Iustin Pop | """Test standard error"""
|
161 | a8083063 | Iustin Pop | cmd = 'echo -n "%s"' % self.magic |
162 | a8083063 | Iustin Pop | result = RunCmd("/bin/sh -c '%s' 1>&2" % cmd)
|
163 | a8083063 | Iustin Pop | self.assertEqual(result.stderr, self.magic) |
164 | 36117c2b | Iustin Pop | result = RunCmd("/bin/sh -c '%s' 1>&2" % cmd, output=self.fname) |
165 | 36117c2b | Iustin Pop | self.assertEqual(result.output, "") |
166 | 36117c2b | Iustin Pop | self.assertFileContent(self.fname, self.magic) |
167 | a8083063 | Iustin Pop | |
168 | a8083063 | Iustin Pop | def testCombined(self): |
169 | a8083063 | Iustin Pop | """Test combined output"""
|
170 | a8083063 | Iustin Pop | cmd = 'echo -n "A%s"; echo -n "B%s" 1>&2' % (self.magic, self.magic) |
171 | 36117c2b | Iustin Pop | expected = "A" + self.magic + "B" + self.magic |
172 | a8083063 | Iustin Pop | result = RunCmd("/bin/sh -c '%s'" % cmd)
|
173 | 36117c2b | Iustin Pop | self.assertEqual(result.output, expected)
|
174 | 36117c2b | Iustin Pop | result = RunCmd("/bin/sh -c '%s'" % cmd, output=self.fname) |
175 | 36117c2b | Iustin Pop | self.assertEqual(result.output, "") |
176 | 36117c2b | Iustin Pop | self.assertFileContent(self.fname, expected) |
177 | a8083063 | Iustin Pop | |
178 | a8083063 | Iustin Pop | def testSignal(self): |
179 | 01fd6005 | Manuel Franceschini | """Test signal"""
|
180 | 01fd6005 | Manuel Franceschini | result = RunCmd(["python", "-c", "import os; os.kill(os.getpid(), 15)"]) |
181 | a8083063 | Iustin Pop | self.assertEqual(result.signal, 15) |
182 | 36117c2b | Iustin Pop | self.assertEqual(result.output, "") |
183 | a8083063 | Iustin Pop | |
184 | 7fcf849f | Iustin Pop | def testListRun(self): |
185 | 7fcf849f | Iustin Pop | """Test list runs"""
|
186 | 7fcf849f | Iustin Pop | result = RunCmd(["true"])
|
187 | 7fcf849f | Iustin Pop | self.assertEqual(result.signal, None) |
188 | 7fcf849f | Iustin Pop | self.assertEqual(result.exit_code, 0) |
189 | 7fcf849f | Iustin Pop | result = RunCmd(["/bin/sh", "-c", "exit 1"]) |
190 | 7fcf849f | Iustin Pop | self.assertEqual(result.signal, None) |
191 | 7fcf849f | Iustin Pop | self.assertEqual(result.exit_code, 1) |
192 | 7fcf849f | Iustin Pop | result = RunCmd(["echo", "-n", self.magic]) |
193 | 7fcf849f | Iustin Pop | self.assertEqual(result.signal, None) |
194 | 7fcf849f | Iustin Pop | self.assertEqual(result.exit_code, 0) |
195 | 7fcf849f | Iustin Pop | self.assertEqual(result.stdout, self.magic) |
196 | 7fcf849f | Iustin Pop | |
197 | 36117c2b | Iustin Pop | def testFileEmptyOutput(self): |
198 | 36117c2b | Iustin Pop | """Test file output"""
|
199 | 36117c2b | Iustin Pop | result = RunCmd(["true"], output=self.fname) |
200 | 36117c2b | Iustin Pop | self.assertEqual(result.signal, None) |
201 | 36117c2b | Iustin Pop | self.assertEqual(result.exit_code, 0) |
202 | 36117c2b | Iustin Pop | self.assertFileContent(self.fname, "") |
203 | 36117c2b | Iustin Pop | |
204 | f6441c7c | Iustin Pop | def testLang(self): |
205 | f6441c7c | Iustin Pop | """Test locale environment"""
|
206 | 23f41a3e | Michael Hanselmann | old_env = os.environ.copy() |
207 | 23f41a3e | Michael Hanselmann | try:
|
208 | 23f41a3e | Michael Hanselmann | os.environ["LANG"] = "en_US.UTF-8" |
209 | 23f41a3e | Michael Hanselmann | os.environ["LC_ALL"] = "en_US.UTF-8" |
210 | 23f41a3e | Michael Hanselmann | result = RunCmd(["locale"])
|
211 | 23f41a3e | Michael Hanselmann | for line in result.output.splitlines(): |
212 | 23f41a3e | Michael Hanselmann | key, value = line.split("=", 1) |
213 | 23f41a3e | Michael Hanselmann | # Ignore these variables, they're overridden by LC_ALL
|
214 | 23f41a3e | Michael Hanselmann | if key == "LANG" or key == "LANGUAGE": |
215 | 23f41a3e | Michael Hanselmann | continue
|
216 | 23f41a3e | Michael Hanselmann | self.failIf(value and value != "C" and value != '"C"', |
217 | 23f41a3e | Michael Hanselmann | "Variable %s is set to the invalid value '%s'" % (key, value))
|
218 | 23f41a3e | Michael Hanselmann | finally:
|
219 | 23f41a3e | Michael Hanselmann | os.environ = old_env |
220 | f6441c7c | Iustin Pop | |
221 | 8797df43 | Iustin Pop | def testDefaultCwd(self): |
222 | 8797df43 | Iustin Pop | """Test default working directory"""
|
223 | 8797df43 | Iustin Pop | self.failUnlessEqual(RunCmd(["pwd"]).stdout.strip(), "/") |
224 | 8797df43 | Iustin Pop | |
225 | 8797df43 | Iustin Pop | def testCwd(self): |
226 | 8797df43 | Iustin Pop | """Test default working directory"""
|
227 | 8797df43 | Iustin Pop | self.failUnlessEqual(RunCmd(["pwd"], cwd="/").stdout.strip(), "/") |
228 | 8797df43 | Iustin Pop | self.failUnlessEqual(RunCmd(["pwd"], cwd="/tmp").stdout.strip(), "/tmp") |
229 | 8797df43 | Iustin Pop | cwd = os.getcwd() |
230 | 8797df43 | Iustin Pop | self.failUnlessEqual(RunCmd(["pwd"], cwd=cwd).stdout.strip(), cwd) |
231 | 8797df43 | Iustin Pop | |
232 | a8083063 | Iustin Pop | |
233 | a8083063 | Iustin Pop | class TestRemoveFile(unittest.TestCase): |
234 | a8083063 | Iustin Pop | """Test case for the RemoveFile function"""
|
235 | a8083063 | Iustin Pop | |
236 | a8083063 | Iustin Pop | def setUp(self): |
237 | a8083063 | Iustin Pop | """Create a temp dir and file for each case"""
|
238 | a8083063 | Iustin Pop | self.tmpdir = tempfile.mkdtemp('', 'ganeti-unittest-') |
239 | a8083063 | Iustin Pop | fd, self.tmpfile = tempfile.mkstemp('', '', self.tmpdir) |
240 | a8083063 | Iustin Pop | os.close(fd) |
241 | a8083063 | Iustin Pop | |
242 | a8083063 | Iustin Pop | def tearDown(self): |
243 | a8083063 | Iustin Pop | if os.path.exists(self.tmpfile): |
244 | a8083063 | Iustin Pop | os.unlink(self.tmpfile)
|
245 | a8083063 | Iustin Pop | os.rmdir(self.tmpdir)
|
246 | a8083063 | Iustin Pop | |
247 | a8083063 | Iustin Pop | |
248 | a8083063 | Iustin Pop | def testIgnoreDirs(self): |
249 | a8083063 | Iustin Pop | """Test that RemoveFile() ignores directories"""
|
250 | a8083063 | Iustin Pop | self.assertEqual(None, RemoveFile(self.tmpdir)) |
251 | a8083063 | Iustin Pop | |
252 | a8083063 | Iustin Pop | |
253 | a8083063 | Iustin Pop | def testIgnoreNotExisting(self): |
254 | a8083063 | Iustin Pop | """Test that RemoveFile() ignores non-existing files"""
|
255 | a8083063 | Iustin Pop | RemoveFile(self.tmpfile)
|
256 | a8083063 | Iustin Pop | RemoveFile(self.tmpfile)
|
257 | a8083063 | Iustin Pop | |
258 | a8083063 | Iustin Pop | |
259 | a8083063 | Iustin Pop | def testRemoveFile(self): |
260 | a8083063 | Iustin Pop | """Test that RemoveFile does remove a file"""
|
261 | a8083063 | Iustin Pop | RemoveFile(self.tmpfile)
|
262 | a8083063 | Iustin Pop | if os.path.exists(self.tmpfile): |
263 | a8083063 | Iustin Pop | self.fail("File '%s' not removed" % self.tmpfile) |
264 | a8083063 | Iustin Pop | |
265 | a8083063 | Iustin Pop | |
266 | a8083063 | Iustin Pop | def testRemoveSymlink(self): |
267 | a8083063 | Iustin Pop | """Test that RemoveFile does remove symlinks"""
|
268 | a8083063 | Iustin Pop | symlink = self.tmpdir + "/symlink" |
269 | a8083063 | Iustin Pop | os.symlink("no-such-file", symlink)
|
270 | a8083063 | Iustin Pop | RemoveFile(symlink) |
271 | a8083063 | Iustin Pop | if os.path.exists(symlink):
|
272 | a8083063 | Iustin Pop | self.fail("File '%s' not removed" % symlink) |
273 | a8083063 | Iustin Pop | os.symlink(self.tmpfile, symlink)
|
274 | a8083063 | Iustin Pop | RemoveFile(symlink) |
275 | a8083063 | Iustin Pop | if os.path.exists(symlink):
|
276 | a8083063 | Iustin Pop | self.fail("File '%s' not removed" % symlink) |
277 | a8083063 | Iustin Pop | |
278 | a8083063 | Iustin Pop | |
279 | 6e797216 | Michael Hanselmann | class TestRename(unittest.TestCase): |
280 | 6e797216 | Michael Hanselmann | """Test case for RenameFile"""
|
281 | 6e797216 | Michael Hanselmann | |
282 | 6e797216 | Michael Hanselmann | def setUp(self): |
283 | 6e797216 | Michael Hanselmann | """Create a temporary directory"""
|
284 | 6e797216 | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
285 | 6e797216 | Michael Hanselmann | self.tmpfile = os.path.join(self.tmpdir, "test1") |
286 | 6e797216 | Michael Hanselmann | |
287 | 6e797216 | Michael Hanselmann | # Touch the file
|
288 | 6e797216 | Michael Hanselmann | open(self.tmpfile, "w").close() |
289 | 6e797216 | Michael Hanselmann | |
290 | 6e797216 | Michael Hanselmann | def tearDown(self): |
291 | 6e797216 | Michael Hanselmann | """Remove temporary directory"""
|
292 | 6e797216 | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
293 | 6e797216 | Michael Hanselmann | |
294 | 6e797216 | Michael Hanselmann | def testSimpleRename1(self): |
295 | 6e797216 | Michael Hanselmann | """Simple rename 1"""
|
296 | 6e797216 | Michael Hanselmann | utils.RenameFile(self.tmpfile, os.path.join(self.tmpdir, "xyz")) |
297 | 6e797216 | Michael Hanselmann | |
298 | 6e797216 | Michael Hanselmann | def testSimpleRename2(self): |
299 | 6e797216 | Michael Hanselmann | """Simple rename 2"""
|
300 | 6e797216 | Michael Hanselmann | utils.RenameFile(self.tmpfile, os.path.join(self.tmpdir, "xyz"), |
301 | 6e797216 | Michael Hanselmann | mkdir=True)
|
302 | 6e797216 | Michael Hanselmann | |
303 | 6e797216 | Michael Hanselmann | def testRenameMkdir(self): |
304 | 6e797216 | Michael Hanselmann | """Rename with mkdir"""
|
305 | 6e797216 | Michael Hanselmann | utils.RenameFile(self.tmpfile, os.path.join(self.tmpdir, "test/xyz"), |
306 | 6e797216 | Michael Hanselmann | mkdir=True)
|
307 | 6e797216 | Michael Hanselmann | |
308 | 6e797216 | Michael Hanselmann | |
309 | a8083063 | Iustin Pop | class TestCheckdict(unittest.TestCase): |
310 | a8083063 | Iustin Pop | """Test case for the CheckDict function"""
|
311 | a8083063 | Iustin Pop | |
312 | a8083063 | Iustin Pop | def testAdd(self): |
313 | a8083063 | Iustin Pop | """Test that CheckDict adds a missing key with the correct value"""
|
314 | a8083063 | Iustin Pop | |
315 | a8083063 | Iustin Pop | tgt = {'a':1} |
316 | a8083063 | Iustin Pop | tmpl = {'b': 2} |
317 | a8083063 | Iustin Pop | CheckDict(tgt, tmpl) |
318 | a8083063 | Iustin Pop | if 'b' not in tgt or tgt['b'] != 2: |
319 | a8083063 | Iustin Pop | self.fail("Failed to update dict") |
320 | a8083063 | Iustin Pop | |
321 | a8083063 | Iustin Pop | |
322 | a8083063 | Iustin Pop | def testNoUpdate(self): |
323 | a8083063 | Iustin Pop | """Test that CheckDict does not overwrite an existing key"""
|
324 | a8083063 | Iustin Pop | tgt = {'a':1, 'b': 3} |
325 | a8083063 | Iustin Pop | tmpl = {'b': 2} |
326 | a8083063 | Iustin Pop | CheckDict(tgt, tmpl) |
327 | a8083063 | Iustin Pop | self.failUnlessEqual(tgt['b'], 3) |
328 | a8083063 | Iustin Pop | |
329 | a8083063 | Iustin Pop | |
330 | a8083063 | Iustin Pop | class TestMatchNameComponent(unittest.TestCase): |
331 | a8083063 | Iustin Pop | """Test case for the MatchNameComponent function"""
|
332 | a8083063 | Iustin Pop | |
333 | a8083063 | Iustin Pop | def testEmptyList(self): |
334 | a8083063 | Iustin Pop | """Test that there is no match against an empty list"""
|
335 | a8083063 | Iustin Pop | |
336 | a8083063 | Iustin Pop | self.failUnlessEqual(MatchNameComponent("", []), None) |
337 | a8083063 | Iustin Pop | self.failUnlessEqual(MatchNameComponent("test", []), None) |
338 | a8083063 | Iustin Pop | |
339 | a8083063 | Iustin Pop | def testSingleMatch(self): |
340 | a8083063 | Iustin Pop | """Test that a single match is performed correctly"""
|
341 | a8083063 | Iustin Pop | mlist = ["test1.example.com", "test2.example.com", "test3.example.com"] |
342 | a8083063 | Iustin Pop | for key in "test2", "test2.example", "test2.example.com": |
343 | a8083063 | Iustin Pop | self.failUnlessEqual(MatchNameComponent(key, mlist), mlist[1]) |
344 | a8083063 | Iustin Pop | |
345 | a8083063 | Iustin Pop | def testMultipleMatches(self): |
346 | a8083063 | Iustin Pop | """Test that a multiple match is returned as None"""
|
347 | a8083063 | Iustin Pop | mlist = ["test1.example.com", "test1.example.org", "test1.example.net"] |
348 | a8083063 | Iustin Pop | for key in "test1", "test1.example": |
349 | a8083063 | Iustin Pop | self.failUnlessEqual(MatchNameComponent(key, mlist), None) |
350 | a8083063 | Iustin Pop | |
351 | a8083063 | Iustin Pop | |
352 | a8083063 | Iustin Pop | class TestFormatUnit(unittest.TestCase): |
353 | a8083063 | Iustin Pop | """Test case for the FormatUnit function"""
|
354 | a8083063 | Iustin Pop | |
355 | a8083063 | Iustin Pop | def testMiB(self): |
356 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1, 'h'), '1M') |
357 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(100, 'h'), '100M') |
358 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1023, 'h'), '1023M') |
359 | 9fbfbb7b | Iustin Pop | |
360 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1, 'm'), '1') |
361 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(100, 'm'), '100') |
362 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1023, 'm'), '1023') |
363 | 9fbfbb7b | Iustin Pop | |
364 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1024, 'm'), '1024') |
365 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1536, 'm'), '1536') |
366 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(17133, 'm'), '17133') |
367 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1024 * 1024 - 1, 'm'), '1048575') |
368 | a8083063 | Iustin Pop | |
369 | a8083063 | Iustin Pop | def testGiB(self): |
370 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1024, 'h'), '1.0G') |
371 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1536, 'h'), '1.5G') |
372 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(17133, 'h'), '16.7G') |
373 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1024 * 1024 - 1, 'h'), '1024.0G') |
374 | 9fbfbb7b | Iustin Pop | |
375 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1024, 'g'), '1.0') |
376 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1536, 'g'), '1.5') |
377 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(17133, 'g'), '16.7') |
378 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1024 * 1024 - 1, 'g'), '1024.0') |
379 | 9fbfbb7b | Iustin Pop | |
380 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1024 * 1024, 'g'), '1024.0') |
381 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(5120 * 1024, 'g'), '5120.0') |
382 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(29829 * 1024, 'g'), '29829.0') |
383 | a8083063 | Iustin Pop | |
384 | a8083063 | Iustin Pop | def testTiB(self): |
385 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1024 * 1024, 'h'), '1.0T') |
386 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(5120 * 1024, 'h'), '5.0T') |
387 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(29829 * 1024, 'h'), '29.1T') |
388 | a8083063 | Iustin Pop | |
389 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(1024 * 1024, 't'), '1.0') |
390 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(5120 * 1024, 't'), '5.0') |
391 | 9fbfbb7b | Iustin Pop | self.assertEqual(FormatUnit(29829 * 1024, 't'), '29.1') |
392 | a8083063 | Iustin Pop | |
393 | a8083063 | Iustin Pop | class TestParseUnit(unittest.TestCase): |
394 | a8083063 | Iustin Pop | """Test case for the ParseUnit function"""
|
395 | a8083063 | Iustin Pop | |
396 | a8083063 | Iustin Pop | SCALES = (('', 1), |
397 | a8083063 | Iustin Pop | ('M', 1), ('G', 1024), ('T', 1024 * 1024), |
398 | a8083063 | Iustin Pop | ('MB', 1), ('GB', 1024), ('TB', 1024 * 1024), |
399 | a8083063 | Iustin Pop | ('MiB', 1), ('GiB', 1024), ('TiB', 1024 * 1024)) |
400 | a8083063 | Iustin Pop | |
401 | a8083063 | Iustin Pop | def testRounding(self): |
402 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('0'), 0) |
403 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('1'), 4) |
404 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('2'), 4) |
405 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('3'), 4) |
406 | a8083063 | Iustin Pop | |
407 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('124'), 124) |
408 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('125'), 128) |
409 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('126'), 128) |
410 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('127'), 128) |
411 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('128'), 128) |
412 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('129'), 132) |
413 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('130'), 132) |
414 | a8083063 | Iustin Pop | |
415 | a8083063 | Iustin Pop | def testFloating(self): |
416 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('0'), 0) |
417 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('0.5'), 4) |
418 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('1.75'), 4) |
419 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('1.99'), 4) |
420 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('2.00'), 4) |
421 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('2.01'), 4) |
422 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('3.99'), 4) |
423 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('4.00'), 4) |
424 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('4.01'), 8) |
425 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('1.5G'), 1536) |
426 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('1.8G'), 1844) |
427 | a8083063 | Iustin Pop | self.assertEqual(ParseUnit('8.28T'), 8682212) |
428 | a8083063 | Iustin Pop | |
429 | a8083063 | Iustin Pop | def testSuffixes(self): |
430 | a8083063 | Iustin Pop | for sep in ('', ' ', ' ', "\t", "\t "): |
431 | a8083063 | Iustin Pop | for suffix, scale in TestParseUnit.SCALES: |
432 | a8083063 | Iustin Pop | for func in (lambda x: x, str.lower, str.upper): |
433 | 667479d5 | Michael Hanselmann | self.assertEqual(ParseUnit('1024' + sep + func(suffix)), |
434 | 667479d5 | Michael Hanselmann | 1024 * scale)
|
435 | a8083063 | Iustin Pop | |
436 | a8083063 | Iustin Pop | def testInvalidInput(self): |
437 | a8083063 | Iustin Pop | for sep in ('-', '_', ',', 'a'): |
438 | a8083063 | Iustin Pop | for suffix, _ in TestParseUnit.SCALES: |
439 | a8083063 | Iustin Pop | self.assertRaises(UnitParseError, ParseUnit, '1' + sep + suffix) |
440 | a8083063 | Iustin Pop | |
441 | a8083063 | Iustin Pop | for suffix, _ in TestParseUnit.SCALES: |
442 | a8083063 | Iustin Pop | self.assertRaises(UnitParseError, ParseUnit, '1,3' + suffix) |
443 | a8083063 | Iustin Pop | |
444 | a8083063 | Iustin Pop | |
445 | c9c4f19e | Michael Hanselmann | class TestSshKeys(testutils.GanetiTestCase): |
446 | a8083063 | Iustin Pop | """Test case for the AddAuthorizedKey function"""
|
447 | a8083063 | Iustin Pop | |
448 | a8083063 | Iustin Pop | KEY_A = 'ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a'
|
449 | a8083063 | Iustin Pop | KEY_B = ('command="/usr/bin/fooserver -t --verbose",from="1.2.3.4" '
|
450 | a8083063 | Iustin Pop | 'ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b')
|
451 | a8083063 | Iustin Pop | |
452 | ebe8ef17 | Michael Hanselmann | def setUp(self): |
453 | 51596eb2 | Iustin Pop | testutils.GanetiTestCase.setUp(self)
|
454 | 51596eb2 | Iustin Pop | self.tmpname = self._CreateTempFile() |
455 | 51596eb2 | Iustin Pop | handle = open(self.tmpname, 'w') |
456 | a8083063 | Iustin Pop | try:
|
457 | 51596eb2 | Iustin Pop | handle.write("%s\n" % TestSshKeys.KEY_A)
|
458 | 51596eb2 | Iustin Pop | handle.write("%s\n" % TestSshKeys.KEY_B)
|
459 | 51596eb2 | Iustin Pop | finally:
|
460 | 51596eb2 | Iustin Pop | handle.close() |
461 | a8083063 | Iustin Pop | |
462 | a8083063 | Iustin Pop | def testAddingNewKey(self): |
463 | ebe8ef17 | Michael Hanselmann | AddAuthorizedKey(self.tmpname, 'ssh-dss AAAAB3NzaC1kc3MAAACB root@test') |
464 | a8083063 | Iustin Pop | |
465 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
466 | ebe8ef17 | Michael Hanselmann | "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
|
467 | ebe8ef17 | Michael Hanselmann | 'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
|
468 | ebe8ef17 | Michael Hanselmann | " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n"
|
469 | ebe8ef17 | Michael Hanselmann | "ssh-dss AAAAB3NzaC1kc3MAAACB root@test\n")
|
470 | a8083063 | Iustin Pop | |
471 | f89f17a8 | Michael Hanselmann | def testAddingAlmostButNotCompletelyTheSameKey(self): |
472 | ebe8ef17 | Michael Hanselmann | AddAuthorizedKey(self.tmpname,
|
473 | ebe8ef17 | Michael Hanselmann | 'ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@test')
|
474 | ebe8ef17 | Michael Hanselmann | |
475 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
476 | ebe8ef17 | Michael Hanselmann | "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
|
477 | ebe8ef17 | Michael Hanselmann | 'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
|
478 | ebe8ef17 | Michael Hanselmann | " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n"
|
479 | ebe8ef17 | Michael Hanselmann | "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@test\n")
|
480 | a8083063 | Iustin Pop | |
481 | a8083063 | Iustin Pop | def testAddingExistingKeyWithSomeMoreSpaces(self): |
482 | ebe8ef17 | Michael Hanselmann | AddAuthorizedKey(self.tmpname,
|
483 | ebe8ef17 | Michael Hanselmann | 'ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a')
|
484 | a8083063 | Iustin Pop | |
485 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
486 | ebe8ef17 | Michael Hanselmann | "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
|
487 | ebe8ef17 | Michael Hanselmann | 'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
|
488 | ebe8ef17 | Michael Hanselmann | " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n")
|
489 | a8083063 | Iustin Pop | |
490 | a8083063 | Iustin Pop | def testRemovingExistingKeyWithSomeMoreSpaces(self): |
491 | ebe8ef17 | Michael Hanselmann | RemoveAuthorizedKey(self.tmpname,
|
492 | ebe8ef17 | Michael Hanselmann | 'ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a')
|
493 | a8083063 | Iustin Pop | |
494 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
495 | ebe8ef17 | Michael Hanselmann | 'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
|
496 | ebe8ef17 | Michael Hanselmann | " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n")
|
497 | a8083063 | Iustin Pop | |
498 | a8083063 | Iustin Pop | def testRemovingNonExistingKey(self): |
499 | ebe8ef17 | Michael Hanselmann | RemoveAuthorizedKey(self.tmpname,
|
500 | ebe8ef17 | Michael Hanselmann | 'ssh-dss AAAAB3Nsdfj230xxjxJjsjwjsjdjU root@test')
|
501 | a8083063 | Iustin Pop | |
502 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
503 | ebe8ef17 | Michael Hanselmann | "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
|
504 | ebe8ef17 | Michael Hanselmann | 'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
|
505 | ebe8ef17 | Michael Hanselmann | " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n")
|
506 | a8083063 | Iustin Pop | |
507 | a8083063 | Iustin Pop | |
508 | c9c4f19e | Michael Hanselmann | class TestEtcHosts(testutils.GanetiTestCase): |
509 | 899d2a81 | Michael Hanselmann | """Test functions modifying /etc/hosts"""
|
510 | 899d2a81 | Michael Hanselmann | |
511 | ebe8ef17 | Michael Hanselmann | def setUp(self): |
512 | 51596eb2 | Iustin Pop | testutils.GanetiTestCase.setUp(self)
|
513 | 51596eb2 | Iustin Pop | self.tmpname = self._CreateTempFile() |
514 | 51596eb2 | Iustin Pop | handle = open(self.tmpname, 'w') |
515 | 899d2a81 | Michael Hanselmann | try:
|
516 | 51596eb2 | Iustin Pop | handle.write('# This is a test file for /etc/hosts\n')
|
517 | 51596eb2 | Iustin Pop | handle.write('127.0.0.1\tlocalhost\n')
|
518 | 51596eb2 | Iustin Pop | handle.write('192.168.1.1 router gw\n')
|
519 | 51596eb2 | Iustin Pop | finally:
|
520 | 51596eb2 | Iustin Pop | handle.close() |
521 | 899d2a81 | Michael Hanselmann | |
522 | 9440aeab | Michael Hanselmann | def testSettingNewIp(self): |
523 | ebe8ef17 | Michael Hanselmann | SetEtcHostsEntry(self.tmpname, '1.2.3.4', 'myhost.domain.tld', ['myhost']) |
524 | 899d2a81 | Michael Hanselmann | |
525 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
526 | ebe8ef17 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
527 | ebe8ef17 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
528 | ebe8ef17 | Michael Hanselmann | "192.168.1.1 router gw\n"
|
529 | ebe8ef17 | Michael Hanselmann | "1.2.3.4\tmyhost.domain.tld myhost\n")
|
530 | 9b977740 | Guido Trotter | self.assertFileMode(self.tmpname, 0644) |
531 | 899d2a81 | Michael Hanselmann | |
532 | 9440aeab | Michael Hanselmann | def testSettingExistingIp(self): |
533 | ebe8ef17 | Michael Hanselmann | SetEtcHostsEntry(self.tmpname, '192.168.1.1', 'myhost.domain.tld', |
534 | ebe8ef17 | Michael Hanselmann | ['myhost'])
|
535 | 899d2a81 | Michael Hanselmann | |
536 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
537 | ebe8ef17 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
538 | ebe8ef17 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
539 | ebe8ef17 | Michael Hanselmann | "192.168.1.1\tmyhost.domain.tld myhost\n")
|
540 | 9b977740 | Guido Trotter | self.assertFileMode(self.tmpname, 0644) |
541 | 899d2a81 | Michael Hanselmann | |
542 | 7fbb1f65 | Michael Hanselmann | def testSettingDuplicateName(self): |
543 | 7fbb1f65 | Michael Hanselmann | SetEtcHostsEntry(self.tmpname, '1.2.3.4', 'myhost', ['myhost']) |
544 | 7fbb1f65 | Michael Hanselmann | |
545 | 7fbb1f65 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
546 | 7fbb1f65 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
547 | 7fbb1f65 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
548 | 7fbb1f65 | Michael Hanselmann | "192.168.1.1 router gw\n"
|
549 | 7fbb1f65 | Michael Hanselmann | "1.2.3.4\tmyhost\n")
|
550 | 9b977740 | Guido Trotter | self.assertFileMode(self.tmpname, 0644) |
551 | 7fbb1f65 | Michael Hanselmann | |
552 | 899d2a81 | Michael Hanselmann | def testRemovingExistingHost(self): |
553 | ebe8ef17 | Michael Hanselmann | RemoveEtcHostsEntry(self.tmpname, 'router') |
554 | 899d2a81 | Michael Hanselmann | |
555 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
556 | ebe8ef17 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
557 | ebe8ef17 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
558 | ebe8ef17 | Michael Hanselmann | "192.168.1.1 gw\n")
|
559 | 9b977740 | Guido Trotter | self.assertFileMode(self.tmpname, 0644) |
560 | 899d2a81 | Michael Hanselmann | |
561 | 899d2a81 | Michael Hanselmann | def testRemovingSingleExistingHost(self): |
562 | ebe8ef17 | Michael Hanselmann | RemoveEtcHostsEntry(self.tmpname, 'localhost') |
563 | 899d2a81 | Michael Hanselmann | |
564 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
565 | ebe8ef17 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
566 | ebe8ef17 | Michael Hanselmann | "192.168.1.1 router gw\n")
|
567 | 9b977740 | Guido Trotter | self.assertFileMode(self.tmpname, 0644) |
568 | 899d2a81 | Michael Hanselmann | |
569 | 899d2a81 | Michael Hanselmann | def testRemovingNonExistingHost(self): |
570 | ebe8ef17 | Michael Hanselmann | RemoveEtcHostsEntry(self.tmpname, 'myhost') |
571 | 899d2a81 | Michael Hanselmann | |
572 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
573 | ebe8ef17 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
574 | ebe8ef17 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
575 | ebe8ef17 | Michael Hanselmann | "192.168.1.1 router gw\n")
|
576 | 9b977740 | Guido Trotter | self.assertFileMode(self.tmpname, 0644) |
577 | 899d2a81 | Michael Hanselmann | |
578 | 9440aeab | Michael Hanselmann | def testRemovingAlias(self): |
579 | ebe8ef17 | Michael Hanselmann | RemoveEtcHostsEntry(self.tmpname, 'gw') |
580 | 9440aeab | Michael Hanselmann | |
581 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
582 | ebe8ef17 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
583 | ebe8ef17 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
584 | ebe8ef17 | Michael Hanselmann | "192.168.1.1 router\n")
|
585 | 9b977740 | Guido Trotter | self.assertFileMode(self.tmpname, 0644) |
586 | 9440aeab | Michael Hanselmann | |
587 | 899d2a81 | Michael Hanselmann | |
588 | a8083063 | Iustin Pop | class TestShellQuoting(unittest.TestCase): |
589 | a8083063 | Iustin Pop | """Test case for shell quoting functions"""
|
590 | a8083063 | Iustin Pop | |
591 | a8083063 | Iustin Pop | def testShellQuote(self): |
592 | a8083063 | Iustin Pop | self.assertEqual(ShellQuote('abc'), "abc") |
593 | a8083063 | Iustin Pop | self.assertEqual(ShellQuote('ab"c'), "'ab\"c'") |
594 | a8083063 | Iustin Pop | self.assertEqual(ShellQuote("a'bc"), "'a'\\''bc'") |
595 | a8083063 | Iustin Pop | self.assertEqual(ShellQuote("a b c"), "'a b c'") |
596 | a8083063 | Iustin Pop | self.assertEqual(ShellQuote("a b\\ c"), "'a b\\ c'") |
597 | a8083063 | Iustin Pop | |
598 | a8083063 | Iustin Pop | def testShellQuoteArgs(self): |
599 | a8083063 | Iustin Pop | self.assertEqual(ShellQuoteArgs(['a', 'b', 'c']), "a b c") |
600 | a8083063 | Iustin Pop | self.assertEqual(ShellQuoteArgs(['a', 'b"', 'c']), "a 'b\"' c") |
601 | a8083063 | Iustin Pop | self.assertEqual(ShellQuoteArgs(['a', 'b\'', 'c']), "a 'b'\\\''' c") |
602 | a8083063 | Iustin Pop | |
603 | a8083063 | Iustin Pop | |
604 | 2c30e9d7 | Alexander Schreiber | class TestTcpPing(unittest.TestCase): |
605 | 2c30e9d7 | Alexander Schreiber | """Testcase for TCP version of ping - against listen(2)ing port"""
|
606 | 2c30e9d7 | Alexander Schreiber | |
607 | 2c30e9d7 | Alexander Schreiber | def setUp(self): |
608 | 2c30e9d7 | Alexander Schreiber | self.listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
609 | 16abfbc2 | Alexander Schreiber | self.listener.bind((constants.LOCALHOST_IP_ADDRESS, 0)) |
610 | 2c30e9d7 | Alexander Schreiber | self.listenerport = self.listener.getsockname()[1] |
611 | 2c30e9d7 | Alexander Schreiber | self.listener.listen(1) |
612 | 2c30e9d7 | Alexander Schreiber | |
613 | 2c30e9d7 | Alexander Schreiber | def tearDown(self): |
614 | 2c30e9d7 | Alexander Schreiber | self.listener.shutdown(socket.SHUT_RDWR)
|
615 | 2c30e9d7 | Alexander Schreiber | del self.listener |
616 | 2c30e9d7 | Alexander Schreiber | del self.listenerport |
617 | 2c30e9d7 | Alexander Schreiber | |
618 | 2c30e9d7 | Alexander Schreiber | def testTcpPingToLocalHostAccept(self): |
619 | 16abfbc2 | Alexander Schreiber | self.assert_(TcpPing(constants.LOCALHOST_IP_ADDRESS,
|
620 | 2c30e9d7 | Alexander Schreiber | self.listenerport,
|
621 | 2c30e9d7 | Alexander Schreiber | timeout=10,
|
622 | b15d625f | Iustin Pop | live_port_needed=True,
|
623 | b15d625f | Iustin Pop | source=constants.LOCALHOST_IP_ADDRESS, |
624 | b15d625f | Iustin Pop | ), |
625 | 2c30e9d7 | Alexander Schreiber | "failed to connect to test listener")
|
626 | 2c30e9d7 | Alexander Schreiber | |
627 | b15d625f | Iustin Pop | self.assert_(TcpPing(constants.LOCALHOST_IP_ADDRESS,
|
628 | b15d625f | Iustin Pop | self.listenerport,
|
629 | b15d625f | Iustin Pop | timeout=10,
|
630 | b15d625f | Iustin Pop | live_port_needed=True,
|
631 | b15d625f | Iustin Pop | ), |
632 | b15d625f | Iustin Pop | "failed to connect to test listener (no source)")
|
633 | b15d625f | Iustin Pop | |
634 | 2c30e9d7 | Alexander Schreiber | |
635 | 2c30e9d7 | Alexander Schreiber | class TestTcpPingDeaf(unittest.TestCase): |
636 | 2c30e9d7 | Alexander Schreiber | """Testcase for TCP version of ping - against non listen(2)ing port"""
|
637 | 2c30e9d7 | Alexander Schreiber | |
638 | 2c30e9d7 | Alexander Schreiber | def setUp(self): |
639 | 2c30e9d7 | Alexander Schreiber | self.deaflistener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
640 | 16abfbc2 | Alexander Schreiber | self.deaflistener.bind((constants.LOCALHOST_IP_ADDRESS, 0)) |
641 | 2c30e9d7 | Alexander Schreiber | self.deaflistenerport = self.deaflistener.getsockname()[1] |
642 | 2c30e9d7 | Alexander Schreiber | |
643 | 2c30e9d7 | Alexander Schreiber | def tearDown(self): |
644 | 2c30e9d7 | Alexander Schreiber | del self.deaflistener |
645 | 2c30e9d7 | Alexander Schreiber | del self.deaflistenerport |
646 | 2c30e9d7 | Alexander Schreiber | |
647 | 2c30e9d7 | Alexander Schreiber | def testTcpPingToLocalHostAcceptDeaf(self): |
648 | 16abfbc2 | Alexander Schreiber | self.failIf(TcpPing(constants.LOCALHOST_IP_ADDRESS,
|
649 | 2c30e9d7 | Alexander Schreiber | self.deaflistenerport,
|
650 | 16abfbc2 | Alexander Schreiber | timeout=constants.TCP_PING_TIMEOUT, |
651 | b15d625f | Iustin Pop | live_port_needed=True,
|
652 | b15d625f | Iustin Pop | source=constants.LOCALHOST_IP_ADDRESS, |
653 | b15d625f | Iustin Pop | ), # need successful connect(2)
|
654 | 2c30e9d7 | Alexander Schreiber | "successfully connected to deaf listener")
|
655 | 2c30e9d7 | Alexander Schreiber | |
656 | b15d625f | Iustin Pop | self.failIf(TcpPing(constants.LOCALHOST_IP_ADDRESS,
|
657 | b15d625f | Iustin Pop | self.deaflistenerport,
|
658 | b15d625f | Iustin Pop | timeout=constants.TCP_PING_TIMEOUT, |
659 | b15d625f | Iustin Pop | live_port_needed=True,
|
660 | b15d625f | Iustin Pop | ), # need successful connect(2)
|
661 | b15d625f | Iustin Pop | "successfully connected to deaf listener (no source addr)")
|
662 | b15d625f | Iustin Pop | |
663 | 2c30e9d7 | Alexander Schreiber | def testTcpPingToLocalHostNoAccept(self): |
664 | 16abfbc2 | Alexander Schreiber | self.assert_(TcpPing(constants.LOCALHOST_IP_ADDRESS,
|
665 | 2c30e9d7 | Alexander Schreiber | self.deaflistenerport,
|
666 | 16abfbc2 | Alexander Schreiber | timeout=constants.TCP_PING_TIMEOUT, |
667 | b15d625f | Iustin Pop | live_port_needed=False,
|
668 | b15d625f | Iustin Pop | source=constants.LOCALHOST_IP_ADDRESS, |
669 | b15d625f | Iustin Pop | ), # ECONNREFUSED is OK
|
670 | 2c30e9d7 | Alexander Schreiber | "failed to ping alive host on deaf port")
|
671 | 2c30e9d7 | Alexander Schreiber | |
672 | b15d625f | Iustin Pop | self.assert_(TcpPing(constants.LOCALHOST_IP_ADDRESS,
|
673 | b15d625f | Iustin Pop | self.deaflistenerport,
|
674 | b15d625f | Iustin Pop | timeout=constants.TCP_PING_TIMEOUT, |
675 | b15d625f | Iustin Pop | live_port_needed=False,
|
676 | b15d625f | Iustin Pop | ), # ECONNREFUSED is OK
|
677 | b15d625f | Iustin Pop | "failed to ping alive host on deaf port (no source addr)")
|
678 | b15d625f | Iustin Pop | |
679 | 2c30e9d7 | Alexander Schreiber | |
680 | caad16e2 | Iustin Pop | class TestOwnIpAddress(unittest.TestCase): |
681 | caad16e2 | Iustin Pop | """Testcase for OwnIpAddress"""
|
682 | caad16e2 | Iustin Pop | |
683 | caad16e2 | Iustin Pop | def testOwnLoopback(self): |
684 | caad16e2 | Iustin Pop | """check having the loopback ip"""
|
685 | caad16e2 | Iustin Pop | self.failUnless(OwnIpAddress(constants.LOCALHOST_IP_ADDRESS),
|
686 | caad16e2 | Iustin Pop | "Should own the loopback address")
|
687 | caad16e2 | Iustin Pop | |
688 | caad16e2 | Iustin Pop | def testNowOwnAddress(self): |
689 | caad16e2 | Iustin Pop | """check that I don't own an address"""
|
690 | caad16e2 | Iustin Pop | |
691 | caad16e2 | Iustin Pop | # network 192.0.2.0/24 is reserved for test/documentation as per
|
692 | caad16e2 | Iustin Pop | # rfc 3330, so we *should* not have an address of this range... if
|
693 | caad16e2 | Iustin Pop | # this fails, we should extend the test to multiple addresses
|
694 | caad16e2 | Iustin Pop | DST_IP = "192.0.2.1"
|
695 | caad16e2 | Iustin Pop | self.failIf(OwnIpAddress(DST_IP), "Should not own IP address %s" % DST_IP) |
696 | caad16e2 | Iustin Pop | |
697 | caad16e2 | Iustin Pop | |
698 | eedbda4b | Michael Hanselmann | class TestListVisibleFiles(unittest.TestCase): |
699 | eedbda4b | Michael Hanselmann | """Test case for ListVisibleFiles"""
|
700 | eedbda4b | Michael Hanselmann | |
701 | eedbda4b | Michael Hanselmann | def setUp(self): |
702 | eedbda4b | Michael Hanselmann | self.path = tempfile.mkdtemp()
|
703 | eedbda4b | Michael Hanselmann | |
704 | eedbda4b | Michael Hanselmann | def tearDown(self): |
705 | eedbda4b | Michael Hanselmann | shutil.rmtree(self.path)
|
706 | eedbda4b | Michael Hanselmann | |
707 | eedbda4b | Michael Hanselmann | def _test(self, files, expected): |
708 | eedbda4b | Michael Hanselmann | # Sort a copy
|
709 | eedbda4b | Michael Hanselmann | expected = expected[:] |
710 | eedbda4b | Michael Hanselmann | expected.sort() |
711 | eedbda4b | Michael Hanselmann | |
712 | eedbda4b | Michael Hanselmann | for name in files: |
713 | eedbda4b | Michael Hanselmann | f = open(os.path.join(self.path, name), 'w') |
714 | eedbda4b | Michael Hanselmann | try:
|
715 | eedbda4b | Michael Hanselmann | f.write("Test\n")
|
716 | eedbda4b | Michael Hanselmann | finally:
|
717 | eedbda4b | Michael Hanselmann | f.close() |
718 | eedbda4b | Michael Hanselmann | |
719 | eedbda4b | Michael Hanselmann | found = ListVisibleFiles(self.path)
|
720 | eedbda4b | Michael Hanselmann | found.sort() |
721 | eedbda4b | Michael Hanselmann | |
722 | eedbda4b | Michael Hanselmann | self.assertEqual(found, expected)
|
723 | eedbda4b | Michael Hanselmann | |
724 | eedbda4b | Michael Hanselmann | def testAllVisible(self): |
725 | eedbda4b | Michael Hanselmann | files = ["a", "b", "c"] |
726 | eedbda4b | Michael Hanselmann | expected = files |
727 | eedbda4b | Michael Hanselmann | self._test(files, expected)
|
728 | eedbda4b | Michael Hanselmann | |
729 | eedbda4b | Michael Hanselmann | def testNoneVisible(self): |
730 | eedbda4b | Michael Hanselmann | files = [".a", ".b", ".c"] |
731 | eedbda4b | Michael Hanselmann | expected = [] |
732 | eedbda4b | Michael Hanselmann | self._test(files, expected)
|
733 | eedbda4b | Michael Hanselmann | |
734 | eedbda4b | Michael Hanselmann | def testSomeVisible(self): |
735 | eedbda4b | Michael Hanselmann | files = ["a", "b", ".c"] |
736 | eedbda4b | Michael Hanselmann | expected = ["a", "b"] |
737 | eedbda4b | Michael Hanselmann | self._test(files, expected)
|
738 | eedbda4b | Michael Hanselmann | |
739 | eedbda4b | Michael Hanselmann | |
740 | 24818e8f | Michael Hanselmann | class TestNewUUID(unittest.TestCase): |
741 | 24818e8f | Michael Hanselmann | """Test case for NewUUID"""
|
742 | 59072e7e | Michael Hanselmann | |
743 | 59072e7e | Michael Hanselmann | _re_uuid = re.compile('^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-'
|
744 | 59072e7e | Michael Hanselmann | '[a-f0-9]{4}-[a-f0-9]{12}$')
|
745 | 59072e7e | Michael Hanselmann | |
746 | 59072e7e | Michael Hanselmann | def runTest(self): |
747 | 24818e8f | Michael Hanselmann | self.failUnless(self._re_uuid.match(utils.NewUUID())) |
748 | 59072e7e | Michael Hanselmann | |
749 | 59072e7e | Michael Hanselmann | |
750 | f7414041 | Michael Hanselmann | class TestUniqueSequence(unittest.TestCase): |
751 | f7414041 | Michael Hanselmann | """Test case for UniqueSequence"""
|
752 | f7414041 | Michael Hanselmann | |
753 | f7414041 | Michael Hanselmann | def _test(self, input, expected): |
754 | f7414041 | Michael Hanselmann | self.assertEqual(utils.UniqueSequence(input), expected) |
755 | f7414041 | Michael Hanselmann | |
756 | f7414041 | Michael Hanselmann | def runTest(self): |
757 | f7414041 | Michael Hanselmann | # Ordered input
|
758 | f7414041 | Michael Hanselmann | self._test([1, 2, 3], [1, 2, 3]) |
759 | f7414041 | Michael Hanselmann | self._test([1, 1, 2, 2, 3, 3], [1, 2, 3]) |
760 | f7414041 | Michael Hanselmann | self._test([1, 2, 2, 3], [1, 2, 3]) |
761 | f7414041 | Michael Hanselmann | self._test([1, 2, 3, 3], [1, 2, 3]) |
762 | f7414041 | Michael Hanselmann | |
763 | f7414041 | Michael Hanselmann | # Unordered input
|
764 | f7414041 | Michael Hanselmann | self._test([1, 2, 3, 1, 2, 3], [1, 2, 3]) |
765 | f7414041 | Michael Hanselmann | self._test([1, 1, 2, 3, 3, 1, 2], [1, 2, 3]) |
766 | f7414041 | Michael Hanselmann | |
767 | f7414041 | Michael Hanselmann | # Strings
|
768 | f7414041 | Michael Hanselmann | self._test(["a", "a"], ["a"]) |
769 | f7414041 | Michael Hanselmann | self._test(["a", "b"], ["a", "b"]) |
770 | f7414041 | Michael Hanselmann | self._test(["a", "b", "a"], ["a", "b"]) |
771 | f7414041 | Michael Hanselmann | |
772 | a87b4824 | Michael Hanselmann | |
773 | 7b4126b7 | Iustin Pop | class TestFirstFree(unittest.TestCase): |
774 | 7b4126b7 | Iustin Pop | """Test case for the FirstFree function"""
|
775 | 7b4126b7 | Iustin Pop | |
776 | 7b4126b7 | Iustin Pop | def test(self): |
777 | 7b4126b7 | Iustin Pop | """Test FirstFree"""
|
778 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([0, 1, 3]), 2) |
779 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([]), None) |
780 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([3, 4, 6]), 0) |
781 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([3, 4, 6], base=3), 5) |
782 | 7b4126b7 | Iustin Pop | self.failUnlessRaises(AssertionError, FirstFree, [0, 3, 4, 6], base=3) |
783 | f7414041 | Michael Hanselmann | |
784 | a87b4824 | Michael Hanselmann | |
785 | f65f63ef | Iustin Pop | class TestTailFile(testutils.GanetiTestCase): |
786 | f65f63ef | Iustin Pop | """Test case for the TailFile function"""
|
787 | f65f63ef | Iustin Pop | |
788 | f65f63ef | Iustin Pop | def testEmpty(self): |
789 | f65f63ef | Iustin Pop | fname = self._CreateTempFile()
|
790 | f65f63ef | Iustin Pop | self.failUnlessEqual(TailFile(fname), [])
|
791 | f65f63ef | Iustin Pop | self.failUnlessEqual(TailFile(fname, lines=25), []) |
792 | f65f63ef | Iustin Pop | |
793 | f65f63ef | Iustin Pop | def testAllLines(self): |
794 | f65f63ef | Iustin Pop | data = ["test %d" % i for i in range(30)] |
795 | f65f63ef | Iustin Pop | for i in range(30): |
796 | f65f63ef | Iustin Pop | fname = self._CreateTempFile()
|
797 | f65f63ef | Iustin Pop | fd = open(fname, "w") |
798 | f65f63ef | Iustin Pop | fd.write("\n".join(data[:i]))
|
799 | f65f63ef | Iustin Pop | if i > 0: |
800 | f65f63ef | Iustin Pop | fd.write("\n")
|
801 | f65f63ef | Iustin Pop | fd.close() |
802 | f65f63ef | Iustin Pop | self.failUnlessEqual(TailFile(fname, lines=i), data[:i])
|
803 | f65f63ef | Iustin Pop | |
804 | f65f63ef | Iustin Pop | def testPartialLines(self): |
805 | f65f63ef | Iustin Pop | data = ["test %d" % i for i in range(30)] |
806 | f65f63ef | Iustin Pop | fname = self._CreateTempFile()
|
807 | f65f63ef | Iustin Pop | fd = open(fname, "w") |
808 | f65f63ef | Iustin Pop | fd.write("\n".join(data))
|
809 | f65f63ef | Iustin Pop | fd.write("\n")
|
810 | f65f63ef | Iustin Pop | fd.close() |
811 | f65f63ef | Iustin Pop | for i in range(1, 30): |
812 | f65f63ef | Iustin Pop | self.failUnlessEqual(TailFile(fname, lines=i), data[-i:])
|
813 | f65f63ef | Iustin Pop | |
814 | f65f63ef | Iustin Pop | def testBigFile(self): |
815 | f65f63ef | Iustin Pop | data = ["test %d" % i for i in range(30)] |
816 | f65f63ef | Iustin Pop | fname = self._CreateTempFile()
|
817 | f65f63ef | Iustin Pop | fd = open(fname, "w") |
818 | f65f63ef | Iustin Pop | fd.write("X" * 1048576) |
819 | f65f63ef | Iustin Pop | fd.write("\n")
|
820 | f65f63ef | Iustin Pop | fd.write("\n".join(data))
|
821 | f65f63ef | Iustin Pop | fd.write("\n")
|
822 | f65f63ef | Iustin Pop | fd.close() |
823 | f65f63ef | Iustin Pop | for i in range(1, 30): |
824 | f65f63ef | Iustin Pop | self.failUnlessEqual(TailFile(fname, lines=i), data[-i:])
|
825 | f65f63ef | Iustin Pop | |
826 | f65f63ef | Iustin Pop | |
827 | a87b4824 | Michael Hanselmann | class TestFileLock(unittest.TestCase): |
828 | a87b4824 | Michael Hanselmann | """Test case for the FileLock class"""
|
829 | a87b4824 | Michael Hanselmann | |
830 | a87b4824 | Michael Hanselmann | def setUp(self): |
831 | a87b4824 | Michael Hanselmann | self.tmpfile = tempfile.NamedTemporaryFile()
|
832 | a87b4824 | Michael Hanselmann | self.lock = utils.FileLock(self.tmpfile.name) |
833 | a87b4824 | Michael Hanselmann | |
834 | a87b4824 | Michael Hanselmann | def testSharedNonblocking(self): |
835 | a87b4824 | Michael Hanselmann | self.lock.Shared(blocking=False) |
836 | a87b4824 | Michael Hanselmann | self.lock.Close()
|
837 | a87b4824 | Michael Hanselmann | |
838 | a87b4824 | Michael Hanselmann | def testExclusiveNonblocking(self): |
839 | a87b4824 | Michael Hanselmann | self.lock.Exclusive(blocking=False) |
840 | a87b4824 | Michael Hanselmann | self.lock.Close()
|
841 | a87b4824 | Michael Hanselmann | |
842 | a87b4824 | Michael Hanselmann | def testUnlockNonblocking(self): |
843 | a87b4824 | Michael Hanselmann | self.lock.Unlock(blocking=False) |
844 | a87b4824 | Michael Hanselmann | self.lock.Close()
|
845 | a87b4824 | Michael Hanselmann | |
846 | a87b4824 | Michael Hanselmann | def testSharedBlocking(self): |
847 | a87b4824 | Michael Hanselmann | self.lock.Shared(blocking=True) |
848 | a87b4824 | Michael Hanselmann | self.lock.Close()
|
849 | a87b4824 | Michael Hanselmann | |
850 | a87b4824 | Michael Hanselmann | def testExclusiveBlocking(self): |
851 | a87b4824 | Michael Hanselmann | self.lock.Exclusive(blocking=True) |
852 | a87b4824 | Michael Hanselmann | self.lock.Close()
|
853 | a87b4824 | Michael Hanselmann | |
854 | a87b4824 | Michael Hanselmann | def testUnlockBlocking(self): |
855 | a87b4824 | Michael Hanselmann | self.lock.Unlock(blocking=True) |
856 | a87b4824 | Michael Hanselmann | self.lock.Close()
|
857 | a87b4824 | Michael Hanselmann | |
858 | a87b4824 | Michael Hanselmann | def testSharedExclusiveUnlock(self): |
859 | a87b4824 | Michael Hanselmann | self.lock.Shared(blocking=False) |
860 | a87b4824 | Michael Hanselmann | self.lock.Exclusive(blocking=False) |
861 | a87b4824 | Michael Hanselmann | self.lock.Unlock(blocking=False) |
862 | a87b4824 | Michael Hanselmann | self.lock.Close()
|
863 | a87b4824 | Michael Hanselmann | |
864 | a87b4824 | Michael Hanselmann | def testExclusiveSharedUnlock(self): |
865 | a87b4824 | Michael Hanselmann | self.lock.Exclusive(blocking=False) |
866 | a87b4824 | Michael Hanselmann | self.lock.Shared(blocking=False) |
867 | a87b4824 | Michael Hanselmann | self.lock.Unlock(blocking=False) |
868 | a87b4824 | Michael Hanselmann | self.lock.Close()
|
869 | a87b4824 | Michael Hanselmann | |
870 | a87b4824 | Michael Hanselmann | def testCloseShared(self): |
871 | a87b4824 | Michael Hanselmann | self.lock.Close()
|
872 | a87b4824 | Michael Hanselmann | self.assertRaises(AssertionError, self.lock.Shared, blocking=False) |
873 | a87b4824 | Michael Hanselmann | |
874 | a87b4824 | Michael Hanselmann | def testCloseExclusive(self): |
875 | a87b4824 | Michael Hanselmann | self.lock.Close()
|
876 | a87b4824 | Michael Hanselmann | self.assertRaises(AssertionError, self.lock.Exclusive, blocking=False) |
877 | a87b4824 | Michael Hanselmann | |
878 | a87b4824 | Michael Hanselmann | def testCloseUnlock(self): |
879 | a87b4824 | Michael Hanselmann | self.lock.Close()
|
880 | a87b4824 | Michael Hanselmann | self.assertRaises(AssertionError, self.lock.Unlock, blocking=False) |
881 | a87b4824 | Michael Hanselmann | |
882 | a87b4824 | Michael Hanselmann | |
883 | 739be818 | Michael Hanselmann | class TestTimeFunctions(unittest.TestCase): |
884 | 739be818 | Michael Hanselmann | """Test case for time functions"""
|
885 | 739be818 | Michael Hanselmann | |
886 | 739be818 | Michael Hanselmann | def runTest(self): |
887 | 739be818 | Michael Hanselmann | self.assertEqual(utils.SplitTime(1), (1, 0)) |
888 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(1.5), (1, 500000)) |
889 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(1218448917.4809151), (1218448917, 480915)) |
890 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.48012), (123, 480120)) |
891 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.9996), (123, 999600)) |
892 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.9995), (123, 999500)) |
893 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.9994), (123, 999400)) |
894 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.999999999), (123, 999999)) |
895 | 45bc5e4a | Michael Hanselmann | |
896 | 45bc5e4a | Michael Hanselmann | self.assertRaises(AssertionError, utils.SplitTime, -1) |
897 | 739be818 | Michael Hanselmann | |
898 | 739be818 | Michael Hanselmann | self.assertEqual(utils.MergeTime((1, 0)), 1.0) |
899 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.MergeTime((1, 500000)), 1.5) |
900 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.MergeTime((1218448917, 500000)), 1218448917.5) |
901 | 739be818 | Michael Hanselmann | |
902 | 45bc5e4a | Michael Hanselmann | self.assertEqual(round(utils.MergeTime((1218448917, 481000)), 3), 1218448917.481) |
903 | 45bc5e4a | Michael Hanselmann | self.assertEqual(round(utils.MergeTime((1, 801000)), 3), 1.801) |
904 | 739be818 | Michael Hanselmann | |
905 | 739be818 | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (0, -1)) |
906 | 45bc5e4a | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (0, 1000000)) |
907 | 45bc5e4a | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (0, 9999999)) |
908 | 739be818 | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (-1, 0)) |
909 | 739be818 | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (-9999, 0)) |
910 | 739be818 | Michael Hanselmann | |
911 | 739be818 | Michael Hanselmann | |
912 | a2d2e1a7 | Iustin Pop | class FieldSetTestCase(unittest.TestCase): |
913 | a2d2e1a7 | Iustin Pop | """Test case for FieldSets"""
|
914 | a2d2e1a7 | Iustin Pop | |
915 | a2d2e1a7 | Iustin Pop | def testSimpleMatch(self): |
916 | a2d2e1a7 | Iustin Pop | f = utils.FieldSet("a", "b", "c", "def") |
917 | a2d2e1a7 | Iustin Pop | self.failUnless(f.Matches("a")) |
918 | a2d2e1a7 | Iustin Pop | self.failIf(f.Matches("d"), "Substring matched") |
919 | a2d2e1a7 | Iustin Pop | self.failIf(f.Matches("defghi"), "Prefix string matched") |
920 | a2d2e1a7 | Iustin Pop | self.failIf(f.NonMatching(["b", "c"])) |
921 | a2d2e1a7 | Iustin Pop | self.failIf(f.NonMatching(["a", "b", "c", "def"])) |
922 | a2d2e1a7 | Iustin Pop | self.failUnless(f.NonMatching(["a", "d"])) |
923 | a2d2e1a7 | Iustin Pop | |
924 | a2d2e1a7 | Iustin Pop | def testRegexMatch(self): |
925 | a2d2e1a7 | Iustin Pop | f = utils.FieldSet("a", "b([0-9]+)", "c") |
926 | a2d2e1a7 | Iustin Pop | self.failUnless(f.Matches("b1")) |
927 | a2d2e1a7 | Iustin Pop | self.failUnless(f.Matches("b99")) |
928 | a2d2e1a7 | Iustin Pop | self.failIf(f.Matches("b/1")) |
929 | a2d2e1a7 | Iustin Pop | self.failIf(f.NonMatching(["b12", "c"])) |
930 | a2d2e1a7 | Iustin Pop | self.failUnless(f.NonMatching(["a", "1"])) |
931 | a2d2e1a7 | Iustin Pop | |
932 | a5728081 | Guido Trotter | class TestForceDictType(unittest.TestCase): |
933 | a5728081 | Guido Trotter | """Test case for ForceDictType"""
|
934 | a5728081 | Guido Trotter | |
935 | a5728081 | Guido Trotter | def setUp(self): |
936 | a5728081 | Guido Trotter | self.key_types = {
|
937 | a5728081 | Guido Trotter | 'a': constants.VTYPE_INT,
|
938 | a5728081 | Guido Trotter | 'b': constants.VTYPE_BOOL,
|
939 | a5728081 | Guido Trotter | 'c': constants.VTYPE_STRING,
|
940 | a5728081 | Guido Trotter | 'd': constants.VTYPE_SIZE,
|
941 | a5728081 | Guido Trotter | } |
942 | a5728081 | Guido Trotter | |
943 | a5728081 | Guido Trotter | def _fdt(self, dict, allowed_values=None): |
944 | a5728081 | Guido Trotter | if allowed_values is None: |
945 | a5728081 | Guido Trotter | ForceDictType(dict, self.key_types) |
946 | a5728081 | Guido Trotter | else:
|
947 | a5728081 | Guido Trotter | ForceDictType(dict, self.key_types, allowed_values=allowed_values) |
948 | a5728081 | Guido Trotter | |
949 | a5728081 | Guido Trotter | return dict |
950 | a5728081 | Guido Trotter | |
951 | a5728081 | Guido Trotter | def testSimpleDict(self): |
952 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({}), {}) |
953 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'a': 1}), {'a': 1}) |
954 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'a': '1'}), {'a': 1}) |
955 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'a': 1, 'b': 1}), {'a':1, 'b': True}) |
956 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 1, 'c': 'foo'}), {'b': True, 'c': 'foo'}) |
957 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 1, 'c': False}), {'b': True, 'c': ''}) |
958 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'false'}), {'b': False}) |
959 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'False'}), {'b': False}) |
960 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'true'}), {'b': True}) |
961 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'True'}), {'b': True}) |
962 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'d': '4'}), {'d': 4}) |
963 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'d': '4M'}), {'d': 4}) |
964 | a5728081 | Guido Trotter | |
965 | a5728081 | Guido Trotter | def testErrors(self): |
966 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'a': 'astring'}) |
967 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'c': True}) |
968 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': 'astring'}) |
969 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': '4 L'}) |
970 | a5728081 | Guido Trotter | |
971 | a2d2e1a7 | Iustin Pop | |
972 | a8083063 | Iustin Pop | if __name__ == '__main__': |
973 | a8083063 | Iustin Pop | unittest.main() |