From 43193ad2923405778fb0ad0a40daebe657b1165a Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Sun, 21 May 2023 07:56:22 +0200 Subject: [PATCH] Implement bootstrap tests Signed-off-by: Georg Pfuetzenreuter --- tests/configs/complete.ini | 15 ++++++++++++- tests/conftest.py | 15 +++++++++++++ tests/scripts/bootstrap_hello_world.sh | 1 + tests/test_cli.py | 8 ++++--- tests/test_vagrant.py | 30 ++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 tests/scripts/bootstrap_hello_world.sh create mode 100644 tests/test_vagrant.py diff --git a/tests/configs/complete.ini b/tests/configs/complete.ini index b90033f..ee795d1 100644 --- a/tests/configs/complete.ini +++ b/tests/configs/complete.ini @@ -1,14 +1,27 @@ [box] bootstrap=tests/scripts/bootstrap.sh +image=https://download.opensuse.org/repositories/home:/crameleon:/appliances/openSUSE_Tumbleweed/Tumbleweed.x86_64-libvirt.box [box.tumbleweed] name=tumbleweed -image=https://download.opensuse.org/repositories/home:/crameleon:/appliances/openSUSE_Tumbleweed/Tumbleweed.x86_64-libvirt.box + +[box.tumbleweed_bootstrap] +name=tumbleweed_bootstrap +bootstrap=tests/scripts/bootstrap_hello_world.sh [suite.one_minion] minions=1 box=tumbleweed +[suite.one_minion_bootstrap] +minions=1 +box=tumbleweed_bootstrap + +[suite.one_minion_one_master_bootstrap] +minions=1 +masters=1 +box=tumbleweed_bootstrap + [suite.two_minions] minions=2 box=tumbleweed diff --git a/tests/conftest.py b/tests/conftest.py index 943e0a6..80b071e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,8 @@ import pytest import os import libvirt +import vagrant +import dotenv @pytest.fixture def script(): @@ -25,3 +27,16 @@ def config(testbase, request): def virt(): return libvirt.openReadOnly('qemu:///system') +@pytest.fixture +def vag(): + return vagrant.Vagrant(quiet_stderr=False, quiet_stdout=False) + +def loadenv(): + env = os.environ.copy() + envmap = dotenv.dotenv_values('.scullery_env') + for variable, value in envmap.items(): + if value is not None: + if isinstance(value, list): + value = ','.join(value) + env[variable] = value + return env diff --git a/tests/scripts/bootstrap_hello_world.sh b/tests/scripts/bootstrap_hello_world.sh new file mode 100644 index 0000000..98185a4 --- /dev/null +++ b/tests/scripts/bootstrap_hello_world.sh @@ -0,0 +1 @@ +echo 'Hello world!' > /srv/hello_world.txt diff --git a/tests/test_cli.py b/tests/test_cli.py index 2f1321b..8d5a182 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -49,7 +49,7 @@ def test_config_undefined(script_runner, script, testbase, section, message): ('two_minions_one_master', "[Status(name='scullery-master0', state='not_created', provider='libvirt'), Status(name='scullery-minion0', state='not_created', provider='libvirt'), Status(name='scullery-minion1', state='not_created', provider='libvirt')]") ]) def test_status_report_not_running(script_runner, script, config, suite, report): - result = script_runner.run(script, '--config', config, '--suite', suite, '--status') + result = script_runner.run(script, '--config', config, '--suite', suite, '--status', '--debug', '--env') assert result.success assert result.stderr.endswith("INFO - main_interactive: Status report: {}\n".format(report)) @@ -114,10 +114,12 @@ def test_envfile(script_runner, script, config, suite, masters, minions): 'two_minions', 'one_master', 'one_minion_one_master', - 'two_minions_one_master' + 'two_minions_one_master', + 'one_minion_bootstrap', + 'one_minion_one_master_bootstrap' ]) def test_stop(script_runner, script, virt, config, suite): - cmd = (script, '--config', config, '--suite', suite, '--stop') + cmd = (script, '--config', config, '--suite', suite, '--stop', '--force') result = script_runner.run(*cmd) assert result.success domains = [] diff --git a/tests/test_vagrant.py b/tests/test_vagrant.py new file mode 100644 index 0000000..cd47123 --- /dev/null +++ b/tests/test_vagrant.py @@ -0,0 +1,30 @@ +import pytest +import os +from conftest import loadenv +import sys + +@pytest.mark.parametrize('config', ['complete'], indirect=True) +@pytest.mark.parametrize('suite', [ + 'one_minion_bootstrap', + 'one_minion_one_master_bootstrap', + ]) +def test_bootstrap(script_runner, script, config, suite, vag, testbase): + bootstrap_script = '{}/scripts/bootstrap_hello_world.txt'.format(testbase) + do_digest = False + if sys.version_info[1] > 10: + do_digest = True + import hashlib + with open(bootstrap_script, 'rb') as fh: + digest_local = hashlib.file_digest(fh, 'md5') + cmd = (script, '--config', config, '--suite', suite) + result = script_runner.run(*cmd, '--env') + assert result.success + assert result.stderr.endswith('main_interactive: Launching {} ...\n'.format(suite)) + v = vag + v.env = loadenv() + assert v.ssh(command='cat /srv/hello_world.txt') == 'Hello world!\n' + if do_digest: + digest_remote = v.ssh(command='md5sum /srv/hello_world.txt | awk "{ print $1 }"') + assert digest_local == digest_remote + assert script_runner.run(*cmd, '--stop') +