|
23 | 23 | # seems to have issues with podman https://github.com/testcontainers/testcontainers-python/issues/753 |
24 | 24 | testcontainers_config.ryuk_disabled = True |
25 | 25 |
|
| 26 | +# Set longer timeout for CI environments where image pulls take longer |
| 27 | +os.environ.setdefault("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/var/run/docker.sock") |
| 28 | +os.environ.setdefault("TESTCONTAINERS_RYUK_DISABLED", "true") |
| 29 | + |
26 | 30 |
|
27 | 31 | def _run_migrator(postgres: PostgresContainer) -> None: |
28 | 32 | # Python recommends using an absolute path when running an executable |
29 | 33 | # to avoid any ambiguity |
30 | 34 | mvn = shutil.which("mvn") or "mvn" |
| 35 | + test_dir = Path(__file__).parent |
| 36 | + migrator_dir = test_dir.parent.parent / "bfd-db-migrator-ng" |
| 37 | + print(f"[MIGRATOR] Running mvn from: {migrator_dir}") |
| 38 | + print(f"[MIGRATOR] Using mvn: {mvn}") |
31 | 39 | try: |
32 | | - subprocess.run( |
| 40 | + result = subprocess.run( |
33 | 41 | f"{mvn} flyway:migrate " |
34 | 42 | "-Dflyway.url=" |
35 | 43 | f"jdbc:postgresql://localhost:{postgres.get_exposed_port(5432)}/{postgres.dbname} " |
36 | 44 | f"-Dflyway.user={postgres.username} " |
37 | 45 | f"-Dflyway.password={postgres.password}", |
38 | | - cwd="../../bfd-db-migrator-ng", |
| 46 | + cwd=str(migrator_dir), |
39 | 47 | shell=True, |
40 | 48 | capture_output=True, |
41 | 49 | check=True, |
| 50 | + timeout=60, |
42 | 51 | ) |
| 52 | + print("[MIGRATOR] Migration completed successfully") |
| 53 | + if result.stdout: |
| 54 | + print(f"[MIGRATOR] stdout: {result.stdout.decode()}") |
| 55 | + except subprocess.TimeoutExpired: |
| 56 | + print("[MIGRATOR] Migration timed out after 60 seconds", file=sys.stderr) |
| 57 | + raise |
43 | 58 | except subprocess.CalledProcessError as ex: |
44 | | - print(ex.output) |
| 59 | + print(f"[MIGRATOR] Migration failed with exit code {ex.returncode}", file=sys.stderr) |
| 60 | + if ex.stdout: |
| 61 | + print(f"[MIGRATOR] stdout: {ex.stdout.decode()}", file=sys.stderr) |
| 62 | + if ex.stderr: |
| 63 | + print(f"[MIGRATOR] stderr: {ex.stderr.decode()}", file=sys.stderr) |
45 | 64 | raise |
46 | 65 |
|
47 | 66 |
|
48 | 67 | @pytest.fixture(scope="module") |
49 | 68 | def setup_db() -> Generator[PostgresContainer]: |
50 | | - with PostgresContainer("postgres:16", driver="") as postgres: |
51 | | - with psycopg.connect(postgres.get_connection_url()) as conn: |
52 | | - with Path("./mock-idr.sql").open() as f: |
53 | | - conn.execute(f.read()) # type: ignore |
54 | | - conn.commit() |
55 | | - |
56 | | - _run_migrator(postgres) |
57 | | - load_from_csv(conn, "./test_samples1") # type: ignore |
58 | | - |
59 | | - info = conn.info |
60 | | - os.environ["BFD_DB_ENDPOINT"] = info.host |
61 | | - os.environ["BFD_DB_PORT"] = str(info.port) |
62 | | - os.environ["BFD_DB_NAME"] = info.dbname |
63 | | - os.environ["BFD_DB_USERNAME"] = info.user |
64 | | - os.environ["BFD_DB_PASSWORD"] = info.password |
65 | | - os.environ["PARALLELISM"] = "2" |
66 | | - os.environ["IDR_BATCH_SIZE"] = "100000" |
67 | | - os.environ["IDR_LOAD_BENES"] = "true" |
68 | | - os.environ["IDR_LOAD_CLAIMS"] = "true" |
69 | | - yield postgres |
| 69 | + test_dir = Path(__file__).parent |
| 70 | + print("\n[FIXTURE] Starting PostgreSQL container with postgres:16") |
| 71 | + try: |
| 72 | + with PostgresContainer("postgres:16", driver="") as postgres: |
| 73 | + print(f"[FIXTURE] Container started on {postgres.get_exposed_port(5432)}") |
| 74 | + with psycopg.connect(postgres.get_connection_url()) as conn: |
| 75 | + print("[FIXTURE] Connected to database") |
| 76 | + print("[FIXTURE] Loading mock-idr.sql") |
| 77 | + with (test_dir / "mock-idr.sql").open() as f: |
| 78 | + conn.execute(f.read()) # type: ignore |
| 79 | + conn.commit() |
| 80 | + |
| 81 | + print("[FIXTURE] Running flyway migrator") |
| 82 | + _run_migrator(postgres) |
| 83 | + print("[FIXTURE] Loading test samples") |
| 84 | + load_from_csv(conn, str(test_dir / "test_samples1")) # type: ignore |
| 85 | + |
| 86 | + info = conn.info |
| 87 | + os.environ["BFD_DB_ENDPOINT"] = info.host |
| 88 | + os.environ["BFD_DB_PORT"] = str(info.port) |
| 89 | + os.environ["BFD_DB_NAME"] = info.dbname |
| 90 | + os.environ["BFD_DB_USERNAME"] = info.user |
| 91 | + os.environ["BFD_DB_PASSWORD"] = info.password |
| 92 | + os.environ["PARALLELISM"] = "2" |
| 93 | + os.environ["IDR_BATCH_SIZE"] = "100000" |
| 94 | + os.environ["IDR_LOAD_BENES"] = "true" |
| 95 | + os.environ["IDR_LOAD_CLAIMS"] = "true" |
| 96 | + print("[FIXTURE] Database setup complete") |
| 97 | + yield postgres |
| 98 | + except Exception as e: |
| 99 | + print(f"[FIXTURE] Error during database setup: {e}", file=sys.stderr) |
| 100 | + raise |
70 | 101 |
|
71 | 102 |
|
72 | 103 | def test_pipeline(setup_db: PostgresContainer) -> None: |
|
0 commit comments