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