Code: Select all
import pytest
import requests
import subprocess
import time
from testcontainers.core.container import DockerContainer
from testcontainers.core.wait_strategies import LogMessageWaitStrategy
port = 8000
@pytest.fixture(scope="module")
def app_container():
# Build the docker image
subprocess.run(["docker", "build", "-t", "my-app:test", "."], check=True)
# Start the container
container = DockerContainer("my-app:test")
container.with_exposed_ports(port)
container.with_env("PORT", str(port))
container.with_env("DATABASE_CONNECTION_STRING", "sqlite:///:memory:")
container.with_env("ENVIRONMENT", "test")
container.waiting_for(LogMessageWaitStrategy("Uvicorn running on"))
container.start()
yield container
container.stop()
@pytest.mark.timeout(20)
def test_actuators_info(app_container):
host: str = app_container.get_container_host_ip()
container_port: int = app_container.get_exposed_port(port)
base_url = f"http://{host}:{container_port}"
# Retry logic in case the app takes a moment to be fully responsive
max_retries = 5
for _ in range(max_retries):
try:
response = requests.get(f"{base_url}/actuators/info")
if response.status_code == 200:
assert response.json() is not None
return
except requests.exceptions.ConnectionError:
pass
time.sleep(0.5)
pytest.fail("Could not connect to the application or received non-200 status")
Grundsätzlich erhalte ich eine Zeitüberschreitung (ich habe sie auf 20 Sekunden eingestellt), und wenn ich die Zeitüberschreitung nicht festlege, bleibt er für immer hängen.
Anhand der Protokolle kann ich sehen, dass das Image erstellt wurde, und es scheint, als würde es hängen bleiben und darauf warten, dass der Container erstellt wird Bereit.
Ist dies der beste Weg, um zu testen, ob das Docker-Image noch funktionsfähig ist? Gibt es noch andere Alternativen? Irgendeine Idee, warum es in GitHub-Aktionen fehlschlägt?
Dies ist der GitHub-Aktionsworkflow:
Code: Select all
---
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.13"]
steps:
- uses: actions/checkout@v6
- name: Install uv
uses: astral-sh/setup-uv@v7
with:
python-version: ${{ matrix.python-version }}
- name: Set up Python
run: uv python install
- name: Install the project
run: uv sync --all-extras --dev
- name: Run tests
run: uv run pytest tests
Mobile version