55grpcbin is a neutral gRPC test service that supports various RPC types.
66"""
77
8- import subprocess
98import time
109import pytest
1110
11+ from localstack .utils .container_utils .container_client import PortMappings
12+ from localstack .utils .docker_utils import DOCKER_CLIENT
1213from localstack .utils .net import wait_for_port_open
1314
1415
@@ -30,55 +31,36 @@ def grpcbin_container():
3031 """
3132 container_name = "pytest-grpcbin"
3233
33- # Check if Docker is available
34+ # Remove any existing container with the same name
3435 try :
35- subprocess .run (
36- ["docker" , "info" ],
37- capture_output = True ,
38- check = True ,
39- timeout = 10 ,
40- )
41- except (subprocess .CalledProcessError , FileNotFoundError , subprocess .TimeoutExpired ):
42- pytest .skip ("Docker is not available" )
36+ DOCKER_CLIENT .remove_container (container_name )
37+ except Exception :
38+ pass # Container may not exist
4339
44- # Remove any existing container with the same name
45- subprocess .run (
46- ["docker" , "rm" , "-f" , container_name ],
47- capture_output = True ,
48- timeout = 30 ,
49- )
40+ # Configure port mappings
41+ ports = PortMappings ()
42+ ports .add (GRPCBIN_INSECURE_PORT )
43+ ports .add (GRPCBIN_SECURE_PORT )
5044
5145 # Start the container
52- result = subprocess .run (
53- [
54- "docker" ,
55- "run" ,
56- "-d" ,
57- "--rm" ,
58- "--name" ,
59- container_name ,
60- "-p" ,
61- f"{ GRPCBIN_INSECURE_PORT } :{ GRPCBIN_INSECURE_PORT } " ,
62- "-p" ,
63- f"{ GRPCBIN_SECURE_PORT } :{ GRPCBIN_SECURE_PORT } " ,
64- GRPCBIN_IMAGE ,
65- ],
66- capture_output = True ,
67- text = True ,
68- timeout = 60 ,
46+ stdout , _ = DOCKER_CLIENT .run_container (
47+ image_name = GRPCBIN_IMAGE ,
48+ name = container_name ,
49+ detach = True ,
50+ remove = True ,
51+ ports = ports ,
6952 )
70-
71- if result .returncode != 0 :
72- pytest .fail (f"Failed to start grpcbin container: { result .stderr } " )
73-
74- container_id = result .stdout .strip ()
53+ container_id = stdout .decode ().strip ()
7554
7655 # Wait for the insecure port to be ready
7756 try :
7857 wait_for_port_open (GRPCBIN_INSECURE_PORT , retries = 60 , sleep_time = 0.5 )
7958 except Exception :
8059 # Clean up and fail
81- subprocess .run (["docker" , "rm" , "-f" , container_name ], capture_output = True )
60+ try :
61+ DOCKER_CLIENT .remove_container (container_name )
62+ except Exception :
63+ pass
8264 pytest .fail (f"grpcbin port { GRPCBIN_INSECURE_PORT } did not become available" )
8365
8466 # Give the gRPC server inside the container a moment to fully initialize
@@ -95,11 +77,10 @@ def grpcbin_container():
9577 }
9678
9779 # Cleanup: stop and remove the container
98- subprocess .run (
99- ["docker" , "rm" , "-f" , container_name ],
100- capture_output = True ,
101- timeout = 30 ,
102- )
80+ try :
81+ DOCKER_CLIENT .remove_container (container_name )
82+ except Exception :
83+ pass
10384
10485
10586@pytest .fixture
0 commit comments