Test without mock
Some checks failed
Remote Deployment Pipeline / Prepare Context (pull_request) Successful in 2s
Remote Deployment Pipeline / Deploy (Staging) (pull_request) Has been skipped
Remote Deployment Pipeline / Deploy (Pre-Prod) (pull_request) Failing after 1m4s
Remote Deployment Pipeline / Deploy (Dev/Preview) (pull_request) Successful in 50s
Remote Deployment Pipeline / Cleanup Preview (pull_request) Has been skipped
Remote Deployment Pipeline / Deploy (Production) (pull_request) Has been skipped

This commit is contained in:
2026-04-01 12:38:00 +02:00
parent 8f1219e27f
commit b88490d187

View File

@@ -11,100 +11,100 @@ BASE_URL = f"http://192.168.1.85:{PORT}"
HTTPS_BASE_URL = f"https://192.168.1.85:{HTTPS_PORT}"
HOST_HEADER = f"test-whoami.{DOMAIN}"
@pytest.fixture(scope="session", autouse=True)
def mock_webserver():
"""Start ephemeral mock webserver containers and configure Traefik."""
import time
# Skip ephemeral mocks in CI environment; test against real services
if os.getenv("CI"):
print("CI environment detected. Skipping ephemeral mock setup; testing against real services.")
yield
return
# In CI (Docker-in-Docker), we need to use the HOST path for volumes, not the container path.
# The workflow mounts the project root to /app, but the Docker daemon is on the host.
# We pass PROJECT_ROOT env var to the test container to tell it where the files are on the HOST.
cwd = os.getenv("PROJECT_ROOT", os.getcwd())
certs_dir = os.path.join(cwd, "tests/mock_nginx/certs")
image = "nginx:alpine"
# Define mocks
mocks = [
{
"name": "mock-legacy-ephemeral",
"alias": "webserver",
"conf": os.path.join(cwd, "tests/mock_nginx/legacy.conf"),
"ports": ["80", "443"]
},
{
"name": "mock-staging-ephemeral",
"alias": "mock",
"conf": os.path.join(cwd, "tests/mock_nginx/staging.conf"),
"ports": ["8080", "445"]
}
]
# Cleanup and Start Mocks
mock_ips = {}
for mock in mocks:
subprocess.run(["docker", "rm", "-f", mock["name"]], capture_output=True)
cmd = [
"docker", "run", "-d", "--rm",
"--name", mock["name"],
"--network", "proxy",
"--network-alias", mock["alias"],
"-v", f"{mock['conf']}:/etc/nginx/nginx.conf:ro",
"-v", f"{certs_dir}:/etc/nginx/certs:ro",
image
]
print(f"Starting {mock['name']}...")
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
print(f"STDOUT: {result.stdout}")
print(f"STDERR: {result.stderr}")
pytest.fail(f"Failed to start {mock['name']}: {result.stderr}")
# Inspect container to get IP
inspect_cmd = ["docker", "inspect", "-f", "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}", mock["name"]]
inspect_res = subprocess.run(inspect_cmd, capture_output=True, text=True)
if inspect_res.returncode != 0:
pytest.fail(f"Failed to inspect {mock['name']}: {inspect_res.stderr}")
mock_ips[mock["alias"]] = inspect_res.stdout.strip()
print(f"{mock['name']} IP: {mock_ips[mock['alias']]}")
# Restart Traefik with IP env vars for extra_hosts
print(f"Restarting Traefik with STAGING_IP={mock_ips['mock']} and LEGACY_IP={mock_ips['webserver']}...")
env = os.environ.copy()
env["STAGING_IP"] = mock_ips["mock"]
env["LEGACY_IP"] = mock_ips["webserver"]
# In CI (Docker-in-Docker), we need to tell docker-compose that the project root
# is the HOST path (PROJECT_ROOT), not the container path (/app).
# This ensures volume mounts use the correct host paths.
if "PROJECT_ROOT" in os.environ:
env["COMPOSE_PROJECT_DIR"] = os.environ["PROJECT_ROOT"]
# We need to recreate the container to pick up extra_hosts changes
try:
subprocess.run(["docker-compose", "--env-file", "preprod.env", "up", "-d", "--force-recreate", "--no-deps", "traefik"], env=env, check=True, capture_output=True, text=True)
except subprocess.CalledProcessError as e:
print(f"Docker Compose STDOUT: {e.stdout}")
print(f"Docker Compose STDERR: {e.stderr}")
raise e
# Wait for everything to settle
time.sleep(5)
yield
print("Stopping mocks and restoring Traefik...")
for mock in mocks:
subprocess.run(["docker", "stop", mock["name"]], capture_output=True)
# Restore Traefik to default (optional)
subprocess.run(["docker-compose", "--env-file", "preprod.env", "up", "-d", "--force-recreate", "--no-deps", "traefik"], check=True)
# @pytest.fixture(scope="session", autouse=True)
# def mock_webserver():
# """Start ephemeral mock webserver containers and configure Traefik."""
# import time
#
# # Skip ephemeral mocks in CI environment; test against real services
# if os.getenv("CI"):
# print("CI environment detected. Skipping ephemeral mock setup; testing against real services.")
# yield
# return
#
# # In CI (Docker-in-Docker), we need to use the HOST path for volumes, not the container path.
# # The workflow mounts the project root to /app, but the Docker daemon is on the host.
# # We pass PROJECT_ROOT env var to the test container to tell it where the files are on the HOST.
# cwd = os.getenv("PROJECT_ROOT", os.getcwd())
# certs_dir = os.path.join(cwd, "tests/mock_nginx/certs")
# image = "nginx:alpine"
#
# # Define mocks
# mocks = [
# {
# "name": "mock-legacy-ephemeral",
# "alias": "webserver",
# "conf": os.path.join(cwd, "tests/mock_nginx/legacy.conf"),
# "ports": ["80", "443"]
# },
# {
# "name": "mock-staging-ephemeral",
# "alias": "mock",
# "conf": os.path.join(cwd, "tests/mock_nginx/staging.conf"),
# "ports": ["8080", "445"]
# }
# ]
#
# # Cleanup and Start Mocks
# mock_ips = {}
# for mock in mocks:
# subprocess.run(["docker", "rm", "-f", mock["name"]], capture_output=True)
# cmd = [
# "docker", "run", "-d", "--rm",
# "--name", mock["name"],
# "--network", "proxy",
# "--network-alias", mock["alias"],
# "-v", f"{mock['conf']}:/etc/nginx/nginx.conf:ro",
# "-v", f"{certs_dir}:/etc/nginx/certs:ro",
# image
# ]
#
# print(f"Starting {mock['name']}...")
# result = subprocess.run(cmd, capture_output=True, text=True)
# if result.returncode != 0:
# print(f"STDOUT: {result.stdout}")
# print(f"STDERR: {result.stderr}")
# pytest.fail(f"Failed to start {mock['name']}: {result.stderr}")
#
# # Inspect container to get IP
# inspect_cmd = ["docker", "inspect", "-f", "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}", mock["name"]]
# inspect_res = subprocess.run(inspect_cmd, capture_output=True, text=True)
# if inspect_res.returncode != 0:
# pytest.fail(f"Failed to inspect {mock['name']}: {inspect_res.stderr}")
# mock_ips[mock["alias"]] = inspect_res.stdout.strip()
# print(f"{mock['name']} IP: {mock_ips[mock['alias']]}")
#
# # Restart Traefik with IP env vars for extra_hosts
# print(f"Restarting Traefik with STAGING_IP={mock_ips['mock']} and LEGACY_IP={mock_ips['webserver']}...")
# env = os.environ.copy()
# env["STAGING_IP"] = mock_ips["mock"]
# env["LEGACY_IP"] = mock_ips["webserver"]
#
# # In CI (Docker-in-Docker), we need to tell docker-compose that the project root
# # is the HOST path (PROJECT_ROOT), not the container path (/app).
# # This ensures volume mounts use the correct host paths.
# if "PROJECT_ROOT" in os.environ:
# env["COMPOSE_PROJECT_DIR"] = os.environ["PROJECT_ROOT"]
#
# # We need to recreate the container to pick up extra_hosts changes
# try:
# subprocess.run(["docker-compose", "--env-file", "preprod.env", "up", "-d", "--force-recreate", "--no-deps", "traefik"], env=env, check=True, capture_output=True, text=True)
# except subprocess.CalledProcessError as e:
# print(f"Docker Compose STDOUT: {e.stdout}")
# print(f"Docker Compose STDERR: {e.stderr}")
# raise e
#
# # Wait for everything to settle
# time.sleep(5)
#
# yield
#
# print("Stopping mocks and restoring Traefik...")
# for mock in mocks:
# subprocess.run(["docker", "stop", mock["name"]], capture_output=True)
#
# # Restore Traefik to default (optional)
# subprocess.run(["docker-compose", "--env-file", "preprod.env", "up", "-d", "--force-recreate", "--no-deps", "traefik"], check=True)
def test_whoami_http_reachable():
"""Verify that the whoami service is reachable via HTTP."""