From 4073d6a8542121504ef83b9cc02ecff94e041e32 Mon Sep 17 00:00:00 2001 From: drebs Date: Mon, 25 Jul 2016 05:44:20 -0300 Subject: [test] use pytest fixture scopes to provide per module soledad server for perf tests --- testing/tests/perf/conftest.py | 110 +++++++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 42 deletions(-) (limited to 'testing/tests') diff --git a/testing/tests/perf/conftest.py b/testing/tests/perf/conftest.py index c66f2863..05f91a45 100644 --- a/testing/tests/perf/conftest.py +++ b/testing/tests/perf/conftest.py @@ -11,6 +11,9 @@ from subprocess import call from leap.soledad.client import Soledad from leap.soledad.common.couch import CouchDatabase +# we have to manually setup the events server in order to be able to signal +# events. This is usually done by the enclosing application using soledad +# client (i.e. bitmask client). from leap.common.events import server server.ensure_server() @@ -24,48 +27,31 @@ DEFAULT_CERTKEY = 'soledad_certkey.pem' DEFAULT_TOKEN = 'an-auth-token' -@pytest.fixture -def certificate(tmpdir): - privkey = os.path.join(tmpdir.strpath, 'privkey.pem') - certkey = os.path.join(tmpdir.strpath, 'certkey.pem') - call([ - 'openssl', - 'req', - '-x509', - '-sha256', - '-nodes', - '-days', '365', - '-newkey', 'rsa:2048', - '-config', './assets/cert_default.conf', # TODO: fix basedir - '-keyout', privkey, - '-out', certkey]) - return privkey, certkey +# +# soledad_dbs fixture: provides all databases needed by soledad server in a per +# module scope (same databases for all tests in this module). +# - -def get_pid(pidfile): - if not os.path.isfile(pidfile): - return 0 - try: - with open(pidfile) as f: - return int(f.read()) - except IOError: - return 0 +def _token_dbname(): + dbname = 'tokens_' + \ + str(int(time.time() / (30 * 24 * 3600))) + return dbname -class CouchUserDatabase(object): +class SoledadDatabases(object): def __init__(self): url = 'http://127.0.0.1:5984/' - self._user_db_url = url + 'user-%s' % DEFAULT_UUID self._token_db_url = url + _token_dbname() self._shared_db_url = url + 'shared' def setup(self): - CouchDatabase.open_database( - url=self._user_db_url, create=True, replica_uid=None) + self._create_dbs() + self._add_token() + + def _create_dbs(self): requests.put(self._token_db_url) requests.put(self._shared_db_url) - self._add_token() def _add_token(self): token = sha512(DEFAULT_TOKEN).hexdigest() @@ -74,28 +60,64 @@ class CouchUserDatabase(object): self._token_db_url + '/' + token, data=json.dumps(content)) def teardown(self): - requests.delete(self._user_db_url) requests.delete(self._token_db_url) requests.delete(self._shared_db_url) +@pytest.fixture(scope='module') +def soledad_dbs(request): + db = SoledadDatabases() + db.setup() + request.addfinalizer(db.teardown) + return db + + +# +# user_db fixture: provides an empty database for a given user in a per +# function scope. +# + +class UserDatabase(object): + + def __init__(self): + url = 'http://127.0.0.1:5984/' + self._user_db_url = url + 'user-%s' % DEFAULT_UUID + + def setup(self): + CouchDatabase.open_database( + url=self._user_db_url, create=True, replica_uid=None) + + def teardown(self): + requests.delete(self._user_db_url) + + @pytest.fixture(scope='function') -def couchdb_user_db(request): - db = CouchUserDatabase() +def user_db(request): + db = UserDatabase() db.setup() request.addfinalizer(db.teardown) return db -def _token_dbname(): - dbname = 'tokens_' + \ - str(int(time.time() / (30 * 24 * 3600))) - return dbname +def get_pid(pidfile): + if not os.path.isfile(pidfile): + return 0 + try: + with open(pidfile) as f: + return int(f.read()) + except IOError: + return 0 + +# +# soledad_server fixture: provides a running soledad server in a per module +# context (same soledad server for all tests in this module). +# class SoledadServer(object): - def __init__(self, tmpdir): + def __init__(self, tmpdir_factory): + tmpdir = tmpdir_factory.mktemp('soledad-server') self._pidfile = os.path.join(tmpdir.strpath, 'soledad-server.pid') self._logfile = os.path.join(tmpdir.strpath, 'soledad-server.log') @@ -114,16 +136,20 @@ class SoledadServer(object): os.kill(pid, signal.SIGKILL) -@pytest.fixture -def soledad_server(tmpdir, couchdb_user_db, request): - server = SoledadServer(tmpdir) +@pytest.fixture(scope='module') +def soledad_server(tmpdir_factory, request): + server = SoledadServer(tmpdir_factory) server.start() request.addfinalizer(server.stop) return server +# +# soledad_client fixture: provides a clean soledad client for a test function. +# + @pytest.fixture() -def soledad_client(tmpdir, soledad_server): +def soledad_client(tmpdir, soledad_server, user_db, soledad_dbs): uuid = DEFAULT_UUID passphrase = DEFAULT_PASSPHRASE secrets_path = os.path.join(tmpdir.strpath, '%s.secret' % uuid) -- cgit v1.2.3