diff --git a/tests/conftest.py b/tests/conftest.py index caef4250..cb858941 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -200,3 +200,10 @@ def net(request): n = _Network() request.addfinalizer(n.close) return n + +def pytest_addoption(parser): + parser.addoption('--vmdebug', action='store_true', help='run `test_debug` test') + +def pytest_cmdline_preparse(args): + if '--vmdebug' in args: + args[:] = ['-k', 'test_vmdebug'] + args diff --git a/tests/regw/Dockerfile b/tests/regw/Dockerfile index 00c3510f..39b0011c 100644 --- a/tests/regw/Dockerfile +++ b/tests/regw/Dockerfile @@ -3,7 +3,7 @@ FROM ubuntu:14.04 RUN set -o xtrace \ && sed -i 's,^deb-src,# no src # &,; s,http://archive.ubuntu.com/ubuntu/,mirror://mirrors.ubuntu.com/mirrors.txt,' /etc/apt/sources.list \ && apt-get update \ - && apt-get install -y libevent-2.0-5 valgrind + && apt-get install -y libevent-2.0-5 valgrind curl COPY redsocks /usr/local/sbin/ COPY redsocks.conf /usr/local/etc/ diff --git a/tests/run b/tests/run index 1acb752b..a46c0e38 100755 --- a/tests/run +++ b/tests/run @@ -1,3 +1,3 @@ #!/bin/sh -exec py.test -v +exec py.test -v "$@" diff --git a/tests/test_smoke.py b/tests/test_smoke.py index 43e9887f..bc696948 100644 --- a/tests/test_smoke.py +++ b/tests/test_smoke.py @@ -4,12 +4,53 @@ import conftest import pytest -#@pytest.skip # used for debugging -#def test_sleep(net): -# check_call('sleep 1h'.split()) +@pytest.mark.skipif(not pytest.config.getoption('--vmdebug'), reason='need --vmdebug option to run') +def test_vmdebug(net): + check_call('sleep 365d'.split()) @pytest.mark.parametrize('tank', conftest.TANKS.keys()) def test_smoke(net, tank): vm = net.vm['tank%d' % conftest.TANKS[tank]] page = vm.do('curl --max-time 0.5 http://10.0.1.80/') assert 'Welcome to nginx!' in page + +RTT = 200 # ms + +@pytest.fixture(scope="function") +def slow_net(request, net): + def close(): + net.vm['gw'].netcall('tc qdisc del dev ethx root') + net.vm['gw'].netcall('tc qdisc del dev ethw root') + request.addfinalizer(close) + net.vm['gw'].netcall('tc qdisc add dev ethw root netem delay %dms' % (RTT / 2)) + net.vm['gw'].netcall('tc qdisc add dev ethx root netem delay %dms' % (RTT / 2)) + return net + +LATENCY = { + 'connect_none': 3 * RTT, + 'connect_basic': 3 * RTT, + 'connect_digest': 3 * RTT, + 'socks5_none': 4 * RTT, + 'socks5_auth': 5 * RTT, + 'regw_direct': 2 * RTT, +} + +def http_ping(vm): + vm.do('curl -o /dev/null http://10.0.1.80/') # heatup L2 and auth caches + s = vm.do('curl -sS -w %{{time_connect}}/%{{time_total}}/%{{http_code}}/%{{size_download}} -o /dev/null http://10.0.1.80/') + connect, total, code, size = s.split('/') + connect, total, code, size = float(connect) * 1000, float(total) * 1000, int(code), int(size) + return connect, total, code, size + +@pytest.mark.parametrize('tank', conftest.TANKS.keys()) +def test_latency_tank(slow_net, tank): + vm = slow_net.vm['tank%d' % conftest.TANKS[tank]] + connect, total, code, size = http_ping(vm) + assert code == 200 and size == 612 + assert connect < 0.005 and LATENCY[tank]-RTT*.2 < total and total < LATENCY[tank]+RTT*.2 + +def test_latency_regw(slow_net): + vm, tank = slow_net.vm['regw'], 'regw_direct' + connect, total, code, size = http_ping(vm) + assert code == 200 and size == 612 + assert RTT*.8 < connect and connect < RTT*1.2 and LATENCY[tank]-RTT*.2 < total and total < LATENCY[tank]+RTT*.2