Commit 2dbbf2d7 authored by Łukasz Nowak's avatar Łukasz Nowak

kedifa: Test stabilizing fixup, wait a bit longer on the cauase first start

See merge request !11
parents bf6754fe a5ea5ecb
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
# See COPYING file for full licensing terms. # See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options. # See https://www.nexedi.com/licensing for rationale and options.
from six import StringIO from six import StringIO, BytesIO
import contextlib import contextlib
import datetime import datetime
from six.moves import http_client as httplib from six.moves import http_client as httplib
...@@ -31,6 +31,7 @@ import requests.exceptions ...@@ -31,6 +31,7 @@ import requests.exceptions
import shutil import shutil
import signal import signal
import sys import sys
import tarfile
import tempfile import tempfile
import time import time
import unittest import unittest
...@@ -60,6 +61,30 @@ def findFreeTCPPort(ip=''): ...@@ -60,6 +61,30 @@ def findFreeTCPPort(ip=''):
s.bind((ip, 0)) s.bind((ip, 0))
return str(s.getsockname()[1]) return str(s.getsockname()[1])
def retry(callback, try_count=10, try_delay=0.1):
"""
Poll <callback> every <try_delay> for <try_count> times or until it returns
a true value.
Always returns the value returned by latest callback invocation.
"""
for _ in xrange(try_count):
result = callback()
if result:
break
time.sleep(try_delay)
return result
def canConnect(caucase_url):
"""
Returns True if a connection can be established to given address, False
otherwise.
"""
try:
requests.get(caucase_url)
except BaseException:
return False
return True
class KedifaMixin(object): class KedifaMixin(object):
def setUp(self): def setUp(self):
...@@ -70,7 +95,52 @@ class KedifaMixin(object): ...@@ -70,7 +95,52 @@ class KedifaMixin(object):
self.addCleanup(cleanTestDir) self.addCleanup(cleanTestDir)
_clean_caucased_snapshot = None
class KedifaMixinCaucase(KedifaMixin): class KedifaMixinCaucase(KedifaMixin):
def _startCaucaseServer(self, argv=(), timeout=10):
"""
Start caucased server
"""
ip, port = os.environ['SLAPOS_TEST_IPV6'],\
findFreeTCPPort(os.environ['SLAPOS_TEST_IPV6'])
self.caucase_runtime = caucase_runtime = multiprocessing.Process(
target=caucase.http.main,
kwargs=dict(
argv=[
'--db', self.caucase_db,
'--server-key', os.path.join(self.caucased, 'server.key.pem'),
'--netloc', '[%s]:%s' % (ip, port),
'--service-auto-approve-count', '50'
]
)
)
self.caucase_runtime.start()
self.caucase_url = 'http://[%s]:%s' % (ip, port)
if not retry(
lambda: (
self.assertTrue(caucase_runtime.is_alive()) or
canConnect(self.caucase_url)
),
try_count=timeout * 10,
):
self._stopCaucaseServer()
raise AssertionError('Could not connect to %r after %i seconds' % (
self.caucase_url,
timeout,
))
def _stopCaucaseServer(self):
"""
Stop a running caucased server
"""
caucase_runtime = self.caucase_runtime
caucase_runtime.terminate()
caucase_runtime.join()
if caucase_runtime.is_alive():
raise ValueError('%r does not wish to die' % (caucase_runtime, ))
def createKey(self): def createKey(self):
key = rsa.generate_private_key( key = rsa.generate_private_key(
public_exponent=65537, key_size=2048, backend=default_backend()) public_exponent=65537, key_size=2048, backend=default_backend())
...@@ -136,40 +206,31 @@ class KedifaMixinCaucase(KedifaMixin): ...@@ -136,40 +206,31 @@ class KedifaMixinCaucase(KedifaMixin):
self.pem = certificate_pem + key_pem self.pem = certificate_pem + key_pem
def setUpCaucase(self): def setUpCaucase(self):
ip, port = os.environ['SLAPOS_TEST_IPV6'],\ global _clean_caucased_snapshot # pylint: disable=global-statement
findFreeTCPPort(os.environ['SLAPOS_TEST_IPV6'])
self.caucased = os.path.join(self.testdir, 'caucased') self.caucased = os.path.join(self.testdir, 'caucased')
caucase_db = os.path.join(self.caucased, 'caucase.sqlite') self.caucase_db = os.path.join(self.caucased, 'caucase.sqlite')
self.caucase_service = os.path.join(self.testdir, 'service') self.caucase_service = os.path.join(self.testdir, 'service')
os.mkdir(self.caucased) os.mkdir(self.caucased)
os.mkdir(self.caucase_service) os.mkdir(self.caucase_service)
self.caucase_runtime = multiprocessing.Process(
target=caucase.http.main, if _clean_caucased_snapshot is None:
kwargs=dict( self._startCaucaseServer(timeout=60)
argv=[ self._stopCaucaseServer()
'--db', caucase_db, server_raw = BytesIO()
'--server-key', os.path.join(self.caucased, 'server.key.pem'), with tarfile.TarFile(mode='w', fileobj=server_raw) as server_tarball:
'--netloc', '[%s]:%s' % (ip, port), server_tarball.add(
'--service-auto-approve-count', '50' self.caucased,
] arcname=os.path.basename(self.caucased),
)
) )
self.addCleanup(self.caucase_runtime.terminate) _clean_caucased_snapshot = server_raw.getvalue()
self.caucase_runtime.start()
self.assertTrue(self.caucase_runtime.is_alive())
self.caucase_url = 'http://[%s]:%s' % (ip, port)
# give 5s for caucase to be available
b = time.time()
for i in range(50):
try:
requests.get(self.caucase_url)
except BaseException:
time.sleep(0.1)
else:
break
else: else:
self.fail( with tarfile.TarFile(
'Caucase not available after %.2fs seconds' % (time.time() - b)) mode='r',
fileobj=BytesIO(_clean_caucased_snapshot),
) as server_tarball:
server_tarball.extractall(path=self.testdir)
self._startCaucaseServer()
self.addCleanup(self.caucase_runtime.terminate)
def setUpKey(self, common_name): def setUpKey(self, common_name):
# create key for the service and keep its downloaded ca_crt # create key for the service and keep its downloaded ca_crt
...@@ -266,7 +327,7 @@ ez+ONyvetfvjD8cxyQ== ...@@ -266,7 +327,7 @@ ez+ONyvetfvjD8cxyQ==
# give 5s for KeDiFa to be available # give 5s for KeDiFa to be available
b = time.time() b = time.time()
for i in range(50): for i in range(100):
try: try:
self.requests_get(self.kedifa_url + 'ping') self.requests_get(self.kedifa_url + 'ping')
except BaseException: except BaseException:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment