summaryrefslogtreecommitdiff
path: root/testing/tests
diff options
context:
space:
mode:
authordrebs <drebs@leap.se>2016-07-24 07:56:52 -0300
committerdrebs <drebs@leap.se>2016-08-01 21:09:03 -0300
commit793180533e4f19b364145c61939d6cad07dd851a (patch)
treec11d84b9a3f91ab4f8189a4c513e082555fd113c /testing/tests
parent1893611393a3ec69d8099a8601fb21262e5f36f4 (diff)
[test] add pytest initial setup for performance tests
Diffstat (limited to 'testing/tests')
-rw-r--r--testing/tests/perf/assets/cert_default.conf15
-rw-r--r--testing/tests/perf/conftest.py143
-rw-r--r--testing/tests/perf/pytest.ini2
-rw-r--r--testing/tests/perf/test_sync.py43
4 files changed, 203 insertions, 0 deletions
diff --git a/testing/tests/perf/assets/cert_default.conf b/testing/tests/perf/assets/cert_default.conf
new file mode 100644
index 00000000..8043cea3
--- /dev/null
+++ b/testing/tests/perf/assets/cert_default.conf
@@ -0,0 +1,15 @@
+[ req ]
+default_bits = 1024
+default_keyfile = keyfile.pem
+distinguished_name = req_distinguished_name
+prompt = no
+output_password = mypass
+
+[ req_distinguished_name ]
+C = GB
+ST = Test State or Province
+L = Test Locality
+O = Organization Name
+OU = Organizational Unit Name
+CN = localhost
+emailAddress = test@email.address
diff --git a/testing/tests/perf/conftest.py b/testing/tests/perf/conftest.py
new file mode 100644
index 00000000..c66f2863
--- /dev/null
+++ b/testing/tests/perf/conftest.py
@@ -0,0 +1,143 @@
+import json
+import os
+import pytest
+import requests
+import signal
+import time
+
+from hashlib import sha512
+from subprocess import call
+
+from leap.soledad.client import Soledad
+from leap.soledad.common.couch import CouchDatabase
+
+from leap.common.events import server
+server.ensure_server()
+
+
+DEFAULT_UUID = '0'
+DEFAULT_PASSPHRASE = '123'
+
+DEFAULT_URL = 'http://127.0.0.1:2424'
+DEFAULT_PRIVKEY = 'soledad_privkey.pem'
+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
+
+
+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
+
+
+class CouchUserDatabase(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)
+ requests.put(self._token_db_url)
+ requests.put(self._shared_db_url)
+ self._add_token()
+
+ def _add_token(self):
+ token = sha512(DEFAULT_TOKEN).hexdigest()
+ content = {'type': 'Token', 'user_id': DEFAULT_UUID}
+ requests.put(
+ 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='function')
+def couchdb_user_db(request):
+ db = CouchUserDatabase()
+ db.setup()
+ request.addfinalizer(db.teardown)
+ return db
+
+
+def _token_dbname():
+ dbname = 'tokens_' + \
+ str(int(time.time() / (30 * 24 * 3600)))
+ return dbname
+
+
+class SoledadServer(object):
+
+ def __init__(self, tmpdir):
+ self._pidfile = os.path.join(tmpdir.strpath, 'soledad-server.pid')
+ self._logfile = os.path.join(tmpdir.strpath, 'soledad-server.log')
+
+ def start(self):
+ call([
+ 'twistd',
+ '--logfile=%s' % self._logfile,
+ '--pidfile=%s' % self._pidfile,
+ 'web',
+ '--wsgi=leap.soledad.server.application',
+ '--port=2424'
+ ])
+
+ def stop(self):
+ pid = get_pid(self._pidfile)
+ os.kill(pid, signal.SIGKILL)
+
+
+@pytest.fixture
+def soledad_server(tmpdir, couchdb_user_db, request):
+ server = SoledadServer(tmpdir)
+ server.start()
+ request.addfinalizer(server.stop)
+ return server
+
+
+@pytest.fixture()
+def soledad_client(tmpdir, soledad_server):
+ uuid = DEFAULT_UUID
+ passphrase = DEFAULT_PASSPHRASE
+ secrets_path = os.path.join(tmpdir.strpath, '%s.secret' % uuid)
+ local_db_path = os.path.join(tmpdir.strpath, '%s.db' % uuid)
+ server_url = DEFAULT_URL
+ token = DEFAULT_TOKEN
+
+ # get a soledad instance
+ return Soledad(
+ uuid,
+ unicode(passphrase),
+ secrets_path=secrets_path,
+ local_db_path=local_db_path,
+ server_url=server_url,
+ cert_file=None,
+ auth_token=token,
+ defer_encryption=True)
diff --git a/testing/tests/perf/pytest.ini b/testing/tests/perf/pytest.ini
new file mode 100644
index 00000000..7a0508ce
--- /dev/null
+++ b/testing/tests/perf/pytest.ini
@@ -0,0 +1,2 @@
+[pytest]
+twisted = yes
diff --git a/testing/tests/perf/test_sync.py b/testing/tests/perf/test_sync.py
new file mode 100644
index 00000000..1e29a86a
--- /dev/null
+++ b/testing/tests/perf/test_sync.py
@@ -0,0 +1,43 @@
+import pytest
+
+from twisted.internet.defer import gatherResults
+
+from leap.soledad.common.couch import CouchDatabase
+from leap.soledad.common.document import ServerDocument
+
+
+@pytest.inlineCallbacks
+def test_upload(soledad_client):
+ # create a bunch of local documents
+ uploads = 100
+ deferreds = []
+ for i in xrange(uploads):
+ d = soledad_client.create_doc({'upload': True})
+ deferreds.append(d)
+ yield gatherResults(deferreds)
+
+ # synchronize
+ yield soledad_client.sync()
+
+ # check that documents reached the remote database
+ remote = CouchDatabase('http://127.0.0.1:5984', 'user-0')
+ remote_count, _ = remote.get_all_docs()
+ assert remote_count == uploads
+
+
+@pytest.inlineCallbacks
+def test_download(soledad_client):
+ # create a bunch of remote documents
+ downloads = 100
+ remote = CouchDatabase('http://127.0.0.1:5984', 'user-0')
+ for i in xrange(downloads):
+ doc = ServerDocument('doc-%d' % i, 'replica:1')
+ doc.content = {'download': True}
+ remote.save_document(None, doc, i)
+
+ # synchronize
+ yield soledad_client.sync()
+
+ # check that documents reached the local database
+ local_count, docs = yield soledad_client.get_all_docs()
+ assert local_count == downloads