* сделала, чтобы Postgres был доступен только внутри compose-сети
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
vasyukova
2026-10-02 16:57:35 +03:00
parent 2ec2ead938
commit 7e05e747d7
3 changed files with 47 additions and 32 deletions
+10 -6
View File
@@ -29,6 +29,10 @@ from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
from client import Client, Failure, arguments, check, run # noqa: E402
# Имя compose-сети проекта: <папка-проекта>_default.
# Проверить: docker network ls | findstr default
COMPOSE_NETWORK = os.environ.get("COMPOSE_NETWORK", "mtusi2026lab2_default")
# ---------- 1. Конкурентная регистрация одинакового username ----------
def test_concurrent_registration(c):
@@ -67,16 +71,16 @@ def _read_dsn() -> str:
dsn = line.split("=", 1)[1].strip()
break
check(dsn, "DATABASE_URL не найден ни в окружении, ни в .env")
# Внутри контейнера хост — host.docker.internal, а не localhost.
return dsn.replace("@localhost:", "@host.docker.internal:").replace(
"@127.0.0.1:", "@host.docker.internal:")
# Postgres доступен только внутри compose-сети; одноразовые psql-контейнеры
# запускаются в этой же сети, поэтому имя хоста postgres резолвится как есть.
return dsn
def _psql(dsn_in_container: str, sql: str) -> str:
r = subprocess.run(
[
"docker", "run", "--rm",
"--add-host=host.docker.internal:host-gateway",
"--network", COMPOSE_NETWORK,
"postgres:18-alpine",
"psql", dsn_in_container, "-tA", "-c", sql,
],
@@ -141,7 +145,7 @@ END $$;
r = subprocess.run(
[
"docker", "run", "--rm",
"--add-host=host.docker.internal:host-gateway",
"--network", COMPOSE_NETWORK,
"postgres:18-alpine",
"psql", dsn_in_container, "-c", seed_sql,
],
@@ -173,7 +177,7 @@ END $$;
r = subprocess.run(
[
"docker", "run", "--rm",
"--add-host=host.docker.internal:host-gateway",
"--network", COMPOSE_NETWORK,
"postgres:18-alpine",
"psql", dsn_in_container, "-c", sql,
],