Commit 51596eb2 authored by Iustin Pop's avatar Iustin Pop
Browse files

Unify some unittest functions

This patch adds unified temporary file handling to the
testutils.GanetiTestCase class, which adds easy creation and automated
cleanup of temporary files.

The patch allows a simpler handling in a couple of test cases but
requires all child classes to call the parent setUp and tearDown
methods.

Reviewed-by: ultrotter
parent 1492cca7
......@@ -101,6 +101,7 @@ class TestDRBD8Status(testutils.GanetiTestCase):
def setUp(self):
"""Read in txt data"""
testutils.GanetiTestCase.setUp(self)
proc_data = self._TestDataFilename("proc_drbd8.txt")
self.proc_data = bdev.DRBD8._GetProcData(filename=proc_data)
self.mass_data = bdev.DRBD8._MassageProcData(self.proc_data)
......
......@@ -37,12 +37,13 @@ class TestKnownHosts(testutils.GanetiTestCase):
"""Test case for function writing the known_hosts file"""
def setUp(self):
self.tmpfile = tempfile.NamedTemporaryFile()
testutils.GanetiTestCase.setUp(self)
self.tmpfile = self._CreateTempFile()
def test(self):
cfg = mocks.FakeConfig()
ssh.WriteKnownHostsFile(cfg, self.tmpfile.name)
self.assertFileContent(self.tmpfile.name,
ssh.WriteKnownHostsFile(cfg, self.tmpfile)
self.assertFileContent(self.tmpfile,
"%s ssh-rsa %s\n" % (cfg.GetClusterName(),
mocks.FAKE_CLUSTER_KEY))
......
......@@ -128,13 +128,9 @@ class TestRunCmd(testutils.GanetiTestCase):
"""Testing case for the RunCmd function"""
def setUp(self):
testutils.GanetiTestCase.setUp(self)
self.magic = time.ctime() + " ganeti test"
fh, self.fname = tempfile.mkstemp()
os.close(fh)
def tearDown(self):
if self.fname:
utils.RemoveFile(self.fname)
self.fname = self._CreateTempFile()
def testOk(self):
"""Test successful exit code"""
......@@ -451,21 +447,14 @@ class TestSshKeys(testutils.GanetiTestCase):
'ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b')
def setUp(self):
(fd, self.tmpname) = tempfile.mkstemp(prefix='ganeti-test')
testutils.GanetiTestCase.setUp(self)
self.tmpname = self._CreateTempFile()
handle = open(self.tmpname, 'w')
try:
handle = os.fdopen(fd, 'w')
try:
handle.write("%s\n" % TestSshKeys.KEY_A)
handle.write("%s\n" % TestSshKeys.KEY_B)
finally:
handle.close()
except:
utils.RemoveFile(self.tmpname)
raise
def tearDown(self):
utils.RemoveFile(self.tmpname)
del self.tmpname
handle.write("%s\n" % TestSshKeys.KEY_A)
handle.write("%s\n" % TestSshKeys.KEY_B)
finally:
handle.close()
def testAddingNewKey(self):
AddAuthorizedKey(self.tmpname, 'ssh-dss AAAAB3NzaC1kc3MAAACB root@test')
......@@ -517,22 +506,15 @@ class TestEtcHosts(testutils.GanetiTestCase):
"""Test functions modifying /etc/hosts"""
def setUp(self):
(fd, self.tmpname) = tempfile.mkstemp(prefix='ganeti-test')
testutils.GanetiTestCase.setUp(self)
self.tmpname = self._CreateTempFile()
handle = open(self.tmpname, 'w')
try:
handle = os.fdopen(fd, 'w')
try:
handle.write('# This is a test file for /etc/hosts\n')
handle.write('127.0.0.1\tlocalhost\n')
handle.write('192.168.1.1 router gw\n')
finally:
handle.close()
except:
utils.RemoveFile(self.tmpname)
raise
def tearDown(self):
utils.RemoveFile(self.tmpname)
del self.tmpname
handle.write('# This is a test file for /etc/hosts\n')
handle.write('127.0.0.1\tlocalhost\n')
handle.write('192.168.1.1 router gw\n')
finally:
handle.close()
def testSettingNewIp(self):
SetEtcHostsEntry(self.tmpname, '1.2.3.4', 'myhost.domain.tld', ['myhost'])
......
......@@ -22,12 +22,29 @@
"""Utilities for unit testing"""
import os
import tempfile
import unittest
from ganeti import utils
class GanetiTestCase(unittest.TestCase):
"""Helper class for unittesting.
This class defines a few utility functions that help in building
unittests. Child classes must call the parent setup and cleanup.
"""
def setUp(self):
self._temp_files = []
def tearDown(self):
while self._temp_files:
try:
utils.RemoveFile(self._temp_files.pop())
except EnvironmentError, err:
pass
def assertFileContent(self, file_name, expected_content):
"""Checks the content of a file is what we expect.
......@@ -67,3 +84,15 @@ class GanetiTestCase(unittest.TestCase):
"""
return utils.ReadFile(cls._TestDataFilename(name))
def _CreateTempFile(self):
"""Creates a temporary file and adds it to the internal cleanup list.
This method simplifies the creation and cleanup of temporary files
during tests.
"""
fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp")
os.close(fh)
self._temp_files.append(fname)
return fname
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment