Skip to content

Commit

Permalink
Add initial readiness assessment integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikram29 committed Feb 25, 2025
1 parent 5f08873 commit d5e67e2
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/dma/lib/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_engine(
database=database,
query={}, # type: ignore[arg-type]
),
isolation_level='AUTOCOMMIT'
isolation_level="AUTOCOMMIT",
)
if src_info.db_type == "MYSQL":
return create_engine(
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ARG PG_VERSION

# Use the official PostgreSQL image as a base
FROM postgres:$PG_VERSION

ARG PG_VERSION
# Install pglogical dependencies and pglogical itself
RUN apt-get update && apt-get install -y \
postgresql-$PG_VERSION-pglogical \
&& apt-get autoremove -y \
&& rm -rf /var/lib/apt/lists/*

# Expose the default PostgreSQL port
EXPOSE 5432

# Set the default command to start PostgreSQL
CMD ["postgres"]
5 changes: 5 additions & 0 deletions tests/integration/postgres/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ def postgres12_sync_engine(
)


@pytest.fixture(scope="session")
def postgres_docker_compose_files() -> list[Path]:
return [Path(Path(__file__).parent / "docker-compose.yml")]


@pytest.fixture(
scope="session",
params=[
Expand Down
112 changes: 112 additions & 0 deletions tests/integration/postgres/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
services:
postgres12:
networks:
- default
build:
context: .
dockerfile: Dockerfile
args:
PG_VERSION: 12
ports:
- "${POSTGRES12_PORT:-5423}:5432" # use a non-standard port here
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-super-secret}
command:
- "postgres"
- "-c"
- "WAL_LEVEL=LOGICAL"
- "-c"
- "shared_preload_libraries=pglogical"
postgres13:
networks:
- default
build:
context: .
dockerfile: Dockerfile
args:
PG_VERSION: 13
ports:
- "${POSTGRES13_PORT:-5424}:5432" # use a non-standard port here
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-super-secret}
command:
- "postgres"
- "-c"
- "WAL_LEVEL=LOGICAL"
- "-c"
- "shared_preload_libraries=pglogical"
postgres14:
networks:
- default
build:
context: .
dockerfile: Dockerfile
args:
PG_VERSION: 14
ports:
- "${POSTGRES14_PORT:-5425}:5432" # use a non-standard port here
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-super-secret}
command:
- "postgres"
- "-c"
- "WAL_LEVEL=LOGICAL"
- "-c"
- "shared_preload_libraries=pglogical"
postgres15:
networks:
- default
build:
context: .
dockerfile: Dockerfile
args:
PG_VERSION: 15
ports:
- "${POSTGRES15_PORT:-5426}:5432" # use a non-standard port here
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-super-secret}
command:
- "postgres"
- "-c"
- "WAL_LEVEL=LOGICAL"
- "-c"
- "shared_preload_libraries=pglogical"
postgres16:
networks:
- default
build:
context: .
dockerfile: Dockerfile
args:
PG_VERSION: 16
ports:
- "${POSTGRES16_PORT:-5427}:5432" # use a non-standard port here
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-super-secret}
command:
- "postgres"
- "-c"
- "WAL_LEVEL=LOGICAL"
- "-c"
- "shared_preload_libraries=pglogical"
postgres17:
networks:
- default
build:
context: .
dockerfile: Dockerfile
args:
PG_VERSION: 17
ports:
- "${POSTGRES17_PORT:-5428}:5432" # use a non-standard port here
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-super-secret}
command:
- "postgres"
- "-c"
- "WAL_LEVEL=LOGICAL"
- "-c"
- "shared_preload_libraries=pglogical"
networks:
default:
driver: bridge
105 changes: 105 additions & 0 deletions tests/integration/postgres/test_readiness_assessment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Integration tests for the CLI."""

from __future__ import annotations

from pathlib import Path
from textwrap import dedent
from typing import TYPE_CHECKING
from urllib.parse import urlparse

import pytest
from sqlalchemy import text

from dma.cli.main import app
from dma.lib.db.local import get_duckdb_connection

if TYPE_CHECKING:
from click.testing import CliRunner
from sqlalchemy import Engine

pytestmark = [
pytest.mark.anyio,
pytest.mark.postgres,
pytest.mark.xdist_group("postgres"),
]


def test_pglogical(
sync_engine: Engine,
runner: CliRunner,
) -> None:
with sync_engine.begin() as conn:
conn.execute(text(dedent("""create extension if not exists pglogical;""")))
url = urlparse(str(sync_engine.url.render_as_string(hide_password=False)))
result = runner.invoke(
app,
[
"readiness-check",
"--db-type",
"postgres",
"--no-prompt",
"--hostname",
f"{url.hostname}",
"--port",
f"{url.port!s}",
"--database",
f"{url.path.lstrip('/')}",
"--username",
f"{url.username}",
"--password",
f"{url.password}",
],
)
assert result.exit_code == 0
with get_duckdb_connection(Path("tmp/")) as local_db:
rows = local_db.sql(
"select severity from readiness_check_summary WHERE rule_code = 'PGLOGICAL_INSTALLED'",
).fetchall()
for row in rows:
assert row[0] == "PASS"


def test_wal_level(
sync_engine: Engine,
runner: CliRunner,
) -> None:
url = urlparse(str(sync_engine.url.render_as_string(hide_password=False)))
result = runner.invoke(
app,
[
"readiness-check",
"--db-type",
"postgres",
"--no-prompt",
"--hostname",
f"{url.hostname}",
"--port",
f"{url.port!s}",
"--database",
f"{url.path.lstrip('/')}",
"--username",
f"{url.username}",
"--password",
f"{url.password}",
],
)
assert result.exit_code == 0
with get_duckdb_connection(Path("tmp/")) as local_db:
rows = local_db.sql(
"select severity from readiness_check_summary WHERE rule_code = 'WAL_LEVEL'",
).fetchall()
for row in rows:
assert row[0] == "PASS"

0 comments on commit d5e67e2

Please sign in to comment.