container: Quote arguments that contain spaces when restoring pytest args

Adds single quotes inside single-quoted string, for example bash command
`echo ' '"'"'test'"'"' '` prints ` 'test' `.

Also:
  - Remove wrong comment in the same function.
  - Fix quote usages in container script.

Tested that running bepasty tests with keyword expression filter
`-k "enable_disable or uninstall` works.

Signed-off-by: Veiko Aasa <veiko17@disroot.org>
[sunil: Use shlex.quote() for quoting]
[sunil: Pipe the script 'ssh sudo bash' instead of sending argument]
[sunil: enable color always for pytest]
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Veiko Aasa 2024-11-07 13:42:40 +02:00 committed by Sunil Mohan Adapa
parent 63ada3ee62
commit 90fd6a71f7
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2

View File

@ -121,6 +121,7 @@ import os
import pathlib
import platform
import re
import shlex
import shutil
import subprocess
import sys
@ -245,6 +246,8 @@ export FREEDOMBOX_SAMBA_PORT=445
chmod --recursive --silent a+rw .pytest_cache/
chmod --recursive --silent a+w htmlcov
chmod --silent a+w .coverage
exit 0
'''
logger = logging.getLogger(__name__)
@ -1079,19 +1082,20 @@ def subcommand_ssh(arguments):
def subcommand_run_tests(arguments):
"""Run tests in the container."""
distribution = arguments.distribution
pytest_args = ' '.join(arguments.pytest_args or [])
pytest_args_list = arguments.pytest_args or []
pytest_args = ' '.join((shlex.quote(arg) for arg in pytest_args_list))
ip_address = _wait_for(lambda: _get_ip_address(distribution))
ssh_command = _get_ssh_command(ip_address, distribution)
# Disable cache as root has no rights to overwrite files on /freedombox
pytest_command = f'py.test-3 {pytest_args}'
pytest_command = f'py.test-3 --color=yes {pytest_args}'
logger.info('Pytest command: %s', pytest_command)
test_script = SETUP_AND_RUN_TESTS_SCRIPT.format(
pytest_command=pytest_command, distribution=distribution)
setup_and_run_command = ['sudo', 'bash', '-c', f"'{test_script}'"]
command = ssh_command + setup_and_run_command
os.execlp('ssh', *command)
command = ssh_command + ['sudo', 'bash']
process = subprocess.run(command, input=test_script.encode(), check=False)
sys.exit(process.returncode)
def subcommand_stop(arguments):
@ -1108,11 +1112,11 @@ def subcommand_destroy(arguments):
def subcommand_update(arguments):
"""Update the disk image."""
if _is_update_required(arguments.distribution):
logger.info("Updating...")
logger.info('Updating...')
_download_disk_image(arguments.distribution, arguments.hkp_client,
force=True)
else:
logger.info("Already using the latest image")
logger.info('Already using the latest image')
def set_URLs():