Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.utils_unittest.py @ 2a52a064

History | View | Annotate | Download (32.2 kB)

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