66
77import json
88import re
9+ import shutil
910import sys
1011from abc import ABC
1112from abc import abstractmethod
1213from dataclasses import dataclass
1314from os import getenv
1415from pathlib import Path
15- from subprocess import check_output
1616from typing import TYPE_CHECKING
1717from typing import Any
1818from typing import Callable
1919from typing import List
2020from typing import Optional
2121from typing import Union
2222
23- import testinfra
2423from _pytest .mark .structures import ParameterSet
2524from pytest import param
2625
26+ from pytest_container import helpers
2727from pytest_container .inspect import BindMount
2828from pytest_container .inspect import Config
2929from pytest_container .inspect import ContainerHealth
@@ -271,23 +271,24 @@ def get_image_size(
271271 else str (image_or_id_or_container )
272272 )
273273 return float (
274- check_output (
274+ helpers . run_command (
275275 [
276276 self .runner_binary ,
277277 "inspect" ,
278278 "-f" ,
279279 '"{{ .Size }}"' ,
280280 id_to_inspect ,
281281 ]
282- )
283- .decode ()
282+ )[1 ]
284283 .strip ()
285284 .replace ('"' , "" )
286285 )
287286
288287 def _get_container_inspect (self , container_id : str ) -> Any :
289288 inspect = json .loads (
290- check_output ([self .runner_binary , "inspect" , container_id ])
289+ helpers .run_command ([self .runner_binary , "inspect" , container_id ])[
290+ 1
291+ ]
291292 )
292293 if len (inspect ) != 1 :
293294 raise RuntimeError (
@@ -305,19 +306,15 @@ def _get_image_entrypoint_cmd(
305306 defined.
306307
307308 """
308- entrypoint = (
309- check_output (
310- [
311- self .runner_binary ,
312- "inspect" ,
313- "-f" ,
314- f"{{{{.Config.{ query_type } }}}}" ,
315- image_url_or_id ,
316- ]
317- )
318- .decode ("utf-8" )
319- .strip ()
320- )
309+ entrypoint = helpers .run_command (
310+ [
311+ self .runner_binary ,
312+ "inspect" ,
313+ "-f" ,
314+ f"{{{{.Config.{ query_type } }}}}" ,
315+ image_url_or_id ,
316+ ]
317+ )[1 ].strip ()
321318 return None if entrypoint == "[]" else entrypoint
322319
323320 @staticmethod
@@ -411,9 +408,6 @@ def __str__(self) -> str:
411408 return self .__class__ .__name__
412409
413410
414- LOCALHOST = testinfra .host .get_host ("local://" )
415-
416-
417411def _get_podman_version (version_stdout : str ) -> Version :
418412 if version_stdout [:15 ] != "podman version " :
419413 raise RuntimeError (
@@ -424,7 +418,7 @@ def _get_podman_version(version_stdout: str) -> Version:
424418
425419
426420def _get_buildah_version () -> Version :
427- version_stdout = LOCALHOST . check_output ( "buildah --version" )
421+ version_stdout = helpers . run_command ([ "buildah" , " --version"])[ 1 ]
428422 build_version_begin = "buildah version "
429423 if not version_stdout .startswith (build_version_begin ):
430424 raise RuntimeError (
@@ -443,11 +437,11 @@ class PodmanRuntime(OciRuntimeBase):
443437 """
444438
445439 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 } " )
440+ podman_ps = helpers . run_command ([ "podman" , " ps"] )
441+ if not podman_ps [ 0 ] == 0 :
442+ raise RuntimeError (f"`podman ps` failed with { podman_ps [ 2 ] } " )
449443
450- self ._buildah_functional = LOCALHOST . run ( "buildah" ). succeeded
444+ self ._buildah_functional = helpers . run_command ([ "buildah" ])[ 0 ] == 0
451445 super ().__init__ (
452446 build_command = (
453447 ["buildah" , "bud" , "--layers" , "--force-rm" ]
@@ -461,8 +455,11 @@ def __init__(self) -> None:
461455 @cached_property
462456 def version (self ) -> Version :
463457 """Returns the version of podman installed on the system"""
458+
464459 return _get_podman_version (
465- LOCALHOST .run_expect ([0 ], "podman --version" ).stdout
460+ helpers .run_command (["podman" , "--version" ], ignore_errors = False )[
461+ 1
462+ ]
466463 )
467464
468465 @cached_property
@@ -539,9 +536,9 @@ class DockerRuntime(OciRuntimeBase):
539536 containers."""
540537
541538 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 } " )
539+ docker_ps = helpers . run_command ([ "docker" , " ps"] )
540+ if not docker_ps [ 0 ] == 0 :
541+ raise RuntimeError (f"`docker ps` failed with { docker_ps [ 2 ] } " )
545542
546543 super ().__init__ (
547544 build_command = ["docker" , "build" , "--force-rm" ],
@@ -552,7 +549,9 @@ def __init__(self) -> None:
552549 def version (self ) -> Version :
553550 """Returns the version of docker installed on this system"""
554551 return _get_docker_version (
555- LOCALHOST .run_expect ([0 ], "docker --version" ).stdout
552+ helpers .run_command (["docker" , "--version" ], ignore_errors = False )[
553+ 1
554+ ]
556555 )
557556
558557 @property
@@ -613,8 +612,8 @@ def get_selected_runtime() -> OciRuntimeBase:
613612
614613 If neither docker nor podman are available, then a ValueError is raised.
615614 """
616- podman_exists = LOCALHOST . exists ("podman" )
617- docker_exists = LOCALHOST . exists ("docker" )
615+ podman_exists = shutil . which ("podman" )
616+ docker_exists = shutil . which ("docker" )
618617
619618 runtime_choice = getenv ("CONTAINER_RUNTIME" , "podman" ).lower ()
620619 if runtime_choice not in ("podman" , "docker" ):
0 commit comments