Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.utils_unittest.py @ 232144d0

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

1020 f93f2016 Michael Hanselmann
  """
1021 f93f2016 Michael Hanselmann
  sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
1022 f93f2016 Michael Hanselmann
  try:
1023 f93f2016 Michael Hanselmann
    sock.settimeout(10)
1024 f93f2016 Michael Hanselmann
    sock.connect(path)
1025 f93f2016 Michael Hanselmann
    return utils.GetSocketCredentials(sock)
1026 f93f2016 Michael Hanselmann
  finally:
1027 f93f2016 Michael Hanselmann
    sock.close()
1028 f93f2016 Michael Hanselmann
1029 f93f2016 Michael Hanselmann
1030 f93f2016 Michael Hanselmann
class TestGetSocketCredentials(unittest.TestCase):
1031 f93f2016 Michael Hanselmann
  def setUp(self):
1032 f93f2016 Michael Hanselmann
    self.tmpdir = tempfile.mkdtemp()
1033 f93f2016 Michael Hanselmann
    self.sockpath = utils.PathJoin(self.tmpdir, "sock")
1034 f93f2016 Michael Hanselmann
1035 f93f2016 Michael Hanselmann
    self.listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
1036 f93f2016 Michael Hanselmann
    self.listener.settimeout(10)
1037 f93f2016 Michael Hanselmann
    self.listener.bind(self.sockpath)
1038 f93f2016 Michael Hanselmann
    self.listener.listen(1)
1039 f93f2016 Michael Hanselmann
1040 f93f2016 Michael Hanselmann
  def tearDown(self):
1041 f93f2016 Michael Hanselmann
    self.listener.shutdown(socket.SHUT_RDWR)
1042 f93f2016 Michael Hanselmann
    self.listener.close()
1043 f93f2016 Michael Hanselmann
    shutil.rmtree(self.tmpdir)
1044 f93f2016 Michael Hanselmann
1045 f93f2016 Michael Hanselmann
  def test(self):
1046 f93f2016 Michael Hanselmann
    (c2pr, c2pw) = os.pipe()
1047 f93f2016 Michael Hanselmann
1048 f93f2016 Michael Hanselmann
    # Start child process
1049 f93f2016 Michael Hanselmann
    child = os.fork()
1050 f93f2016 Michael Hanselmann
    if child == 0:
1051 f93f2016 Michael Hanselmann
      try:
1052 f93f2016 Michael Hanselmann
        data = serializer.DumpJson(_GetSocketCredentials(self.sockpath))
1053 f93f2016 Michael Hanselmann
1054 f93f2016 Michael Hanselmann
        os.write(c2pw, data)
1055 f93f2016 Michael Hanselmann
        os.close(c2pw)
1056 f93f2016 Michael Hanselmann
1057 f93f2016 Michael Hanselmann
        os._exit(0)
1058 f93f2016 Michael Hanselmann
      finally:
1059 f93f2016 Michael Hanselmann
        os._exit(1)
1060 f93f2016 Michael Hanselmann
1061 f93f2016 Michael Hanselmann
    os.close(c2pw)
1062 f93f2016 Michael Hanselmann
1063 f93f2016 Michael Hanselmann
    # Wait for one connection
1064 f93f2016 Michael Hanselmann
    (conn, _) = self.listener.accept()
1065 f93f2016 Michael Hanselmann
    conn.recv(1)
1066 f93f2016 Michael Hanselmann
    conn.close()
1067 f93f2016 Michael Hanselmann
1068 f93f2016 Michael Hanselmann
    # Wait for result
1069 f93f2016 Michael Hanselmann
    result = os.read(c2pr, 4096)
1070 f93f2016 Michael Hanselmann
    os.close(c2pr)
1071 f93f2016 Michael Hanselmann
1072 f93f2016 Michael Hanselmann
    # Check child's exit code
1073 f93f2016 Michael Hanselmann
    (_, status) = os.waitpid(child, 0)
1074 f93f2016 Michael Hanselmann
    self.assertFalse(os.WIFSIGNALED(status))
1075 f93f2016 Michael Hanselmann
    self.assertEqual(os.WEXITSTATUS(status), 0)
1076 f93f2016 Michael Hanselmann
1077 f93f2016 Michael Hanselmann
    # Check result
1078 f93f2016 Michael Hanselmann
    (pid, uid, gid) = serializer.LoadJson(result)
1079 f93f2016 Michael Hanselmann
    self.assertEqual(pid, os.getpid())
1080 f93f2016 Michael Hanselmann
    self.assertEqual(uid, os.getuid())
1081 f93f2016 Michael Hanselmann
    self.assertEqual(gid, os.getgid())
1082 f93f2016 Michael Hanselmann
1083 f93f2016 Michael Hanselmann
1084 eedbda4b Michael Hanselmann
class TestListVisibleFiles(unittest.TestCase):
1085 eedbda4b Michael Hanselmann
  """Test case for ListVisibleFiles"""
1086 eedbda4b Michael Hanselmann
1087 eedbda4b Michael Hanselmann
  def setUp(self):
1088 eedbda4b Michael Hanselmann
    self.path = tempfile.mkdtemp()
1089 eedbda4b Michael Hanselmann
1090 eedbda4b Michael Hanselmann
  def tearDown(self):
1091 eedbda4b Michael Hanselmann
    shutil.rmtree(self.path)
1092 eedbda4b Michael Hanselmann
1093 eedbda4b Michael Hanselmann
  def _test(self, files, expected):
1094 eedbda4b Michael Hanselmann
    # Sort a copy
1095 eedbda4b Michael Hanselmann
    expected = expected[:]
1096 eedbda4b Michael Hanselmann
    expected.sort()
1097 eedbda4b Michael Hanselmann
1098 eedbda4b Michael Hanselmann
    for name in files:
1099 eedbda4b Michael Hanselmann
      f = open(os.path.join(self.path, name), 'w')
1100 eedbda4b Michael Hanselmann
      try:
1101 eedbda4b Michael Hanselmann
        f.write("Test\n")
1102 eedbda4b Michael Hanselmann
      finally:
1103 eedbda4b Michael Hanselmann
        f.close()
1104 eedbda4b Michael Hanselmann
1105 eedbda4b Michael Hanselmann
    found = ListVisibleFiles(self.path)
1106 eedbda4b Michael Hanselmann
    found.sort()
1107 eedbda4b Michael Hanselmann
1108 eedbda4b Michael Hanselmann
    self.assertEqual(found, expected)
1109 eedbda4b Michael Hanselmann
1110 eedbda4b Michael Hanselmann
  def testAllVisible(self):
1111 eedbda4b Michael Hanselmann
    files = ["a", "b", "c"]
1112 eedbda4b Michael Hanselmann
    expected = files
1113 eedbda4b Michael Hanselmann
    self._test(files, expected)
1114 eedbda4b Michael Hanselmann
1115 eedbda4b Michael Hanselmann
  def testNoneVisible(self):
1116 eedbda4b Michael Hanselmann
    files = [".a", ".b", ".c"]
1117 eedbda4b Michael Hanselmann
    expected = []
1118 eedbda4b Michael Hanselmann
    self._test(files, expected)
1119 eedbda4b Michael Hanselmann
1120 eedbda4b Michael Hanselmann
  def testSomeVisible(self):
1121 eedbda4b Michael Hanselmann
    files = ["a", "b", ".c"]
1122 eedbda4b Michael Hanselmann
    expected = ["a", "b"]
1123 eedbda4b Michael Hanselmann
    self._test(files, expected)
1124 eedbda4b Michael Hanselmann
1125 04a69a18 Iustin Pop
  def testNonAbsolutePath(self):
1126 04a69a18 Iustin Pop
    self.failUnlessRaises(errors.ProgrammerError, ListVisibleFiles, "abc")
1127 04a69a18 Iustin Pop
1128 04a69a18 Iustin Pop
  def testNonNormalizedPath(self):
1129 04a69a18 Iustin Pop
    self.failUnlessRaises(errors.ProgrammerError, ListVisibleFiles,
1130 04a69a18 Iustin Pop
                          "/bin/../tmp")
1131 04a69a18 Iustin Pop
1132 eedbda4b Michael Hanselmann
1133 24818e8f Michael Hanselmann
class TestNewUUID(unittest.TestCase):
1134 24818e8f Michael Hanselmann
  """Test case for NewUUID"""
1135 59072e7e Michael Hanselmann
1136 59072e7e Michael Hanselmann
  _re_uuid = re.compile('^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-'
1137 59072e7e Michael Hanselmann
                        '[a-f0-9]{4}-[a-f0-9]{12}$')
1138 59072e7e Michael Hanselmann
1139 59072e7e Michael Hanselmann
  def runTest(self):
1140 24818e8f Michael Hanselmann
    self.failUnless(self._re_uuid.match(utils.NewUUID()))
1141 59072e7e Michael Hanselmann
1142 59072e7e Michael Hanselmann
1143 f7414041 Michael Hanselmann
class TestUniqueSequence(unittest.TestCase):
1144 f7414041 Michael Hanselmann
  """Test case for UniqueSequence"""
1145 f7414041 Michael Hanselmann
1146 f7414041 Michael Hanselmann
  def _test(self, input, expected):
1147 f7414041 Michael Hanselmann
    self.assertEqual(utils.UniqueSequence(input), expected)
1148 f7414041 Michael Hanselmann
1149 f7414041 Michael Hanselmann
  def runTest(self):
1150 f7414041 Michael Hanselmann
    # Ordered input
1151 f7414041 Michael Hanselmann
    self._test([1, 2, 3], [1, 2, 3])
1152 f7414041 Michael Hanselmann
    self._test([1, 1, 2, 2, 3, 3], [1, 2, 3])
1153 f7414041 Michael Hanselmann
    self._test([1, 2, 2, 3], [1, 2, 3])
1154 f7414041 Michael Hanselmann
    self._test([1, 2, 3, 3], [1, 2, 3])
1155 f7414041 Michael Hanselmann
1156 f7414041 Michael Hanselmann
    # Unordered input
1157 f7414041 Michael Hanselmann
    self._test([1, 2, 3, 1, 2, 3], [1, 2, 3])
1158 f7414041 Michael Hanselmann
    self._test([1, 1, 2, 3, 3, 1, 2], [1, 2, 3])
1159 f7414041 Michael Hanselmann
1160 f7414041 Michael Hanselmann
    # Strings
1161 f7414041 Michael Hanselmann
    self._test(["a", "a"], ["a"])
1162 f7414041 Michael Hanselmann
    self._test(["a", "b"], ["a", "b"])
1163 f7414041 Michael Hanselmann
    self._test(["a", "b", "a"], ["a", "b"])
1164 f7414041 Michael Hanselmann
1165 a87b4824 Michael Hanselmann
1166 7b4126b7 Iustin Pop
class TestFirstFree(unittest.TestCase):
1167 7b4126b7 Iustin Pop
  """Test case for the FirstFree function"""
1168 7b4126b7 Iustin Pop
1169 7b4126b7 Iustin Pop
  def test(self):
1170 7b4126b7 Iustin Pop
    """Test FirstFree"""
1171 7b4126b7 Iustin Pop
    self.failUnlessEqual(FirstFree([0, 1, 3]), 2)
1172 7b4126b7 Iustin Pop
    self.failUnlessEqual(FirstFree([]), None)
1173 7b4126b7 Iustin Pop
    self.failUnlessEqual(FirstFree([3, 4, 6]), 0)
1174 7b4126b7 Iustin Pop
    self.failUnlessEqual(FirstFree([3, 4, 6], base=3), 5)
1175 7b4126b7 Iustin Pop
    self.failUnlessRaises(AssertionError, FirstFree, [0, 3, 4, 6], base=3)
1176 f7414041 Michael Hanselmann
1177 a87b4824 Michael Hanselmann
1178 f65f63ef Iustin Pop
class TestTailFile(testutils.GanetiTestCase):
1179 f65f63ef Iustin Pop
  """Test case for the TailFile function"""
1180 f65f63ef Iustin Pop
1181 f65f63ef Iustin Pop
  def testEmpty(self):
1182 f65f63ef Iustin Pop
    fname = self._CreateTempFile()
1183 f65f63ef Iustin Pop
    self.failUnlessEqual(TailFile(fname), [])
1184 f65f63ef Iustin Pop
    self.failUnlessEqual(TailFile(fname, lines=25), [])
1185 f65f63ef Iustin Pop
1186 f65f63ef Iustin Pop
  def testAllLines(self):
1187 f65f63ef Iustin Pop
    data = ["test %d" % i for i in range(30)]
1188 f65f63ef Iustin Pop
    for i in range(30):
1189 f65f63ef Iustin Pop
      fname = self._CreateTempFile()
1190 f65f63ef Iustin Pop
      fd = open(fname, "w")
1191 f65f63ef Iustin Pop
      fd.write("\n".join(data[:i]))
1192 f65f63ef Iustin Pop
      if i > 0:
1193 f65f63ef Iustin Pop
        fd.write("\n")
1194 f65f63ef Iustin Pop
      fd.close()
1195 f65f63ef Iustin Pop
      self.failUnlessEqual(TailFile(fname, lines=i), data[:i])
1196 f65f63ef Iustin Pop
1197 f65f63ef Iustin Pop
  def testPartialLines(self):
1198 f65f63ef Iustin Pop
    data = ["test %d" % i for i in range(30)]
1199 f65f63ef Iustin Pop
    fname = self._CreateTempFile()
1200 f65f63ef Iustin Pop
    fd = open(fname, "w")
1201 f65f63ef Iustin Pop
    fd.write("\n".join(data))
1202 f65f63ef Iustin Pop
    fd.write("\n")
1203 f65f63ef Iustin Pop
    fd.close()
1204 f65f63ef Iustin Pop
    for i in range(1, 30):
1205 f65f63ef Iustin Pop
      self.failUnlessEqual(TailFile(fname, lines=i), data[-i:])
1206 f65f63ef Iustin Pop
1207 f65f63ef Iustin Pop
  def testBigFile(self):
1208 f65f63ef Iustin Pop
    data = ["test %d" % i for i in range(30)]
1209 f65f63ef Iustin Pop
    fname = self._CreateTempFile()
1210 f65f63ef Iustin Pop
    fd = open(fname, "w")
1211 f65f63ef Iustin Pop
    fd.write("X" * 1048576)
1212 f65f63ef Iustin Pop
    fd.write("\n")
1213 f65f63ef Iustin Pop
    fd.write("\n".join(data))
1214 f65f63ef Iustin Pop
    fd.write("\n")
1215 f65f63ef Iustin Pop
    fd.close()
1216 f65f63ef Iustin Pop
    for i in range(1, 30):
1217 f65f63ef Iustin Pop
      self.failUnlessEqual(TailFile(fname, lines=i), data[-i:])
1218 f65f63ef Iustin Pop
1219 f65f63ef Iustin Pop
1220 b4478d34 Michael Hanselmann
class _BaseFileLockTest:
1221 a87b4824 Michael Hanselmann
  """Test case for the FileLock class"""
1222 a87b4824 Michael Hanselmann
1223 a87b4824 Michael Hanselmann
  def testSharedNonblocking(self):
1224 a87b4824 Michael Hanselmann
    self.lock.Shared(blocking=False)
1225 a87b4824 Michael Hanselmann
    self.lock.Close()
1226 a87b4824 Michael Hanselmann
1227 a87b4824 Michael Hanselmann
  def testExclusiveNonblocking(self):
1228 a87b4824 Michael Hanselmann
    self.lock.Exclusive(blocking=False)
1229 a87b4824 Michael Hanselmann
    self.lock.Close()
1230 a87b4824 Michael Hanselmann
1231 a87b4824 Michael Hanselmann
  def testUnlockNonblocking(self):
1232 a87b4824 Michael Hanselmann
    self.lock.Unlock(blocking=False)
1233 a87b4824 Michael Hanselmann
    self.lock.Close()
1234 a87b4824 Michael Hanselmann
1235 a87b4824 Michael Hanselmann
  def testSharedBlocking(self):
1236 a87b4824 Michael Hanselmann
    self.lock.Shared(blocking=True)
1237 a87b4824 Michael Hanselmann
    self.lock.Close()
1238 a87b4824 Michael Hanselmann
1239 a87b4824 Michael Hanselmann
  def testExclusiveBlocking(self):
1240 a87b4824 Michael Hanselmann
    self.lock.Exclusive(blocking=True)
1241 a87b4824 Michael Hanselmann
    self.lock.Close()
1242 a87b4824 Michael Hanselmann
1243 a87b4824 Michael Hanselmann
  def testUnlockBlocking(self):
1244 a87b4824 Michael Hanselmann
    self.lock.Unlock(blocking=True)
1245 a87b4824 Michael Hanselmann
    self.lock.Close()
1246 a87b4824 Michael Hanselmann
1247 a87b4824 Michael Hanselmann
  def testSharedExclusiveUnlock(self):
1248 a87b4824 Michael Hanselmann
    self.lock.Shared(blocking=False)
1249 a87b4824 Michael Hanselmann
    self.lock.Exclusive(blocking=False)
1250 a87b4824 Michael Hanselmann
    self.lock.Unlock(blocking=False)
1251 a87b4824 Michael Hanselmann
    self.lock.Close()
1252 a87b4824 Michael Hanselmann
1253 a87b4824 Michael Hanselmann
  def testExclusiveSharedUnlock(self):
1254 a87b4824 Michael Hanselmann
    self.lock.Exclusive(blocking=False)
1255 a87b4824 Michael Hanselmann
    self.lock.Shared(blocking=False)
1256 a87b4824 Michael Hanselmann
    self.lock.Unlock(blocking=False)
1257 a87b4824 Michael Hanselmann
    self.lock.Close()
1258 a87b4824 Michael Hanselmann
1259 b4478d34 Michael Hanselmann
  def testSimpleTimeout(self):
1260 b4478d34 Michael Hanselmann
    # These will succeed on the first attempt, hence a short timeout
1261 b4478d34 Michael Hanselmann
    self.lock.Shared(blocking=True, timeout=10.0)
1262 b4478d34 Michael Hanselmann
    self.lock.Exclusive(blocking=False, timeout=10.0)
1263 b4478d34 Michael Hanselmann
    self.lock.Unlock(blocking=True, timeout=10.0)
1264 b4478d34 Michael Hanselmann
    self.lock.Close()
1265 b4478d34 Michael Hanselmann
1266 b4478d34 Michael Hanselmann
  @staticmethod
1267 b4478d34 Michael Hanselmann
  def _TryLockInner(filename, shared, blocking):
1268 b4478d34 Michael Hanselmann
    lock = utils.FileLock.Open(filename)
1269 b4478d34 Michael Hanselmann
1270 b4478d34 Michael Hanselmann
    if shared:
1271 b4478d34 Michael Hanselmann
      fn = lock.Shared
1272 b4478d34 Michael Hanselmann
    else:
1273 b4478d34 Michael Hanselmann
      fn = lock.Exclusive
1274 b4478d34 Michael Hanselmann
1275 b4478d34 Michael Hanselmann
    try:
1276 b4478d34 Michael Hanselmann
      # The timeout doesn't really matter as the parent process waits for us to
1277 b4478d34 Michael Hanselmann
      # finish anyway.
1278 b4478d34 Michael Hanselmann
      fn(blocking=blocking, timeout=0.01)
1279 b4478d34 Michael Hanselmann
    except errors.LockError, err:
1280 b4478d34 Michael Hanselmann
      return False
1281 b4478d34 Michael Hanselmann
1282 b4478d34 Michael Hanselmann
    return True
1283 b4478d34 Michael Hanselmann
1284 b4478d34 Michael Hanselmann
  def _TryLock(self, *args):
1285 b4478d34 Michael Hanselmann
    return utils.RunInSeparateProcess(self._TryLockInner, self.tmpfile.name,
1286 b4478d34 Michael Hanselmann
                                      *args)
1287 b4478d34 Michael Hanselmann
1288 b4478d34 Michael Hanselmann
  def testTimeout(self):
1289 b4478d34 Michael Hanselmann
    for blocking in [True, False]:
1290 b4478d34 Michael Hanselmann
      self.lock.Exclusive(blocking=True)
1291 b4478d34 Michael Hanselmann
      self.failIf(self._TryLock(False, blocking))
1292 b4478d34 Michael Hanselmann
      self.failIf(self._TryLock(True, blocking))
1293 b4478d34 Michael Hanselmann
1294 b4478d34 Michael Hanselmann
      self.lock.Shared(blocking=True)
1295 b4478d34 Michael Hanselmann
      self.assert_(self._TryLock(True, blocking))
1296 b4478d34 Michael Hanselmann
      self.failIf(self._TryLock(False, blocking))
1297 b4478d34 Michael Hanselmann
1298 a87b4824 Michael Hanselmann
  def testCloseShared(self):
1299 a87b4824 Michael Hanselmann
    self.lock.Close()
1300 a87b4824 Michael Hanselmann
    self.assertRaises(AssertionError, self.lock.Shared, blocking=False)
1301 a87b4824 Michael Hanselmann
1302 a87b4824 Michael Hanselmann
  def testCloseExclusive(self):
1303 a87b4824 Michael Hanselmann
    self.lock.Close()
1304 a87b4824 Michael Hanselmann
    self.assertRaises(AssertionError, self.lock.Exclusive, blocking=False)
1305 a87b4824 Michael Hanselmann
1306 a87b4824 Michael Hanselmann
  def testCloseUnlock(self):
1307 a87b4824 Michael Hanselmann
    self.lock.Close()
1308 a87b4824 Michael Hanselmann
    self.assertRaises(AssertionError, self.lock.Unlock, blocking=False)
1309 a87b4824 Michael Hanselmann
1310 a87b4824 Michael Hanselmann
1311 b4478d34 Michael Hanselmann
class TestFileLockWithFilename(testutils.GanetiTestCase, _BaseFileLockTest):
1312 b4478d34 Michael Hanselmann
  TESTDATA = "Hello World\n" * 10
1313 b4478d34 Michael Hanselmann
1314 b4478d34 Michael Hanselmann
  def setUp(self):
1315 b4478d34 Michael Hanselmann
    testutils.GanetiTestCase.setUp(self)
1316 b4478d34 Michael Hanselmann
1317 b4478d34 Michael Hanselmann
    self.tmpfile = tempfile.NamedTemporaryFile()
1318 b4478d34 Michael Hanselmann
    utils.WriteFile(self.tmpfile.name, data=self.TESTDATA)
1319 b4478d34 Michael Hanselmann
    self.lock = utils.FileLock.Open(self.tmpfile.name)
1320 b4478d34 Michael Hanselmann
1321 b4478d34 Michael Hanselmann
    # Ensure "Open" didn't truncate file
1322 b4478d34 Michael Hanselmann
    self.assertFileContent(self.tmpfile.name, self.TESTDATA)
1323 b4478d34 Michael Hanselmann
1324 b4478d34 Michael Hanselmann
  def tearDown(self):
1325 b4478d34 Michael Hanselmann
    self.assertFileContent(self.tmpfile.name, self.TESTDATA)
1326 b4478d34 Michael Hanselmann
1327 b4478d34 Michael Hanselmann
    testutils.GanetiTestCase.tearDown(self)
1328 b4478d34 Michael Hanselmann
1329 b4478d34 Michael Hanselmann
1330 b4478d34 Michael Hanselmann
class TestFileLockWithFileObject(unittest.TestCase, _BaseFileLockTest):
1331 b4478d34 Michael Hanselmann
  def setUp(self):
1332 b4478d34 Michael Hanselmann
    self.tmpfile = tempfile.NamedTemporaryFile()
1333 b4478d34 Michael Hanselmann
    self.lock = utils.FileLock(open(self.tmpfile.name, "w"), self.tmpfile.name)
1334 b4478d34 Michael Hanselmann
1335 b4478d34 Michael Hanselmann
1336 739be818 Michael Hanselmann
class TestTimeFunctions(unittest.TestCase):
1337 739be818 Michael Hanselmann
  """Test case for time functions"""
1338 739be818 Michael Hanselmann
1339 739be818 Michael Hanselmann
  def runTest(self):
1340 739be818 Michael Hanselmann
    self.assertEqual(utils.SplitTime(1), (1, 0))
1341 45bc5e4a Michael Hanselmann
    self.assertEqual(utils.SplitTime(1.5), (1, 500000))
1342 45bc5e4a Michael Hanselmann
    self.assertEqual(utils.SplitTime(1218448917.4809151), (1218448917, 480915))
1343 45bc5e4a Michael Hanselmann
    self.assertEqual(utils.SplitTime(123.48012), (123, 480120))
1344 45bc5e4a Michael Hanselmann
    self.assertEqual(utils.SplitTime(123.9996), (123, 999600))
1345 45bc5e4a Michael Hanselmann
    self.assertEqual(utils.SplitTime(123.9995), (123, 999500))
1346 45bc5e4a Michael Hanselmann
    self.assertEqual(utils.SplitTime(123.9994), (123, 999400))
1347 45bc5e4a Michael Hanselmann
    self.assertEqual(utils.SplitTime(123.999999999), (123, 999999))
1348 45bc5e4a Michael Hanselmann
1349 45bc5e4a Michael Hanselmann
    self.assertRaises(AssertionError, utils.SplitTime, -1)
1350 739be818 Michael Hanselmann
1351 739be818 Michael Hanselmann
    self.assertEqual(utils.MergeTime((1, 0)), 1.0)
1352 45bc5e4a Michael Hanselmann
    self.assertEqual(utils.MergeTime((1, 500000)), 1.5)
1353 45bc5e4a Michael Hanselmann
    self.assertEqual(utils.MergeTime((1218448917, 500000)), 1218448917.5)
1354 739be818 Michael Hanselmann
1355 4d4a651d Michael Hanselmann
    self.assertEqual(round(utils.MergeTime((1218448917, 481000)), 3),
1356 4d4a651d Michael Hanselmann
                     1218448917.481)
1357 45bc5e4a Michael Hanselmann
    self.assertEqual(round(utils.MergeTime((1, 801000)), 3), 1.801)
1358 739be818 Michael Hanselmann
1359 739be818 Michael Hanselmann
    self.assertRaises(AssertionError, utils.MergeTime, (0, -1))
1360 45bc5e4a Michael Hanselmann
    self.assertRaises(AssertionError, utils.MergeTime, (0, 1000000))
1361 45bc5e4a Michael Hanselmann
    self.assertRaises(AssertionError, utils.MergeTime, (0, 9999999))
1362 739be818 Michael Hanselmann
    self.assertRaises(AssertionError, utils.MergeTime, (-1, 0))
1363 739be818 Michael Hanselmann
    self.assertRaises(AssertionError, utils.MergeTime, (-9999, 0))
1364 739be818 Michael Hanselmann
1365 739be818 Michael Hanselmann
1366 a2d2e1a7 Iustin Pop
class FieldSetTestCase(unittest.TestCase):
1367 a2d2e1a7 Iustin Pop
  """Test case for FieldSets"""
1368 a2d2e1a7 Iustin Pop
1369 a2d2e1a7 Iustin Pop
  def testSimpleMatch(self):
1370 a2d2e1a7 Iustin Pop
    f = utils.FieldSet("a", "b", "c", "def")
1371 a2d2e1a7 Iustin Pop
    self.failUnless(f.Matches("a"))
1372 a2d2e1a7 Iustin Pop
    self.failIf(f.Matches("d"), "Substring matched")
1373 a2d2e1a7 Iustin Pop
    self.failIf(f.Matches("defghi"), "Prefix string matched")
1374 a2d2e1a7 Iustin Pop
    self.failIf(f.NonMatching(["b", "c"]))
1375 a2d2e1a7 Iustin Pop
    self.failIf(f.NonMatching(["a", "b", "c", "def"]))
1376 a2d2e1a7 Iustin Pop
    self.failUnless(f.NonMatching(["a", "d"]))
1377 a2d2e1a7 Iustin Pop
1378 a2d2e1a7 Iustin Pop
  def testRegexMatch(self):
1379 a2d2e1a7 Iustin Pop
    f = utils.FieldSet("a", "b([0-9]+)", "c")
1380 a2d2e1a7 Iustin Pop
    self.failUnless(f.Matches("b1"))
1381 a2d2e1a7 Iustin Pop
    self.failUnless(f.Matches("b99"))
1382 a2d2e1a7 Iustin Pop
    self.failIf(f.Matches("b/1"))
1383 a2d2e1a7 Iustin Pop
    self.failIf(f.NonMatching(["b12", "c"]))
1384 a2d2e1a7 Iustin Pop
    self.failUnless(f.NonMatching(["a", "1"]))
1385 a2d2e1a7 Iustin Pop
1386 a5728081 Guido Trotter
class TestForceDictType(unittest.TestCase):
1387 a5728081 Guido Trotter
  """Test case for ForceDictType"""
1388 a5728081 Guido Trotter
1389 a5728081 Guido Trotter
  def setUp(self):
1390 a5728081 Guido Trotter
    self.key_types = {
1391 a5728081 Guido Trotter
      'a': constants.VTYPE_INT,
1392 a5728081 Guido Trotter
      'b': constants.VTYPE_BOOL,
1393 a5728081 Guido Trotter
      'c': constants.VTYPE_STRING,
1394 a5728081 Guido Trotter
      'd': constants.VTYPE_SIZE,
1395 a5728081 Guido Trotter
      }
1396 a5728081 Guido Trotter
1397 a5728081 Guido Trotter
  def _fdt(self, dict, allowed_values=None):
1398 a5728081 Guido Trotter
    if allowed_values is None:
1399 a5728081 Guido Trotter
      ForceDictType(dict, self.key_types)
1400 a5728081 Guido Trotter
    else:
1401 a5728081 Guido Trotter
      ForceDictType(dict, self.key_types, allowed_values=allowed_values)
1402 a5728081 Guido Trotter
1403 a5728081 Guido Trotter
    return dict
1404 a5728081 Guido Trotter
1405 a5728081 Guido Trotter
  def testSimpleDict(self):
1406 a5728081 Guido Trotter
    self.assertEqual(self._fdt({}), {})
1407 a5728081 Guido Trotter
    self.assertEqual(self._fdt({'a': 1}), {'a': 1})
1408 a5728081 Guido Trotter
    self.assertEqual(self._fdt({'a': '1'}), {'a': 1})
1409 a5728081 Guido Trotter
    self.assertEqual(self._fdt({'a': 1, 'b': 1}), {'a':1, 'b': True})
1410 a5728081 Guido Trotter
    self.assertEqual(self._fdt({'b': 1, 'c': 'foo'}), {'b': True, 'c': 'foo'})
1411 a5728081 Guido Trotter
    self.assertEqual(self._fdt({'b': 1, 'c': False}), {'b': True, 'c': ''})
1412 a5728081 Guido Trotter
    self.assertEqual(self._fdt({'b': 'false'}), {'b': False})
1413 a5728081 Guido Trotter
    self.assertEqual(self._fdt({'b': 'False'}), {'b': False})
1414 a5728081 Guido Trotter
    self.assertEqual(self._fdt({'b': 'true'}), {'b': True})
1415 a5728081 Guido Trotter
    self.assertEqual(self._fdt({'b': 'True'}), {'b': True})
1416 a5728081 Guido Trotter
    self.assertEqual(self._fdt({'d': '4'}), {'d': 4})
1417 a5728081 Guido Trotter
    self.assertEqual(self._fdt({'d': '4M'}), {'d': 4})
1418 a5728081 Guido Trotter
1419 a5728081 Guido Trotter
  def testErrors(self):
1420 a5728081 Guido Trotter
    self.assertRaises(errors.TypeEnforcementError, self._fdt, {'a': 'astring'})
1421 a5728081 Guido Trotter
    self.assertRaises(errors.TypeEnforcementError, self._fdt, {'c': True})
1422 a5728081 Guido Trotter
    self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': 'astring'})
1423 a5728081 Guido Trotter
    self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': '4 L'})
1424 a5728081 Guido Trotter
1425 a2d2e1a7 Iustin Pop
1426 05489142 Guido Trotter
class TestIsNormAbsPath(unittest.TestCase):
1427 05489142 Guido Trotter
  """Testing case for IsNormAbsPath"""
1428 da961187 Guido Trotter
1429 da961187 Guido Trotter
  def _pathTestHelper(self, path, result):
1430 da961187 Guido Trotter
    if result:
1431 da961187 Guido Trotter
      self.assert_(IsNormAbsPath(path),
1432 17c61836 Guido Trotter
          "Path %s should result absolute and normalized" % path)
1433 da961187 Guido Trotter
    else:
1434 da961187 Guido Trotter
      self.assert_(not IsNormAbsPath(path),
1435 17c61836 Guido Trotter
          "Path %s should not result absolute and normalized" % path)
1436 da961187 Guido Trotter
1437 da961187 Guido Trotter
  def testBase(self):
1438 da961187 Guido Trotter
    self._pathTestHelper('/etc', True)
1439 da961187 Guido Trotter
    self._pathTestHelper('/srv', True)
1440 da961187 Guido Trotter
    self._pathTestHelper('etc', False)
1441 da961187 Guido Trotter
    self._pathTestHelper('/etc/../root', False)
1442 da961187 Guido Trotter
    self._pathTestHelper('/etc/', False)
1443 da961187 Guido Trotter
1444 af0413bb Guido Trotter
1445 d392fa34 Iustin Pop
class TestSafeEncode(unittest.TestCase):
1446 d392fa34 Iustin Pop
  """Test case for SafeEncode"""
1447 d392fa34 Iustin Pop
1448 d392fa34 Iustin Pop
  def testAscii(self):
1449 d392fa34 Iustin Pop
    for txt in [string.digits, string.letters, string.punctuation]:
1450 d392fa34 Iustin Pop
      self.failUnlessEqual(txt, SafeEncode(txt))
1451 d392fa34 Iustin Pop
1452 d392fa34 Iustin Pop
  def testDoubleEncode(self):
1453 d392fa34 Iustin Pop
    for i in range(255):
1454 d392fa34 Iustin Pop
      txt = SafeEncode(chr(i))
1455 d392fa34 Iustin Pop
      self.failUnlessEqual(txt, SafeEncode(txt))
1456 d392fa34 Iustin Pop
1457 d392fa34 Iustin Pop
  def testUnicode(self):
1458 d392fa34 Iustin Pop
    # 1024 is high enough to catch non-direct ASCII mappings
1459 d392fa34 Iustin Pop
    for i in range(1024):
1460 d392fa34 Iustin Pop
      txt = SafeEncode(unichr(i))
1461 d392fa34 Iustin Pop
      self.failUnlessEqual(txt, SafeEncode(txt))
1462 d392fa34 Iustin Pop
1463 d392fa34 Iustin Pop
1464 3b813dd2 Iustin Pop
class TestFormatTime(unittest.TestCase):
1465 3b813dd2 Iustin Pop
  """Testing case for FormatTime"""
1466 3b813dd2 Iustin Pop
1467 3b813dd2 Iustin Pop
  def testNone(self):
1468 3b813dd2 Iustin Pop
    self.failUnlessEqual(FormatTime(None), "N/A")
1469 3b813dd2 Iustin Pop
1470 3b813dd2 Iustin Pop
  def testInvalid(self):
1471 3b813dd2 Iustin Pop
    self.failUnlessEqual(FormatTime(()), "N/A")
1472 3b813dd2 Iustin Pop
1473 3b813dd2 Iustin Pop
  def testNow(self):
1474 3b813dd2 Iustin Pop
    # tests that we accept time.time input
1475 3b813dd2 Iustin Pop
    FormatTime(time.time())
1476 3b813dd2 Iustin Pop
    # tests that we accept int input
1477 3b813dd2 Iustin Pop
    FormatTime(int(time.time()))
1478 3b813dd2 Iustin Pop
1479 3b813dd2 Iustin Pop
1480 eb58f7bd Michael Hanselmann
class RunInSeparateProcess(unittest.TestCase):
1481 eb58f7bd Michael Hanselmann
  def test(self):
1482 eb58f7bd Michael Hanselmann
    for exp in [True, False]:
1483 eb58f7bd Michael Hanselmann
      def _child():
1484 eb58f7bd Michael Hanselmann
        return exp
1485 eb58f7bd Michael Hanselmann
1486 eb58f7bd Michael Hanselmann
      self.assertEqual(exp, utils.RunInSeparateProcess(_child))
1487 eb58f7bd Michael Hanselmann
1488 bdefe5dd Michael Hanselmann
  def testArgs(self):
1489 bdefe5dd Michael Hanselmann
    for arg in [0, 1, 999, "Hello World", (1, 2, 3)]:
1490 bdefe5dd Michael Hanselmann
      def _child(carg1, carg2):
1491 bdefe5dd Michael Hanselmann
        return carg1 == "Foo" and carg2 == arg
1492 bdefe5dd Michael Hanselmann
1493 bdefe5dd Michael Hanselmann
      self.assert_(utils.RunInSeparateProcess(_child, "Foo", arg))
1494 bdefe5dd Michael Hanselmann
1495 eb58f7bd Michael Hanselmann
  def testPid(self):
1496 eb58f7bd Michael Hanselmann
    parent_pid = os.getpid()
1497 eb58f7bd Michael Hanselmann
1498 eb58f7bd Michael Hanselmann
    def _check():
1499 eb58f7bd Michael Hanselmann
      return os.getpid() == parent_pid
1500 eb58f7bd Michael Hanselmann
1501 eb58f7bd Michael Hanselmann
    self.failIf(utils.RunInSeparateProcess(_check))
1502 eb58f7bd Michael Hanselmann
1503 eb58f7bd Michael Hanselmann
  def testSignal(self):
1504 eb58f7bd Michael Hanselmann
    def _kill():
1505 eb58f7bd Michael Hanselmann
      os.kill(os.getpid(), signal.SIGTERM)
1506 eb58f7bd Michael Hanselmann
1507 eb58f7bd Michael Hanselmann
    self.assertRaises(errors.GenericError,
1508 eb58f7bd Michael Hanselmann
                      utils.RunInSeparateProcess, _kill)
1509 eb58f7bd Michael Hanselmann
1510 eb58f7bd Michael Hanselmann
  def testException(self):
1511 eb58f7bd Michael Hanselmann
    def _exc():
1512 eb58f7bd Michael Hanselmann
      raise errors.GenericError("This is a test")
1513 eb58f7bd Michael Hanselmann
1514 eb58f7bd Michael Hanselmann
    self.assertRaises(errors.GenericError,
1515 eb58f7bd Michael Hanselmann
                      utils.RunInSeparateProcess, _exc)
1516 eb58f7bd Michael Hanselmann
1517 eb58f7bd Michael Hanselmann
1518 fabee4b2 Michael Hanselmann
class TestFingerprintFile(unittest.TestCase):
1519 fabee4b2 Michael Hanselmann
  def setUp(self):
1520 fabee4b2 Michael Hanselmann
    self.tmpfile = tempfile.NamedTemporaryFile()
1521 fabee4b2 Michael Hanselmann
1522 fabee4b2 Michael Hanselmann
  def test(self):
1523 fabee4b2 Michael Hanselmann
    self.assertEqual(utils._FingerprintFile(self.tmpfile.name),
1524 fabee4b2 Michael Hanselmann
                     "da39a3ee5e6b4b0d3255bfef95601890afd80709")
1525 fabee4b2 Michael Hanselmann
1526 fabee4b2 Michael Hanselmann
    utils.WriteFile(self.tmpfile.name, data="Hello World\n")
1527 fabee4b2 Michael Hanselmann
    self.assertEqual(utils._FingerprintFile(self.tmpfile.name),
1528 fabee4b2 Michael Hanselmann
                     "648a6a6ffffdaa0badb23b8baf90b6168dd16b3a")
1529 fabee4b2 Michael Hanselmann
1530 fabee4b2 Michael Hanselmann
1531 5b69bc7c Iustin Pop
class TestUnescapeAndSplit(unittest.TestCase):
1532 5b69bc7c Iustin Pop
  """Testing case for UnescapeAndSplit"""
1533 5b69bc7c Iustin Pop
1534 5b69bc7c Iustin Pop
  def setUp(self):
1535 5b69bc7c Iustin Pop
    # testing more that one separator for regexp safety
1536 5b69bc7c Iustin Pop
    self._seps = [",", "+", "."]
1537 5b69bc7c Iustin Pop
1538 5b69bc7c Iustin Pop
  def testSimple(self):
1539 5b69bc7c Iustin Pop
    a = ["a", "b", "c", "d"]
1540 5b69bc7c Iustin Pop
    for sep in self._seps:
1541 5b69bc7c Iustin Pop
      self.failUnlessEqual(UnescapeAndSplit(sep.join(a), sep=sep), a)
1542 5b69bc7c Iustin Pop
1543 5b69bc7c Iustin Pop
  def testEscape(self):
1544 5b69bc7c Iustin Pop
    for sep in self._seps:
1545 5b69bc7c Iustin Pop
      a = ["a", "b\\" + sep + "c", "d"]
1546 5b69bc7c Iustin Pop
      b = ["a", "b" + sep + "c", "d"]
1547 5b69bc7c Iustin Pop
      self.failUnlessEqual(UnescapeAndSplit(sep.join(a), sep=sep), b)
1548 5b69bc7c Iustin Pop
1549 5b69bc7c Iustin Pop
  def testDoubleEscape(self):
1550 5b69bc7c Iustin Pop
    for sep in self._seps:
1551 5b69bc7c Iustin Pop
      a = ["a", "b\\\\", "c", "d"]
1552 5b69bc7c Iustin Pop
      b = ["a", "b\\", "c", "d"]
1553 5b69bc7c Iustin Pop
      self.failUnlessEqual(UnescapeAndSplit(sep.join(a), sep=sep), b)
1554 5b69bc7c Iustin Pop
1555 5b69bc7c Iustin Pop
  def testThreeEscape(self):
1556 5b69bc7c Iustin Pop
    for sep in self._seps:
1557 5b69bc7c Iustin Pop
      a = ["a", "b\\\\\\" + sep + "c", "d"]
1558 5b69bc7c Iustin Pop
      b = ["a", "b\\" + sep + "c", "d"]
1559 5b69bc7c Iustin Pop
      self.failUnlessEqual(UnescapeAndSplit(sep.join(a), sep=sep), b)
1560 5b69bc7c Iustin Pop
1561 5b69bc7c Iustin Pop
1562 4bb678e9 Iustin Pop
class TestPathJoin(unittest.TestCase):
1563 4bb678e9 Iustin Pop
  """Testing case for PathJoin"""
1564 4bb678e9 Iustin Pop
1565 4bb678e9 Iustin Pop
  def testBasicItems(self):
1566 4bb678e9 Iustin Pop
    mlist = ["/a", "b", "c"]
1567 4bb678e9 Iustin Pop
    self.failUnlessEqual(PathJoin(*mlist), "/".join(mlist))
1568 4bb678e9 Iustin Pop
1569 4bb678e9 Iustin Pop
  def testNonAbsPrefix(self):
1570 4bb678e9 Iustin Pop
    self.failUnlessRaises(ValueError, PathJoin, "a", "b")
1571 4bb678e9 Iustin Pop
1572 4bb678e9 Iustin Pop
  def testBackTrack(self):
1573 4bb678e9 Iustin Pop
    self.failUnlessRaises(ValueError, PathJoin, "/a", "b/../c")
1574 4bb678e9 Iustin Pop
1575 4bb678e9 Iustin Pop
  def testMultiAbs(self):
1576 4bb678e9 Iustin Pop
    self.failUnlessRaises(ValueError, PathJoin, "/a", "/b")
1577 4bb678e9 Iustin Pop
1578 4bb678e9 Iustin Pop
1579 26288e68 Iustin Pop
class TestHostInfo(unittest.TestCase):
1580 26288e68 Iustin Pop
  """Testing case for HostInfo"""
1581 26288e68 Iustin Pop
1582 26288e68 Iustin Pop
  def testUppercase(self):
1583 26288e68 Iustin Pop
    data = "AbC.example.com"
1584 26288e68 Iustin Pop
    self.failUnlessEqual(HostInfo.NormalizeName(data), data.lower())
1585 26288e68 Iustin Pop
1586 26288e68 Iustin Pop
  def testTooLongName(self):
1587 26288e68 Iustin Pop
    data = "a.b." + "c" * 255
1588 26288e68 Iustin Pop
    self.failUnlessRaises(OpPrereqError, HostInfo.NormalizeName, data)
1589 26288e68 Iustin Pop
1590 26288e68 Iustin Pop
  def testTrailingDot(self):
1591 26288e68 Iustin Pop
    data = "a.b.c"
1592 26288e68 Iustin Pop
    self.failUnlessEqual(HostInfo.NormalizeName(data + "."), data)
1593 26288e68 Iustin Pop
1594 26288e68 Iustin Pop
  def testInvalidName(self):
1595 26288e68 Iustin Pop
    data = [
1596 26288e68 Iustin Pop
      "a b",
1597 26288e68 Iustin Pop
      "a/b",
1598 26288e68 Iustin Pop
      ".a.b",
1599 26288e68 Iustin Pop
      "a..b",
1600 26288e68 Iustin Pop
      ]
1601 26288e68 Iustin Pop
    for value in data:
1602 26288e68 Iustin Pop
      self.failUnlessRaises(OpPrereqError, HostInfo.NormalizeName, value)
1603 26288e68 Iustin Pop
1604 26288e68 Iustin Pop
  def testValidName(self):
1605 26288e68 Iustin Pop
    data = [
1606 26288e68 Iustin Pop
      "a.b",
1607 26288e68 Iustin Pop
      "a-b",
1608 26288e68 Iustin Pop
      "a_b",
1609 26288e68 Iustin Pop
      "a.b.c",
1610 26288e68 Iustin Pop
      ]
1611 26288e68 Iustin Pop
    for value in data:
1612 26288e68 Iustin Pop
      HostInfo.NormalizeName(value)
1613 26288e68 Iustin Pop
1614 26288e68 Iustin Pop
1615 27e46076 Michael Hanselmann
class TestParseAsn1Generalizedtime(unittest.TestCase):
1616 27e46076 Michael Hanselmann
  def test(self):
1617 27e46076 Michael Hanselmann
    # UTC
1618 27e46076 Michael Hanselmann
    self.assertEqual(utils._ParseAsn1Generalizedtime("19700101000000Z"), 0)
1619 27e46076 Michael Hanselmann
    self.assertEqual(utils._ParseAsn1Generalizedtime("20100222174152Z"),
1620 27e46076 Michael Hanselmann
                     1266860512)
1621 27e46076 Michael Hanselmann
    self.assertEqual(utils._ParseAsn1Generalizedtime("20380119031407Z"),
1622 27e46076 Michael Hanselmann
                     (2**31) - 1)
1623 27e46076 Michael Hanselmann
1624 27e46076 Michael Hanselmann
    # With offset
1625 27e46076 Michael Hanselmann
    self.assertEqual(utils._ParseAsn1Generalizedtime("20100222174152+0000"),
1626 27e46076 Michael Hanselmann
                     1266860512)
1627 27e46076 Michael Hanselmann
    self.assertEqual(utils._ParseAsn1Generalizedtime("20100223131652+0000"),
1628 27e46076 Michael Hanselmann
                     1266931012)
1629 27e46076 Michael Hanselmann
    self.assertEqual(utils._ParseAsn1Generalizedtime("20100223051808-0800"),
1630 27e46076 Michael Hanselmann
                     1266931088)
1631 27e46076 Michael Hanselmann
    self.assertEqual(utils._ParseAsn1Generalizedtime("20100224002135+1100"),
1632 27e46076 Michael Hanselmann
                     1266931295)
1633 27e46076 Michael Hanselmann
    self.assertEqual(utils._ParseAsn1Generalizedtime("19700101000000-0100"),
1634 27e46076 Michael Hanselmann
                     3600)
1635 27e46076 Michael Hanselmann
1636 27e46076 Michael Hanselmann
    # Leap seconds are not supported by datetime.datetime
1637 27e46076 Michael Hanselmann
    self.assertRaises(ValueError, utils._ParseAsn1Generalizedtime,
1638 27e46076 Michael Hanselmann
                      "19841231235960+0000")
1639 27e46076 Michael Hanselmann
    self.assertRaises(ValueError, utils._ParseAsn1Generalizedtime,
1640 27e46076 Michael Hanselmann
                      "19920630235960+0000")
1641 27e46076 Michael Hanselmann
1642 27e46076 Michael Hanselmann
    # Errors
1643 27e46076 Michael Hanselmann
    self.assertRaises(ValueError, utils._ParseAsn1Generalizedtime, "")
1644 27e46076 Michael Hanselmann
    self.assertRaises(ValueError, utils._ParseAsn1Generalizedtime, "invalid")
1645 27e46076 Michael Hanselmann
    self.assertRaises(ValueError, utils._ParseAsn1Generalizedtime,
1646 27e46076 Michael Hanselmann
                      "20100222174152")
1647 27e46076 Michael Hanselmann
    self.assertRaises(ValueError, utils._ParseAsn1Generalizedtime,
1648 27e46076 Michael Hanselmann
                      "Mon Feb 22 17:47:02 UTC 2010")
1649 27e46076 Michael Hanselmann
    self.assertRaises(ValueError, utils._ParseAsn1Generalizedtime,
1650 27e46076 Michael Hanselmann
                      "2010-02-22 17:42:02")
1651 27e46076 Michael Hanselmann
1652 27e46076 Michael Hanselmann
1653 27e46076 Michael Hanselmann
class TestGetX509CertValidity(testutils.GanetiTestCase):
1654 27e46076 Michael Hanselmann
  def setUp(self):
1655 27e46076 Michael Hanselmann
    testutils.GanetiTestCase.setUp(self)
1656 27e46076 Michael Hanselmann
1657 27e46076 Michael Hanselmann
    pyopenssl_version = distutils.version.LooseVersion(OpenSSL.__version__)
1658 27e46076 Michael Hanselmann
1659 27e46076 Michael Hanselmann
    # Test whether we have pyOpenSSL 0.7 or above
1660 27e46076 Michael Hanselmann
    self.pyopenssl0_7 = (pyopenssl_version >= "0.7")
1661 27e46076 Michael Hanselmann
1662 27e46076 Michael Hanselmann
    if not self.pyopenssl0_7:
1663 27e46076 Michael Hanselmann
      warnings.warn("This test requires pyOpenSSL 0.7 or above to"
1664 27e46076 Michael Hanselmann
                    " function correctly")
1665 27e46076 Michael Hanselmann
1666 27e46076 Michael Hanselmann
  def _LoadCert(self, name):
1667 27e46076 Michael Hanselmann
    return OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
1668 27e46076 Michael Hanselmann
                                           self._ReadTestData(name))
1669 27e46076 Michael Hanselmann
1670 27e46076 Michael Hanselmann
  def test(self):
1671 27e46076 Michael Hanselmann
    validity = utils.GetX509CertValidity(self._LoadCert("cert1.pem"))
1672 27e46076 Michael Hanselmann
    if self.pyopenssl0_7:
1673 27e46076 Michael Hanselmann
      self.assertEqual(validity, (1266919967, 1267524767))
1674 27e46076 Michael Hanselmann
    else:
1675 27e46076 Michael Hanselmann
      self.assertEqual(validity, (None, None))
1676 27e46076 Michael Hanselmann
1677 26288e68 Iustin Pop
1678 76e5f8b5 Michael Hanselmann
class TestMakedirs(unittest.TestCase):
1679 76e5f8b5 Michael Hanselmann
  def setUp(self):
1680 76e5f8b5 Michael Hanselmann
    self.tmpdir = tempfile.mkdtemp()
1681 76e5f8b5 Michael Hanselmann
1682 76e5f8b5 Michael Hanselmann
  def tearDown(self):
1683 76e5f8b5 Michael Hanselmann
    shutil.rmtree(self.tmpdir)
1684 76e5f8b5 Michael Hanselmann
1685 76e5f8b5 Michael Hanselmann
  def testNonExisting(self):
1686 76e5f8b5 Michael Hanselmann
    path = utils.PathJoin(self.tmpdir, "foo")
1687 76e5f8b5 Michael Hanselmann
    utils.Makedirs(path)
1688 76e5f8b5 Michael Hanselmann
    self.assert_(os.path.isdir(path))
1689 76e5f8b5 Michael Hanselmann
1690 76e5f8b5 Michael Hanselmann
  def testExisting(self):
1691 76e5f8b5 Michael Hanselmann
    path = utils.PathJoin(self.tmpdir, "foo")
1692 76e5f8b5 Michael Hanselmann
    os.mkdir(path)
1693 76e5f8b5 Michael Hanselmann
    utils.Makedirs(path)
1694 76e5f8b5 Michael Hanselmann
    self.assert_(os.path.isdir(path))
1695 76e5f8b5 Michael Hanselmann
1696 76e5f8b5 Michael Hanselmann
  def testRecursiveNonExisting(self):
1697 76e5f8b5 Michael Hanselmann
    path = utils.PathJoin(self.tmpdir, "foo/bar/baz")
1698 76e5f8b5 Michael Hanselmann
    utils.Makedirs(path)
1699 76e5f8b5 Michael Hanselmann
    self.assert_(os.path.isdir(path))
1700 76e5f8b5 Michael Hanselmann
1701 76e5f8b5 Michael Hanselmann
  def testRecursiveExisting(self):
1702 76e5f8b5 Michael Hanselmann
    path = utils.PathJoin(self.tmpdir, "B/moo/xyz")
1703 76e5f8b5 Michael Hanselmann
    self.assert_(not os.path.exists(path))
1704 76e5f8b5 Michael Hanselmann
    os.mkdir(utils.PathJoin(self.tmpdir, "B"))
1705 76e5f8b5 Michael Hanselmann
    utils.Makedirs(path)
1706 76e5f8b5 Michael Hanselmann
    self.assert_(os.path.isdir(path))
1707 76e5f8b5 Michael Hanselmann
1708 76e5f8b5 Michael Hanselmann
1709 1b429e2a Iustin Pop
class TestRetry(testutils.GanetiTestCase):
1710 45cc4913 Guido Trotter
  def setUp(self):
1711 45cc4913 Guido Trotter
    testutils.GanetiTestCase.setUp(self)
1712 45cc4913 Guido Trotter
    self.retries = 0
1713 45cc4913 Guido Trotter
1714 1b429e2a Iustin Pop
  @staticmethod
1715 1b429e2a Iustin Pop
  def _RaiseRetryAgain():
1716 1b429e2a Iustin Pop
    raise utils.RetryAgain()
1717 1b429e2a Iustin Pop
1718 506be7c5 Guido Trotter
  @staticmethod
1719 506be7c5 Guido Trotter
  def _RaiseRetryAgainWithArg(args):
1720 506be7c5 Guido Trotter
    raise utils.RetryAgain(*args)
1721 506be7c5 Guido Trotter
1722 1b429e2a Iustin Pop
  def _WrongNestedLoop(self):
1723 1b429e2a Iustin Pop
    return utils.Retry(self._RaiseRetryAgain, 0.01, 0.02)
1724 1b429e2a Iustin Pop
1725 45cc4913 Guido Trotter
  def _RetryAndSucceed(self, retries):
1726 45cc4913 Guido Trotter
    if self.retries < retries:
1727 45cc4913 Guido Trotter
      self.retries += 1
1728 45cc4913 Guido Trotter
      raise utils.RetryAgain()
1729 45cc4913 Guido Trotter
    else:
1730 45cc4913 Guido Trotter
      return True
1731 45cc4913 Guido Trotter
1732 1b429e2a Iustin Pop
  def testRaiseTimeout(self):
1733 1b429e2a Iustin Pop
    self.failUnlessRaises(utils.RetryTimeout, utils.Retry,
1734 1b429e2a Iustin Pop
                          self._RaiseRetryAgain, 0.01, 0.02)
1735 45cc4913 Guido Trotter
    self.failUnlessRaises(utils.RetryTimeout, utils.Retry,
1736 45cc4913 Guido Trotter
                          self._RetryAndSucceed, 0.01, 0, args=[1])
1737 45cc4913 Guido Trotter
    self.failUnlessEqual(self.retries, 1)
1738 1b429e2a Iustin Pop
1739 1b429e2a Iustin Pop
  def testComplete(self):
1740 1b429e2a Iustin Pop
    self.failUnlessEqual(utils.Retry(lambda: True, 0, 1), True)
1741 45cc4913 Guido Trotter
    self.failUnlessEqual(utils.Retry(self._RetryAndSucceed, 0, 1, args=[2]),
1742 45cc4913 Guido Trotter
                         True)
1743 45cc4913 Guido Trotter
    self.failUnlessEqual(self.retries, 2)
1744 1b429e2a Iustin Pop
1745 1b429e2a Iustin Pop
  def testNestedLoop(self):
1746 1b429e2a Iustin Pop
    try:
1747 1b429e2a Iustin Pop
      self.failUnlessRaises(errors.ProgrammerError, utils.Retry,
1748 1b429e2a Iustin Pop
                            self._WrongNestedLoop, 0, 1)
1749 1b429e2a Iustin Pop
    except utils.RetryTimeout:
1750 1b429e2a Iustin Pop
      self.fail("Didn't detect inner loop's exception")
1751 1b429e2a Iustin Pop
1752 506be7c5 Guido Trotter
  def testTimeoutArgument(self):
1753 506be7c5 Guido Trotter
    retry_arg="my_important_debugging_message"
1754 506be7c5 Guido Trotter
    try:
1755 506be7c5 Guido Trotter
      utils.Retry(self._RaiseRetryAgainWithArg, 0.01, 0.02, args=[[retry_arg]])
1756 506be7c5 Guido Trotter
    except utils.RetryTimeout, err:
1757 506be7c5 Guido Trotter
      self.failUnlessEqual(err.args, (retry_arg, ))
1758 506be7c5 Guido Trotter
    else:
1759 506be7c5 Guido Trotter
      self.fail("Expected timeout didn't happen")
1760 506be7c5 Guido Trotter
1761 506be7c5 Guido Trotter
  def testRaiseInnerWithExc(self):
1762 506be7c5 Guido Trotter
    retry_arg="my_important_debugging_message"
1763 506be7c5 Guido Trotter
    try:
1764 506be7c5 Guido Trotter
      try:
1765 506be7c5 Guido Trotter
        utils.Retry(self._RaiseRetryAgainWithArg, 0.01, 0.02,
1766 506be7c5 Guido Trotter
                    args=[[errors.GenericError(retry_arg, retry_arg)]])
1767 506be7c5 Guido Trotter
      except utils.RetryTimeout, err:
1768 506be7c5 Guido Trotter
        err.RaiseInner()
1769 506be7c5 Guido Trotter
      else:
1770 506be7c5 Guido Trotter
        self.fail("Expected timeout didn't happen")
1771 506be7c5 Guido Trotter
    except errors.GenericError, err:
1772 506be7c5 Guido Trotter
      self.failUnlessEqual(err.args, (retry_arg, retry_arg))
1773 506be7c5 Guido Trotter
    else:
1774 506be7c5 Guido Trotter
      self.fail("Expected GenericError didn't happen")
1775 506be7c5 Guido Trotter
1776 506be7c5 Guido Trotter
  def testRaiseInnerWithMsg(self):
1777 506be7c5 Guido Trotter
    retry_arg="my_important_debugging_message"
1778 506be7c5 Guido Trotter
    try:
1779 506be7c5 Guido Trotter
      try:
1780 506be7c5 Guido Trotter
        utils.Retry(self._RaiseRetryAgainWithArg, 0.01, 0.02,
1781 506be7c5 Guido Trotter
                    args=[[retry_arg, retry_arg]])
1782 506be7c5 Guido Trotter
      except utils.RetryTimeout, err:
1783 506be7c5 Guido Trotter
        err.RaiseInner()
1784 506be7c5 Guido Trotter
      else:
1785 506be7c5 Guido Trotter
        self.fail("Expected timeout didn't happen")
1786 506be7c5 Guido Trotter
    except utils.RetryTimeout, err:
1787 506be7c5 Guido Trotter
      self.failUnlessEqual(err.args, (retry_arg, retry_arg))
1788 506be7c5 Guido Trotter
    else:
1789 506be7c5 Guido Trotter
      self.fail("Expected RetryTimeout didn't happen")
1790 506be7c5 Guido Trotter
1791 1b429e2a Iustin Pop
1792 339be5a8 Michael Hanselmann
class TestLineSplitter(unittest.TestCase):
1793 339be5a8 Michael Hanselmann
  def test(self):
1794 339be5a8 Michael Hanselmann
    lines = []
1795 339be5a8 Michael Hanselmann
    ls = utils.LineSplitter(lines.append)
1796 339be5a8 Michael Hanselmann
    ls.write("Hello World\n")
1797 339be5a8 Michael Hanselmann
    self.assertEqual(lines, [])
1798 339be5a8 Michael Hanselmann
    ls.write("Foo\n Bar\r\n ")
1799 339be5a8 Michael Hanselmann
    ls.write("Baz")
1800 339be5a8 Michael Hanselmann
    ls.write("Moo")
1801 339be5a8 Michael Hanselmann
    self.assertEqual(lines, [])
1802 339be5a8 Michael Hanselmann
    ls.flush()
1803 339be5a8 Michael Hanselmann
    self.assertEqual(lines, ["Hello World", "Foo", " Bar"])
1804 339be5a8 Michael Hanselmann
    ls.close()
1805 339be5a8 Michael Hanselmann
    self.assertEqual(lines, ["Hello World", "Foo", " Bar", " BazMoo"])
1806 339be5a8 Michael Hanselmann
1807 339be5a8 Michael Hanselmann
  def _testExtra(self, line, all_lines, p1, p2):
1808 339be5a8 Michael Hanselmann
    self.assertEqual(p1, 999)
1809 339be5a8 Michael Hanselmann
    self.assertEqual(p2, "extra")
1810 339be5a8 Michael Hanselmann
    all_lines.append(line)
1811 339be5a8 Michael Hanselmann
1812 339be5a8 Michael Hanselmann
  def testExtraArgsNoFlush(self):
1813 339be5a8 Michael Hanselmann
    lines = []
1814 339be5a8 Michael Hanselmann
    ls = utils.LineSplitter(self._testExtra, lines, 999, "extra")
1815 339be5a8 Michael Hanselmann
    ls.write("\n\nHello World\n")
1816 339be5a8 Michael Hanselmann
    ls.write("Foo\n Bar\r\n ")
1817 339be5a8 Michael Hanselmann
    ls.write("")
1818 339be5a8 Michael Hanselmann
    ls.write("Baz")
1819 339be5a8 Michael Hanselmann
    ls.write("Moo\n\nx\n")
1820 339be5a8 Michael Hanselmann
    self.assertEqual(lines, [])
1821 339be5a8 Michael Hanselmann
    ls.close()
1822 339be5a8 Michael Hanselmann
    self.assertEqual(lines, ["", "", "Hello World", "Foo", " Bar", " BazMoo",
1823 339be5a8 Michael Hanselmann
                             "", "x"])
1824 339be5a8 Michael Hanselmann
1825 339be5a8 Michael Hanselmann
1826 232144d0 Guido Trotter
class TestIgnoreSignals(unittest.TestCase):
1827 232144d0 Guido Trotter
  """Test the IgnoreSignals decorator"""
1828 232144d0 Guido Trotter
1829 232144d0 Guido Trotter
  @staticmethod
1830 232144d0 Guido Trotter
  def _Raise(exception):
1831 232144d0 Guido Trotter
    raise exception
1832 232144d0 Guido Trotter
1833 232144d0 Guido Trotter
  @staticmethod
1834 232144d0 Guido Trotter
  def _Return(rval):
1835 232144d0 Guido Trotter
    return rval
1836 232144d0 Guido Trotter
1837 232144d0 Guido Trotter
  def testIgnoreSignals(self):
1838 232144d0 Guido Trotter
    sock_err_intr = socket.error(errno.EINTR, "Message")
1839 232144d0 Guido Trotter
    sock_err_intr.errno = errno.EINTR
1840 232144d0 Guido Trotter
    sock_err_inval = socket.error(errno.EINVAL, "Message")
1841 232144d0 Guido Trotter
    sock_err_inval.errno = errno.EINVAL
1842 232144d0 Guido Trotter
1843 232144d0 Guido Trotter
    env_err_intr = EnvironmentError(errno.EINTR, "Message")
1844 232144d0 Guido Trotter
    env_err_inval = EnvironmentError(errno.EINVAL, "Message")
1845 232144d0 Guido Trotter
1846 232144d0 Guido Trotter
    self.assertRaises(socket.error, self._Raise, sock_err_intr)
1847 232144d0 Guido Trotter
    self.assertRaises(socket.error, self._Raise, sock_err_inval)
1848 232144d0 Guido Trotter
    self.assertRaises(EnvironmentError, self._Raise, env_err_intr)
1849 232144d0 Guido Trotter
    self.assertRaises(EnvironmentError, self._Raise, env_err_inval)
1850 232144d0 Guido Trotter
1851 232144d0 Guido Trotter
    self.assertEquals(utils.IgnoreSignals(self._Raise, sock_err_intr), None)
1852 232144d0 Guido Trotter
    self.assertEquals(utils.IgnoreSignals(self._Raise, env_err_intr), None)
1853 232144d0 Guido Trotter
    self.assertRaises(socket.error, utils.IgnoreSignals, self._Raise,
1854 232144d0 Guido Trotter
                      sock_err_inval)
1855 232144d0 Guido Trotter
    self.assertRaises(EnvironmentError, utils.IgnoreSignals, self._Raise,
1856 232144d0 Guido Trotter
                      env_err_inval)
1857 232144d0 Guido Trotter
1858 232144d0 Guido Trotter
    self.assertEquals(utils.IgnoreSignals(self._Return, True), True)
1859 232144d0 Guido Trotter
    self.assertEquals(utils.IgnoreSignals(self._Return, 33), 33)
1860 232144d0 Guido Trotter
1861 232144d0 Guido Trotter
1862 a8083063 Iustin Pop
if __name__ == '__main__':
1863 25231ec5 Michael Hanselmann
  testutils.GanetiTestProgram()