66
77import json
88import re
9+ import shutil
910import sys
1011from abc import ABC
1112from abc import abstractmethod
2021from typing import Optional
2122from typing import Union
2223
23- import testinfra
24+
2425from _pytest .mark .structures import ParameterSet
2526from pytest import param
2627
28+ from pytest_container import helpers
2729from pytest_container .inspect import BindMount
2830from pytest_container .inspect import Config
2931from pytest_container .inspect import ContainerHealth
@@ -411,9 +413,6 @@ def __str__(self) -> str:
411413 return self .__class__ .__name__
412414
413415
414- LOCALHOST = testinfra .host .get_host ("local://" )
415-
416-
417416def _get_podman_version (version_stdout : str ) -> Version :
418417 if version_stdout [:15 ] != "podman version " :
419418 raise RuntimeError (
@@ -424,7 +423,7 @@ def _get_podman_version(version_stdout: str) -> Version:
424423
425424
426425def _get_buildah_version () -> Version :
427- version_stdout = LOCALHOST . check_output ( "buildah --version" )
426+ version_stdout = helpers . run_command ([ "buildah" , " --version"])[ 1 ]
428427 build_version_begin = "buildah version "
429428 if not version_stdout .startswith (build_version_begin ):
430429 raise RuntimeError (
@@ -443,11 +442,11 @@ class PodmanRuntime(OciRuntimeBase):
443442 """
444443
445444 def __init__ (self ) -> None :
446- podman_ps = LOCALHOST . run ( "podman ps" )
447- if not podman_ps . succeeded :
448- raise RuntimeError (f"`podman ps` failed with { podman_ps . stderr } " )
445+ podman_ps = helpers . run_command ([ "podman" , " ps"] )
446+ if not podman_ps [ 0 ] == 0 :
447+ raise RuntimeError (f"`podman ps` failed with { podman_ps [ 2 ] } " )
449448
450- self ._buildah_functional = LOCALHOST . run ( "buildah" ). succeeded
449+ self ._buildah_functional = helpers . run_command ([ "buildah" ])[ 0 ] == 0
451450 super ().__init__ (
452451 build_command = (
453452 ["buildah" , "bud" , "--layers" , "--force-rm" ]
@@ -461,8 +460,11 @@ def __init__(self) -> None:
461460 @cached_property
462461 def version (self ) -> Version :
463462 """Returns the version of podman installed on the system"""
463+
464464 return _get_podman_version (
465- LOCALHOST .run_expect ([0 ], "podman --version" ).stdout
465+ helpers .run_command (["podman" , "--version" ], ignore_errors = False )[
466+ 1
467+ ]
466468 )
467469
468470 @cached_property
@@ -539,9 +541,9 @@ class DockerRuntime(OciRuntimeBase):
539541 containers."""
540542
541543 def __init__ (self ) -> None :
542- docker_ps = LOCALHOST . run ( "docker ps" )
543- if not docker_ps . succeeded :
544- raise RuntimeError (f"`docker ps` failed with { docker_ps . stderr } " )
544+ docker_ps = helpers . run_command ([ "docker" , " ps"] )
545+ if not docker_ps [ 0 ] == 0 :
546+ raise RuntimeError (f"`docker ps` failed with { docker_ps [ 2 ] } " )
545547
546548 super ().__init__ (
547549 build_command = ["docker" , "build" , "--force-rm" ],
@@ -552,7 +554,9 @@ def __init__(self) -> None:
552554 def version (self ) -> Version :
553555 """Returns the version of docker installed on this system"""
554556 return _get_docker_version (
555- LOCALHOST .run_expect ([0 ], "docker --version" ).stdout
557+ helpers .run_command (["docker" , "--version" ], ignore_errors = False )[
558+ 1
559+ ]
556560 )
557561
558562 @property
@@ -613,8 +617,8 @@ def get_selected_runtime() -> OciRuntimeBase:
613617
614618 If neither docker nor podman are available, then a ValueError is raised.
615619 """
616- podman_exists = LOCALHOST . exists ("podman" )
617- docker_exists = LOCALHOST . exists ("docker" )
620+ podman_exists = shutil . which ("podman" )
621+ docker_exists = shutil . which ("docker" )
618622
619623 runtime_choice = getenv ("CONTAINER_RUNTIME" , "podman" ).lower ()
620624 if runtime_choice not in ("podman" , "docker" ):
0 commit comments