From b88490d18707dd28cef1a73d7e429ee94d066aaf Mon Sep 17 00:00:00 2001 From: kovagoadi Date: Wed, 1 Apr 2026 12:38:00 +0200 Subject: [PATCH] Test without mock --- tests/e2e/test_traefik.py | 188 +++++++++++++++++++------------------- 1 file changed, 94 insertions(+), 94 deletions(-) diff --git a/tests/e2e/test_traefik.py b/tests/e2e/test_traefik.py index d248752..879c97b 100644 --- a/tests/e2e/test_traefik.py +++ b/tests/e2e/test_traefik.py @@ -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."""