From 64e5f2c6c1ee08c89fe5c7c81704fa7b3bcb535e Mon Sep 17 00:00:00 2001 From: Fabian Freyer Date: Wed, 2 Jan 2019 14:34:55 +0100 Subject: [PATCH] python: add tests --- .cirrus.yml | 6 +++- bindings/python/setup.py | 2 +- bindings/python/tests/test_jail.py | 47 ++++++++++++++++++++++++++++++ bindings/python/tests/wqal | 21 +++++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 bindings/python/tests/test_jail.py create mode 100644 bindings/python/tests/wqal diff --git a/.cirrus.yml b/.cirrus.yml index 12c186a9d..0addaaf23 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -29,7 +29,7 @@ task: PYPREFIX: py27 install_script: | pkg install -y curl ${PYTHON_PKG} ${PYPREFIX}-pip - ${PYTHON} -m pip install setuptools-rust + ${PYTHON} -m pip install setuptools-rust pytest curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain nightly . $HOME/.cargo/env build_script: | @@ -37,3 +37,7 @@ task: . $HOME/.cargo/env cd bindings/python ${PYTHON} setup.py install + test_script: | + . $HOME/.cargo/env + cd bindings/python + ${PYTHON} setup.py test diff --git a/bindings/python/setup.py b/bindings/python/setup.py index fcfd3862b..3fa5e2093 100644 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -22,7 +22,7 @@ def run(self): import subprocess import sys - errno = subprocess.call([sys.executable, '-m', 'pytest', 'tests']) + errno = subprocess.call(['pytest', '-v', 'tests']) raise SystemExit(errno) setup_requires = ['setuptools-rust>=0.10.1'] diff --git a/bindings/python/tests/test_jail.py b/bindings/python/tests/test_jail.py new file mode 100644 index 000000000..aaaeb2ad3 --- /dev/null +++ b/bindings/python/tests/test_jail.py @@ -0,0 +1,47 @@ +import pytest + +from jail import StoppedJail, RunningJail + + +def start_stop(s): + j = s.start() + j.stop() + +def test_start_stop_jail(benchmark): + s = StoppedJail('/rescue') + + benchmark(start_stop, s) + + +def get_hostname(j): + return j.parameters['host.hostname'] + +def test_get_parameter(benchmark): + s = StoppedJail('/rescue', parameters={'host.hostname': 'test'}) + j = s.start() + hostname = benchmark(get_hostname, j) + assert hostname == 'test' + j.stop() + + +def spawn_hello_world(j): + child = j.spawn(['/echo', 'hello world']) + child.wait() + +def test_spawn(benchmark): + s = StoppedJail('/rescue') + j = s.start() + benchmark(spawn_hello_world, j) + j.stop() + + +import subprocess +def popen_hello_world(j): + child = subprocess.Popen(['/echo', 'hello world'], preexec_fn=lambda: j.attach()) + child.wait() + +def test_popen(benchmark): + s = StoppedJail('/rescue') + j = s.start() + benchmark(popen_hello_world, j) + j.stop() diff --git a/bindings/python/tests/wqal b/bindings/python/tests/wqal new file mode 100644 index 000000000..f415901e9 --- /dev/null +++ b/bindings/python/tests/wqal @@ -0,0 +1,21 @@ +import pytest + +from jail import StoppedJail, RunningJail + +def test_start_stop_jail(benchmark): + s = StoppedJail('/rescue') + + def start_stop(): + j = s.start() + j.stop() + + benchmark(start_stop) + +def test_get_parameter(benchmark): + s = StoppedJail('/rescue', params=dict(hostname='test')) + j = s.start() + + hostname = benchmark(lambda: j.params['hostname']) + assert hostname == 'test' + + j.stop()