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
This commit is contained in:
Angus Salkeld 2013-05-18 17:26:38 +10:00
parent dea1ceb45a
commit 6693c9fb04
5 changed files with 14 additions and 18 deletions

View File

@ -18,8 +18,10 @@
# under the License. # under the License.
"""Test base classes. """Test base classes.
""" """
import fixtures
import mox import mox
from oslo.config import cfg from oslo.config import cfg
import os.path
import stubout import stubout
import testtools import testtools
@ -32,6 +34,9 @@ class TestCase(testtools.TestCase):
super(TestCase, self).setUp() super(TestCase, self).setUp()
self.mox = mox.Mox() self.mox = mox.Mox()
self.stubs = stubout.StubOutForTesting() 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 # Set a default location for the pipeline config file so the
# tests work even if ceilometer is not installed globally on # tests work even if ceilometer is not installed globally on
# the system. # the system.
@ -40,6 +45,9 @@ class TestCase(testtools.TestCase):
'../etc/ceilometer/pipeline.yaml', '../etc/ceilometer/pipeline.yaml',
) )
def temp_config_file_path(self, name='ceilometer.conf'):
return os.path.join(self.tempdir.path, name)
def tearDown(self): def tearDown(self):
self.mox.UnsetStubs() self.mox.UnsetStubs()
self.stubs.UnsetAll() self.stubs.UnsetAll()

View File

@ -18,7 +18,6 @@
"""Test basic ceilometer-api app """Test basic ceilometer-api app
""" """
import os import os
import tempfile
from oslo.config import cfg from oslo.config import cfg
@ -42,7 +41,7 @@ class TestApp(base.TestCase):
self.assertEqual(api_app.wsgi_app.auth_protocol, 'foottp') self.assertEqual(api_app.wsgi_app.auth_protocol, 'foottp')
def test_keystone_middleware_parse_conffile(self): def test_keystone_middleware_parse_conffile(self):
tmpfile = tempfile.mktemp() tmpfile = self.temp_config_file_path()
with open(tmpfile, "w") as f: with open(tmpfile, "w") as f:
f.write("[%s]\nauth_protocol = barttp" % acl.OPT_GROUP_NAME) f.write("[%s]\nauth_protocol = barttp" % acl.OPT_GROUP_NAME)
f.write("\nauth_version = v2.0") f.write("\nauth_version = v2.0")

View File

@ -18,7 +18,6 @@
"""Test basic ceilometer-api app """Test basic ceilometer-api app
""" """
import os import os
import tempfile
from oslo.config import cfg from oslo.config import cfg
@ -46,7 +45,7 @@ class TestApp(base.TestCase):
self.assertEqual(api_app.auth_protocol, 'foottp') self.assertEqual(api_app.auth_protocol, 'foottp')
def test_keystone_middleware_parse_conffile(self): def test_keystone_middleware_parse_conffile(self):
tmpfile = tempfile.mktemp() tmpfile = self.temp_config_file_path()
with open(tmpfile, "w") as f: with open(tmpfile, "w") as f:
f.write("[DEFAULT]\n") f.write("[DEFAULT]\n")
f.write("pipeline_cfg_file = ../etc/ceilometer/pipeline.yaml\n") f.write("pipeline_cfg_file = ../etc/ceilometer/pipeline.yaml\n")

View File

@ -19,11 +19,9 @@
import httplib2 import httplib2
import json import json
import os
import random import random
import socket import socket
import subprocess import subprocess
import tempfile
import time import time
from ceilometer.tests import base from ceilometer.tests import base
@ -32,7 +30,7 @@ from ceilometer.tests import base
class BinDbsyncTestCase(base.TestCase): class BinDbsyncTestCase(base.TestCase):
def setUp(self): def setUp(self):
super(BinDbsyncTestCase, self).setUp() super(BinDbsyncTestCase, self).setUp()
self.tempfile = tempfile.mktemp() self.tempfile = self.temp_config_file_path()
with open(self.tempfile, 'w') as tmp: with open(self.tempfile, 'w') as tmp:
tmp.write("[DEFAULT]\n") tmp.write("[DEFAULT]\n")
tmp.write("database_connection=log://localhost\n") tmp.write("database_connection=log://localhost\n")
@ -42,15 +40,11 @@ class BinDbsyncTestCase(base.TestCase):
"--config-file=%s" % self.tempfile]) "--config-file=%s" % self.tempfile])
self.assertEqual(subp.wait(), 0) self.assertEqual(subp.wait(), 0)
def tearDown(self):
super(BinDbsyncTestCase, self).tearDown()
os.unlink(self.tempfile)
class BinSendCounterTestCase(base.TestCase): class BinSendCounterTestCase(base.TestCase):
def setUp(self): def setUp(self):
super(BinSendCounterTestCase, self).setUp() super(BinSendCounterTestCase, self).setUp()
self.tempfile = tempfile.mktemp() self.tempfile = self.temp_config_file_path()
with open(self.tempfile, 'w') as tmp: with open(self.tempfile, 'w') as tmp:
tmp.write("[DEFAULT]\n") tmp.write("[DEFAULT]\n")
tmp.write( tmp.write(
@ -65,10 +59,6 @@ class BinSendCounterTestCase(base.TestCase):
"--counter-name=mycounter"]) "--counter-name=mycounter"])
self.assertEqual(subp.wait(), 0) self.assertEqual(subp.wait(), 0)
def tearDown(self):
super(BinSendCounterTestCase, self).tearDown()
os.unlink(self.tempfile)
class BinApiTestCase(base.TestCase): class BinApiTestCase(base.TestCase):
@ -76,7 +66,7 @@ class BinApiTestCase(base.TestCase):
super(BinApiTestCase, self).setUp() super(BinApiTestCase, self).setUp()
self.api_port = random.randint(10000, 11000) self.api_port = random.randint(10000, 11000)
self.http = httplib2.Http() self.http = httplib2.Http()
self.tempfile = tempfile.mktemp() self.tempfile = self.temp_config_file_path()
with open(self.tempfile, 'w') as tmp: with open(self.tempfile, 'w') as tmp:
tmp.write("[DEFAULT]\n") tmp.write("[DEFAULT]\n")
tmp.write( tmp.write(
@ -95,7 +85,6 @@ class BinApiTestCase(base.TestCase):
def tearDown(self): def tearDown(self):
super(BinApiTestCase, self).tearDown() super(BinApiTestCase, self).tearDown()
os.unlink(self.tempfile)
self.subp.kill() self.subp.kill()
self.subp.wait() self.subp.wait()

View File

@ -2,6 +2,7 @@ nose
coverage coverage
mock mock
mox mox
fixtures>=0.3.12
Babel>=0.9.6 Babel>=0.9.6
# NOTE(dhellmann): Ming is necessary to provide the Mongo-in-memory # NOTE(dhellmann): Ming is necessary to provide the Mongo-in-memory
# implementation of MongoDB. # implementation of MongoDB.