11# pylint: disable=missing-function-docstring,missing-module-docstring
22import os
3+ import shutil
34from pathlib import Path
45from typing import Callable
56from typing import Type
89
910import pytest
1011
11- from pytest_container . runtime import LOCALHOST
12+ from pytest_container import helpers
1213from pytest_container .runtime import DockerRuntime
1314from pytest_container .runtime import OciRuntimeBase
1415from pytest_container .runtime import PodmanRuntime
@@ -27,45 +28,6 @@ def container_runtime_envvar(request):
2728 yield
2829
2930
30- # pylint: disable-next=unused-argument
31- def _mock_run_success (* args , ** kwargs ):
32- class Succeeded :
33- """Class that mocks the returned object of `testinfra`'s `run`."""
34-
35- @property
36- def succeeded (self ) -> bool :
37- return True
38-
39- @property
40- def rc (self ) -> int :
41- return 0
42-
43- return Succeeded ()
44-
45-
46- def generate_mock_fail (* , rc : int = 1 , stderr : str = "failure!!" ):
47- # pylint: disable-next=unused-argument
48- def mock_run_fail (cmd : str ):
49- class Failure :
50- """Class that mocks the returned object of `testinfra`'s `run`."""
51-
52- @property
53- def succeeded (self ) -> bool :
54- return False
55-
56- @property
57- def rc (self ) -> int :
58- return rc
59-
60- @property
61- def stderr (self ) -> str :
62- return stderr
63-
64- return Failure ()
65-
66- return mock_run_fail
67-
68-
6931def _create_mock_exists (
7032 podman_should_exist : bool , docker_should_exist : bool
7133) -> Callable [[str ], bool ]:
@@ -96,8 +58,7 @@ def test_runtime_selection(
9658 runtime : OciRuntimeBase ,
9759 monkeypatch : pytest .MonkeyPatch ,
9860):
99- monkeypatch .setattr (LOCALHOST , "run" , _mock_run_success )
100- monkeypatch .setattr (LOCALHOST , "exists" , _create_mock_exists (True , True ))
61+ monkeypatch .setattr (shutil , "which" , _create_mock_exists (True , True ))
10162
10263 assert get_selected_runtime () == runtime
10364
@@ -107,7 +68,7 @@ def test_value_err_when_docker_and_podman_missing(
10768 runtime : str , monkeypatch : pytest .MonkeyPatch
10869) -> None :
10970 monkeypatch .setenv ("CONTAINER_RUNTIME" , runtime )
110- monkeypatch .setattr (LOCALHOST , "exists " , _create_mock_exists (False , False ))
71+ monkeypatch .setattr (shutil , "which " , _create_mock_exists (False , False ))
11172 with pytest .raises (ValueError ) as val_err_ctx :
11273 get_selected_runtime ()
11374
@@ -125,7 +86,11 @@ def test_runtime_construction_fails_if_ps_fails(
12586 monkeypatch : pytest .MonkeyPatch ,
12687) -> None :
12788 stderr = "container runtime failed"
128- monkeypatch .setattr (LOCALHOST , "run" , generate_mock_fail (stderr = stderr ))
89+ monkeypatch .setattr (
90+ helpers ,
91+ "run_command" ,
92+ lambda _ : (1 , "" , stderr ),
93+ )
12994 with pytest .raises (RuntimeError ) as rt_err_ctx :
13095 cls ()
13196
@@ -145,7 +110,9 @@ def test_buildah_version_parsing(
145110 monkeypatch : pytest .MonkeyPatch ,
146111) -> None :
147112 monkeypatch .setattr (
148- LOCALHOST , "check_output" , lambda _ : f"buildah version { version_str } "
113+ helpers ,
114+ "run_command" ,
115+ lambda _ : (0 , f"buildah version { version_str } " , "" ),
149116 )
150117
151118 assert _get_buildah_version () == expected_version
@@ -154,7 +121,7 @@ def test_buildah_version_parsing(
154121def test_get_buildah_version_fails_on_unexpected_stdout (
155122 monkeypatch : pytest .MonkeyPatch ,
156123) -> None :
157- monkeypatch .setattr (LOCALHOST , "check_output " , lambda _ : "foobar" )
124+ monkeypatch .setattr (helpers , "run_command " , lambda _ : ( 0 , "foobar" , "" ) )
158125 with pytest .raises (RuntimeError ) as rt_err_ctx :
159126 _get_buildah_version ()
160127
0 commit comments