-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathconftest.py
More file actions
152 lines (125 loc) · 3.64 KB
/
conftest.py
File metadata and controls
152 lines (125 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import subprocess
import time
from pathlib import Path
import pytest
DATABASE_URL = "postgresql://postgres:postgres@localhost:5433/clusterdev"
@pytest.fixture(scope="module")
def docker_compose(project_root: Path):
"""Start a test database and run migrations"""
subprocess.check_call(
["docker", "compose", "-f", "docker-compose.test.yml", "up", "-d"], cwd=project_root
)
try:
# Wait for migrations to finish
while True:
result = subprocess.run(
["docker", "compose", "-f", "docker-compose.test.yml", "ps", "-q", "migrate-test"],
capture_output=True,
text=True,
cwd=project_root,
)
if not result.stdout.strip(): # Container no longer exists
break
time.sleep(1)
# Check if migrations succeeded
logs = subprocess.run(
["docker", "compose", "-f", "docker-compose.test.yml", "logs", "migrate-test"],
capture_output=True,
text=True,
cwd=project_root,
)
if "error" in logs.stdout.lower():
raise Exception(f"Migrations failed: {logs.stdout}")
yield
finally:
subprocess.run(
["docker", "compose", "-f", "docker-compose.test.yml", "down", "-v"], cwd=project_root
)
def _nuke_contents(db):
db.cursor.execute(
"TRUNCATE leaderboard.code_files, leaderboard.submission, leaderboard.runs, "
"leaderboard.leaderboard, leaderboard.user_info, leaderboard.templates, "
"leaderboard.gpu_type, leaderboard.user_rate_limits RESTART IDENTITY CASCADE"
)
db.connection.commit()
@pytest.fixture()
def database(docker_compose):
from libkernelbot import leaderboard_db
db = leaderboard_db.LeaderboardDB(
url=DATABASE_URL,
ssl_mode="disable",
)
with db:
_nuke_contents(db)
yield db
with db:
_nuke_contents(db)
@pytest.fixture()
def bot(docker_compose, database):
from types import SimpleNamespace
from libkernelbot import backend
env = SimpleNamespace()
env.DATABASE_URL = DATABASE_URL
env.DISABLE_SSL = "1"
yield backend.KernelBackend(env, False)
TASK_YAML = """
lang: py
description: "Test task description"
ranking_by: geom
test_timeout: 120
files:
- name: "kernel.py"
source: "kernel.py"
- name: "submission.py"
source: "@SUBMISSION@"
config:
main: "kernel.py"
tests:
- input_size: 1000
dtype: "float32"
benchmarks:
- input_size: 10000
dtype: "float32"
templates:
Python: "template.py"
CUDA: "template.cu"
"""
MULTi_GPU_TASK_YAML = """
lang: py
description: "Test task description"
ranking_by: geom
multi_gpu: true
test_timeout: 120
files:
- name: "kernel.py"
source: "kernel.py"
- name: "submission.py"
source: "@SUBMISSION@"
config:
main: "kernel.py"
tests:
- input_size: 1000
world_size: 4
dtype: "float32"
benchmarks:
- input_size: 10000
world_size: 4
dtype: "float32"
templates:
Python: "template.py"
CUDA: "template.cu"
"""
@pytest.fixture
def task_directory(tmp_path):
"""Create a temporary directory structure for task definition testing"""
# Create source files
Path.write_text(tmp_path / "kernel.py", "def kernel(): pass")
Path.write_text(tmp_path / "template.py", "# Python template")
Path.write_text(tmp_path / "template.cu", "// CUDA template")
# Create task.yml
Path.write_text(tmp_path / "task.yml", TASK_YAML)
Path.write_text(tmp_path / "multi-task.yml", MULTi_GPU_TASK_YAML)
return tmp_path
@pytest.fixture(scope="session")
def project_root():
return Path(__file__).parent.parent