-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] Direct database access test for traefik v3 (Not finished yet)
- Loading branch information
1 parent
b7ddb7e
commit ad20d08
Showing
4 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |