root / test / ganeti.utils_unittest.py @ c50645c0
History | View | Annotate | Download (33.3 kB)
1 | a8083063 | Iustin Pop | #!/usr/bin/python
|
---|---|---|---|
2 | a8083063 | Iustin Pop | #
|
3 | a8083063 | Iustin Pop | |
4 | 7831fc5f | Michael Hanselmann | # Copyright (C) 2006, 2007, 2010, 2011 Google Inc.
|
5 | a8083063 | Iustin Pop | #
|
6 | a8083063 | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | a8083063 | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | a8083063 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | a8083063 | Iustin Pop | # (at your option) any later version.
|
10 | a8083063 | Iustin Pop | #
|
11 | a8083063 | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | a8083063 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | a8083063 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | a8083063 | Iustin Pop | # General Public License for more details.
|
15 | a8083063 | Iustin Pop | #
|
16 | a8083063 | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | a8083063 | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | a8083063 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | a8083063 | Iustin Pop | # 02110-1301, USA.
|
20 | a8083063 | Iustin Pop | |
21 | a8083063 | Iustin Pop | |
22 | a8083063 | Iustin Pop | """Script for unittesting the utils module"""
|
23 | a8083063 | Iustin Pop | |
24 | a744b676 | Manuel Franceschini | import errno |
25 | a744b676 | Manuel Franceschini | import fcntl |
26 | a744b676 | Manuel Franceschini | import glob |
27 | a8083063 | Iustin Pop | import os |
28 | a8083063 | Iustin Pop | import os.path |
29 | a744b676 | Manuel Franceschini | import re |
30 | a744b676 | Manuel Franceschini | import shutil |
31 | 740c5aab | Guido Trotter | import signal |
32 | 2c30e9d7 | Alexander Schreiber | import socket |
33 | a744b676 | Manuel Franceschini | import stat |
34 | a744b676 | Manuel Franceschini | import tempfile |
35 | a744b676 | Manuel Franceschini | import time |
36 | a744b676 | Manuel Franceschini | import unittest |
37 | 27e46076 | Michael Hanselmann | import warnings |
38 | 153533f3 | Michael Hanselmann | import random |
39 | 153533f3 | Michael Hanselmann | import operator |
40 | a8083063 | Iustin Pop | |
41 | c9c4f19e | Michael Hanselmann | import testutils |
42 | 16abfbc2 | Alexander Schreiber | from ganeti import constants |
43 | 716a32cb | Guido Trotter | from ganeti import compat |
44 | 59072e7e | Michael Hanselmann | from ganeti import utils |
45 | a5728081 | Guido Trotter | from ganeti import errors |
46 | 3865ca48 | Michael Hanselmann | from ganeti.utils import RunCmd, \ |
47 | 3865ca48 | Michael Hanselmann | FirstFree, \
|
48 | 3865ca48 | Michael Hanselmann | RunParts, \
|
49 | 3865ca48 | Michael Hanselmann | SetEtcHostsEntry, RemoveEtcHostsEntry |
50 | a8083063 | Iustin Pop | |
51 | d9f311d7 | Iustin Pop | |
52 | a8083063 | Iustin Pop | class TestIsProcessAlive(unittest.TestCase): |
53 | a8083063 | Iustin Pop | """Testing case for IsProcessAlive"""
|
54 | 740c5aab | Guido Trotter | |
55 | a8083063 | Iustin Pop | def testExists(self): |
56 | a8083063 | Iustin Pop | mypid = os.getpid() |
57 | a744b676 | Manuel Franceschini | self.assert_(utils.IsProcessAlive(mypid), "can't find myself running") |
58 | a8083063 | Iustin Pop | |
59 | a8083063 | Iustin Pop | def testNotExisting(self): |
60 | 09352fa4 | Iustin Pop | pid_non_existing = os.fork() |
61 | 09352fa4 | Iustin Pop | if pid_non_existing == 0: |
62 | 09352fa4 | Iustin Pop | os._exit(0)
|
63 | 09352fa4 | Iustin Pop | elif pid_non_existing < 0: |
64 | 09352fa4 | Iustin Pop | raise SystemError("can't fork") |
65 | 09352fa4 | Iustin Pop | os.waitpid(pid_non_existing, 0)
|
66 | a744b676 | Manuel Franceschini | self.assertFalse(utils.IsProcessAlive(pid_non_existing),
|
67 | 158206e0 | Manuel Franceschini | "nonexisting process detected")
|
68 | a8083063 | Iustin Pop | |
69 | d9f311d7 | Iustin Pop | |
70 | a01b500b | Michael Hanselmann | class TestGetProcStatusPath(unittest.TestCase): |
71 | a01b500b | Michael Hanselmann | def test(self): |
72 | a01b500b | Michael Hanselmann | self.assert_("/1234/" in utils._GetProcStatusPath(1234)) |
73 | a01b500b | Michael Hanselmann | self.assertNotEqual(utils._GetProcStatusPath(1), |
74 | a01b500b | Michael Hanselmann | utils._GetProcStatusPath(2))
|
75 | a01b500b | Michael Hanselmann | |
76 | a01b500b | Michael Hanselmann | |
77 | a01b500b | Michael Hanselmann | class TestIsProcessHandlingSignal(unittest.TestCase): |
78 | a01b500b | Michael Hanselmann | def setUp(self): |
79 | a01b500b | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
80 | a01b500b | Michael Hanselmann | |
81 | a01b500b | Michael Hanselmann | def tearDown(self): |
82 | a01b500b | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
83 | a01b500b | Michael Hanselmann | |
84 | a01b500b | Michael Hanselmann | def testParseSigsetT(self): |
85 | a01b500b | Michael Hanselmann | self.assertEqual(len(utils._ParseSigsetT("0")), 0) |
86 | a01b500b | Michael Hanselmann | self.assertEqual(utils._ParseSigsetT("1"), set([1])) |
87 | a01b500b | Michael Hanselmann | self.assertEqual(utils._ParseSigsetT("1000a"), set([2, 4, 17])) |
88 | a01b500b | Michael Hanselmann | self.assertEqual(utils._ParseSigsetT("810002"), set([2, 17, 24, ])) |
89 | a01b500b | Michael Hanselmann | self.assertEqual(utils._ParseSigsetT("0000000180000202"), |
90 | a01b500b | Michael Hanselmann | set([2, 10, 32, 33])) |
91 | a01b500b | Michael Hanselmann | self.assertEqual(utils._ParseSigsetT("0000000180000002"), |
92 | a01b500b | Michael Hanselmann | set([2, 32, 33])) |
93 | a01b500b | Michael Hanselmann | self.assertEqual(utils._ParseSigsetT("0000000188000002"), |
94 | a01b500b | Michael Hanselmann | set([2, 28, 32, 33])) |
95 | a01b500b | Michael Hanselmann | self.assertEqual(utils._ParseSigsetT("000000004b813efb"), |
96 | a01b500b | Michael Hanselmann | set([1, 2, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 17, |
97 | a01b500b | Michael Hanselmann | 24, 25, 26, 28, 31])) |
98 | a01b500b | Michael Hanselmann | self.assertEqual(utils._ParseSigsetT("ffffff"), set(range(1, 25))) |
99 | a01b500b | Michael Hanselmann | |
100 | a01b500b | Michael Hanselmann | def testGetProcStatusField(self): |
101 | a01b500b | Michael Hanselmann | for field in ["SigCgt", "Name", "FDSize"]: |
102 | a01b500b | Michael Hanselmann | for value in ["", "0", "cat", " 1234 KB"]: |
103 | a01b500b | Michael Hanselmann | pstatus = "\n".join([
|
104 | a01b500b | Michael Hanselmann | "VmPeak: 999 kB",
|
105 | a01b500b | Michael Hanselmann | "%s: %s" % (field, value),
|
106 | a01b500b | Michael Hanselmann | "TracerPid: 0",
|
107 | a01b500b | Michael Hanselmann | ]) |
108 | a01b500b | Michael Hanselmann | result = utils._GetProcStatusField(pstatus, field) |
109 | a01b500b | Michael Hanselmann | self.assertEqual(result, value.strip())
|
110 | a01b500b | Michael Hanselmann | |
111 | a01b500b | Michael Hanselmann | def test(self): |
112 | 3865ca48 | Michael Hanselmann | sp = utils.PathJoin(self.tmpdir, "status") |
113 | a01b500b | Michael Hanselmann | |
114 | a01b500b | Michael Hanselmann | utils.WriteFile(sp, data="\n".join([
|
115 | a01b500b | Michael Hanselmann | "Name: bash",
|
116 | a01b500b | Michael Hanselmann | "State: S (sleeping)",
|
117 | a01b500b | Michael Hanselmann | "SleepAVG: 98%",
|
118 | a01b500b | Michael Hanselmann | "Pid: 22250",
|
119 | a01b500b | Michael Hanselmann | "PPid: 10858",
|
120 | a01b500b | Michael Hanselmann | "TracerPid: 0",
|
121 | a01b500b | Michael Hanselmann | "SigBlk: 0000000000010000",
|
122 | a01b500b | Michael Hanselmann | "SigIgn: 0000000000384004",
|
123 | a01b500b | Michael Hanselmann | "SigCgt: 000000004b813efb",
|
124 | a01b500b | Michael Hanselmann | "CapEff: 0000000000000000",
|
125 | a01b500b | Michael Hanselmann | ])) |
126 | a01b500b | Michael Hanselmann | |
127 | a01b500b | Michael Hanselmann | self.assert_(utils.IsProcessHandlingSignal(1234, 10, status_path=sp)) |
128 | a01b500b | Michael Hanselmann | |
129 | a01b500b | Michael Hanselmann | def testNoSigCgt(self): |
130 | 3865ca48 | Michael Hanselmann | sp = utils.PathJoin(self.tmpdir, "status") |
131 | a01b500b | Michael Hanselmann | |
132 | a01b500b | Michael Hanselmann | utils.WriteFile(sp, data="\n".join([
|
133 | a01b500b | Michael Hanselmann | "Name: bash",
|
134 | a01b500b | Michael Hanselmann | ])) |
135 | a01b500b | Michael Hanselmann | |
136 | a01b500b | Michael Hanselmann | self.assertRaises(RuntimeError, utils.IsProcessHandlingSignal, |
137 | a01b500b | Michael Hanselmann | 1234, 10, status_path=sp) |
138 | a01b500b | Michael Hanselmann | |
139 | a01b500b | Michael Hanselmann | def testNoSuchFile(self): |
140 | 3865ca48 | Michael Hanselmann | sp = utils.PathJoin(self.tmpdir, "notexist") |
141 | a01b500b | Michael Hanselmann | |
142 | a01b500b | Michael Hanselmann | self.assertFalse(utils.IsProcessHandlingSignal(1234, 10, status_path=sp)) |
143 | a01b500b | Michael Hanselmann | |
144 | a01b500b | Michael Hanselmann | @staticmethod
|
145 | a01b500b | Michael Hanselmann | def _TestRealProcess(): |
146 | a01b500b | Michael Hanselmann | signal.signal(signal.SIGUSR1, signal.SIG_DFL) |
147 | a01b500b | Michael Hanselmann | if utils.IsProcessHandlingSignal(os.getpid(), signal.SIGUSR1):
|
148 | a01b500b | Michael Hanselmann | raise Exception("SIGUSR1 is handled when it should not be") |
149 | a01b500b | Michael Hanselmann | |
150 | a01b500b | Michael Hanselmann | signal.signal(signal.SIGUSR1, lambda signum, frame: None) |
151 | a01b500b | Michael Hanselmann | if not utils.IsProcessHandlingSignal(os.getpid(), signal.SIGUSR1): |
152 | a01b500b | Michael Hanselmann | raise Exception("SIGUSR1 is not handled when it should be") |
153 | a01b500b | Michael Hanselmann | |
154 | a01b500b | Michael Hanselmann | signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
155 | a01b500b | Michael Hanselmann | if utils.IsProcessHandlingSignal(os.getpid(), signal.SIGUSR1):
|
156 | a01b500b | Michael Hanselmann | raise Exception("SIGUSR1 is not handled when it should be") |
157 | a01b500b | Michael Hanselmann | |
158 | a01b500b | Michael Hanselmann | signal.signal(signal.SIGUSR1, signal.SIG_DFL) |
159 | a01b500b | Michael Hanselmann | if utils.IsProcessHandlingSignal(os.getpid(), signal.SIGUSR1):
|
160 | a01b500b | Michael Hanselmann | raise Exception("SIGUSR1 is handled when it should not be") |
161 | a01b500b | Michael Hanselmann | |
162 | a01b500b | Michael Hanselmann | return True |
163 | a01b500b | Michael Hanselmann | |
164 | a01b500b | Michael Hanselmann | def testRealProcess(self): |
165 | a01b500b | Michael Hanselmann | self.assert_(utils.RunInSeparateProcess(self._TestRealProcess)) |
166 | a01b500b | Michael Hanselmann | |
167 | a01b500b | Michael Hanselmann | |
168 | 36117c2b | Iustin Pop | class TestRunCmd(testutils.GanetiTestCase): |
169 | a8083063 | Iustin Pop | """Testing case for the RunCmd function"""
|
170 | a8083063 | Iustin Pop | |
171 | a8083063 | Iustin Pop | def setUp(self): |
172 | 51596eb2 | Iustin Pop | testutils.GanetiTestCase.setUp(self)
|
173 | a8083063 | Iustin Pop | self.magic = time.ctime() + " ganeti test" |
174 | 51596eb2 | Iustin Pop | self.fname = self._CreateTempFile() |
175 | c74cda62 | René Nussbaumer | self.fifo_tmpdir = tempfile.mkdtemp()
|
176 | c74cda62 | René Nussbaumer | self.fifo_file = os.path.join(self.fifo_tmpdir, "ganeti_test_fifo") |
177 | c74cda62 | René Nussbaumer | os.mkfifo(self.fifo_file)
|
178 | c74cda62 | René Nussbaumer | |
179 | c74cda62 | René Nussbaumer | def tearDown(self): |
180 | c74cda62 | René Nussbaumer | shutil.rmtree(self.fifo_tmpdir)
|
181 | 9e691184 | Michael Hanselmann | testutils.GanetiTestCase.tearDown(self)
|
182 | a8083063 | Iustin Pop | |
183 | a8083063 | Iustin Pop | def testOk(self): |
184 | 31ee599c | Michael Hanselmann | """Test successful exit code"""
|
185 | a8083063 | Iustin Pop | result = RunCmd("/bin/sh -c 'exit 0'")
|
186 | a8083063 | Iustin Pop | self.assertEqual(result.exit_code, 0) |
187 | 36117c2b | Iustin Pop | self.assertEqual(result.output, "") |
188 | a8083063 | Iustin Pop | |
189 | a8083063 | Iustin Pop | def testFail(self): |
190 | a8083063 | Iustin Pop | """Test fail exit code"""
|
191 | a8083063 | Iustin Pop | result = RunCmd("/bin/sh -c 'exit 1'")
|
192 | a8083063 | Iustin Pop | self.assertEqual(result.exit_code, 1) |
193 | 36117c2b | Iustin Pop | self.assertEqual(result.output, "") |
194 | a8083063 | Iustin Pop | |
195 | a8083063 | Iustin Pop | def testStdout(self): |
196 | a8083063 | Iustin Pop | """Test standard output"""
|
197 | a8083063 | Iustin Pop | cmd = 'echo -n "%s"' % self.magic |
198 | a8083063 | Iustin Pop | result = RunCmd("/bin/sh -c '%s'" % cmd)
|
199 | a8083063 | Iustin Pop | self.assertEqual(result.stdout, self.magic) |
200 | 36117c2b | Iustin Pop | result = RunCmd("/bin/sh -c '%s'" % cmd, output=self.fname) |
201 | 36117c2b | Iustin Pop | self.assertEqual(result.output, "") |
202 | 36117c2b | Iustin Pop | self.assertFileContent(self.fname, self.magic) |
203 | a8083063 | Iustin Pop | |
204 | a8083063 | Iustin Pop | def testStderr(self): |
205 | a8083063 | Iustin Pop | """Test standard error"""
|
206 | a8083063 | Iustin Pop | cmd = 'echo -n "%s"' % self.magic |
207 | a8083063 | Iustin Pop | result = RunCmd("/bin/sh -c '%s' 1>&2" % cmd)
|
208 | a8083063 | Iustin Pop | self.assertEqual(result.stderr, self.magic) |
209 | 36117c2b | Iustin Pop | result = RunCmd("/bin/sh -c '%s' 1>&2" % cmd, output=self.fname) |
210 | 36117c2b | Iustin Pop | self.assertEqual(result.output, "") |
211 | 36117c2b | Iustin Pop | self.assertFileContent(self.fname, self.magic) |
212 | a8083063 | Iustin Pop | |
213 | a8083063 | Iustin Pop | def testCombined(self): |
214 | a8083063 | Iustin Pop | """Test combined output"""
|
215 | a8083063 | Iustin Pop | cmd = 'echo -n "A%s"; echo -n "B%s" 1>&2' % (self.magic, self.magic) |
216 | 36117c2b | Iustin Pop | expected = "A" + self.magic + "B" + self.magic |
217 | a8083063 | Iustin Pop | result = RunCmd("/bin/sh -c '%s'" % cmd)
|
218 | 36117c2b | Iustin Pop | self.assertEqual(result.output, expected)
|
219 | 36117c2b | Iustin Pop | result = RunCmd("/bin/sh -c '%s'" % cmd, output=self.fname) |
220 | 36117c2b | Iustin Pop | self.assertEqual(result.output, "") |
221 | 36117c2b | Iustin Pop | self.assertFileContent(self.fname, expected) |
222 | a8083063 | Iustin Pop | |
223 | a8083063 | Iustin Pop | def testSignal(self): |
224 | 01fd6005 | Manuel Franceschini | """Test signal"""
|
225 | 01fd6005 | Manuel Franceschini | result = RunCmd(["python", "-c", "import os; os.kill(os.getpid(), 15)"]) |
226 | a8083063 | Iustin Pop | self.assertEqual(result.signal, 15) |
227 | 36117c2b | Iustin Pop | self.assertEqual(result.output, "") |
228 | a8083063 | Iustin Pop | |
229 | c74cda62 | René Nussbaumer | def testTimeoutClean(self): |
230 | c74cda62 | René Nussbaumer | cmd = "trap 'exit 0' TERM; read < %s" % self.fifo_file |
231 | c74cda62 | René Nussbaumer | result = RunCmd(["/bin/sh", "-c", cmd], timeout=0.2) |
232 | c74cda62 | René Nussbaumer | self.assertEqual(result.exit_code, 0) |
233 | c74cda62 | René Nussbaumer | |
234 | c74cda62 | René Nussbaumer | def testTimeoutKill(self): |
235 | 815bf6d5 | Iustin Pop | cmd = ["/bin/sh", "-c", "trap '' TERM; read < %s" % self.fifo_file] |
236 | c74cda62 | René Nussbaumer | timeout = 0.2
|
237 | 815bf6d5 | Iustin Pop | out, err, status, ta = utils._RunCmdPipe(cmd, {}, False, "/", False, |
238 | c74cda62 | René Nussbaumer | timeout, _linger_timeout=0.2)
|
239 | c74cda62 | René Nussbaumer | self.assert_(status < 0) |
240 | c74cda62 | René Nussbaumer | self.assertEqual(-status, signal.SIGKILL)
|
241 | c74cda62 | René Nussbaumer | |
242 | c74cda62 | René Nussbaumer | def testTimeoutOutputAfterTerm(self): |
243 | c74cda62 | René Nussbaumer | cmd = "trap 'echo sigtermed; exit 1' TERM; read < %s" % self.fifo_file |
244 | c74cda62 | René Nussbaumer | result = RunCmd(["/bin/sh", "-c", cmd], timeout=0.2) |
245 | c74cda62 | René Nussbaumer | self.assert_(result.failed)
|
246 | c74cda62 | René Nussbaumer | self.assertEqual(result.stdout, "sigtermed\n") |
247 | c74cda62 | René Nussbaumer | |
248 | 7fcf849f | Iustin Pop | def testListRun(self): |
249 | 7fcf849f | Iustin Pop | """Test list runs"""
|
250 | 7fcf849f | Iustin Pop | result = RunCmd(["true"])
|
251 | 7fcf849f | Iustin Pop | self.assertEqual(result.signal, None) |
252 | 7fcf849f | Iustin Pop | self.assertEqual(result.exit_code, 0) |
253 | 7fcf849f | Iustin Pop | result = RunCmd(["/bin/sh", "-c", "exit 1"]) |
254 | 7fcf849f | Iustin Pop | self.assertEqual(result.signal, None) |
255 | 7fcf849f | Iustin Pop | self.assertEqual(result.exit_code, 1) |
256 | 7fcf849f | Iustin Pop | result = RunCmd(["echo", "-n", self.magic]) |
257 | 7fcf849f | Iustin Pop | self.assertEqual(result.signal, None) |
258 | 7fcf849f | Iustin Pop | self.assertEqual(result.exit_code, 0) |
259 | 7fcf849f | Iustin Pop | self.assertEqual(result.stdout, self.magic) |
260 | 7fcf849f | Iustin Pop | |
261 | 36117c2b | Iustin Pop | def testFileEmptyOutput(self): |
262 | 36117c2b | Iustin Pop | """Test file output"""
|
263 | 36117c2b | Iustin Pop | result = RunCmd(["true"], output=self.fname) |
264 | 36117c2b | Iustin Pop | self.assertEqual(result.signal, None) |
265 | 36117c2b | Iustin Pop | self.assertEqual(result.exit_code, 0) |
266 | 36117c2b | Iustin Pop | self.assertFileContent(self.fname, "") |
267 | 36117c2b | Iustin Pop | |
268 | f6441c7c | Iustin Pop | def testLang(self): |
269 | f6441c7c | Iustin Pop | """Test locale environment"""
|
270 | 23f41a3e | Michael Hanselmann | old_env = os.environ.copy() |
271 | 23f41a3e | Michael Hanselmann | try:
|
272 | 23f41a3e | Michael Hanselmann | os.environ["LANG"] = "en_US.UTF-8" |
273 | 23f41a3e | Michael Hanselmann | os.environ["LC_ALL"] = "en_US.UTF-8" |
274 | 23f41a3e | Michael Hanselmann | result = RunCmd(["locale"])
|
275 | 23f41a3e | Michael Hanselmann | for line in result.output.splitlines(): |
276 | 23f41a3e | Michael Hanselmann | key, value = line.split("=", 1) |
277 | 23f41a3e | Michael Hanselmann | # Ignore these variables, they're overridden by LC_ALL
|
278 | 23f41a3e | Michael Hanselmann | if key == "LANG" or key == "LANGUAGE": |
279 | 23f41a3e | Michael Hanselmann | continue
|
280 | 23f41a3e | Michael Hanselmann | self.failIf(value and value != "C" and value != '"C"', |
281 | 23f41a3e | Michael Hanselmann | "Variable %s is set to the invalid value '%s'" % (key, value))
|
282 | 23f41a3e | Michael Hanselmann | finally:
|
283 | 23f41a3e | Michael Hanselmann | os.environ = old_env |
284 | f6441c7c | Iustin Pop | |
285 | 8797df43 | Iustin Pop | def testDefaultCwd(self): |
286 | 8797df43 | Iustin Pop | """Test default working directory"""
|
287 | 8797df43 | Iustin Pop | self.failUnlessEqual(RunCmd(["pwd"]).stdout.strip(), "/") |
288 | 8797df43 | Iustin Pop | |
289 | 8797df43 | Iustin Pop | def testCwd(self): |
290 | 8797df43 | Iustin Pop | """Test default working directory"""
|
291 | 8797df43 | Iustin Pop | self.failUnlessEqual(RunCmd(["pwd"], cwd="/").stdout.strip(), "/") |
292 | 8797df43 | Iustin Pop | self.failUnlessEqual(RunCmd(["pwd"], cwd="/tmp").stdout.strip(), "/tmp") |
293 | 8797df43 | Iustin Pop | cwd = os.getcwd() |
294 | 8797df43 | Iustin Pop | self.failUnlessEqual(RunCmd(["pwd"], cwd=cwd).stdout.strip(), cwd) |
295 | 8797df43 | Iustin Pop | |
296 | bf4daac9 | Guido Trotter | def testResetEnv(self): |
297 | bf4daac9 | Guido Trotter | """Test environment reset functionality"""
|
298 | bf4daac9 | Guido Trotter | self.failUnlessEqual(RunCmd(["env"], reset_env=True).stdout.strip(), "") |
299 | 0babc371 | Michael Hanselmann | self.failUnlessEqual(RunCmd(["env"], reset_env=True, |
300 | 0babc371 | Michael Hanselmann | env={"FOO": "bar",}).stdout.strip(), "FOO=bar") |
301 | bf4daac9 | Guido Trotter | |
302 | f40ae421 | Iustin Pop | def testNoFork(self): |
303 | f40ae421 | Iustin Pop | """Test that nofork raise an error"""
|
304 | 7b4baeb1 | Michael Hanselmann | self.assertFalse(utils._no_fork)
|
305 | 7b4baeb1 | Michael Hanselmann | utils.DisableFork() |
306 | f40ae421 | Iustin Pop | try:
|
307 | 7b4baeb1 | Michael Hanselmann | self.assertTrue(utils._no_fork)
|
308 | f40ae421 | Iustin Pop | self.assertRaises(errors.ProgrammerError, RunCmd, ["true"]) |
309 | f40ae421 | Iustin Pop | finally:
|
310 | 7b4baeb1 | Michael Hanselmann | utils._no_fork = False
|
311 | f40ae421 | Iustin Pop | |
312 | f40ae421 | Iustin Pop | def testWrongParams(self): |
313 | f40ae421 | Iustin Pop | """Test wrong parameters"""
|
314 | f40ae421 | Iustin Pop | self.assertRaises(errors.ProgrammerError, RunCmd, ["true"], |
315 | f40ae421 | Iustin Pop | output="/dev/null", interactive=True) |
316 | f40ae421 | Iustin Pop | |
317 | a8083063 | Iustin Pop | |
318 | f40ae421 | Iustin Pop | class TestRunParts(testutils.GanetiTestCase): |
319 | 6bb65e3a | Guido Trotter | """Testing case for the RunParts function"""
|
320 | 6bb65e3a | Guido Trotter | |
321 | 6bb65e3a | Guido Trotter | def setUp(self): |
322 | 6bb65e3a | Guido Trotter | self.rundir = tempfile.mkdtemp(prefix="ganeti-test", suffix=".tmp") |
323 | 6bb65e3a | Guido Trotter | |
324 | 6bb65e3a | Guido Trotter | def tearDown(self): |
325 | 6bb65e3a | Guido Trotter | shutil.rmtree(self.rundir)
|
326 | 6bb65e3a | Guido Trotter | |
327 | 6bb65e3a | Guido Trotter | def testEmpty(self): |
328 | 6bb65e3a | Guido Trotter | """Test on an empty dir"""
|
329 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(RunParts(self.rundir, reset_env=True), []) |
330 | 6bb65e3a | Guido Trotter | |
331 | 6bb65e3a | Guido Trotter | def testSkipWrongName(self): |
332 | 6bb65e3a | Guido Trotter | """Test that wrong files are skipped"""
|
333 | 6bb65e3a | Guido Trotter | fname = os.path.join(self.rundir, "00test.dot") |
334 | 6bb65e3a | Guido Trotter | utils.WriteFile(fname, data="")
|
335 | 6bb65e3a | Guido Trotter | os.chmod(fname, stat.S_IREAD | stat.S_IEXEC) |
336 | 6bb65e3a | Guido Trotter | relname = os.path.basename(fname) |
337 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(RunParts(self.rundir, reset_env=True), |
338 | 6bb65e3a | Guido Trotter | [(relname, constants.RUNPARTS_SKIP, None)])
|
339 | 6bb65e3a | Guido Trotter | |
340 | 6bb65e3a | Guido Trotter | def testSkipNonExec(self): |
341 | 6bb65e3a | Guido Trotter | """Test that non executable files are skipped"""
|
342 | 6bb65e3a | Guido Trotter | fname = os.path.join(self.rundir, "00test") |
343 | 6bb65e3a | Guido Trotter | utils.WriteFile(fname, data="")
|
344 | 6bb65e3a | Guido Trotter | relname = os.path.basename(fname) |
345 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(RunParts(self.rundir, reset_env=True), |
346 | 6bb65e3a | Guido Trotter | [(relname, constants.RUNPARTS_SKIP, None)])
|
347 | 6bb65e3a | Guido Trotter | |
348 | 6bb65e3a | Guido Trotter | def testError(self): |
349 | 6bb65e3a | Guido Trotter | """Test error on a broken executable"""
|
350 | 6bb65e3a | Guido Trotter | fname = os.path.join(self.rundir, "00test") |
351 | 6bb65e3a | Guido Trotter | utils.WriteFile(fname, data="")
|
352 | 6bb65e3a | Guido Trotter | os.chmod(fname, stat.S_IREAD | stat.S_IEXEC) |
353 | 6bb65e3a | Guido Trotter | (relname, status, error) = RunParts(self.rundir, reset_env=True)[0] |
354 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(relname, os.path.basename(fname))
|
355 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(status, constants.RUNPARTS_ERR)
|
356 | 6bb65e3a | Guido Trotter | self.failUnless(error)
|
357 | 6bb65e3a | Guido Trotter | |
358 | 6bb65e3a | Guido Trotter | def testSorted(self): |
359 | 6bb65e3a | Guido Trotter | """Test executions are sorted"""
|
360 | 6bb65e3a | Guido Trotter | files = [] |
361 | 6bb65e3a | Guido Trotter | files.append(os.path.join(self.rundir, "64test")) |
362 | 6bb65e3a | Guido Trotter | files.append(os.path.join(self.rundir, "00test")) |
363 | 6bb65e3a | Guido Trotter | files.append(os.path.join(self.rundir, "42test")) |
364 | 6bb65e3a | Guido Trotter | |
365 | 6bb65e3a | Guido Trotter | for fname in files: |
366 | 6bb65e3a | Guido Trotter | utils.WriteFile(fname, data="")
|
367 | 6bb65e3a | Guido Trotter | |
368 | 6bb65e3a | Guido Trotter | results = RunParts(self.rundir, reset_env=True) |
369 | 6bb65e3a | Guido Trotter | |
370 | 6bb65e3a | Guido Trotter | for fname in sorted(files): |
371 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(os.path.basename(fname), results.pop(0)[0]) |
372 | 6bb65e3a | Guido Trotter | |
373 | 6bb65e3a | Guido Trotter | def testOk(self): |
374 | 6bb65e3a | Guido Trotter | """Test correct execution"""
|
375 | 6bb65e3a | Guido Trotter | fname = os.path.join(self.rundir, "00test") |
376 | 6bb65e3a | Guido Trotter | utils.WriteFile(fname, data="#!/bin/sh\n\necho -n ciao")
|
377 | 6bb65e3a | Guido Trotter | os.chmod(fname, stat.S_IREAD | stat.S_IEXEC) |
378 | 6bb65e3a | Guido Trotter | (relname, status, runresult) = RunParts(self.rundir, reset_env=True)[0] |
379 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(relname, os.path.basename(fname))
|
380 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(status, constants.RUNPARTS_RUN)
|
381 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(runresult.stdout, "ciao") |
382 | 6bb65e3a | Guido Trotter | |
383 | 6bb65e3a | Guido Trotter | def testRunFail(self): |
384 | 6bb65e3a | Guido Trotter | """Test correct execution, with run failure"""
|
385 | 6bb65e3a | Guido Trotter | fname = os.path.join(self.rundir, "00test") |
386 | 6bb65e3a | Guido Trotter | utils.WriteFile(fname, data="#!/bin/sh\n\nexit 1")
|
387 | 6bb65e3a | Guido Trotter | os.chmod(fname, stat.S_IREAD | stat.S_IEXEC) |
388 | 6bb65e3a | Guido Trotter | (relname, status, runresult) = RunParts(self.rundir, reset_env=True)[0] |
389 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(relname, os.path.basename(fname))
|
390 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(status, constants.RUNPARTS_RUN)
|
391 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(runresult.exit_code, 1) |
392 | 6bb65e3a | Guido Trotter | self.failUnless(runresult.failed)
|
393 | 6bb65e3a | Guido Trotter | |
394 | 6bb65e3a | Guido Trotter | def testRunMix(self): |
395 | 6bb65e3a | Guido Trotter | files = [] |
396 | 6bb65e3a | Guido Trotter | files.append(os.path.join(self.rundir, "00test")) |
397 | 6bb65e3a | Guido Trotter | files.append(os.path.join(self.rundir, "42test")) |
398 | 6bb65e3a | Guido Trotter | files.append(os.path.join(self.rundir, "64test")) |
399 | 6bb65e3a | Guido Trotter | files.append(os.path.join(self.rundir, "99test")) |
400 | 6bb65e3a | Guido Trotter | |
401 | 6bb65e3a | Guido Trotter | files.sort() |
402 | 6bb65e3a | Guido Trotter | |
403 | 6bb65e3a | Guido Trotter | # 1st has errors in execution
|
404 | 6bb65e3a | Guido Trotter | utils.WriteFile(files[0], data="#!/bin/sh\n\nexit 1") |
405 | 6bb65e3a | Guido Trotter | os.chmod(files[0], stat.S_IREAD | stat.S_IEXEC)
|
406 | 6bb65e3a | Guido Trotter | |
407 | 6bb65e3a | Guido Trotter | # 2nd is skipped
|
408 | 6bb65e3a | Guido Trotter | utils.WriteFile(files[1], data="") |
409 | 6bb65e3a | Guido Trotter | |
410 | 6bb65e3a | Guido Trotter | # 3rd cannot execute properly
|
411 | 6bb65e3a | Guido Trotter | utils.WriteFile(files[2], data="") |
412 | 6bb65e3a | Guido Trotter | os.chmod(files[2], stat.S_IREAD | stat.S_IEXEC)
|
413 | 6bb65e3a | Guido Trotter | |
414 | 6bb65e3a | Guido Trotter | # 4th execs
|
415 | 6bb65e3a | Guido Trotter | utils.WriteFile(files[3], data="#!/bin/sh\n\necho -n ciao") |
416 | 6bb65e3a | Guido Trotter | os.chmod(files[3], stat.S_IREAD | stat.S_IEXEC)
|
417 | 6bb65e3a | Guido Trotter | |
418 | 6bb65e3a | Guido Trotter | results = RunParts(self.rundir, reset_env=True) |
419 | 6bb65e3a | Guido Trotter | |
420 | 6bb65e3a | Guido Trotter | (relname, status, runresult) = results[0]
|
421 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(relname, os.path.basename(files[0])) |
422 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(status, constants.RUNPARTS_RUN)
|
423 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(runresult.exit_code, 1) |
424 | 6bb65e3a | Guido Trotter | self.failUnless(runresult.failed)
|
425 | 6bb65e3a | Guido Trotter | |
426 | 6bb65e3a | Guido Trotter | (relname, status, runresult) = results[1]
|
427 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(relname, os.path.basename(files[1])) |
428 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(status, constants.RUNPARTS_SKIP)
|
429 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(runresult, None) |
430 | 6bb65e3a | Guido Trotter | |
431 | 6bb65e3a | Guido Trotter | (relname, status, runresult) = results[2]
|
432 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(relname, os.path.basename(files[2])) |
433 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(status, constants.RUNPARTS_ERR)
|
434 | 6bb65e3a | Guido Trotter | self.failUnless(runresult)
|
435 | 6bb65e3a | Guido Trotter | |
436 | 6bb65e3a | Guido Trotter | (relname, status, runresult) = results[3]
|
437 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(relname, os.path.basename(files[3])) |
438 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(status, constants.RUNPARTS_RUN)
|
439 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(runresult.output, "ciao") |
440 | 6bb65e3a | Guido Trotter | self.failUnlessEqual(runresult.exit_code, 0) |
441 | 6bb65e3a | Guido Trotter | self.failUnless(not runresult.failed) |
442 | 6bb65e3a | Guido Trotter | |
443 | f40ae421 | Iustin Pop | def testMissingDirectory(self): |
444 | f40ae421 | Iustin Pop | nosuchdir = utils.PathJoin(self.rundir, "no/such/directory") |
445 | f40ae421 | Iustin Pop | self.assertEqual(RunParts(nosuchdir), [])
|
446 | f40ae421 | Iustin Pop | |
447 | a8083063 | Iustin Pop | |
448 | c1dd99d4 | Michael Hanselmann | class TestStartDaemon(testutils.GanetiTestCase): |
449 | c1dd99d4 | Michael Hanselmann | def setUp(self): |
450 | c1dd99d4 | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp(prefix="ganeti-test") |
451 | c1dd99d4 | Michael Hanselmann | self.tmpfile = os.path.join(self.tmpdir, "test") |
452 | c1dd99d4 | Michael Hanselmann | |
453 | c1dd99d4 | Michael Hanselmann | def tearDown(self): |
454 | c1dd99d4 | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
455 | c1dd99d4 | Michael Hanselmann | |
456 | c1dd99d4 | Michael Hanselmann | def testShell(self): |
457 | c1dd99d4 | Michael Hanselmann | utils.StartDaemon("echo Hello World > %s" % self.tmpfile) |
458 | c1dd99d4 | Michael Hanselmann | self._wait(self.tmpfile, 60.0, "Hello World") |
459 | c1dd99d4 | Michael Hanselmann | |
460 | c1dd99d4 | Michael Hanselmann | def testShellOutput(self): |
461 | c1dd99d4 | Michael Hanselmann | utils.StartDaemon("echo Hello World", output=self.tmpfile) |
462 | c1dd99d4 | Michael Hanselmann | self._wait(self.tmpfile, 60.0, "Hello World") |
463 | c1dd99d4 | Michael Hanselmann | |
464 | c1dd99d4 | Michael Hanselmann | def testNoShellNoOutput(self): |
465 | c1dd99d4 | Michael Hanselmann | utils.StartDaemon(["pwd"])
|
466 | c1dd99d4 | Michael Hanselmann | |
467 | c1dd99d4 | Michael Hanselmann | def testNoShellNoOutputTouch(self): |
468 | c1dd99d4 | Michael Hanselmann | testfile = os.path.join(self.tmpdir, "check") |
469 | c1dd99d4 | Michael Hanselmann | self.failIf(os.path.exists(testfile))
|
470 | c1dd99d4 | Michael Hanselmann | utils.StartDaemon(["touch", testfile])
|
471 | c1dd99d4 | Michael Hanselmann | self._wait(testfile, 60.0, "") |
472 | c1dd99d4 | Michael Hanselmann | |
473 | c1dd99d4 | Michael Hanselmann | def testNoShellOutput(self): |
474 | c1dd99d4 | Michael Hanselmann | utils.StartDaemon(["pwd"], output=self.tmpfile) |
475 | c1dd99d4 | Michael Hanselmann | self._wait(self.tmpfile, 60.0, "/") |
476 | c1dd99d4 | Michael Hanselmann | |
477 | c1dd99d4 | Michael Hanselmann | def testNoShellOutputCwd(self): |
478 | c1dd99d4 | Michael Hanselmann | utils.StartDaemon(["pwd"], output=self.tmpfile, cwd=os.getcwd()) |
479 | c1dd99d4 | Michael Hanselmann | self._wait(self.tmpfile, 60.0, os.getcwd()) |
480 | c1dd99d4 | Michael Hanselmann | |
481 | c1dd99d4 | Michael Hanselmann | def testShellEnv(self): |
482 | c1dd99d4 | Michael Hanselmann | utils.StartDaemon("echo \"$GNT_TEST_VAR\"", output=self.tmpfile, |
483 | c1dd99d4 | Michael Hanselmann | env={ "GNT_TEST_VAR": "Hello World", }) |
484 | c1dd99d4 | Michael Hanselmann | self._wait(self.tmpfile, 60.0, "Hello World") |
485 | c1dd99d4 | Michael Hanselmann | |
486 | c1dd99d4 | Michael Hanselmann | def testNoShellEnv(self): |
487 | c1dd99d4 | Michael Hanselmann | utils.StartDaemon(["printenv", "GNT_TEST_VAR"], output=self.tmpfile, |
488 | c1dd99d4 | Michael Hanselmann | env={ "GNT_TEST_VAR": "Hello World", }) |
489 | c1dd99d4 | Michael Hanselmann | self._wait(self.tmpfile, 60.0, "Hello World") |
490 | c1dd99d4 | Michael Hanselmann | |
491 | c1dd99d4 | Michael Hanselmann | def testOutputFd(self): |
492 | c1dd99d4 | Michael Hanselmann | fd = os.open(self.tmpfile, os.O_WRONLY | os.O_CREAT)
|
493 | c1dd99d4 | Michael Hanselmann | try:
|
494 | c1dd99d4 | Michael Hanselmann | utils.StartDaemon(["pwd"], output_fd=fd, cwd=os.getcwd())
|
495 | c1dd99d4 | Michael Hanselmann | finally:
|
496 | c1dd99d4 | Michael Hanselmann | os.close(fd) |
497 | c1dd99d4 | Michael Hanselmann | self._wait(self.tmpfile, 60.0, os.getcwd()) |
498 | c1dd99d4 | Michael Hanselmann | |
499 | c1dd99d4 | Michael Hanselmann | def testPid(self): |
500 | c1dd99d4 | Michael Hanselmann | pid = utils.StartDaemon("echo $$ > %s" % self.tmpfile) |
501 | c1dd99d4 | Michael Hanselmann | self._wait(self.tmpfile, 60.0, str(pid)) |
502 | c1dd99d4 | Michael Hanselmann | |
503 | c1dd99d4 | Michael Hanselmann | def testPidFile(self): |
504 | c1dd99d4 | Michael Hanselmann | pidfile = os.path.join(self.tmpdir, "pid") |
505 | c1dd99d4 | Michael Hanselmann | checkfile = os.path.join(self.tmpdir, "abort") |
506 | c1dd99d4 | Michael Hanselmann | |
507 | c1dd99d4 | Michael Hanselmann | pid = utils.StartDaemon("while sleep 5; do :; done", pidfile=pidfile,
|
508 | c1dd99d4 | Michael Hanselmann | output=self.tmpfile)
|
509 | c1dd99d4 | Michael Hanselmann | try:
|
510 | c1dd99d4 | Michael Hanselmann | fd = os.open(pidfile, os.O_RDONLY) |
511 | c1dd99d4 | Michael Hanselmann | try:
|
512 | c1dd99d4 | Michael Hanselmann | # Check file is locked
|
513 | c1dd99d4 | Michael Hanselmann | self.assertRaises(errors.LockError, utils.LockFile, fd)
|
514 | c1dd99d4 | Michael Hanselmann | |
515 | c1dd99d4 | Michael Hanselmann | pidtext = os.read(fd, 100)
|
516 | c1dd99d4 | Michael Hanselmann | finally:
|
517 | c1dd99d4 | Michael Hanselmann | os.close(fd) |
518 | c1dd99d4 | Michael Hanselmann | |
519 | c1dd99d4 | Michael Hanselmann | self.assertEqual(int(pidtext.strip()), pid) |
520 | c1dd99d4 | Michael Hanselmann | |
521 | c1dd99d4 | Michael Hanselmann | self.assert_(utils.IsProcessAlive(pid))
|
522 | c1dd99d4 | Michael Hanselmann | finally:
|
523 | c1dd99d4 | Michael Hanselmann | # No matter what happens, kill daemon
|
524 | c1dd99d4 | Michael Hanselmann | utils.KillProcess(pid, timeout=5.0, waitpid=False) |
525 | c1dd99d4 | Michael Hanselmann | self.failIf(utils.IsProcessAlive(pid))
|
526 | c1dd99d4 | Michael Hanselmann | |
527 | c1dd99d4 | Michael Hanselmann | self.assertEqual(utils.ReadFile(self.tmpfile), "") |
528 | c1dd99d4 | Michael Hanselmann | |
529 | c1dd99d4 | Michael Hanselmann | def _wait(self, path, timeout, expected): |
530 | c1dd99d4 | Michael Hanselmann | # Due to the asynchronous nature of daemon processes, polling is necessary.
|
531 | c1dd99d4 | Michael Hanselmann | # A timeout makes sure the test doesn't hang forever.
|
532 | c1dd99d4 | Michael Hanselmann | def _CheckFile(): |
533 | c1dd99d4 | Michael Hanselmann | if not (os.path.isfile(path) and |
534 | c1dd99d4 | Michael Hanselmann | utils.ReadFile(path).strip() == expected): |
535 | c1dd99d4 | Michael Hanselmann | raise utils.RetryAgain()
|
536 | c1dd99d4 | Michael Hanselmann | |
537 | c1dd99d4 | Michael Hanselmann | try:
|
538 | c1dd99d4 | Michael Hanselmann | utils.Retry(_CheckFile, (0.01, 1.5, 1.0), timeout) |
539 | c1dd99d4 | Michael Hanselmann | except utils.RetryTimeout:
|
540 | c1dd99d4 | Michael Hanselmann | self.fail("Apparently the daemon didn't run in %s seconds and/or" |
541 | c1dd99d4 | Michael Hanselmann | " didn't write the correct output" % timeout)
|
542 | c1dd99d4 | Michael Hanselmann | |
543 | c1dd99d4 | Michael Hanselmann | def testError(self): |
544 | c1dd99d4 | Michael Hanselmann | self.assertRaises(errors.OpExecError, utils.StartDaemon,
|
545 | c1dd99d4 | Michael Hanselmann | ["./does-NOT-EXIST/here/0123456789"])
|
546 | c1dd99d4 | Michael Hanselmann | self.assertRaises(errors.OpExecError, utils.StartDaemon,
|
547 | c1dd99d4 | Michael Hanselmann | ["./does-NOT-EXIST/here/0123456789"],
|
548 | c1dd99d4 | Michael Hanselmann | output=os.path.join(self.tmpdir, "DIR/NOT/EXIST")) |
549 | c1dd99d4 | Michael Hanselmann | self.assertRaises(errors.OpExecError, utils.StartDaemon,
|
550 | c1dd99d4 | Michael Hanselmann | ["./does-NOT-EXIST/here/0123456789"],
|
551 | c1dd99d4 | Michael Hanselmann | cwd=os.path.join(self.tmpdir, "DIR/NOT/EXIST")) |
552 | c1dd99d4 | Michael Hanselmann | self.assertRaises(errors.OpExecError, utils.StartDaemon,
|
553 | c1dd99d4 | Michael Hanselmann | ["./does-NOT-EXIST/here/0123456789"],
|
554 | c1dd99d4 | Michael Hanselmann | output=os.path.join(self.tmpdir, "DIR/NOT/EXIST")) |
555 | c1dd99d4 | Michael Hanselmann | |
556 | c1dd99d4 | Michael Hanselmann | fd = os.open(self.tmpfile, os.O_WRONLY | os.O_CREAT)
|
557 | c1dd99d4 | Michael Hanselmann | try:
|
558 | c1dd99d4 | Michael Hanselmann | self.assertRaises(errors.ProgrammerError, utils.StartDaemon,
|
559 | c1dd99d4 | Michael Hanselmann | ["./does-NOT-EXIST/here/0123456789"],
|
560 | c1dd99d4 | Michael Hanselmann | output=self.tmpfile, output_fd=fd)
|
561 | c1dd99d4 | Michael Hanselmann | finally:
|
562 | c1dd99d4 | Michael Hanselmann | os.close(fd) |
563 | c1dd99d4 | Michael Hanselmann | |
564 | c1dd99d4 | Michael Hanselmann | |
565 | 31155d60 | Balazs Lecz | class TestParseCpuMask(unittest.TestCase): |
566 | 31155d60 | Balazs Lecz | """Test case for the ParseCpuMask function."""
|
567 | 31155d60 | Balazs Lecz | |
568 | 31155d60 | Balazs Lecz | def testWellFormed(self): |
569 | 31155d60 | Balazs Lecz | self.assertEqual(utils.ParseCpuMask(""), []) |
570 | 31155d60 | Balazs Lecz | self.assertEqual(utils.ParseCpuMask("1"), [1]) |
571 | 31155d60 | Balazs Lecz | self.assertEqual(utils.ParseCpuMask("0-2,4,5-5"), [0,1,2,4,5]) |
572 | 31155d60 | Balazs Lecz | |
573 | 31155d60 | Balazs Lecz | def testInvalidInput(self): |
574 | f40ae421 | Iustin Pop | for data in ["garbage", "0,", "0-1-2", "2-1", "1-a"]: |
575 | f40ae421 | Iustin Pop | self.assertRaises(errors.ParseError, utils.ParseCpuMask, data)
|
576 | f40ae421 | Iustin Pop | |
577 | 31155d60 | Balazs Lecz | |
578 | c9c4f19e | Michael Hanselmann | class TestEtcHosts(testutils.GanetiTestCase): |
579 | 899d2a81 | Michael Hanselmann | """Test functions modifying /etc/hosts"""
|
580 | 899d2a81 | Michael Hanselmann | |
581 | ebe8ef17 | Michael Hanselmann | def setUp(self): |
582 | 51596eb2 | Iustin Pop | testutils.GanetiTestCase.setUp(self)
|
583 | 51596eb2 | Iustin Pop | self.tmpname = self._CreateTempFile() |
584 | 51596eb2 | Iustin Pop | handle = open(self.tmpname, 'w') |
585 | 899d2a81 | Michael Hanselmann | try:
|
586 | 51596eb2 | Iustin Pop | handle.write('# This is a test file for /etc/hosts\n')
|
587 | 51596eb2 | Iustin Pop | handle.write('127.0.0.1\tlocalhost\n')
|
588 | 926feaf1 | Manuel Franceschini | handle.write('192.0.2.1 router gw\n')
|
589 | 51596eb2 | Iustin Pop | finally:
|
590 | 51596eb2 | Iustin Pop | handle.close() |
591 | 899d2a81 | Michael Hanselmann | |
592 | 9440aeab | Michael Hanselmann | def testSettingNewIp(self): |
593 | 926feaf1 | Manuel Franceschini | SetEtcHostsEntry(self.tmpname, '198.51.100.4', 'myhost.example.com', |
594 | 926feaf1 | Manuel Franceschini | ['myhost'])
|
595 | 899d2a81 | Michael Hanselmann | |
596 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
597 | ebe8ef17 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
598 | ebe8ef17 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
599 | 926feaf1 | Manuel Franceschini | "192.0.2.1 router gw\n"
|
600 | 926feaf1 | Manuel Franceschini | "198.51.100.4\tmyhost.example.com myhost\n")
|
601 | 9b977740 | Guido Trotter | self.assertFileMode(self.tmpname, 0644) |
602 | 899d2a81 | Michael Hanselmann | |
603 | 9440aeab | Michael Hanselmann | def testSettingExistingIp(self): |
604 | 926feaf1 | Manuel Franceschini | SetEtcHostsEntry(self.tmpname, '192.0.2.1', 'myhost.example.com', |
605 | ebe8ef17 | Michael Hanselmann | ['myhost'])
|
606 | 899d2a81 | Michael Hanselmann | |
607 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
608 | ebe8ef17 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
609 | ebe8ef17 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
610 | 926feaf1 | Manuel Franceschini | "192.0.2.1\tmyhost.example.com myhost\n")
|
611 | 9b977740 | Guido Trotter | self.assertFileMode(self.tmpname, 0644) |
612 | 899d2a81 | Michael Hanselmann | |
613 | 7fbb1f65 | Michael Hanselmann | def testSettingDuplicateName(self): |
614 | 926feaf1 | Manuel Franceschini | SetEtcHostsEntry(self.tmpname, '198.51.100.4', 'myhost', ['myhost']) |
615 | 7fbb1f65 | Michael Hanselmann | |
616 | 7fbb1f65 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
617 | 7fbb1f65 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
618 | 7fbb1f65 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
619 | 926feaf1 | Manuel Franceschini | "192.0.2.1 router gw\n"
|
620 | 926feaf1 | Manuel Franceschini | "198.51.100.4\tmyhost\n")
|
621 | 9b977740 | Guido Trotter | self.assertFileMode(self.tmpname, 0644) |
622 | 7fbb1f65 | Michael Hanselmann | |
623 | 899d2a81 | Michael Hanselmann | def testRemovingExistingHost(self): |
624 | ebe8ef17 | Michael Hanselmann | RemoveEtcHostsEntry(self.tmpname, 'router') |
625 | 899d2a81 | Michael Hanselmann | |
626 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
627 | ebe8ef17 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
628 | ebe8ef17 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
629 | 926feaf1 | Manuel Franceschini | "192.0.2.1 gw\n")
|
630 | 9b977740 | Guido Trotter | self.assertFileMode(self.tmpname, 0644) |
631 | 899d2a81 | Michael Hanselmann | |
632 | 899d2a81 | Michael Hanselmann | def testRemovingSingleExistingHost(self): |
633 | ebe8ef17 | Michael Hanselmann | RemoveEtcHostsEntry(self.tmpname, 'localhost') |
634 | 899d2a81 | Michael Hanselmann | |
635 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
636 | ebe8ef17 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
637 | 926feaf1 | Manuel Franceschini | "192.0.2.1 router gw\n")
|
638 | 9b977740 | Guido Trotter | self.assertFileMode(self.tmpname, 0644) |
639 | 899d2a81 | Michael Hanselmann | |
640 | 899d2a81 | Michael Hanselmann | def testRemovingNonExistingHost(self): |
641 | ebe8ef17 | Michael Hanselmann | RemoveEtcHostsEntry(self.tmpname, 'myhost') |
642 | 899d2a81 | Michael Hanselmann | |
643 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
644 | ebe8ef17 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
645 | ebe8ef17 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
646 | 926feaf1 | Manuel Franceschini | "192.0.2.1 router gw\n")
|
647 | 9b977740 | Guido Trotter | self.assertFileMode(self.tmpname, 0644) |
648 | 899d2a81 | Michael Hanselmann | |
649 | 9440aeab | Michael Hanselmann | def testRemovingAlias(self): |
650 | ebe8ef17 | Michael Hanselmann | RemoveEtcHostsEntry(self.tmpname, 'gw') |
651 | 9440aeab | Michael Hanselmann | |
652 | ebe8ef17 | Michael Hanselmann | self.assertFileContent(self.tmpname, |
653 | ebe8ef17 | Michael Hanselmann | "# This is a test file for /etc/hosts\n"
|
654 | ebe8ef17 | Michael Hanselmann | "127.0.0.1\tlocalhost\n"
|
655 | 926feaf1 | Manuel Franceschini | "192.0.2.1 router\n")
|
656 | 9b977740 | Guido Trotter | self.assertFileMode(self.tmpname, 0644) |
657 | 9440aeab | Michael Hanselmann | |
658 | 899d2a81 | Michael Hanselmann | |
659 | 1b045f5d | Balazs Lecz | class TestGetMounts(unittest.TestCase): |
660 | 1b045f5d | Balazs Lecz | """Test case for GetMounts()."""
|
661 | 1b045f5d | Balazs Lecz | |
662 | 1b045f5d | Balazs Lecz | TESTDATA = ( |
663 | 1b045f5d | Balazs Lecz | "rootfs / rootfs rw 0 0\n"
|
664 | 1b045f5d | Balazs Lecz | "none /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0\n"
|
665 | 1b045f5d | Balazs Lecz | "none /proc proc rw,nosuid,nodev,noexec,relatime 0 0\n")
|
666 | 1b045f5d | Balazs Lecz | |
667 | 1b045f5d | Balazs Lecz | def setUp(self): |
668 | 1b045f5d | Balazs Lecz | self.tmpfile = tempfile.NamedTemporaryFile()
|
669 | 1b045f5d | Balazs Lecz | utils.WriteFile(self.tmpfile.name, data=self.TESTDATA) |
670 | 1b045f5d | Balazs Lecz | |
671 | 1b045f5d | Balazs Lecz | def testGetMounts(self): |
672 | 1b045f5d | Balazs Lecz | self.assertEqual(utils.GetMounts(filename=self.tmpfile.name), |
673 | 1b045f5d | Balazs Lecz | [ |
674 | 1b045f5d | Balazs Lecz | ("rootfs", "/", "rootfs", "rw"), |
675 | 1b045f5d | Balazs Lecz | ("none", "/sys", "sysfs", "rw,nosuid,nodev,noexec,relatime"), |
676 | 1b045f5d | Balazs Lecz | ("none", "/proc", "proc", "rw,nosuid,nodev,noexec,relatime"), |
677 | 1b045f5d | Balazs Lecz | ]) |
678 | 1b045f5d | Balazs Lecz | |
679 | 24818e8f | Michael Hanselmann | class TestNewUUID(unittest.TestCase): |
680 | 24818e8f | Michael Hanselmann | """Test case for NewUUID"""
|
681 | 59072e7e | Michael Hanselmann | |
682 | 59072e7e | Michael Hanselmann | def runTest(self): |
683 | 05636402 | Guido Trotter | self.failUnless(utils.UUID_RE.match(utils.NewUUID()))
|
684 | 59072e7e | Michael Hanselmann | |
685 | 59072e7e | Michael Hanselmann | |
686 | 7b4126b7 | Iustin Pop | class TestFirstFree(unittest.TestCase): |
687 | 7b4126b7 | Iustin Pop | """Test case for the FirstFree function"""
|
688 | 7b4126b7 | Iustin Pop | |
689 | 7b4126b7 | Iustin Pop | def test(self): |
690 | 7b4126b7 | Iustin Pop | """Test FirstFree"""
|
691 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([0, 1, 3]), 2) |
692 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([]), None) |
693 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([3, 4, 6]), 0) |
694 | 7b4126b7 | Iustin Pop | self.failUnlessEqual(FirstFree([3, 4, 6], base=3), 5) |
695 | 7b4126b7 | Iustin Pop | self.failUnlessRaises(AssertionError, FirstFree, [0, 3, 4, 6], base=3) |
696 | f7414041 | Michael Hanselmann | |
697 | a87b4824 | Michael Hanselmann | |
698 | 739be818 | Michael Hanselmann | class TestTimeFunctions(unittest.TestCase): |
699 | 739be818 | Michael Hanselmann | """Test case for time functions"""
|
700 | 739be818 | Michael Hanselmann | |
701 | 739be818 | Michael Hanselmann | def runTest(self): |
702 | 739be818 | Michael Hanselmann | self.assertEqual(utils.SplitTime(1), (1, 0)) |
703 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(1.5), (1, 500000)) |
704 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(1218448917.4809151), (1218448917, 480915)) |
705 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.48012), (123, 480120)) |
706 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.9996), (123, 999600)) |
707 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.9995), (123, 999500)) |
708 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.9994), (123, 999400)) |
709 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.SplitTime(123.999999999), (123, 999999)) |
710 | 45bc5e4a | Michael Hanselmann | |
711 | 45bc5e4a | Michael Hanselmann | self.assertRaises(AssertionError, utils.SplitTime, -1) |
712 | 739be818 | Michael Hanselmann | |
713 | 739be818 | Michael Hanselmann | self.assertEqual(utils.MergeTime((1, 0)), 1.0) |
714 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.MergeTime((1, 500000)), 1.5) |
715 | 45bc5e4a | Michael Hanselmann | self.assertEqual(utils.MergeTime((1218448917, 500000)), 1218448917.5) |
716 | 739be818 | Michael Hanselmann | |
717 | 4d4a651d | Michael Hanselmann | self.assertEqual(round(utils.MergeTime((1218448917, 481000)), 3), |
718 | 4d4a651d | Michael Hanselmann | 1218448917.481)
|
719 | 45bc5e4a | Michael Hanselmann | self.assertEqual(round(utils.MergeTime((1, 801000)), 3), 1.801) |
720 | 739be818 | Michael Hanselmann | |
721 | 739be818 | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (0, -1)) |
722 | 45bc5e4a | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (0, 1000000)) |
723 | 45bc5e4a | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (0, 9999999)) |
724 | 739be818 | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (-1, 0)) |
725 | 739be818 | Michael Hanselmann | self.assertRaises(AssertionError, utils.MergeTime, (-9999, 0)) |
726 | 739be818 | Michael Hanselmann | |
727 | 739be818 | Michael Hanselmann | |
728 | a2d2e1a7 | Iustin Pop | class FieldSetTestCase(unittest.TestCase): |
729 | a2d2e1a7 | Iustin Pop | """Test case for FieldSets"""
|
730 | a2d2e1a7 | Iustin Pop | |
731 | a2d2e1a7 | Iustin Pop | def testSimpleMatch(self): |
732 | a2d2e1a7 | Iustin Pop | f = utils.FieldSet("a", "b", "c", "def") |
733 | a2d2e1a7 | Iustin Pop | self.failUnless(f.Matches("a")) |
734 | a2d2e1a7 | Iustin Pop | self.failIf(f.Matches("d"), "Substring matched") |
735 | a2d2e1a7 | Iustin Pop | self.failIf(f.Matches("defghi"), "Prefix string matched") |
736 | a2d2e1a7 | Iustin Pop | self.failIf(f.NonMatching(["b", "c"])) |
737 | a2d2e1a7 | Iustin Pop | self.failIf(f.NonMatching(["a", "b", "c", "def"])) |
738 | a2d2e1a7 | Iustin Pop | self.failUnless(f.NonMatching(["a", "d"])) |
739 | a2d2e1a7 | Iustin Pop | |
740 | a2d2e1a7 | Iustin Pop | def testRegexMatch(self): |
741 | a2d2e1a7 | Iustin Pop | f = utils.FieldSet("a", "b([0-9]+)", "c") |
742 | a2d2e1a7 | Iustin Pop | self.failUnless(f.Matches("b1")) |
743 | a2d2e1a7 | Iustin Pop | self.failUnless(f.Matches("b99")) |
744 | a2d2e1a7 | Iustin Pop | self.failIf(f.Matches("b/1")) |
745 | a2d2e1a7 | Iustin Pop | self.failIf(f.NonMatching(["b12", "c"])) |
746 | a2d2e1a7 | Iustin Pop | self.failUnless(f.NonMatching(["a", "1"])) |
747 | a2d2e1a7 | Iustin Pop | |
748 | a5728081 | Guido Trotter | class TestForceDictType(unittest.TestCase): |
749 | a5728081 | Guido Trotter | """Test case for ForceDictType"""
|
750 | f40ae421 | Iustin Pop | KEY_TYPES = { |
751 | f40ae421 | Iustin Pop | "a": constants.VTYPE_INT,
|
752 | f40ae421 | Iustin Pop | "b": constants.VTYPE_BOOL,
|
753 | f40ae421 | Iustin Pop | "c": constants.VTYPE_STRING,
|
754 | f40ae421 | Iustin Pop | "d": constants.VTYPE_SIZE,
|
755 | f40ae421 | Iustin Pop | "e": constants.VTYPE_MAYBE_STRING,
|
756 | f40ae421 | Iustin Pop | } |
757 | a5728081 | Guido Trotter | |
758 | a5728081 | Guido Trotter | def _fdt(self, dict, allowed_values=None): |
759 | a5728081 | Guido Trotter | if allowed_values is None: |
760 | f40ae421 | Iustin Pop | utils.ForceDictType(dict, self.KEY_TYPES) |
761 | a5728081 | Guido Trotter | else:
|
762 | f40ae421 | Iustin Pop | utils.ForceDictType(dict, self.KEY_TYPES, allowed_values=allowed_values) |
763 | a5728081 | Guido Trotter | |
764 | a5728081 | Guido Trotter | return dict |
765 | a5728081 | Guido Trotter | |
766 | a5728081 | Guido Trotter | def testSimpleDict(self): |
767 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({}), {}) |
768 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'a': 1}), {'a': 1}) |
769 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'a': '1'}), {'a': 1}) |
770 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'a': 1, 'b': 1}), {'a':1, 'b': True}) |
771 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 1, 'c': 'foo'}), {'b': True, 'c': 'foo'}) |
772 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 1, 'c': False}), {'b': True, 'c': ''}) |
773 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'false'}), {'b': False}) |
774 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'False'}), {'b': False}) |
775 | f40ae421 | Iustin Pop | self.assertEqual(self._fdt({'b': False}), {'b': False}) |
776 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'true'}), {'b': True}) |
777 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'b': 'True'}), {'b': True}) |
778 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'d': '4'}), {'d': 4}) |
779 | a5728081 | Guido Trotter | self.assertEqual(self._fdt({'d': '4M'}), {'d': 4}) |
780 | 59525e1f | Michael Hanselmann | self.assertEqual(self._fdt({"e": None, }), {"e": None, }) |
781 | 59525e1f | Michael Hanselmann | self.assertEqual(self._fdt({"e": "Hello World", }), {"e": "Hello World", }) |
782 | 59525e1f | Michael Hanselmann | self.assertEqual(self._fdt({"e": False, }), {"e": '', }) |
783 | f40ae421 | Iustin Pop | self.assertEqual(self._fdt({"b": "hello", }, ["hello"]), {"b": "hello"}) |
784 | a5728081 | Guido Trotter | |
785 | a5728081 | Guido Trotter | def testErrors(self): |
786 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'a': 'astring'}) |
787 | f40ae421 | Iustin Pop | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"b": "hello"}) |
788 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'c': True}) |
789 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': 'astring'}) |
790 | a5728081 | Guido Trotter | self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': '4 L'}) |
791 | 59525e1f | Michael Hanselmann | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"e": object(), }) |
792 | 59525e1f | Michael Hanselmann | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"e": [], }) |
793 | f40ae421 | Iustin Pop | self.assertRaises(errors.TypeEnforcementError, self._fdt, {"x": None, }) |
794 | f40ae421 | Iustin Pop | self.assertRaises(errors.TypeEnforcementError, self._fdt, []) |
795 | f40ae421 | Iustin Pop | self.assertRaises(errors.ProgrammerError, utils.ForceDictType,
|
796 | f40ae421 | Iustin Pop | {"b": "hello"}, {"b": "no-such-type"}) |
797 | a5728081 | Guido Trotter | |
798 | a2d2e1a7 | Iustin Pop | |
799 | eb58f7bd | Michael Hanselmann | class RunInSeparateProcess(unittest.TestCase): |
800 | eb58f7bd | Michael Hanselmann | def test(self): |
801 | eb58f7bd | Michael Hanselmann | for exp in [True, False]: |
802 | eb58f7bd | Michael Hanselmann | def _child(): |
803 | eb58f7bd | Michael Hanselmann | return exp
|
804 | eb58f7bd | Michael Hanselmann | |
805 | eb58f7bd | Michael Hanselmann | self.assertEqual(exp, utils.RunInSeparateProcess(_child))
|
806 | eb58f7bd | Michael Hanselmann | |
807 | bdefe5dd | Michael Hanselmann | def testArgs(self): |
808 | bdefe5dd | Michael Hanselmann | for arg in [0, 1, 999, "Hello World", (1, 2, 3)]: |
809 | bdefe5dd | Michael Hanselmann | def _child(carg1, carg2): |
810 | bdefe5dd | Michael Hanselmann | return carg1 == "Foo" and carg2 == arg |
811 | bdefe5dd | Michael Hanselmann | |
812 | bdefe5dd | Michael Hanselmann | self.assert_(utils.RunInSeparateProcess(_child, "Foo", arg)) |
813 | bdefe5dd | Michael Hanselmann | |
814 | eb58f7bd | Michael Hanselmann | def testPid(self): |
815 | eb58f7bd | Michael Hanselmann | parent_pid = os.getpid() |
816 | eb58f7bd | Michael Hanselmann | |
817 | eb58f7bd | Michael Hanselmann | def _check(): |
818 | eb58f7bd | Michael Hanselmann | return os.getpid() == parent_pid
|
819 | eb58f7bd | Michael Hanselmann | |
820 | eb58f7bd | Michael Hanselmann | self.failIf(utils.RunInSeparateProcess(_check))
|
821 | eb58f7bd | Michael Hanselmann | |
822 | eb58f7bd | Michael Hanselmann | def testSignal(self): |
823 | eb58f7bd | Michael Hanselmann | def _kill(): |
824 | eb58f7bd | Michael Hanselmann | os.kill(os.getpid(), signal.SIGTERM) |
825 | eb58f7bd | Michael Hanselmann | |
826 | eb58f7bd | Michael Hanselmann | self.assertRaises(errors.GenericError,
|
827 | eb58f7bd | Michael Hanselmann | utils.RunInSeparateProcess, _kill) |
828 | eb58f7bd | Michael Hanselmann | |
829 | eb58f7bd | Michael Hanselmann | def testException(self): |
830 | eb58f7bd | Michael Hanselmann | def _exc(): |
831 | eb58f7bd | Michael Hanselmann | raise errors.GenericError("This is a test") |
832 | eb58f7bd | Michael Hanselmann | |
833 | eb58f7bd | Michael Hanselmann | self.assertRaises(errors.GenericError,
|
834 | eb58f7bd | Michael Hanselmann | utils.RunInSeparateProcess, _exc) |
835 | eb58f7bd | Michael Hanselmann | |
836 | eb58f7bd | Michael Hanselmann | |
837 | 28f34048 | Michael Hanselmann | class TestValidateServiceName(unittest.TestCase): |
838 | 28f34048 | Michael Hanselmann | def testValid(self): |
839 | 28f34048 | Michael Hanselmann | testnames = [ |
840 | 28f34048 | Michael Hanselmann | 0, 1, 2, 3, 1024, 65000, 65534, 65535, |
841 | 28f34048 | Michael Hanselmann | "ganeti",
|
842 | 28f34048 | Michael Hanselmann | "gnt-masterd",
|
843 | 28f34048 | Michael Hanselmann | "HELLO_WORLD_SVC",
|
844 | 28f34048 | Michael Hanselmann | "hello.world.1",
|
845 | 28f34048 | Michael Hanselmann | "0", "80", "1111", "65535", |
846 | 28f34048 | Michael Hanselmann | ] |
847 | 28f34048 | Michael Hanselmann | |
848 | 28f34048 | Michael Hanselmann | for name in testnames: |
849 | 28f34048 | Michael Hanselmann | self.assertEqual(utils.ValidateServiceName(name), name)
|
850 | 28f34048 | Michael Hanselmann | |
851 | 28f34048 | Michael Hanselmann | def testInvalid(self): |
852 | 28f34048 | Michael Hanselmann | testnames = [ |
853 | 28f34048 | Michael Hanselmann | -15756, -1, 65536, 133428083, |
854 | 28f34048 | Michael Hanselmann | "", "Hello World!", "!", "'", "\"", "\t", "\n", "`", |
855 | 28f34048 | Michael Hanselmann | "-8546", "-1", "65536", |
856 | 28f34048 | Michael Hanselmann | (129 * "A"), |
857 | 28f34048 | Michael Hanselmann | ] |
858 | 28f34048 | Michael Hanselmann | |
859 | 28f34048 | Michael Hanselmann | for name in testnames: |
860 | a744b676 | Manuel Franceschini | self.assertRaises(errors.OpPrereqError, utils.ValidateServiceName, name)
|
861 | 28f34048 | Michael Hanselmann | |
862 | 28f34048 | Michael Hanselmann | |
863 | debed9ae | Michael Hanselmann | class TestReadLockedPidFile(unittest.TestCase): |
864 | debed9ae | Michael Hanselmann | def setUp(self): |
865 | debed9ae | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
866 | debed9ae | Michael Hanselmann | |
867 | debed9ae | Michael Hanselmann | def tearDown(self): |
868 | debed9ae | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
869 | debed9ae | Michael Hanselmann | |
870 | debed9ae | Michael Hanselmann | def testNonExistent(self): |
871 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "nonexist") |
872 | debed9ae | Michael Hanselmann | self.assert_(utils.ReadLockedPidFile(path) is None) |
873 | debed9ae | Michael Hanselmann | |
874 | debed9ae | Michael Hanselmann | def testUnlocked(self): |
875 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "pid") |
876 | debed9ae | Michael Hanselmann | utils.WriteFile(path, data="123")
|
877 | debed9ae | Michael Hanselmann | self.assert_(utils.ReadLockedPidFile(path) is None) |
878 | debed9ae | Michael Hanselmann | |
879 | debed9ae | Michael Hanselmann | def testLocked(self): |
880 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "pid") |
881 | debed9ae | Michael Hanselmann | utils.WriteFile(path, data="123")
|
882 | debed9ae | Michael Hanselmann | |
883 | debed9ae | Michael Hanselmann | fl = utils.FileLock.Open(path) |
884 | debed9ae | Michael Hanselmann | try:
|
885 | debed9ae | Michael Hanselmann | fl.Exclusive(blocking=True)
|
886 | debed9ae | Michael Hanselmann | |
887 | debed9ae | Michael Hanselmann | self.assertEqual(utils.ReadLockedPidFile(path), 123) |
888 | debed9ae | Michael Hanselmann | finally:
|
889 | debed9ae | Michael Hanselmann | fl.Close() |
890 | debed9ae | Michael Hanselmann | |
891 | debed9ae | Michael Hanselmann | self.assert_(utils.ReadLockedPidFile(path) is None) |
892 | debed9ae | Michael Hanselmann | |
893 | debed9ae | Michael Hanselmann | def testError(self): |
894 | 3865ca48 | Michael Hanselmann | path = utils.PathJoin(self.tmpdir, "foobar", "pid") |
895 | 3865ca48 | Michael Hanselmann | utils.WriteFile(utils.PathJoin(self.tmpdir, "foobar"), data="") |
896 | debed9ae | Michael Hanselmann | # open(2) should return ENOTDIR
|
897 | debed9ae | Michael Hanselmann | self.assertRaises(EnvironmentError, utils.ReadLockedPidFile, path) |
898 | debed9ae | Michael Hanselmann | |
899 | debed9ae | Michael Hanselmann | |
900 | 691c81b7 | Michael Hanselmann | class TestFindMatch(unittest.TestCase): |
901 | 691c81b7 | Michael Hanselmann | def test(self): |
902 | 691c81b7 | Michael Hanselmann | data = { |
903 | 691c81b7 | Michael Hanselmann | "aaaa": "Four A", |
904 | 691c81b7 | Michael Hanselmann | "bb": {"Two B": True}, |
905 | 691c81b7 | Michael Hanselmann | re.compile(r"^x(foo|bar|bazX)([0-9]+)$"): (1, 2, 3), |
906 | 691c81b7 | Michael Hanselmann | } |
907 | 691c81b7 | Michael Hanselmann | |
908 | 691c81b7 | Michael Hanselmann | self.assertEqual(utils.FindMatch(data, "aaaa"), ("Four A", [])) |
909 | 691c81b7 | Michael Hanselmann | self.assertEqual(utils.FindMatch(data, "bb"), ({"Two B": True}, [])) |
910 | 691c81b7 | Michael Hanselmann | |
911 | 691c81b7 | Michael Hanselmann | for i in ["foo", "bar", "bazX"]: |
912 | 691c81b7 | Michael Hanselmann | for j in range(1, 100, 7): |
913 | 691c81b7 | Michael Hanselmann | self.assertEqual(utils.FindMatch(data, "x%s%s" % (i, j)), |
914 | 691c81b7 | Michael Hanselmann | ((1, 2, 3), [i, str(j)])) |
915 | 691c81b7 | Michael Hanselmann | |
916 | 691c81b7 | Michael Hanselmann | def testNoMatch(self): |
917 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch({}, "") is None) |
918 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch({}, "foo") is None) |
919 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch({}, 1234) is None) |
920 | 691c81b7 | Michael Hanselmann | |
921 | 691c81b7 | Michael Hanselmann | data = { |
922 | 691c81b7 | Michael Hanselmann | "X": "Hello World", |
923 | 691c81b7 | Michael Hanselmann | re.compile("^(something)$"): "Hello World", |
924 | 691c81b7 | Michael Hanselmann | } |
925 | 691c81b7 | Michael Hanselmann | |
926 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch(data, "") is None) |
927 | 691c81b7 | Michael Hanselmann | self.assert_(utils.FindMatch(data, "Hello World") is None) |
928 | 691c81b7 | Michael Hanselmann | |
929 | 691c81b7 | Michael Hanselmann | |
930 | e6784773 | René Nussbaumer | class TimeMock: |
931 | e6784773 | René Nussbaumer | def __init__(self, values): |
932 | e6784773 | René Nussbaumer | self.values = values
|
933 | e6784773 | René Nussbaumer | |
934 | e6784773 | René Nussbaumer | def __call__(self): |
935 | e6784773 | René Nussbaumer | return self.values.pop(0) |
936 | e6784773 | René Nussbaumer | |
937 | e6784773 | René Nussbaumer | |
938 | e6784773 | René Nussbaumer | class TestRunningTimeout(unittest.TestCase): |
939 | e6784773 | René Nussbaumer | def setUp(self): |
940 | e6784773 | René Nussbaumer | self.time_fn = TimeMock([0.0, 0.3, 4.6, 6.5]) |
941 | e6784773 | René Nussbaumer | |
942 | e6784773 | René Nussbaumer | def testRemainingFloat(self): |
943 | e6784773 | René Nussbaumer | timeout = utils.RunningTimeout(5.0, True, _time_fn=self.time_fn) |
944 | e6784773 | René Nussbaumer | self.assertAlmostEqual(timeout.Remaining(), 4.7) |
945 | e6784773 | René Nussbaumer | self.assertAlmostEqual(timeout.Remaining(), 0.4) |
946 | e6784773 | René Nussbaumer | self.assertAlmostEqual(timeout.Remaining(), -1.5) |
947 | e6784773 | René Nussbaumer | |
948 | e6784773 | René Nussbaumer | def testRemaining(self): |
949 | e6784773 | René Nussbaumer | self.time_fn = TimeMock([0, 2, 4, 5, 6]) |
950 | e6784773 | René Nussbaumer | timeout = utils.RunningTimeout(5, True, _time_fn=self.time_fn) |
951 | e6784773 | René Nussbaumer | self.assertEqual(timeout.Remaining(), 3) |
952 | e6784773 | René Nussbaumer | self.assertEqual(timeout.Remaining(), 1) |
953 | e6784773 | René Nussbaumer | self.assertEqual(timeout.Remaining(), 0) |
954 | e6784773 | René Nussbaumer | self.assertEqual(timeout.Remaining(), -1) |
955 | e6784773 | René Nussbaumer | |
956 | e6784773 | René Nussbaumer | def testRemainingNonNegative(self): |
957 | e6784773 | René Nussbaumer | timeout = utils.RunningTimeout(5.0, False, _time_fn=self.time_fn) |
958 | e6784773 | René Nussbaumer | self.assertAlmostEqual(timeout.Remaining(), 4.7) |
959 | e6784773 | René Nussbaumer | self.assertAlmostEqual(timeout.Remaining(), 0.4) |
960 | e6784773 | René Nussbaumer | self.assertEqual(timeout.Remaining(), 0.0) |
961 | e6784773 | René Nussbaumer | |
962 | e6784773 | René Nussbaumer | def testNegativeTimeout(self): |
963 | e6784773 | René Nussbaumer | self.assertRaises(ValueError, utils.RunningTimeout, -1.0, True) |
964 | e6784773 | René Nussbaumer | |
965 | e6784773 | René Nussbaumer | |
966 | f40ae421 | Iustin Pop | class TestTryConvert(unittest.TestCase): |
967 | f40ae421 | Iustin Pop | def test(self): |
968 | f40ae421 | Iustin Pop | for src, fn, result in [ |
969 | f40ae421 | Iustin Pop | ("1", int, 1), |
970 | f40ae421 | Iustin Pop | ("a", int, "a"), |
971 | f40ae421 | Iustin Pop | ("", bool, False), |
972 | f40ae421 | Iustin Pop | ("a", bool, True), |
973 | f40ae421 | Iustin Pop | ]: |
974 | f40ae421 | Iustin Pop | self.assertEqual(utils.TryConvert(fn, src), result)
|
975 | f40ae421 | Iustin Pop | |
976 | f40ae421 | Iustin Pop | |
977 | f40ae421 | Iustin Pop | class TestIsValidShellParam(unittest.TestCase): |
978 | f40ae421 | Iustin Pop | def test(self): |
979 | f40ae421 | Iustin Pop | for val, result in [ |
980 | f40ae421 | Iustin Pop | ("abc", True), |
981 | f40ae421 | Iustin Pop | ("ab;cd", False), |
982 | f40ae421 | Iustin Pop | ]: |
983 | f40ae421 | Iustin Pop | self.assertEqual(utils.IsValidShellParam(val), result)
|
984 | f40ae421 | Iustin Pop | |
985 | f40ae421 | Iustin Pop | |
986 | f40ae421 | Iustin Pop | class TestBuildShellCmd(unittest.TestCase): |
987 | f40ae421 | Iustin Pop | def test(self): |
988 | f40ae421 | Iustin Pop | self.assertRaises(errors.ProgrammerError, utils.BuildShellCmd,
|
989 | f40ae421 | Iustin Pop | "ls %s", "ab;cd") |
990 | f40ae421 | Iustin Pop | self.assertEqual(utils.BuildShellCmd("ls %s", "ab"), "ls ab") |
991 | f40ae421 | Iustin Pop | |
992 | f40ae421 | Iustin Pop | |
993 | a8083063 | Iustin Pop | if __name__ == '__main__': |
994 | 25231ec5 | Michael Hanselmann | testutils.GanetiTestProgram() |