diff --git a/pytest_container/container.py b/pytest_container/container.py index 1c4c664..a0e87fe 100644 --- a/pytest_container/container.py +++ b/pytest_container/container.py @@ -44,8 +44,11 @@ import testinfra from filelock import BaseFileLock from filelock import FileLock +from pytest import Config from pytest import param from pytest_container.helpers import get_always_pull_option +from pytest_container.helpers import get_extra_build_args +from pytest_container.helpers import get_extra_run_args from pytest_container.inspect import ContainerHealth from pytest_container.inspect import ContainerInspect from pytest_container.inspect import PortForwarding @@ -1046,6 +1049,22 @@ class ContainerLauncher: default_factory=lambda: join(tempfile.gettempdir(), str(uuid4())) ) + @staticmethod + def from_pytestconfig( + container: Union[Container, DerivedContainer], + container_runtime: OciRuntimeBase, + pytestconfig: Config, + container_name: str = "", + ) -> "ContainerLauncher": + return ContainerLauncher( + container=container, + container_runtime=container_runtime, + rootdir=pytestconfig.rootpath, + extra_build_args=get_extra_build_args(pytestconfig), + extra_run_args=get_extra_run_args(pytestconfig), + container_name=container_name, + ) + def __enter__(self) -> "ContainerLauncher": return self diff --git a/pytest_container/plugin.py b/pytest_container/plugin.py index 058f294..186597c 100644 --- a/pytest_container/plugin.py +++ b/pytest_container/plugin.py @@ -11,9 +11,6 @@ from pytest_container.container import container_and_marks_from_pytest_param from pytest_container.container import ContainerData from pytest_container.container import ContainerLauncher -from pytest_container.helpers import get_extra_build_args -from pytest_container.helpers import get_extra_pod_create_args -from pytest_container.helpers import get_extra_run_args from pytest_container.logging import _logger from pytest_container.pod import pod_from_pytest_param from pytest_container.pod import PodData @@ -91,12 +88,10 @@ def fixture_funct( f"A singleton container ({container}) cannot be used in a session level fixture" ) - with ContainerLauncher( + with ContainerLauncher.from_pytestconfig( container=container, container_runtime=container_runtime, - rootdir=pytestconfig.rootpath, - extra_build_args=get_extra_build_args(pytestconfig), - extra_run_args=get_extra_run_args(pytestconfig), + pytestconfig=pytestconfig, ) as launcher: # we want to ensure that the container's logs are saved at "all # cost", especially when the container fails to launch for some @@ -131,13 +126,7 @@ def fixture_funct( skip("Pods are only supported in podman") pod = pod_from_pytest_param(request.param) - with PodLauncher( - pod, - rootdir=pytestconfig.rootpath, - extra_build_args=get_extra_build_args(pytestconfig), - extra_run_args=get_extra_run_args(pytestconfig), - extra_pod_create_args=get_extra_pod_create_args(pytestconfig), - ) as launcher: + with PodLauncher.from_pytestconfig(pod, pytestconfig) as launcher: try: launcher.launch_pod() pod_data = launcher.pod_data diff --git a/pytest_container/pod.py b/pytest_container/pod.py index 57b9b68..1b286c8 100644 --- a/pytest_container/pod.py +++ b/pytest_container/pod.py @@ -12,12 +12,16 @@ from typing import Union from _pytest.mark import ParameterSet +from pytest import Config from pytest_container.container import Container from pytest_container.container import ContainerData from pytest_container.container import ContainerLauncher from pytest_container.container import create_host_port_port_forward from pytest_container.container import DerivedContainer from pytest_container.container import lock_host_port_search +from pytest_container.helpers import get_extra_build_args +from pytest_container.helpers import get_extra_pod_create_args +from pytest_container.helpers import get_extra_run_args from pytest_container.inspect import PortForwarding from pytest_container.logging import _logger from pytest_container.runtime import get_selected_runtime @@ -128,6 +132,19 @@ class PodLauncher: _stack: contextlib.ExitStack = field(default_factory=contextlib.ExitStack) + @staticmethod + def from_pytestconfig( + pod: Pod, pytestconfig: Config, pod_name: str = "" + ) -> "PodLauncher": + return PodLauncher( + pod, + pytestconfig.rootpath, + extra_build_args=get_extra_build_args(pytestconfig), + extra_run_args=get_extra_run_args(pytestconfig), + extra_pod_create_args=get_extra_pod_create_args(pytestconfig), + pod_name=pod_name, + ) + def __enter__(self) -> "PodLauncher": runtime = get_selected_runtime() if runtime != PodmanRuntime(): diff --git a/tests/test_launcher.py b/tests/test_launcher.py index 1030207..f1d433e 100644 --- a/tests/test_launcher.py +++ b/tests/test_launcher.py @@ -88,8 +88,8 @@ def test_launcher_creates_and_cleanes_up_volumes( pytestconfig: pytest.Config, container_runtime: OciRuntimeBase, ) -> None: - with ContainerLauncher( - cont, container_runtime, pytestconfig.rootpath + with ContainerLauncher.from_pytestconfig( + cont, container_runtime, pytestconfig ) as launcher: launcher.launch_container() @@ -130,8 +130,8 @@ def test_launcher_cleanes_up_volumes_from_image( container_runtime: OciRuntimeBase, host: Any, ) -> None: - with ContainerLauncher( - cont, container_runtime, pytestconfig.rootpath + with ContainerLauncher.from_pytestconfig( + cont, container_runtime, pytestconfig ) as launcher: launcher.launch_container() @@ -158,8 +158,8 @@ def test_launcher_cleanes_up_volumes_from_image( def test_launcher_container_data_not_available_after_exit( container_runtime: OciRuntimeBase, pytestconfig: pytest.Config ) -> None: - with ContainerLauncher( - LEAP, container_runtime, pytestconfig.rootpath + with ContainerLauncher.from_pytestconfig( + LEAP, container_runtime, pytestconfig ) as launcher: launcher.launch_container() assert launcher.container_data @@ -175,10 +175,10 @@ def test_launcher_fails_on_failing_healthcheck( ): container_name = "container_with_failing_healthcheck" with pytest.raises(RuntimeError) as runtime_err_ctx: - with ContainerLauncher( + with ContainerLauncher.from_pytestconfig( container=CONTAINER_THAT_FAILS_TO_LAUNCH, container_runtime=container_runtime, - rootdir=pytestconfig.rootpath, + pytestconfig=pytestconfig, container_name=container_name, ) as launcher: launcher.launch_container() @@ -252,8 +252,8 @@ def test_derived_container_pulls_base( host.run(f"{container_runtime.runner_binary} rmi {registry_url}") reg = DerivedContainer(base=registry_url) - with ContainerLauncher( - reg, container_runtime, pytestconfig.rootpath + with ContainerLauncher.from_pytestconfig( + reg, container_runtime, pytestconfig ) as launcher: launcher.launch_container() assert launcher.container_data.container_id @@ -333,10 +333,10 @@ def test_launcher_unlocks_on_preparation_failure( def try_launch(): with pytest.raises(subprocess.CalledProcessError): - with ContainerLauncher( + with ContainerLauncher.from_pytestconfig( container_with_wrong_url, container_runtime, - pytestconfig.rootpath, + pytestconfig, ) as launcher: launcher.launch_container() assert False, "The container must not have launched" diff --git a/tests/test_pod.py b/tests/test_pod.py index 37df9c5..a847493 100644 --- a/tests/test_pod.py +++ b/tests/test_pod.py @@ -78,7 +78,9 @@ def test_pod_launcher_pod_data_not_ready( if container_runtime != PodmanRuntime(): pytest.skip("pods only work with podman") - with PodLauncher(pod=TEST_POD, rootdir=pytestconfig.rootpath) as launcher: + with PodLauncher.from_pytestconfig( + pod=TEST_POD, pytestconfig=pytestconfig + ) as launcher: with pytest.raises(RuntimeError) as rt_err_ctx: _ = launcher.pod_data @@ -97,9 +99,9 @@ def test_pod_launcher_cleanup( name = "i_will_fail_to_launch" with pytest.raises(RuntimeError) as rt_err_ctx: - with PodLauncher( + with PodLauncher.from_pytestconfig( pod=Pod(containers=[LEAP, CONTAINER_THAT_FAILS_TO_LAUNCH]), - rootdir=pytestconfig.rootpath, + pytestconfig=pytestconfig, pod_name=name, ) as launcher: launcher.launch_pod() @@ -121,7 +123,7 @@ def test_pod_launcher_fails_with_non_podman( pytest.skip("pods work with podman") with pytest.raises(RuntimeError) as rt_err_ctx: - with PodLauncher(pod=TEST_POD, rootdir=Path("/")) as _: + with PodLauncher(pod=TEST_POD, rootdir=Path("/tmp")) as _: pass assert "pods can only be created with podman" in str(rt_err_ctx.value) diff --git a/tests/test_port_forwarding.py b/tests/test_port_forwarding.py index b3d87cd..3db2136 100644 --- a/tests/test_port_forwarding.py +++ b/tests/test_port_forwarding.py @@ -276,7 +276,7 @@ def test_pod_bind_to_host_port( ], ) - with PodLauncher(pod=pod, rootdir=pytestconfig.rootpath) as launcher: + with PodLauncher.from_pytestconfig(pod, pytestconfig) as launcher: launcher.launch_pod() assert launcher.pod_data.forwarded_ports[0].host_port == PORT