Isolate tests a bit more

This patch isolates mongodb tests and adds new `shortcut-methods` to
base clases in order to make it easier for subclasses to access conf
instances, override them or even hook them.

Change-Id: I8fe05bf383198244616499adb60480859f1ec7c6
This commit is contained in:
Flavio Percoco 2013-10-21 16:47:03 +02:00
parent 7bd2912f8a
commit a828ec96cd
22 changed files with 93 additions and 81 deletions

View File

@ -28,6 +28,8 @@ class TestBase(testtools.TestCase):
test method. test method.
""" """
config_file = None
def setUp(self): def setUp(self):
super(TestBase, self).setUp() super(TestBase, self).setUp()
@ -38,8 +40,10 @@ class TestBase(testtools.TestCase):
stderr = self.useFixture(fixtures.StringStream('stderr')).stream stderr = self.useFixture(fixtures.StringStream('stderr')).stream
self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
def tearDown(self): if self.config_file:
super(TestBase, self).tearDown() self.conf = self.load_conf(self.config_file)
else:
self.conf = cfg.ConfigOpts()
@classmethod @classmethod
def conf_path(cls, filename): def conf_path(cls, filename):

View File

@ -18,7 +18,6 @@ import time
import uuid import uuid
import ddt import ddt
from oslo.config import cfg
import six import six
from testtools import matchers from testtools import matchers
@ -47,9 +46,23 @@ class ControllerBaseTest(testing.TestBase):
self.controller_class, self.controller_class,
self.controller_base_class)) self.controller_base_class))
self.driver = self.driver_class(cfg.ConfigOpts()) self.driver = self.driver_class(self.conf)
self._prepare_conf()
self.addCleanup(self._purge_databases)
self.controller = self.controller_class(self.driver) self.controller = self.controller_class(self.driver)
def _prepare_conf(self):
"""Prepare the conf before running tests
Classes overriding this method, must use
the `self.conf` instance and alter its state.
"""
def _purge_databases(self):
"""Override to clean databases."""
def tearDown(self): def tearDown(self):
timeutils.clear_time_override() timeutils.clear_time_override()
super(ControllerBaseTest, self).tearDown() super(ControllerBaseTest, self).tearDown()

View File

@ -25,7 +25,7 @@ from tests.unit.queues.transport.wsgi import base
class TestBase(base.TestBase): class TestBase(base.TestBase):
config_filename = "wsgi_proxy_memory.conf" config_file = "wsgi_proxy_memory.conf"
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):

View File

@ -14,6 +14,7 @@
# limitations under the License. # limitations under the License.
import time import time
import uuid
import mock import mock
from pymongo import cursor from pymongo import cursor
@ -25,11 +26,23 @@ from marconi.queues import storage
from marconi.queues.storage import errors from marconi.queues.storage import errors
from marconi.queues.storage import mongodb from marconi.queues.storage import mongodb
from marconi.queues.storage.mongodb import controllers from marconi.queues.storage.mongodb import controllers
from marconi.queues.storage.mongodb import options
from marconi.queues.storage.mongodb import utils from marconi.queues.storage.mongodb import utils
from marconi import tests as testing from marconi import tests as testing
from marconi.tests.queues.storage import base from marconi.tests.queues.storage import base
class MongodbTestMixin(object):
def _purge_databases(self):
"""Override to clean databases."""
databases = (self.driver.message_databases +
[self.driver.queues_database])
for db in databases:
self.driver.connection.drop_database(db)
class MongodbUtilsTest(testing.TestBase): class MongodbUtilsTest(testing.TestBase):
def test_scope_queue_name(self): def test_scope_queue_name(self):
@ -74,38 +87,31 @@ class MongodbUtilsTest(testing.TestBase):
@testing.requires_mongodb @testing.requires_mongodb
class MongodbDriverTest(testing.TestBase): class MongodbDriverTest(testing.TestBase, MongodbTestMixin):
def setUp(self): config_file = 'wsgi_mongodb.conf'
super(MongodbDriverTest, self).setUp()
self._conf = self.load_conf('wsgi_mongodb.conf')
def test_db_instance(self): def test_db_instance(self):
driver = mongodb.DataDriver(self._conf) driver = mongodb.DataDriver(self.conf)
databases = (driver.message_databases +
[driver.queues_database])
databases = driver.message_databases + [driver.queues_database]
for db in databases: for db in databases:
self.assertThat(db.name, matchers.StartsWith( self.assertThat(db.name, matchers.StartsWith(
driver.mongodb_conf.database)) driver.mongodb_conf.database))
@testing.requires_mongodb @testing.requires_mongodb
class MongodbQueueTests(base.QueueControllerTest): class MongodbQueueTests(base.QueueControllerTest, MongodbTestMixin):
driver_class = mongodb.DataDriver driver_class = mongodb.DataDriver
config_file = 'wsgi_mongodb.conf'
controller_class = controllers.QueueController controller_class = controllers.QueueController
def setUp(self): def _prepare_conf(self):
super(MongodbQueueTests, self).setUp() self.config(options.MONGODB_GROUP,
self.load_conf('wsgi_mongodb.conf') database=uuid.uuid4().hex)
def tearDown(self):
self.controller._collection.drop()
for collection in self.message_controller._collections:
collection.drop()
super(MongodbQueueTests, self).tearDown()
def test_indexes(self): def test_indexes(self):
collection = self.controller._collection collection = self.controller._collection
@ -134,24 +140,18 @@ class MongodbQueueTests(base.QueueControllerTest):
@testing.requires_mongodb @testing.requires_mongodb
class MongodbMessageTests(base.MessageControllerTest): class MongodbMessageTests(base.MessageControllerTest, MongodbTestMixin):
driver_class = mongodb.DataDriver driver_class = mongodb.DataDriver
config_file = 'wsgi_mongodb.conf'
controller_class = controllers.MessageController controller_class = controllers.MessageController
# NOTE(kgriffs): MongoDB's TTL scavenger only runs once a minute # NOTE(kgriffs): MongoDB's TTL scavenger only runs once a minute
gc_interval = 60 gc_interval = 60
def setUp(self): def _prepare_conf(self):
super(MongodbMessageTests, self).setUp() self.config(options.MONGODB_GROUP,
self.load_conf('wsgi_mongodb.conf') database=uuid.uuid4().hex)
def tearDown(self):
self.queue_controller._collection.drop()
for collection in self.controller._collections:
collection.drop()
super(MongodbMessageTests, self).tearDown()
def test_indexes(self): def test_indexes(self):
for collection in self.controller._collections: for collection in self.controller._collections:
@ -294,20 +294,15 @@ class MongodbMessageTests(base.MessageControllerTest):
@testing.requires_mongodb @testing.requires_mongodb
class MongodbClaimTests(base.ClaimControllerTest): class MongodbClaimTests(base.ClaimControllerTest, MongodbTestMixin):
driver_class = mongodb.DataDriver driver_class = mongodb.DataDriver
config_file = 'wsgi_mongodb.conf'
controller_class = controllers.ClaimController controller_class = controllers.ClaimController
def setUp(self): def _prepare_conf(self):
super(MongodbClaimTests, self).setUp() self.config(options.MONGODB_GROUP,
self.load_conf('wsgi_mongodb.conf') database=uuid.uuid4().hex)
def tearDown(self):
for collection in self.message_controller._collections:
collection.drop()
self.queue_controller._collection.drop()
super(MongodbClaimTests, self).tearDown()
def test_claim_doesnt_exist(self): def test_claim_doesnt_exist(self):
"""Verifies that operations fail on expired/missing claims. """Verifies that operations fail on expired/missing claims.

View File

@ -29,14 +29,15 @@ from marconi import tests as testing
# have shards/catalogue implementations. # have shards/catalogue implementations.
class TestShardCatalog(testing.TestBase): class TestShardCatalog(testing.TestBase):
config_file = 'wsgi_mongodb_sharded.conf'
@testing.requires_mongodb @testing.requires_mongodb
def setUp(self): def setUp(self):
super(TestShardCatalog, self).setUp() super(TestShardCatalog, self).setUp()
conf = self.load_conf('wsgi_mongodb_sharded.conf')
conf.register_opts([cfg.StrOpt('storage')], self.conf.register_opts([cfg.StrOpt('storage')],
group='queues:drivers') group='queues:drivers')
control = utils.load_storage_driver(conf, control_mode=True) control = utils.load_storage_driver(self.conf, control_mode=True)
self.catalogue_ctrl = control.catalogue_controller self.catalogue_ctrl = control.catalogue_controller
self.shards_ctrl = control.shards_controller self.shards_ctrl = control.shards_controller
@ -46,7 +47,7 @@ class TestShardCatalog(testing.TestBase):
self.project = str(uuid.uuid1()) self.project = str(uuid.uuid1())
self.shards_ctrl.create(self.shard, 100, 'sqlite://memory') self.shards_ctrl.create(self.shard, 100, 'sqlite://memory')
self.catalogue_ctrl.insert(self.project, self.queue, self.shard) self.catalogue_ctrl.insert(self.project, self.queue, self.shard)
self.catalog = sharding.Catalog(conf, control) self.catalog = sharding.Catalog(self.conf, control)
def tearDown(self): def tearDown(self):
self.catalogue_ctrl.drop_all() self.catalogue_ctrl.drop_all()

View File

@ -23,22 +23,21 @@ from marconi import tests as testing
class TestBase(testing.TestBase): class TestBase(testing.TestBase):
config_filename = None config_file = None
def setUp(self): def setUp(self):
if self.config_filename is None:
self.skipTest('No config specified')
super(TestBase, self).setUp() super(TestBase, self).setUp()
self.conf = self.load_conf(self.conf_path(self.config_filename)) if not self.config_file:
self.skipTest("No config specified")
self.conf.register_opts(driver._WSGI_OPTIONS, self.conf.register_opts(driver._WSGI_OPTIONS,
group=driver._WSGI_GROUP) group=driver._WSGI_GROUP)
self.wsgi_cfg = self.conf[driver._WSGI_GROUP] self.wsgi_cfg = self.conf[driver._WSGI_GROUP]
self.conf.admin_mode = True self.conf.admin_mode = True
self.boot = bootstrap.Bootstrap(self.conf) self.boot = bootstrap.Bootstrap(self.conf)
self.app = self.boot.transport.app self.app = self.boot.transport.app
self.srmock = ftest.StartResponseMock() self.srmock = ftest.StartResponseMock()

View File

@ -25,7 +25,7 @@ import base # noqa
class TestWSGIAuth(base.TestBase): class TestWSGIAuth(base.TestBase):
config_filename = 'keystone_auth.conf' config_file = 'keystone_auth.conf'
def setUp(self): def setUp(self):
super(TestWSGIAuth, self).setUp() super(TestWSGIAuth, self).setUp()

View File

@ -235,7 +235,7 @@ class ClaimsBaseTest(base.TestBase):
@testing.requires_mongodb @testing.requires_mongodb
class ClaimsMongoDBTests(ClaimsBaseTest): class ClaimsMongoDBTests(ClaimsBaseTest):
config_filename = 'wsgi_mongodb.conf' config_file = 'wsgi_mongodb.conf'
def setUp(self): def setUp(self):
super(ClaimsMongoDBTests, self).setUp() super(ClaimsMongoDBTests, self).setUp()
@ -254,12 +254,12 @@ class ClaimsMongoDBTests(ClaimsBaseTest):
class ClaimsSQLiteTests(ClaimsBaseTest): class ClaimsSQLiteTests(ClaimsBaseTest):
config_filename = 'wsgi_sqlite.conf' config_file = 'wsgi_sqlite.conf'
class ClaimsFaultyDriverTests(base.TestBaseFaulty): class ClaimsFaultyDriverTests(base.TestBaseFaulty):
config_filename = 'wsgi_faulty.conf' config_file = 'wsgi_faulty.conf'
def test_simple(self): def test_simple(self):
project_id = '480924' project_id = '480924'

View File

@ -21,7 +21,7 @@ import base # noqa
class TestHealth(base.TestBase): class TestHealth(base.TestBase):
config_filename = 'wsgi_sqlite.conf' config_file = 'wsgi_sqlite.conf'
def test_get(self): def test_get(self):
response = self.simulate_get('/v1/health') response = self.simulate_get('/v1/health')

View File

@ -23,7 +23,7 @@ import base # noqa
class TestHomeDocument(base.TestBase): class TestHomeDocument(base.TestBase):
config_filename = 'wsgi_sqlite.conf' config_file = 'wsgi_sqlite.conf'
def test_json_response(self): def test_json_response(self):
body = self.simulate_get('/v1') body = self.simulate_get('/v1')

View File

@ -25,7 +25,7 @@ import base # noqa
@ddt.ddt @ddt.ddt
class TestWSGIMediaType(base.TestBase): class TestWSGIMediaType(base.TestBase):
config_filename = 'wsgi_sqlite.conf' config_file = 'wsgi_sqlite.conf'
@ddt.data( @ddt.data(
('GET', '/v1/queues'), ('GET', '/v1/queues'),

View File

@ -434,7 +434,7 @@ class MessagesBaseTest(base.TestBase):
class MessagesSQLiteTests(MessagesBaseTest): class MessagesSQLiteTests(MessagesBaseTest):
config_filename = 'wsgi_sqlite.conf' config_file = 'wsgi_sqlite.conf'
# TODO(cpp-cabrera): restore sqlite sharded test suite once shards and # TODO(cpp-cabrera): restore sqlite sharded test suite once shards and
@ -444,7 +444,7 @@ class MessagesSQLiteTests(MessagesBaseTest):
@testing.requires_mongodb @testing.requires_mongodb
class MessagesMongoDBTests(MessagesBaseTest): class MessagesMongoDBTests(MessagesBaseTest):
config_filename = 'wsgi_mongodb.conf' config_file = 'wsgi_mongodb.conf'
def setUp(self): def setUp(self):
super(MessagesMongoDBTests, self).setUp() super(MessagesMongoDBTests, self).setUp()
@ -456,7 +456,7 @@ class MessagesMongoDBTests(MessagesBaseTest):
@testing.requires_mongodb @testing.requires_mongodb
class MessagesMongoDBShardedTests(MessagesBaseTest): class MessagesMongoDBShardedTests(MessagesBaseTest):
config_filename = 'wsgi_mongodb_sharded.conf' config_file = 'wsgi_mongodb_sharded.conf'
def setUp(self): def setUp(self):
super(MessagesMongoDBShardedTests, self).setUp() super(MessagesMongoDBShardedTests, self).setUp()
@ -472,7 +472,7 @@ class MessagesMongoDBShardedTests(MessagesBaseTest):
class MessagesFaultyDriverTests(base.TestBaseFaulty): class MessagesFaultyDriverTests(base.TestBaseFaulty):
config_filename = 'wsgi_faulty.conf' config_file = 'wsgi_faulty.conf'
def test_simple(self): def test_simple(self):
project_id = 'xyz' project_id = 'xyz'

View File

@ -27,7 +27,7 @@ from marconi import tests as testing
@ddt.ddt @ddt.ddt
class QueueLifecycleBaseTest(base.TestBase): class QueueLifecycleBaseTest(base.TestBase):
config_filename = None config_file = None
def setUp(self): def setUp(self):
super(QueueLifecycleBaseTest, self).setUp() super(QueueLifecycleBaseTest, self).setUp()
@ -307,7 +307,7 @@ class QueueLifecycleBaseTest(base.TestBase):
class QueueLifecycleMongoDBTests(QueueLifecycleBaseTest): class QueueLifecycleMongoDBTests(QueueLifecycleBaseTest):
config_filename = 'wsgi_mongodb.conf' config_file = 'wsgi_mongodb.conf'
@testing.requires_mongodb @testing.requires_mongodb
def setUp(self): def setUp(self):
@ -327,12 +327,12 @@ class QueueLifecycleMongoDBTests(QueueLifecycleBaseTest):
class QueueLifecycleSQLiteTests(QueueLifecycleBaseTest): class QueueLifecycleSQLiteTests(QueueLifecycleBaseTest):
config_filename = 'wsgi_sqlite.conf' config_file = 'wsgi_sqlite.conf'
class QueueFaultyDriverTests(base.TestBaseFaulty): class QueueFaultyDriverTests(base.TestBaseFaulty):
config_filename = 'wsgi_faulty.conf' config_file = 'wsgi_faulty.conf'
def test_simple(self): def test_simple(self):
path = '/v1/queues/gumshoe' path = '/v1/queues/gumshoe'

View File

@ -285,7 +285,7 @@ class ShardsBaseTest(base.TestBase):
@testing.requires_mongodb @testing.requires_mongodb
class ShardsMongoDBTests(ShardsBaseTest): class ShardsMongoDBTests(ShardsBaseTest):
config_filename = 'wsgi_mongodb.conf' config_file = 'wsgi_mongodb.conf'
def setUp(self): def setUp(self):
super(ShardsMongoDBTests, self).setUp() super(ShardsMongoDBTests, self).setUp()

View File

@ -23,7 +23,7 @@ import base # noqa
class ValidationTest(base.TestBase): class ValidationTest(base.TestBase):
config_filename = 'wsgi_sqlite_validation.conf' config_file = 'wsgi_sqlite_validation.conf'
def setUp(self): def setUp(self):
super(ValidationTest, self).setUp() super(ValidationTest, self).setUp()

View File

@ -23,7 +23,7 @@ import base # noqa
class DefaultLimitsTest(base.TestBase): class DefaultLimitsTest(base.TestBase):
config_filename = 'wsgi_sqlite_default_limits.conf' config_file = 'wsgi_sqlite_default_limits.conf'
def setUp(self): def setUp(self):
super(DefaultLimitsTest, self).setUp() super(DefaultLimitsTest, self).setUp()

View File

@ -29,25 +29,25 @@ class TestBootstrap(base.TestBase):
return bootstrap.Bootstrap(self.conf) return bootstrap.Bootstrap(self.conf)
def test_storage_invalid(self): def test_storage_invalid(self):
boot = self._bootstrap('etc/drivers_storage_invalid.conf') bootstrap = self._bootstrap('drivers_storage_invalid.conf')
self.assertRaises(errors.InvalidDriver, self.assertRaises(errors.InvalidDriver,
lambda: boot.storage) lambda: bootstrap.storage)
def test_storage_sqlite(self): def test_storage_sqlite(self):
bootstrap = self._bootstrap('etc/wsgi_sqlite.conf') bootstrap = self._bootstrap('wsgi_sqlite.conf')
self.assertIsInstance(bootstrap.storage, pipeline.DataDriver) self.assertIsInstance(bootstrap.storage, pipeline.DataDriver)
self.assertIsInstance(bootstrap.storage._storage, sqlite.DataDriver) self.assertIsInstance(bootstrap.storage._storage, sqlite.DataDriver)
def test_storage_sqlite_sharded(self): def test_storage_sqlite_sharded(self):
"""Makes sure we can load the shard driver.""" """Makes sure we can load the shard driver."""
bootstrap = self._bootstrap('etc/wsgi_sqlite_sharded.conf') bootstrap = self._bootstrap('wsgi_sqlite_sharded.conf')
self.assertIsInstance(bootstrap.storage._storage, sharding.DataDriver) self.assertIsInstance(bootstrap.storage._storage, sharding.DataDriver)
def test_transport_invalid(self): def test_transport_invalid(self):
boot = self._bootstrap('etc/drivers_transport_invalid.conf') bootstrap = self._bootstrap('drivers_transport_invalid.conf')
self.assertRaises(errors.InvalidDriver, self.assertRaises(errors.InvalidDriver,
lambda: boot.transport) lambda: bootstrap.transport)
def test_transport_wsgi(self): def test_transport_wsgi(self):
bootstrap = self._bootstrap('etc/wsgi_sqlite.conf') bootstrap = self._bootstrap('wsgi_sqlite.conf')
self.assertIsInstance(bootstrap.transport, wsgi.Driver) self.assertIsInstance(bootstrap.transport, wsgi.Driver)