diff options
author | drebs <drebs@leap.se> | 2015-06-04 11:21:40 -0300 |
---|---|---|
committer | drebs <drebs@leap.se> | 2015-06-04 11:21:40 -0300 |
commit | 293c71080e9a21115d248e46d1a706c53cc8ee37 (patch) | |
tree | 3353672f1f770ef5f5c87ea7e3e27debe737a50b /scripts/profiling/mail/couchdb_wrapper.py | |
parent | fa7708e256ba56cd1e9913993d68611b4ae95824 (diff) | |
parent | 9fb1c47ca7da06d6feef6846b812aec28128ed78 (diff) |
Merge tag '0.7.0'
Tag version 0.7.0.
Conflicts:
CHANGELOG
client/src/leap/soledad/client/__init__.py
client/src/leap/soledad/client/sqlcipher.py
client/src/leap/soledad/client/target.py
server/pkg/soledad-server
Diffstat (limited to 'scripts/profiling/mail/couchdb_wrapper.py')
-rw-r--r-- | scripts/profiling/mail/couchdb_wrapper.py | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/scripts/profiling/mail/couchdb_wrapper.py b/scripts/profiling/mail/couchdb_wrapper.py new file mode 100644 index 00000000..cad1205b --- /dev/null +++ b/scripts/profiling/mail/couchdb_wrapper.py @@ -0,0 +1,84 @@ +import re +import os +import tempfile +import subprocess +import time +import shutil + + +from leap.common.files import mkdir_p + + +class CouchDBWrapper(object): + """ + Wrapper for external CouchDB instance. + """ + + def start(self): + """ + Start a CouchDB instance for a test. + """ + self.tempdir = tempfile.mkdtemp(suffix='.couch.test') + + path = os.path.join(os.path.dirname(__file__), + 'couchdb.ini.template') + handle = open(path) + conf = handle.read() % { + 'tempdir': self.tempdir, + } + handle.close() + + confPath = os.path.join(self.tempdir, 'test.ini') + handle = open(confPath, 'w') + handle.write(conf) + handle.close() + + # create the dirs from the template + mkdir_p(os.path.join(self.tempdir, 'lib')) + mkdir_p(os.path.join(self.tempdir, 'log')) + args = ['couchdb', '-n', '-a', confPath] + null = open('/dev/null', 'w') + + self.process = subprocess.Popen( + args, env=None, stdout=null.fileno(), stderr=null.fileno(), + close_fds=True) + # find port + logPath = os.path.join(self.tempdir, 'log', 'couch.log') + while not os.path.exists(logPath): + if self.process.poll() is not None: + got_stdout, got_stderr = "", "" + if self.process.stdout is not None: + got_stdout = self.process.stdout.read() + + if self.process.stderr is not None: + got_stderr = self.process.stderr.read() + raise Exception(""" +couchdb exited with code %d. +stdout: +%s +stderr: +%s""" % ( + self.process.returncode, got_stdout, got_stderr)) + time.sleep(0.01) + while os.stat(logPath).st_size == 0: + time.sleep(0.01) + PORT_RE = re.compile( + 'Apache CouchDB has started on http://127.0.0.1:(?P<port>\d+)') + + handle = open(logPath) + m = None + line = handle.readline() + while m is None: + m = PORT_RE.search(line) + line = handle.readline() + handle.close() + self.port = int(m.group('port')) + + def stop(self): + """ + Terminate the CouchDB instance. + """ + self.process.terminate() + self.process.communicate() + shutil.rmtree(self.tempdir) + |