Skip to content

Commit

Permalink
[ADD] Direct database access test for traefik v3 (Not finished yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
josep-tecnativa committed Oct 4, 2024
1 parent b7ddb7e commit 222ded6
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ jobs:
- name: Patch $PATH
run: echo "$HOME/.local/bin" >> $GITHUB_PATH
- run: poetry install --no-root
- name: Install psycopg2-binary
run: poetry run pip install psycopg2-binary
# Precreate shared networks to avoid race conditions
- run: docker network create inverseproxy_shared
- run: docker network create globalwhitelist_shared
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def traefik_host(request):
"--entryPoints.web-alt.address=:8080",
"--entryPoints.web-insecure.address=:80",
"--entryPoints.web-main.address=:443",
"--entryPoints.postgres-entrypoint.address=:5432",
"--log.level=debug",
"--providers.docker.exposedByDefault=false",
"--providers.docker.network=inverseproxy_shared",
Expand Down
82 changes: 82 additions & 0 deletions tests/test_postgres_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import time
import uuid
from pathlib import Path

import psycopg2
import pytest
from copier import run_copy
from packaging import version
from plumbum import local
from python_on_whales import DockerClient

from .conftest import DBVER_PER_ODOO


@pytest.mark.parametrize("environment", ("test", "prod"))
def test_database_external_connection(
cloned_template: Path,
supported_odoo_version: float,
tmp_path: Path,
traefik_host: dict,
environment: str,
):
"""Test that database is accessible via Traefik."""
traefik_version = version.parse(traefik_host["traefik_version"])
if traefik_version < version.parse("3"):
pytest.skip("This test only runs with Traefik v3 or higher")

base_domain = traefik_host["hostname"]

data = {
"odoo_version": supported_odoo_version,
"project_name": uuid.uuid4().hex,
"postgres_exposed": False,
"postgres_via_traefik": True,
"traefik_version": int(traefik_version.major),
"postgres_version": DBVER_PER_ODOO[supported_odoo_version]["latest"],
f"domains_{environment}": [
{"hosts": [f"db.{base_domain}"], "cert_resolver": False}
],
}
if supported_odoo_version < 16:
data["postgres_version"] = 13

dc = DockerClient(compose_files=[f"{environment}.yaml"])
with local.cwd(tmp_path):
run_copy(
src_path=str(cloned_template),
dst_path=".",
data=data,
vcs_ref="test",
defaults=True,
overwrite=True,
unsafe=True,
)
try:
dc.compose.build()
dc.compose.up(detach=True)
# Wait for the services to be ready
max_retries = 30
delay = 2
connected = False
for _ in range(max_retries):
try:
connection = psycopg2.connect(
dbname="postgres",
user="odoo",
password="odoo",
host=traefik_host["hostname"],
port=5432,
connect_timeout=1,
)
cursor = connection.cursor()
cursor.execute("SELECT 1;")
result = cursor.fetchone()
assert result == (1,)
connected = True
break
except Exception:
time.sleep(delay)
assert connected, "Could not connect to the database via Traefik"
finally:
dc.compose.down(remove_images="local", remove_orphans=True)

0 comments on commit 222ded6

Please sign in to comment.