|
11 | 11 | import redis |
12 | 12 | from dotenv import load_dotenv |
13 | 13 |
|
14 | | -# Load environment variables |
15 | | -load_dotenv() |
| 14 | +# Load environment-specific .env file for tests |
| 15 | +environment = os.getenv("ENVIRONMENT", "integrate_test") |
| 16 | +env_file = f".env.{environment}" |
| 17 | + |
| 18 | +# Try to load environment-specific file, fallback to .env.test, then .env |
| 19 | +if os.path.exists(env_file): |
| 20 | + load_dotenv(env_file) |
| 21 | +elif os.path.exists("tests/.env.test"): |
| 22 | + load_dotenv("tests/.env.test") |
| 23 | +else: |
| 24 | + load_dotenv() |
16 | 25 |
|
17 | 26 | class TestConfig: |
18 | 27 | """Test configuration settings""" |
19 | 28 |
|
20 | 29 | # Server settings |
21 | | - BASE_URL = os.getenv("TEST_BASE_URL", "http://localhost:3000") |
| 30 | + BASE_URL = os.getenv("TEST_BASE_URL", "http://localhost:3001") # Use port 3001 for tests |
22 | 31 | HEALTH_ENDPOINT = "/health" |
23 | 32 |
|
24 | 33 | # Database settings |
@@ -50,11 +59,26 @@ def __init__(self): |
50 | 59 | self.docker_client = docker.from_env() |
51 | 60 | self.server_process = None |
52 | 61 | self.containers_started = [] |
| 62 | + self.is_github_actions = self._detect_github_actions() |
| 63 | + |
| 64 | + def _detect_github_actions(self) -> bool: |
| 65 | + """Detect if running in GitHub Actions environment""" |
| 66 | + return ( |
| 67 | + os.getenv("GITHUB_ACTIONS") == "true" or |
| 68 | + os.getenv("CI") == "true" or |
| 69 | + os.getenv("RUNNER_OS") is not None |
| 70 | + ) |
53 | 71 |
|
54 | 72 | def start_dependencies(self): |
55 | 73 | """Start PostgreSQL and Redis using Docker""" |
56 | 74 | print("Starting test dependencies...") |
57 | 75 |
|
| 76 | + # Skip Docker container creation in GitHub Actions since services are provided |
| 77 | + if self.is_github_actions: |
| 78 | + print("Detected GitHub Actions environment - using provided services") |
| 79 | + self._wait_for_dependencies() |
| 80 | + return |
| 81 | + |
58 | 82 | # Start PostgreSQL |
59 | 83 | try: |
60 | 84 | postgres_container = self.docker_client.containers.run( |
@@ -138,9 +162,10 @@ def start_server(self): |
138 | 162 | # Set environment variables for the server |
139 | 163 | env = os.environ.copy() |
140 | 164 | env.update({ |
| 165 | + "ENVIRONMENT": "integrate_test", # This will load .env.integrate_test |
141 | 166 | "DATABASE_URL": TestConfig.DATABASE_URL, |
142 | 167 | "REDIS_URL": TestConfig.REDIS_URL, |
143 | | - "PORT": "3000", |
| 168 | + "PORT": "3001", # Use port 3001 to avoid conflicts |
144 | 169 | "JWT_SECRET": "test-jwt-secret-key", |
145 | 170 | "JWT_EXPIRES_IN": "3600", |
146 | 171 | "API_KEY_PREFIX": "ce_test_", |
@@ -188,12 +213,16 @@ def stop_server(self): |
188 | 213 | self.server_process.wait() |
189 | 214 | print("Server stopped") |
190 | 215 |
|
191 | | - for container in self.containers_started: |
192 | | - try: |
193 | | - container.stop() |
194 | | - print(f"Stopped container: {container.id[:12]}") |
195 | | - except docker.errors.NotFound: |
196 | | - pass |
| 216 | + # Only stop containers if we started them (not in GitHub Actions) |
| 217 | + if not self.is_github_actions: |
| 218 | + for container in self.containers_started: |
| 219 | + try: |
| 220 | + container.stop() |
| 221 | + print(f"Stopped container: {container.id[:12]}") |
| 222 | + except docker.errors.NotFound: |
| 223 | + pass |
| 224 | + else: |
| 225 | + print("Skipping container cleanup in GitHub Actions environment") |
197 | 226 |
|
198 | 227 | def is_server_running(self) -> bool: |
199 | 228 | """Check if the server is running""" |
|
0 commit comments