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