From b67ce15f8993ad23e54d7145bc51bce03261dd2a Mon Sep 17 00:00:00 2001 From: Veiko Aasa Date: Wed, 2 Oct 2024 17:27:53 +0300 Subject: [PATCH] functional tests: Add pytest testinfra plugin Adds ability to run local commands with functional tests. By default, commands are run locally. It is possible to set remote host connection parameters from pytest command line, for example: `--hosts 'fbx@IP' --ssh-identity-file '.container/ssh/id_ed25519'` For more options, see documentation https://testinfra.readthedocs.io/en/latest/backends.html#ssh. Includes a fixture `host_sudo` to run commands as sudo. Relates to https://salsa.debian.org/freedombox-team/freedombox/-/issues/2451#note_530752. Signed-off-by: Veiko Aasa Reviewed-by: Sunil Mohan Adapa --- plinth/conftest.py | 9 ++++++++- plinth/tests/functional/install.sh | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/plinth/conftest.py b/plinth/conftest.py index 9236f7fa6..6ab851b0c 100644 --- a/plinth/conftest.py +++ b/plinth/conftest.py @@ -223,7 +223,7 @@ def fixture_fix_session_browser_screenshots(request): continue value = fixture_def.cached_result[0] - should_take_screenshot = (hasattr(value, "__splinter_browser__") + should_take_screenshot = (hasattr(value, '__splinter_browser__') and splinter_make_screenshot_on_failure and getattr(request.node, 'splinter_failure', True)) @@ -250,3 +250,10 @@ def fixture_fix_session_browser_screenshots(request): } plugin._take_screenshot(**kwargs) + + +@pytest.fixture(name='host_sudo') +def fixture_host_sudo(host): + """Pytest fixture to run commands with sudo.""" + with host.sudo(): + yield host diff --git a/plinth/tests/functional/install.sh b/plinth/tests/functional/install.sh index a51cfbb1c..94ff71e82 100755 --- a/plinth/tests/functional/install.sh +++ b/plinth/tests/functional/install.sh @@ -5,7 +5,8 @@ IFS=$'\n\t' echo "Installing requirements" sudo apt-get install -yq --no-install-recommends \ python3-pytest python3-pytest-django python3-pytest-instafail \ - python3-pytest-xdist python3-pip python3-wheel firefox-esr git smbclient + python3-pytest-xdist python3-pip python3-wheel firefox-esr git smbclient \ + python3-testinfra # Use compatible versions of Splinter and Selenium PIP_VERSION=$(dpkg-query -W -f '${Version}' python3-pip)