Revision 51596eb2

b/test/ganeti.bdev_unittest.py
101 101

  
102 102
  def setUp(self):
103 103
    """Read in txt data"""
104
    testutils.GanetiTestCase.setUp(self)
104 105
    proc_data = self._TestDataFilename("proc_drbd8.txt")
105 106
    self.proc_data = bdev.DRBD8._GetProcData(filename=proc_data)
106 107
    self.mass_data = bdev.DRBD8._MassageProcData(self.proc_data)
b/test/ganeti.ssh_unittest.py
37 37
  """Test case for function writing the known_hosts file"""
38 38

  
39 39
  def setUp(self):
40
    self.tmpfile = tempfile.NamedTemporaryFile()
40
    testutils.GanetiTestCase.setUp(self)
41
    self.tmpfile = self._CreateTempFile()
41 42

  
42 43
  def test(self):
43 44
    cfg = mocks.FakeConfig()
44
    ssh.WriteKnownHostsFile(cfg, self.tmpfile.name)
45
    self.assertFileContent(self.tmpfile.name,
45
    ssh.WriteKnownHostsFile(cfg, self.tmpfile)
46
    self.assertFileContent(self.tmpfile,
46 47
        "%s ssh-rsa %s\n" % (cfg.GetClusterName(),
47 48
                             mocks.FAKE_CLUSTER_KEY))
48 49

  
b/test/ganeti.utils_unittest.py
128 128
  """Testing case for the RunCmd function"""
129 129

  
130 130
  def setUp(self):
131
    testutils.GanetiTestCase.setUp(self)
131 132
    self.magic = time.ctime() + " ganeti test"
132
    fh, self.fname = tempfile.mkstemp()
133
    os.close(fh)
134

  
135
  def tearDown(self):
136
    if self.fname:
137
      utils.RemoveFile(self.fname)
133
    self.fname = self._CreateTempFile()
138 134

  
139 135
  def testOk(self):
140 136
    """Test successful exit code"""
......
451 447
           'ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b')
452 448

  
453 449
  def setUp(self):
454
    (fd, self.tmpname) = tempfile.mkstemp(prefix='ganeti-test')
450
    testutils.GanetiTestCase.setUp(self)
451
    self.tmpname = self._CreateTempFile()
452
    handle = open(self.tmpname, 'w')
455 453
    try:
456
      handle = os.fdopen(fd, 'w')
457
      try:
458
        handle.write("%s\n" % TestSshKeys.KEY_A)
459
        handle.write("%s\n" % TestSshKeys.KEY_B)
460
      finally:
461
        handle.close()
462
    except:
463
      utils.RemoveFile(self.tmpname)
464
      raise
465

  
466
  def tearDown(self):
467
    utils.RemoveFile(self.tmpname)
468
    del self.tmpname
454
      handle.write("%s\n" % TestSshKeys.KEY_A)
455
      handle.write("%s\n" % TestSshKeys.KEY_B)
456
    finally:
457
      handle.close()
469 458

  
470 459
  def testAddingNewKey(self):
471 460
    AddAuthorizedKey(self.tmpname, 'ssh-dss AAAAB3NzaC1kc3MAAACB root@test')
......
517 506
  """Test functions modifying /etc/hosts"""
518 507

  
519 508
  def setUp(self):
520
    (fd, self.tmpname) = tempfile.mkstemp(prefix='ganeti-test')
509
    testutils.GanetiTestCase.setUp(self)
510
    self.tmpname = self._CreateTempFile()
511
    handle = open(self.tmpname, 'w')
521 512
    try:
522
      handle = os.fdopen(fd, 'w')
523
      try:
524
        handle.write('# This is a test file for /etc/hosts\n')
525
        handle.write('127.0.0.1\tlocalhost\n')
526
        handle.write('192.168.1.1 router gw\n')
527
      finally:
528
        handle.close()
529
    except:
530
      utils.RemoveFile(self.tmpname)
531
      raise
532

  
533
  def tearDown(self):
534
    utils.RemoveFile(self.tmpname)
535
    del self.tmpname
513
      handle.write('# This is a test file for /etc/hosts\n')
514
      handle.write('127.0.0.1\tlocalhost\n')
515
      handle.write('192.168.1.1 router gw\n')
516
    finally:
517
      handle.close()
536 518

  
537 519
  def testSettingNewIp(self):
538 520
    SetEtcHostsEntry(self.tmpname, '1.2.3.4', 'myhost.domain.tld', ['myhost'])
b/test/testutils.py
22 22
"""Utilities for unit testing"""
23 23

  
24 24
import os
25
import tempfile
25 26
import unittest
26 27

  
27 28
from ganeti import utils
28 29

  
29 30

  
30 31
class GanetiTestCase(unittest.TestCase):
32
  """Helper class for unittesting.
33

  
34
  This class defines a few utility functions that help in building
35
  unittests. Child classes must call the parent setup and cleanup.
36

  
37
  """
38
  def setUp(self):
39
    self._temp_files = []
40

  
41
  def tearDown(self):
42
    while self._temp_files:
43
      try:
44
        utils.RemoveFile(self._temp_files.pop())
45
      except EnvironmentError, err:
46
        pass
47

  
31 48
  def assertFileContent(self, file_name, expected_content):
32 49
    """Checks the content of a file is what we expect.
33 50

  
......
67 84
    """
68 85

  
69 86
    return utils.ReadFile(cls._TestDataFilename(name))
87

  
88
  def _CreateTempFile(self):
89
    """Creates a temporary file and adds it to the internal cleanup list.
90

  
91
    This method simplifies the creation and cleanup of temporary files
92
    during tests.
93

  
94
    """
95
    fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp")
96
    os.close(fh)
97
    self._temp_files.append(fname)
98
    return fname

Also available in: Unified diff