From 51596eb2c8b20e8d7dd01e23fe76625e82cf17c3 Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Tue, 20 Jan 2009 11:18:10 +0000 Subject: [PATCH] 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 --- test/ganeti.bdev_unittest.py | 1 + test/ganeti.ssh_unittest.py | 7 +++-- test/ganeti.utils_unittest.py | 52 ++++++++++++----------------------- test/testutils.py | 29 +++++++++++++++++++ 4 files changed, 51 insertions(+), 38 deletions(-) diff --git a/test/ganeti.bdev_unittest.py b/test/ganeti.bdev_unittest.py index e43fd1e68..04823eb62 100755 --- a/test/ganeti.bdev_unittest.py +++ b/test/ganeti.bdev_unittest.py @@ -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) diff --git a/test/ganeti.ssh_unittest.py b/test/ganeti.ssh_unittest.py index 2121355be..d99992947 100755 --- a/test/ganeti.ssh_unittest.py +++ b/test/ganeti.ssh_unittest.py @@ -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)) diff --git a/test/ganeti.utils_unittest.py b/test/ganeti.utils_unittest.py index 29fddfdd0..133d68765 100755 --- a/test/ganeti.utils_unittest.py +++ b/test/ganeti.utils_unittest.py @@ -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']) diff --git a/test/testutils.py b/test/testutils.py index 737962e46..f2980ddc9 100644 --- a/test/testutils.py +++ b/test/testutils.py @@ -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 -- GitLab