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 ad20d08
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 7 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ jobs:
- "3.10"
- 3.11
odoo-version:
- 11.0
- 12.0
- 13.0
- 14.0
- 15.0
- 16.0
include:
Expand All @@ -50,8 +46,6 @@ jobs:
- python-version: 3.11
odoo-version: 17.0
traefik_version:
- 1
- 2
- 3

steps:
Expand Down Expand Up @@ -85,6 +79,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
6 changes: 6 additions & 0 deletions common.yaml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ services:
environment:
POSTGRES_DB: *dbname
POSTGRES_USER: *dbuser
{%- if db_environment_extra %}
# Only for tests
{%- for key, value in db_environment_extra.items() %}
{{ key }}: {{ value }}
{%- endfor %}
{%- endif %}
CONF_EXTRA: |
work_mem = 512MB
volumes:
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def traefik_host(request):
docker = DockerClient()
if request.param == "3":
traefik_container = docker.run(
"traefik:v3.0",
"traefik:v3.1.2",
detach=True,
privileged=True,
networks=["inverseproxy_shared"],
Expand All @@ -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
87 changes: 87 additions & 0 deletions tests/test_postgres_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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": 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}
],
"db_environment_extra": {
"WAN_DATABASES": '["prod"]',
"WAN_USERS": '["odoo"]',
},
}
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,
sslmode="disable",
)
cursor = connection.cursor()
cursor.execute("SELECT 1;")
result = cursor.fetchone()
assert result == (1,)
connected = True
break
except Exception as e:
last_exception = e # Almacena la excepción
time.sleep(delay)
assert connected, f"Could not connect to databse: {last_exception}"
finally:
dc.compose.down(remove_images="local", remove_orphans=True)

0 comments on commit ad20d08

Please sign in to comment.