Skip to content

Commit f651122

Browse files
author
Julien Larocque-Dupont
authored
Merge pull request #240 from homeassistant-ai/feature/custom-test-port
feat: add HA_TEST_PORT env var for custom test container port
2 parents aed5fa1 + 4743ee8 commit f651122

2 files changed

Lines changed: 42 additions & 8 deletions

File tree

tests/src/e2e/conftest.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
44
This provides testcontainers integration but falls back to the existing
55
Docker environment if testcontainers has issues.
6+
7+
Environment Variables:
8+
HA_TEST_PORT: Optional fixed port for Home Assistant container (default: dynamic)
9+
Set this to bind to a specific host port instead of random assignment.
10+
Example: HA_TEST_PORT=8123
611
"""
712

813
import asyncio
914
import logging
15+
import os
1016
import shutil
1117
import sys
1218
import tempfile
@@ -103,12 +109,22 @@ def ha_container_with_fresh_config():
103109
f"📁 Fresh HA config prepared at: {config_path} with proper permissions"
104110
)
105111

106-
# Create testcontainer with automatic port assignment
112+
# Create testcontainer with port configuration
107113
# renovate: datasource=docker depName=ghcr.io/home-assistant/home-assistant
108114
container = DockerContainer("ghcr.io/home-assistant/home-assistant:2025.11.3")
109-
container = container.with_exposed_ports(
110-
8123
111-
) # Expose port, let testcontainers assign host port
115+
116+
# Check for custom port via environment variable
117+
custom_port = os.environ.get("HA_TEST_PORT")
118+
if custom_port:
119+
try:
120+
port = int(custom_port)
121+
container = container.with_bind_ports(8123, port)
122+
logger.info(f"🔌 Using fixed port {port} (from HA_TEST_PORT)")
123+
except ValueError:
124+
logger.warning(f"⚠️ Invalid HA_TEST_PORT '{custom_port}', using dynamic port")
125+
container = container.with_exposed_ports(8123)
126+
else:
127+
container = container.with_exposed_ports(8123) # Dynamic port assignment
112128
container = container.with_volume_mapping(
113129
str(config_path), "/config", "rw"
114130
) # Ensure read-write mount

tests/test_env_manager.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
55
Interactive test environment for Home Assistant MCP Server development.
66
Uses testcontainers to manage a Home Assistant instance for testing.
7+
8+
Environment Variables:
9+
HA_TEST_PORT: Optional fixed port for Home Assistant container (default: random)
10+
Example: HA_TEST_PORT=8123
711
"""
812

913
import logging
@@ -97,11 +101,25 @@ def start_container(self) -> None:
97101
# Set up config directory
98102
config_dir = self._setup_config_directory()
99103

100-
# Create and start container
104+
# Create container with port configuration
105+
# renovate: datasource=docker depName=ghcr.io/home-assistant/home-assistant
106+
container = DockerContainer("ghcr.io/home-assistant/home-assistant:2025.11.3")
107+
108+
# Check for custom port via environment variable
109+
custom_port = os.environ.get("HA_TEST_PORT")
110+
if custom_port:
111+
try:
112+
port = int(custom_port)
113+
container = container.with_bind_ports(8123, port)
114+
logger.info(f"🔌 Using fixed port {port} (from HA_TEST_PORT)")
115+
except ValueError:
116+
logger.warning(f"⚠️ Invalid HA_TEST_PORT '{custom_port}', using random port")
117+
container = container.with_bind_ports(8123, None)
118+
else:
119+
container = container.with_bind_ports(8123, None) # Random host port
120+
101121
self.container = (
102-
# renovate: datasource=docker depName=ghcr.io/home-assistant/home-assistant
103-
DockerContainer("ghcr.io/home-assistant/home-assistant:2025.11.3")
104-
.with_bind_ports(8123, None) # Random host port
122+
container
105123
.with_volume_mapping(str(config_dir), "/config", "rw")
106124
.with_env("TZ", "UTC")
107125
.with_kwargs(privileged=True)

0 commit comments

Comments
 (0)