From 7ed1f6fbd3cde319ff5343b90d2b2c90366bda55 Mon Sep 17 00:00:00 2001 From: Lukasz Tekieli Date: Thu, 12 Feb 2026 10:47:24 +0100 Subject: [PATCH] Add ssh tests with docker - test ssh core against docker - extend docker configuration --- examples/examples/itf/BUILD | 8 ++--- examples/examples/itf/test_qemu.py | 1 + examples/examples/itf/test_ssh.py | 1 - score/itf/plugins/docker.py | 57 ++++++++++++++++++++++++++++-- test/BUILD | 23 +++++++++--- test/test_qemu.py | 23 ++++++++++++ test/test_ssh.py | 29 ++++++++++++--- 7 files changed, 125 insertions(+), 17 deletions(-) create mode 120000 examples/examples/itf/test_qemu.py delete mode 120000 examples/examples/itf/test_ssh.py create mode 100644 test/test_qemu.py diff --git a/examples/examples/itf/BUILD b/examples/examples/itf/BUILD index 34ed816..75fe45b 100644 --- a/examples/examples/itf/BUILD +++ b/examples/examples/itf/BUILD @@ -45,9 +45,9 @@ py_itf_test( ) py_itf_test( - name = "test_ssh_bridge_network", + name = "test_qemu_ssh_bridge_network", srcs = [ - "test_ssh.py", + "test_qemu.py", ], args = [ "--target_config=$(location @score_itf//test/resources:target_config)", @@ -68,9 +68,9 @@ py_itf_test( ) py_itf_test( - name = "test_ssh_port_forwarding", + name = "test_qemu_ssh_port_forwarding", srcs = [ - "test_ssh.py", + "test_qemu.py", ], args = [ "--target_config=$(location @score_itf//test/resources:target_config)", diff --git a/examples/examples/itf/test_qemu.py b/examples/examples/itf/test_qemu.py new file mode 120000 index 0000000..e7d1b99 --- /dev/null +++ b/examples/examples/itf/test_qemu.py @@ -0,0 +1 @@ +../../../test/test_qemu.py \ No newline at end of file diff --git a/examples/examples/itf/test_ssh.py b/examples/examples/itf/test_ssh.py deleted file mode 120000 index c7d69bc..0000000 --- a/examples/examples/itf/test_ssh.py +++ /dev/null @@ -1 +0,0 @@ -../../../test/test_ssh.py \ No newline at end of file diff --git a/score/itf/plugins/docker.py b/score/itf/plugins/docker.py index ebf7bc6..5e0a717 100644 --- a/score/itf/plugins/docker.py +++ b/score/itf/plugins/docker.py @@ -18,6 +18,8 @@ from score.itf.plugins.core import determine_target_scope from score.itf.plugins.core import Target +from score.itf.core.com.ssh import Ssh + logger = logging.getLogger(__name__) @@ -48,9 +50,53 @@ def __init__(self, container, capabilities=DOCKER_CAPABILITIES): def __getattr__(self, name): return getattr(self.container, name) + def get_ip(self): + self.container.reload() + return self.container.attrs["NetworkSettings"]["Networks"]["bridge"]["IPAddress"] + + def get_gateway(self): + self.container.reload() + return self.container.attrs["NetworkSettings"]["Networks"]["bridge"]["Gateway"] + + def ssh(self, username="score", password="score", port=2222): + return Ssh(target_ip=self.get_ip(), port=port, username=username, password=password) + + +@pytest.fixture(scope=determine_target_scope) +def docker_configuration(): + """ + Fixture that provides a customization point for Docker configuration in tests. + + This fixture allows tests to override and customize Docker settings by providing + a dictionary of configuration parameters. Tests can use this fixture to inject + custom Docker configuration values as needed for their specific test scenarios. + + Returns: + dict: An empty dictionary that can be populated with custom Docker configuration + parameters by tests or through pytest fixtures/parametrization. + + Scope: + The fixture scope is determined dynamically based on the target scope. + """ + return {} + + +@pytest.fixture(scope=determine_target_scope) +def _docker_configuration(docker_configuration): + configuration = { + "environment": {}, + "command": "sleep infinity", + "init": True, + } + merged_configuration = {**configuration, **docker_configuration} + + return merged_configuration + @pytest.fixture(scope=determine_target_scope) -def target_init(request): +def target_init(request, _docker_configuration): + print(_docker_configuration) + docker_image_bootstrap = request.config.getoption("docker_image_bootstrap") if docker_image_bootstrap: logger.info(f"Executing custom image bootstrap command: {docker_image_bootstrap}") @@ -58,6 +104,13 @@ def target_init(request): docker_image = request.config.getoption("docker_image") client = pypi_docker.from_env() - container = client.containers.run(docker_image, "sleep infinity", detach=True, auto_remove=True, init=True) + container = client.containers.run( + docker_image, + _docker_configuration["command"], + detach=True, + auto_remove=True, + init=_docker_configuration["init"], + environment=_docker_configuration["environment"], + ) yield DockerTarget(container) container.stop(timeout=1) diff --git a/test/BUILD b/test/BUILD index 447ed87..d78c300 100644 --- a/test/BUILD +++ b/test/BUILD @@ -51,10 +51,23 @@ py_itf_test( ) py_itf_test( - name = "test_ssh_bridge_network", + name = "test_ssh", srcs = [ "test_ssh.py", ], + args = [ + "--docker-image=linuxserver/openssh-server:version-10.2_p1-r0", + ], + plugins = [ + docker, + ], +) + +py_itf_test( + name = "test_qemu_bridge_network", + srcs = [ + "test_qemu.py", + ], args = [ "--target_config=$(location //test/resources:target_config)", "--dlt-config=$(location //test/resources:dlt_config)", @@ -76,9 +89,9 @@ py_itf_test( ) py_itf_test( - name = "test_ssh_bridge_network_no_dlt", + name = "test_qemu_bridge_network_no_dlt", srcs = [ - "test_ssh.py", + "test_qemu.py", ], args = [ "--target_config=$(location //test/resources:target_config)", @@ -98,9 +111,9 @@ py_itf_test( ) py_itf_test( - name = "test_ssh_port_forwarding", + name = "test_qemu_port_forwarding", srcs = [ - "test_ssh.py", + "test_qemu.py", ], args = [ "--target_config=$(location //test/resources:target_config)", diff --git a/test/test_qemu.py b/test/test_qemu.py new file mode 100644 index 0000000..e24f588 --- /dev/null +++ b/test/test_qemu.py @@ -0,0 +1,23 @@ +# ******************************************************************************* +# Copyright (c) 2025 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* +from score.itf.core.com.ssh import execute_command + + +def test_ssh_with_default_user(target_fixture): + with target_fixture.sut.ssh() as ssh: + execute_command(ssh, "echo 'Username:' $USER && uname -a") + + +def test_ssh_with_qnx_user(target_fixture): + with target_fixture.sut.ssh(username="qnxuser") as ssh: + execute_command(ssh, "echo 'Username:' $USER && uname -a") diff --git a/test/test_ssh.py b/test/test_ssh.py index e24f588..38fb3b3 100644 --- a/test/test_ssh.py +++ b/test/test_ssh.py @@ -10,14 +10,33 @@ # # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* +import pytest + from score.itf.core.com.ssh import execute_command -def test_ssh_with_default_user(target_fixture): - with target_fixture.sut.ssh() as ssh: - execute_command(ssh, "echo 'Username:' $USER && uname -a") +@pytest.fixture(scope="session") +def docker_configuration(): + return { + "environment": { + "PASSWORD_ACCESS": "true", + "USER_NAME": "score", + "USER_PASSWORD": "score", + }, + "command": None, + "init": False, + } + + +def check_command_exec(target, message): + exit_code, output = target.exec_run(f"echo -n {message}") + return f"{message}" == output.decode() + + +def test_docker_runs_1(target): + assert check_command_exec(target, "hello, world 1") -def test_ssh_with_qnx_user(target_fixture): - with target_fixture.sut.ssh(username="qnxuser") as ssh: +def test_ssh_with_default_user(target): + with target.ssh() as ssh: execute_command(ssh, "echo 'Username:' $USER && uname -a")