Skip to content

Commit

Permalink
Use fixtures in the tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
asalkeld committed May 18, 2013
1 parent dea1ceb commit 6693c9f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 18 deletions.
8 changes: 8 additions & 0 deletions ceilometer/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -32,6 +34,9 @@ def setUp(self):
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.
Expand All @@ -40,6 +45,9 @@ def setUp(self):
'../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()
Expand Down
3 changes: 1 addition & 2 deletions tests/api/v1/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"""Test basic ceilometer-api app
"""
import os
import tempfile

from oslo.config import cfg

Expand All @@ -42,7 +41,7 @@ def test_keystone_middleware_conf(self):
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")
Expand Down
3 changes: 1 addition & 2 deletions tests/api/v2/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"""Test basic ceilometer-api app
"""
import os
import tempfile

from oslo.config import cfg

Expand Down Expand Up @@ -46,7 +45,7 @@ def test_keystone_middleware_conf(self):
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")
Expand Down
17 changes: 3 additions & 14 deletions tests/test_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,7 +30,7 @@
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")
Expand All @@ -42,15 +40,11 @@ def test_dbsync_run(self):
"--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(
Expand All @@ -65,18 +59,14 @@ def test_send_counter_run(self):
"--counter-name=mycounter"])
self.assertEqual(subp.wait(), 0)

def tearDown(self):
super(BinSendCounterTestCase, self).tearDown()
os.unlink(self.tempfile)


class BinApiTestCase(base.TestCase):

def setUp(self):
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(
Expand All @@ -95,7 +85,6 @@ def setUp(self):

def tearDown(self):
super(BinApiTestCase, self).tearDown()
os.unlink(self.tempfile)
self.subp.kill()
self.subp.wait()

Expand Down
1 change: 1 addition & 0 deletions tools/test-requires
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6693c9f

Please sign in to comment.