Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.utils_unittest.py @ b774bb10

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

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