|
| 1 | +""" |
| 2 | +Integration test fixtures for utils package. |
| 3 | +
|
| 4 | +Provides fixtures for running tests against the grpcbin Docker container. |
| 5 | +grpcbin is a neutral gRPC test service that supports various RPC types. |
| 6 | +""" |
| 7 | + |
| 8 | +import subprocess |
| 9 | +import time |
| 10 | +import socket |
| 11 | +import pytest |
| 12 | + |
| 13 | + |
| 14 | +GRPCBIN_IMAGE = "moul/grpcbin" |
| 15 | +GRPCBIN_INSECURE_PORT = 9000 # HTTP/2 without TLS |
| 16 | +GRPCBIN_SECURE_PORT = 9001 # HTTP/2 with TLS |
| 17 | + |
| 18 | + |
| 19 | +def is_port_open(host: str, port: int, timeout: float = 1.0) -> bool: |
| 20 | + """Check if a port is open and accepting connections.""" |
| 21 | + try: |
| 22 | + with socket.create_connection((host, port), timeout=timeout): |
| 23 | + return True |
| 24 | + except (socket.timeout, socket.error, ConnectionRefusedError, OSError): |
| 25 | + return False |
| 26 | + |
| 27 | + |
| 28 | +def wait_for_port(host: str, port: int, timeout: float = 30.0) -> bool: |
| 29 | + """Wait for a port to become available.""" |
| 30 | + start_time = time.time() |
| 31 | + while time.time() - start_time < timeout: |
| 32 | + if is_port_open(host, port): |
| 33 | + return True |
| 34 | + time.sleep(0.5) |
| 35 | + return False |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture(scope="session") |
| 39 | +def grpcbin_container(): |
| 40 | + """ |
| 41 | + Start a grpcbin Docker container for testing. |
| 42 | +
|
| 43 | + The container exposes: |
| 44 | + - Port 9000: Insecure gRPC (HTTP/2 without TLS) |
| 45 | + - Port 9001: Secure gRPC (HTTP/2 with TLS) |
| 46 | +
|
| 47 | + The container is automatically removed after tests complete. |
| 48 | + """ |
| 49 | + container_name = "pytest-grpcbin" |
| 50 | + |
| 51 | + # Check if Docker is available |
| 52 | + try: |
| 53 | + subprocess.run( |
| 54 | + ["docker", "info"], |
| 55 | + capture_output=True, |
| 56 | + check=True, |
| 57 | + timeout=10, |
| 58 | + ) |
| 59 | + except (subprocess.CalledProcessError, FileNotFoundError, subprocess.TimeoutExpired): |
| 60 | + pytest.skip("Docker is not available") |
| 61 | + |
| 62 | + # Remove any existing container with the same name |
| 63 | + subprocess.run( |
| 64 | + ["docker", "rm", "-f", container_name], |
| 65 | + capture_output=True, |
| 66 | + timeout=30, |
| 67 | + ) |
| 68 | + |
| 69 | + # Start the container |
| 70 | + result = subprocess.run( |
| 71 | + [ |
| 72 | + "docker", |
| 73 | + "run", |
| 74 | + "-d", |
| 75 | + "--rm", |
| 76 | + "--name", |
| 77 | + container_name, |
| 78 | + "-p", |
| 79 | + f"{GRPCBIN_INSECURE_PORT}:{GRPCBIN_INSECURE_PORT}", |
| 80 | + "-p", |
| 81 | + f"{GRPCBIN_SECURE_PORT}:{GRPCBIN_SECURE_PORT}", |
| 82 | + GRPCBIN_IMAGE, |
| 83 | + ], |
| 84 | + capture_output=True, |
| 85 | + text=True, |
| 86 | + timeout=60, |
| 87 | + ) |
| 88 | + |
| 89 | + if result.returncode != 0: |
| 90 | + pytest.fail(f"Failed to start grpcbin container: {result.stderr}") |
| 91 | + |
| 92 | + container_id = result.stdout.strip() |
| 93 | + |
| 94 | + # Wait for the insecure port to be ready |
| 95 | + if not wait_for_port("localhost", GRPCBIN_INSECURE_PORT, timeout=30): |
| 96 | + # Clean up and fail |
| 97 | + subprocess.run(["docker", "rm", "-f", container_name], capture_output=True) |
| 98 | + pytest.fail(f"grpcbin port {GRPCBIN_INSECURE_PORT} did not become available") |
| 99 | + |
| 100 | + # Give the gRPC server inside the container a moment to fully initialize |
| 101 | + # The port may be open before the HTTP/2 server is ready to process requests |
| 102 | + time.sleep(1.0) |
| 103 | + |
| 104 | + # Provide connection info to tests |
| 105 | + yield { |
| 106 | + "container_id": container_id, |
| 107 | + "container_name": container_name, |
| 108 | + "host": "localhost", |
| 109 | + "insecure_port": GRPCBIN_INSECURE_PORT, |
| 110 | + "secure_port": GRPCBIN_SECURE_PORT, |
| 111 | + } |
| 112 | + |
| 113 | + # Cleanup: stop and remove the container |
| 114 | + subprocess.run( |
| 115 | + ["docker", "rm", "-f", container_name], |
| 116 | + capture_output=True, |
| 117 | + timeout=30, |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +@pytest.fixture |
| 122 | +def grpcbin_host(grpcbin_container): |
| 123 | + """Return the host address for the grpcbin container.""" |
| 124 | + return grpcbin_container["host"] |
| 125 | + |
| 126 | + |
| 127 | +@pytest.fixture |
| 128 | +def grpcbin_insecure_port(grpcbin_container): |
| 129 | + """Return the insecure (HTTP/2 without TLS) port for grpcbin.""" |
| 130 | + return grpcbin_container["insecure_port"] |
| 131 | + |
| 132 | + |
| 133 | +@pytest.fixture |
| 134 | +def grpcbin_secure_port(grpcbin_container): |
| 135 | + """Return the secure (HTTP/2 with TLS) port for grpcbin.""" |
| 136 | + return grpcbin_container["secure_port"] |
0 commit comments