From 6693c9fb04722de9b911fd51a708d70158760178 Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Sat, 18 May 2013 17:26:38 +1000 Subject: [PATCH] Use fixtures in the tests TempDir() makes sure all files created under it are deleted at the end of the tests. FakeLogger() hides logs when the test passes and displays logs for failed tests. part of bug 1177924 Change-Id: I07acb66daa1932d7864a5431f1b64570b747ce5a --- ceilometer/tests/base.py | 8 ++++++++ tests/api/v1/test_app.py | 3 +-- tests/api/v2/test_app.py | 3 +-- tests/test_bin.py | 17 +++-------------- tools/test-requires | 1 + 5 files changed, 14 insertions(+), 18 deletions(-) diff --git a/ceilometer/tests/base.py b/ceilometer/tests/base.py index 9263c540b..befe9b437 100644 --- a/ceilometer/tests/base.py +++ b/ceilometer/tests/base.py @@ -18,8 +18,10 @@ # under the License. """Test base classes. """ +import fixtures import mox from oslo.config import cfg +import os.path import stubout import testtools @@ -32,6 +34,9 @@ class TestCase(testtools.TestCase): super(TestCase, self).setUp() self.mox = mox.Mox() self.stubs = stubout.StubOutForTesting() + self.tempdir = self.useFixture(fixtures.TempDir()) + self.useFixture(fixtures.FakeLogger()) + # Set a default location for the pipeline config file so the # tests work even if ceilometer is not installed globally on # the system. @@ -40,6 +45,9 @@ class TestCase(testtools.TestCase): '../etc/ceilometer/pipeline.yaml', ) + def temp_config_file_path(self, name='ceilometer.conf'): + return os.path.join(self.tempdir.path, name) + def tearDown(self): self.mox.UnsetStubs() self.stubs.UnsetAll() diff --git a/tests/api/v1/test_app.py b/tests/api/v1/test_app.py index f72b678a0..69c908a76 100644 --- a/tests/api/v1/test_app.py +++ b/tests/api/v1/test_app.py @@ -18,7 +18,6 @@ """Test basic ceilometer-api app """ import os -import tempfile from oslo.config import cfg @@ -42,7 +41,7 @@ class TestApp(base.TestCase): self.assertEqual(api_app.wsgi_app.auth_protocol, 'foottp') def test_keystone_middleware_parse_conffile(self): - tmpfile = tempfile.mktemp() + tmpfile = self.temp_config_file_path() with open(tmpfile, "w") as f: f.write("[%s]\nauth_protocol = barttp" % acl.OPT_GROUP_NAME) f.write("\nauth_version = v2.0") diff --git a/tests/api/v2/test_app.py b/tests/api/v2/test_app.py index 908e84016..ede22be93 100644 --- a/tests/api/v2/test_app.py +++ b/tests/api/v2/test_app.py @@ -18,7 +18,6 @@ """Test basic ceilometer-api app """ import os -import tempfile from oslo.config import cfg @@ -46,7 +45,7 @@ class TestApp(base.TestCase): self.assertEqual(api_app.auth_protocol, 'foottp') def test_keystone_middleware_parse_conffile(self): - tmpfile = tempfile.mktemp() + tmpfile = self.temp_config_file_path() with open(tmpfile, "w") as f: f.write("[DEFAULT]\n") f.write("pipeline_cfg_file = ../etc/ceilometer/pipeline.yaml\n") diff --git a/tests/test_bin.py b/tests/test_bin.py index 8e5ae2e27..396b39736 100644 --- a/tests/test_bin.py +++ b/tests/test_bin.py @@ -19,11 +19,9 @@ import httplib2 import json -import os import random import socket import subprocess -import tempfile import time from ceilometer.tests import base @@ -32,7 +30,7 @@ from ceilometer.tests import base class BinDbsyncTestCase(base.TestCase): def setUp(self): super(BinDbsyncTestCase, self).setUp() - self.tempfile = tempfile.mktemp() + self.tempfile = self.temp_config_file_path() with open(self.tempfile, 'w') as tmp: tmp.write("[DEFAULT]\n") tmp.write("database_connection=log://localhost\n") @@ -42,15 +40,11 @@ class BinDbsyncTestCase(base.TestCase): "--config-file=%s" % self.tempfile]) self.assertEqual(subp.wait(), 0) - def tearDown(self): - super(BinDbsyncTestCase, self).tearDown() - os.unlink(self.tempfile) - class BinSendCounterTestCase(base.TestCase): def setUp(self): super(BinSendCounterTestCase, self).setUp() - self.tempfile = tempfile.mktemp() + self.tempfile = self.temp_config_file_path() with open(self.tempfile, 'w') as tmp: tmp.write("[DEFAULT]\n") tmp.write( @@ -65,10 +59,6 @@ class BinSendCounterTestCase(base.TestCase): "--counter-name=mycounter"]) self.assertEqual(subp.wait(), 0) - def tearDown(self): - super(BinSendCounterTestCase, self).tearDown() - os.unlink(self.tempfile) - class BinApiTestCase(base.TestCase): @@ -76,7 +66,7 @@ class BinApiTestCase(base.TestCase): super(BinApiTestCase, self).setUp() self.api_port = random.randint(10000, 11000) self.http = httplib2.Http() - self.tempfile = tempfile.mktemp() + self.tempfile = self.temp_config_file_path() with open(self.tempfile, 'w') as tmp: tmp.write("[DEFAULT]\n") tmp.write( @@ -95,7 +85,6 @@ class BinApiTestCase(base.TestCase): def tearDown(self): super(BinApiTestCase, self).tearDown() - os.unlink(self.tempfile) self.subp.kill() self.subp.wait() diff --git a/tools/test-requires b/tools/test-requires index 66a4bb5d4..47dfc659d 100644 --- a/tools/test-requires +++ b/tools/test-requires @@ -2,6 +2,7 @@ nose coverage mock mox +fixtures>=0.3.12 Babel>=0.9.6 # NOTE(dhellmann): Ming is necessary to provide the Mongo-in-memory # implementation of MongoDB.