Skip to content

Commit

Permalink
[FIX] Test postgres
Browse files Browse the repository at this point in the history
Use a unique Compose project name in tests to avoid volume collisions when running multiple PostgreSQL versions in parallel.
  • Loading branch information
josep-tecnativa committed Feb 5, 2025
1 parent 1204813 commit 6fe3c9f
Showing 1 changed file with 17 additions and 39 deletions.
56 changes: 17 additions & 39 deletions tests/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,24 @@


def _get_db_service_name(dc: DockerClient) -> str:
"""
Use python-on-whales to retrieve the final Compose configuration
and return the DB service name that ends with '-db'.
Fallback to any service containing 'db' or 'postgres'.
Finally, fallback to 'db'.
"""
config_data = dc.compose.config()

if ComposeConfig and isinstance(config_data, ComposeConfig):
services_dict = {s.name: s for s in config_data.services}
# (1) In newer versions of python-on-whales:
# config_data is a ComposeConfig => config_data.services is a dict:
# { "service_name": ServiceConfig(...) }
# (2) In older versions: config_data is a "legacy" dict
# => config_data["services"] => { "service_name": {...} }

elif isinstance(config_data, dict):
services_dict = config_data["services"]
if ComposeConfig and isinstance(config_data, ComposeConfig):
services_dict = config_data.services
else:
raise TypeError(
f"dc.compose.config() devolvió un tipo inesperado: {type(config_data)}"
)
for svc_name in services_dict:
if svc_name.lower().endswith("-db"):
return svc_name
for svc_name in services_dict:
lower_svc_name = svc_name.lower()
if "postgres" in lower_svc_name or "db" in lower_svc_name:
return svc_name
return "db"


def _get_db_service_name(dc: DockerClient) -> str:
"""
Use python-on-whales to retrieve the final Compose configuration
and return the DB service name that ends with '-db'. Fallback to
any service containing 'db' or 'postgres'. Finally, fallback to 'db'.
"""
config_data = dc.compose.config() # Returns a dict with { "services": {...}, ... }
services_dict = config_data["services"]
# First pass: Look for a service name that exactly ends with '-db'
services_dict = config_data["services"]
for svc_name in services_dict:
if svc_name.lower().endswith("-db"):
return svc_name

# Second pass: Fallback to any name containing 'db' or 'postgres'
for svc_name in services_dict:
if "postgres" in svc_name.lower() or "db" in svc_name.lower():
return svc_name

# Final fallback
return "db"


Expand All @@ -72,18 +44,24 @@ def test_postgresql_client_versions(
dbver: str,
):
"""Test multiple postgresql-client versions in odoo, db and duplicity services"""
unique_project_name = f"test_{uuid.uuid4().hex}"
dc_prod = DockerClient(
compose_files=["prod.yaml"],
compose_project_name=unique_project_name,
)

dbver_raw = DBVER_PER_ODOO[supported_odoo_version][dbver]
dbver_mver = dbver_raw.split(".")[0]
dc_prod = DockerClient(compose_files=["prod.yaml"])

with local.cwd(tmp_path):
print(str(cloned_template))
assert True

run_copy(
str(cloned_template),
dst_path=".",
data={
"odoo_version": supported_odoo_version,
"project_name": uuid.uuid4().hex,
"project_name": unique_project_name,
"odoo_proxy": "",
"postgres_version": dbver_raw,
"backup_dst": "/tmp/dummy",
Expand Down

0 comments on commit 6fe3c9f

Please sign in to comment.