diff --git a/swh/storage/tests/server_testing.py b/swh/storage/tests/server_testing.py --- a/swh/storage/tests/server_testing.py +++ b/swh/storage/tests/server_testing.py @@ -140,6 +140,7 @@ """ def define_worker_function(self): def worker(app, port): - return aiohttp.web.run_app(app, port=int(port)) + return aiohttp.web.run_app(app, port=int(port), + print=lambda *_: None) return worker diff --git a/swh/storage/tests/storage_testing.py b/swh/storage/tests/storage_testing.py new file mode 100644 --- /dev/null +++ b/swh/storage/tests/storage_testing.py @@ -0,0 +1,58 @@ +# Copyright (C) 2015-2017 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import tempfile +import pathlib + +from swh.storage import get_storage + + +class StorageTestFixture: + """Mix this in a test subject class to get Storage testing support. + + This fixture requires to come before DbTestFixture in the inheritance list + as it uses its methods to setup its own internal database. + + Usage example: + + class TestStorage(StorageTestFixture, DbTestFixture): + ... + """ + TEST_STORAGE_DB_NAME = 'softwareheritage-test-storage' + + @classmethod + def setUpClass(cls): + if not hasattr(cls, 'DB_TEST_FIXTURE_IMPORTED'): + raise RuntimeError("StorageTestFixture needs to be preceded by " + "DbTestFixture in the inheritance list.") + + test_dir = pathlib.Path(__file__).absolute().parent + test_data_dir = test_dir / '../../../../swh-storage-testdata' + test_db_dump = (test_data_dir / 'dumps/swh.dump').absolute() + cls.add_db(cls.TEST_STORAGE_DB_NAME, str(test_db_dump), 'pg_dump') + super().setUpClass() + + def setUp(self): + super().setUp() + self.objtmp = tempfile.TemporaryDirectory() + + storage_conf = { + 'cls': 'local', + 'args': { + 'db': self.test_db[self.TEST_STORAGE_DB_NAME].conn, + 'objstorage': { + 'cls': 'pathslicing', + 'args': { + 'root': self.objtmp.name, + 'slicing': '0:1/1:5', + }, + }, + }, + } + self.storage = get_storage(**storage_conf) + + def tearDown(self): + self.objtmp.cleanup() + super().tearDown() diff --git a/swh/storage/tests/test_api_client.py b/swh/storage/tests/test_api_client.py --- a/swh/storage/tests/test_api_client.py +++ b/swh/storage/tests/test_api_client.py @@ -32,7 +32,7 @@ 'storage': { 'cls': 'local', 'args': { - 'db': 'dbname=%s' % self.dbname, + 'db': 'dbname=%s' % self.TEST_STORAGE_DB_NAME, 'objstorage': { 'cls': 'pathslicing', 'args': { diff --git a/swh/storage/tests/test_archiver.py b/swh/storage/tests/test_archiver.py --- a/swh/storage/tests/test_archiver.py +++ b/swh/storage/tests/test_archiver.py @@ -13,7 +13,7 @@ from nose.tools import istest from nose.plugins.attrib import attr -from swh.core.tests.db_testing import DbsTestFixture +from swh.core.tests.db_testing import SingleDbTestFixture from swh.storage.archiver.storage import get_archiver_storage @@ -38,20 +38,14 @@ @attr('db') -class TestArchiver(DbsTestFixture, ServerTestFixture, +class TestArchiver(SingleDbTestFixture, ServerTestFixture, unittest.TestCase): """ Test the objstorage archiver. """ - TEST_DB_NAMES = [ - 'softwareheritage-archiver-test', - ] - TEST_DB_DUMPS = [ - os.path.join(TEST_DATA_DIR, 'dumps/swh-archiver.dump'), - ] - TEST_DB_DUMP_TYPES = [ - 'pg_dump', - ] + TEST_DB_NAME = 'softwareheritage-archiver-test' + TEST_DB_DUMP = os.path.join(TEST_DATA_DIR, 'dumps/swh-archiver.dump') + TEST_DB_DUMP_TYPE = 'pg_dump' def setUp(self): # Launch the backup server @@ -69,10 +63,6 @@ self.app = app super().setUp() - # Retrieve connection (depends on the order in TEST_DB_NAMES) - self.conn = self.conns[0] # archiver db's connection - self.cursor = self.cursors[0] - # Create source storage self.src_root = tempfile.mkdtemp() src_config = { diff --git a/swh/storage/tests/test_db.py b/swh/storage/tests/test_db.py --- a/swh/storage/tests/test_db.py +++ b/swh/storage/tests/test_db.py @@ -9,7 +9,7 @@ from nose.tools import istest from nose.plugins.attrib import attr -from swh.core.tests.db_testing import DbTestFixture +from swh.core.tests.db_testing import SingleDbTestFixture from swh.model.hashutil import hash_to_bytes from swh.storage.db import Db @@ -19,7 +19,7 @@ @attr('db') -class TestDb(DbTestFixture, unittest.TestCase): +class TestDb(SingleDbTestFixture, unittest.TestCase): TEST_DB_DUMP = os.path.join(TEST_DATA_DIR, 'dumps/swh.dump') diff --git a/swh/storage/tests/test_storage.py b/swh/storage/tests/test_storage.py --- a/swh/storage/tests/test_storage.py +++ b/swh/storage/tests/test_storage.py @@ -6,10 +6,7 @@ import copy import datetime from operator import itemgetter -import os import psycopg2 -import shutil -import tempfile import unittest from uuid import UUID @@ -18,42 +15,23 @@ from nose.tools import istest from nose.plugins.attrib import attr -from swh.core.tests.db_testing import DbTestFixture from swh.model import identifiers from swh.model.hashutil import hash_to_bytes - -from swh.storage import get_storage from swh.storage.db import cursor_to_bytes - - -TEST_DIR = os.path.dirname(os.path.abspath(__file__)) -TEST_DATA_DIR = os.path.join(TEST_DIR, '../../../../swh-storage-testdata') +from swh.core.tests.db_testing import DbTestFixture +from swh.storage.tests.storage_testing import StorageTestFixture @attr('db') -class BaseTestStorage(DbTestFixture): - TEST_DB_DUMP = os.path.join(TEST_DATA_DIR, 'dumps/swh.dump') - +class BaseTestStorage(StorageTestFixture, DbTestFixture): def setUp(self): super().setUp() - self.maxDiff = None - self.objroot = tempfile.mkdtemp() - - storage_conf = { - 'cls': 'local', - 'args': { - 'db': self.conn, - 'objstorage': { - 'cls': 'pathslicing', - 'args': { - 'root': self.objroot, - 'slicing': '0:2/2:4/4:6', - }, - }, - }, - } - self.storage = get_storage(**storage_conf) + db = self.test_db[self.TEST_STORAGE_DB_NAME] + self.conn = db.conn + self.cursor = db.cursor + + self.maxDiff = None self.cont = { 'data': b'42\n', @@ -567,8 +545,6 @@ } def tearDown(self): - shutil.rmtree(self.objroot) - self.cursor.execute("""SELECT table_name FROM information_schema.tables WHERE table_schema = %s""", ('public',))