Skip to content

Commit 3e6ba65

Browse files
Copilotmsaroufim
andcommitted
Fix CI failures by using GitHub Actions service containers instead of Docker
Co-authored-by: msaroufim <[email protected]>
1 parent 7d8b80b commit 3e6ba65

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

.github/workflows/test.yml

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ jobs:
3535
name: Backend Tests
3636
runs-on: ubuntu-latest
3737

38+
services:
39+
postgres:
40+
image: postgres:16
41+
env:
42+
POSTGRES_PASSWORD: test
43+
POSTGRES_DB: kernelboard_test
44+
options: >-
45+
--health-cmd pg_isready
46+
--health-interval 10s
47+
--health-timeout 5s
48+
--health-retries 5
49+
ports:
50+
- 5432:5432
51+
52+
redis:
53+
image: redis:7-alpine
54+
options: >-
55+
--health-cmd "redis-cli ping"
56+
--health-interval 10s
57+
--health-timeout 5s
58+
--health-retries 5
59+
ports:
60+
- 6379:6379
61+
3862
steps:
3963
- uses: actions/checkout@v4
4064

@@ -51,8 +75,9 @@ jobs:
5175
5276
- name: Run backend tests
5377
env:
54-
REDIS_URL: memory://
55-
DATABASE_URL: sqlite:///tmp/test.db
78+
CI: true
79+
REDIS_URL: redis://localhost:6379/0
80+
DATABASE_URL: postgresql://postgres:test@localhost:5432/kernelboard_test
5681
DISCORD_CLIENT_ID: test
5782
DISCORD_CLIENT_SECRET: test
5883
SECRET_KEY: test-secret-key
@@ -62,8 +87,9 @@ jobs:
6287
6388
- name: Run coverage
6489
env:
65-
REDIS_URL: memory://
66-
DATABASE_URL: sqlite:///tmp/test.db
90+
CI: true
91+
REDIS_URL: redis://localhost:6379/0
92+
DATABASE_URL: postgresql://postgres:test@localhost:5432/kernelboard_test
6793
DISCORD_CLIENT_ID: test
6894
DISCORD_CLIENT_SECRET: test
6995
SECRET_KEY: test-secret-key

tests/conftest.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import subprocess
77
import time
88
import secrets
9+
import os
910

1011

1112
def get_test_redis_url(port: int):
@@ -28,6 +29,11 @@ def get_test_db_info():
2829
}
2930

3031

32+
def is_ci_environment():
33+
"""Check if we're running in a CI environment where Docker may not be available."""
34+
return os.environ.get("CI") == "true" or os.environ.get("GITHUB_ACTIONS") == "true"
35+
36+
3137
def _short_random_string() -> str:
3238
"""Returns a random string of 6 lowercase letters."""
3339
return "".join(random.choice(string.ascii_lowercase) for i in range(6))
@@ -50,7 +56,44 @@ def _execute_sql(url: str, sql: str):
5056
@pytest.fixture(scope="session")
5157
def db_server():
5258
"""Starts a DB server and creates a template DB once per session."""
53-
59+
60+
# In CI environments, use the service containers provided by GitHub Actions
61+
if is_ci_environment():
62+
# GitHub Actions provides PostgreSQL service container
63+
db_url = "postgresql://postgres:test@localhost:5432"
64+
65+
# Create a template database and load data
66+
template_db = f"kernelboard_template_{_short_random_string()}"
67+
_execute_sql(db_url, f"CREATE DATABASE {template_db}")
68+
69+
# Load data.sql into the template database using psql
70+
result = subprocess.run(
71+
[
72+
"psql",
73+
"-h",
74+
"localhost",
75+
"-U",
76+
"postgres",
77+
"-p",
78+
"5432",
79+
"-d",
80+
template_db,
81+
"-f",
82+
"tests/data.sql",
83+
],
84+
env={"PGPASSWORD": "test"},
85+
)
86+
87+
if result.returncode != 0:
88+
pytest.exit("Error loading data.sql in CI", returncode=1)
89+
90+
yield {"db_url": db_url, "db_name": template_db}
91+
92+
# Cleanup: drop the template database
93+
_execute_sql(db_url, f"DROP DATABASE {template_db}")
94+
return
95+
96+
# Original Docker-based logic for local development
5497
container_name = f"kernelboard_db_{_short_random_string()}"
5598

5699
test_db = get_test_db_info()
@@ -166,7 +209,14 @@ def db_server():
166209
def redis_server():
167210
"""
168211
Starts a Redis Docker container for the test session.
212+
In CI environments, uses the service container provided by GitHub Actions.
169213
"""
214+
215+
# In CI environments, use the service containers provided by GitHub Actions
216+
if is_ci_environment():
217+
yield "redis://localhost:6379/0"
218+
return
219+
170220
container_name = f"kernelboard_redis_{_short_random_string()}"
171221
port = get_test_redis_port()
172222
redis_url = get_test_redis_url(port)
@@ -301,6 +351,12 @@ def runner(app):
301351

302352
@pytest.fixture(autouse=True)
303353
def set_env(monkeypatch):
354+
# In CI environments, rely on environment variables set by GitHub Actions
355+
if is_ci_environment():
356+
# Don't override environment variables in CI mode - use what's already set
357+
return
358+
359+
# For local development, set the traditional test environment variables
304360
monkeypatch.setenv("DATABASE_URL", get_test_db_info()["db_url"])
305361
monkeypatch.setenv("DISCORD_CLIENT_ID", "test")
306362
monkeypatch.setenv("DISCORD_CLIENT_SECRET", "test")

0 commit comments

Comments
 (0)