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 14, 2024
1 parent 9f1770c commit cd64c5b
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 20 deletions.
25 changes: 6 additions & 19 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,13 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version:
- 3.8
- 3.9
- "3.10"
- 3.11
odoo-version:
- 11.0
- 12.0
- 13.0
- 14.0
- 15.0
- 16.0
- 17.0
include:
- python-version: "3.10"
odoo-version: 18.0
odoo-version: 17.0
traefik_version: 3
- python-version: 3.11
odoo-version: 18.0
traefik_version:
- 1
- 2
- 3
odoo-version: 17.0
traefik_version: 3

steps:
# Shared steps
Expand Down Expand Up @@ -86,6 +71,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
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,18 @@ 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"],
volumes=[("/var/run/docker.sock", "/var/run/docker.sock", "ro")],
publish=[(5432, 5432)],
command=[
"--accessLog=true",
"--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
90 changes: 90 additions & 0 deletions tests/test_postgres_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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.sequential
@pytest.mark.parametrize("environment", ("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_listdb": True,
"traefik_version": int(
str(version.parse(traefik_host["traefik_version"])).split(".")[0]
),
"postgres_exposed": True,
"odoo_version": supported_odoo_version,
"postgres_version": DBVER_PER_ODOO[supported_odoo_version]["latest"],
"project_name": uuid.uuid4().hex,
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="prod",
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 cd64c5b

Please sign in to comment.