Commit 79cbb321 authored by Petr Špaček's avatar Petr Špaček

Merge branch 'ci-stabilize-pytests' into 'master'

ci: stabilize pytests

Closes #427

See merge request !744
parents df7a7404 57df846a
Pipeline #44683 canceled with stages
in 7 minutes and 14 seconds
...@@ -17,8 +17,8 @@ from kresd import Kresd ...@@ -17,8 +17,8 @@ from kresd import Kresd
import utils import utils
MAX_SOCKETS = 20000 # upper bound of how many connections to open MAX_SOCKETS = 10000 # upper bound of how many connections to open
MAX_ITERATIONS = 20 # number of iterations to run the test MAX_ITERATIONS = 10 # number of iterations to run the test
# we can't use softlimit ifself since kresd already has open sockets, # we can't use softlimit ifself since kresd already has open sockets,
# so use lesser value # so use lesser value
...@@ -50,8 +50,8 @@ def test_conn_flood(tmpdir, sock_func_name): ...@@ -50,8 +50,8 @@ def test_conn_flood(tmpdir, sock_func_name):
for s in sockets: for s in sockets:
utils.ping_alive(s) utils.ping_alive(s)
# break when no more than 10% additional sockets are created # break when no more than 20% additional sockets are created
if additional_sockets / len(sockets) < 0.1: if additional_sockets / len(sockets) < 0.2:
return sockets return sockets
max_num_of_open_files = resource.getrlimit(resource.RLIMIT_NOFILE)[0] - RESERVED_NOFILE max_num_of_open_files = resource.getrlimit(resource.RLIMIT_NOFILE)[0] - RESERVED_NOFILE
......
...@@ -20,8 +20,8 @@ TEMPLATES_DIR = os.path.join(PYTESTS_DIR, 'templates') ...@@ -20,8 +20,8 @@ TEMPLATES_DIR = os.path.join(PYTESTS_DIR, 'templates')
KRESD_CONF_TEMPLATE = 'kresd.conf.j2' KRESD_CONF_TEMPLATE = 'kresd.conf.j2'
KRESD_STARTUP_MSGID = 10005 # special unique ID at the start of the "test" log KRESD_STARTUP_MSGID = 10005 # special unique ID at the start of the "test" log
KRESD_PORTDIR = '/tmp/pytest-kresd-portdir' KRESD_PORTDIR = '/tmp/pytest-kresd-portdir'
KRESD_TESTPORT_MIN = 10000 KRESD_TESTPORT_MIN = 1024
KRESD_TESTPORT_MAX = 49000 KRESD_TESTPORT_MAX = 32768 # avoid overlap with docker ephemeral port range
def init_portdir(): def init_portdir():
...@@ -109,6 +109,8 @@ class Kresd(ContextDecorator): ...@@ -109,6 +109,8 @@ class Kresd(ContextDecorator):
raise RuntimeError("Kresd crashed with returncode: {}".format( raise RuntimeError("Kresd crashed with returncode: {}".format(
self.process.returncode)) self.process.returncode))
except (RuntimeError, ConnectionError): # pylint: disable=try-except-raise except (RuntimeError, ConnectionError): # pylint: disable=try-except-raise
with open(self.logfile_path) as log: # print log for debugging
print(log.read())
raise raise
return self return self
...@@ -127,24 +129,35 @@ class Kresd(ContextDecorator): ...@@ -127,24 +129,35 @@ class Kresd(ContextDecorator):
def all_ports_alive(self, msgid=10001): def all_ports_alive(self, msgid=10001):
alive = True alive = True
if self.ip: if self.ip:
alive &= utils.try_ping_alive(self.ip_tls_socket(), close=True, msgid=msgid) alive &= utils.try_ping_alive(self.ip_tcp_socket(), close=True, msgid=msgid)
alive &= utils.try_ping_alive(self.ip_tcp_socket(), close=True, msgid=msgid + 1) alive &= utils.try_ping_alive(self.ip_tls_socket(), close=True, msgid=msgid + 1)
if self.ip6: if self.ip6:
alive &= utils.try_ping_alive(self.ip6_tls_socket(), close=True, msgid=msgid + 2) alive &= utils.try_ping_alive(self.ip6_tcp_socket(), close=True, msgid=msgid + 2)
alive &= utils.try_ping_alive(self.ip6_tcp_socket(), close=True, msgid=msgid + 3) alive &= utils.try_ping_alive(self.ip6_tls_socket(), close=True, msgid=msgid + 3)
return alive return alive
def _wait_for_tcp_port(self, delay=0.1, max_attempts=20): def _wait_for_tcp_port(self, max_delay=10, delay_step=0.2):
family = socket.AF_INET if self.ip else socket.AF_INET6 family = socket.AF_INET if self.ip else socket.AF_INET6
for _ in range(max_attempts): i = 0
end_time = time.time() + max_delay
while time.time() < end_time:
i += 1
# use exponential backoff algorhitm to choose next delay
rand_delay = random.randrange(0, i)
time.sleep(rand_delay * delay_step)
try: try:
sock, dest = self.stream_socket(family, timeout=3) sock, dest = self.stream_socket(family, timeout=5)
sock.connect(dest) sock.connect(dest)
except ConnectionRefusedError: except ConnectionRefusedError:
time.sleep(delay)
continue continue
else: else:
return utils.try_ping_alive(sock, close=True, msgid=10000) try:
return utils.try_ping_alive(sock, close=True, msgid=10000)
except socket.timeout:
continue
finally: finally:
sock.close() sock.close()
raise RuntimeError("Kresd didn't start in time") raise RuntimeError("Kresd didn't start in time")
......
...@@ -61,7 +61,7 @@ def test_pipelining(kresd_sock): ...@@ -61,7 +61,7 @@ def test_pipelining(kresd_sock):
assert msg_answer.id == msgid_pre assert msg_answer.id == msgid_pre
# test # test
buff1, msgid1 = utils.get_msgbuff('1000.delay.getdnsapi.net.', msgid=1) buff1, msgid1 = utils.get_msgbuff('1500.delay.getdnsapi.net.', msgid=1)
buff2, msgid2 = utils.get_msgbuff('1.delay.getdnsapi.net.', msgid=2) buff2, msgid2 = utils.get_msgbuff('1.delay.getdnsapi.net.', msgid=2)
buff = buff1 + buff2 buff = buff1 + buff2
kresd_sock.sendall(buff) kresd_sock.sendall(buff)
...@@ -76,7 +76,7 @@ def test_pipelining(kresd_sock): ...@@ -76,7 +76,7 @@ def test_pipelining(kresd_sock):
@pytest.mark.parametrize('duration, delay', [ @pytest.mark.parametrize('duration, delay', [
(utils.MAX_TIMEOUT, 0.1), (utils.MAX_TIMEOUT, 0.1),
(utils.MAX_TIMEOUT, 3), (utils.MAX_TIMEOUT, 3),
(utils.MAX_TIMEOUT, 8), (utils.MAX_TIMEOUT, 7),
(utils.MAX_TIMEOUT + 10, 3), (utils.MAX_TIMEOUT + 10, 3),
]) ])
def test_long_lived(kresd_sock, duration, delay): def test_long_lived(kresd_sock, duration, delay):
......
...@@ -100,7 +100,7 @@ def ping_alive(sock, msgid=None): ...@@ -100,7 +100,7 @@ def ping_alive(sock, msgid=None):
def expect_kresd_close(rst_ok=False): def expect_kresd_close(rst_ok=False):
with pytest.raises(BrokenPipeError, message="kresd didn't close the connection"): with pytest.raises(BrokenPipeError, message="kresd didn't close the connection"):
try: try:
time.sleep(0.05) # give kresd time to close connection with TCP FIN time.sleep(0.2) # give kresd time to close connection with TCP FIN
yield yield
except ConnectionResetError: except ConnectionResetError:
if rst_ok: if rst_ok:
......
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