forked from Light-Heart-Labs/DreamServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_model_activate.py
More file actions
280 lines (208 loc) · 10 KB
/
test_model_activate.py
File metadata and controls
280 lines (208 loc) · 10 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""Tests for AMD model activation helpers in dream-host-agent.py."""
import importlib.util
import subprocess
import sys
from pathlib import Path
# Import the host agent module from bin/ using importlib.
# The module has an ``if __name__ == "__main__":`` guard so no server starts.
_agent_path = Path(__file__).resolve().parents[4] / "bin" / "dream-host-agent.py"
_spec = importlib.util.spec_from_file_location("dream_host_agent_activate", _agent_path)
_mod = importlib.util.module_from_spec(_spec)
sys.modules["dream_host_agent_activate"] = _mod
_spec.loader.exec_module(_mod)
_check_lemonade_health = _mod._check_lemonade_health
_send_lemonade_warmup = _mod._send_lemonade_warmup
_write_lemonade_config = _mod._write_lemonade_config
_compose_restart_llama_server = _mod._compose_restart_llama_server
_launch_native_llama_server = _mod._launch_native_llama_server
# --- _check_lemonade_health ---
class TestCheckLemonadeHealth:
def test_model_loaded(self):
body = '{"status": "ok", "model_loaded": "extra.Qwen3.5-9B-Q4_K_M.gguf"}'
assert _check_lemonade_health(body) is True
def test_model_null(self):
body = '{"status": "ok", "model_loaded": null}'
assert _check_lemonade_health(body) is False
def test_no_model_loaded_key(self):
body = '{"status": "ok"}'
assert _check_lemonade_health(body) is False
def test_invalid_json(self):
assert _check_lemonade_health("not json") is False
def test_empty_string(self):
assert _check_lemonade_health("") is False
def test_model_loaded_false_is_truthy(self):
"""model_loaded=false is unusual but non-null, so should be True."""
body = '{"model_loaded": false}'
assert _check_lemonade_health(body) is True
def test_model_loaded_empty_string(self):
"""model_loaded="" is non-null, so should be True."""
body = '{"model_loaded": ""}'
assert _check_lemonade_health(body) is True
# --- _send_lemonade_warmup ---
class TestSendLemonadeWarmup:
def test_success(self, monkeypatch):
calls = []
def fake_run(cmd, **kwargs):
calls.append(cmd)
result = subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")
return result
monkeypatch.setattr(subprocess, "run", fake_run)
assert _send_lemonade_warmup("localhost", "8080", "model.gguf", 0) is True
assert len(calls) == 1
# Verify curl is called with correct URL and model ID
cmd = calls[0]
assert "http://localhost:8080/api/v1/chat/completions" in cmd
payload_idx = cmd.index("-d") + 1
assert '"extra.model.gguf"' in cmd[payload_idx]
def test_failure(self, monkeypatch):
def fake_run(cmd, **kwargs):
return subprocess.CompletedProcess(cmd, 1, stdout="", stderr="error")
monkeypatch.setattr(subprocess, "run", fake_run)
assert _send_lemonade_warmup("localhost", "8080", "model.gguf", 0) is False
def test_timeout(self, monkeypatch):
def fake_run(cmd, **kwargs):
raise subprocess.TimeoutExpired(cmd, kwargs.get("timeout", 35))
monkeypatch.setattr(subprocess, "run", fake_run)
assert _send_lemonade_warmup("localhost", "8080", "model.gguf", 0) is False
def test_containerized_host(self, monkeypatch):
"""Verify the host parameter is used (not hardcoded to localhost)."""
calls = []
def fake_run(cmd, **kwargs):
calls.append(cmd)
return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")
monkeypatch.setattr(subprocess, "run", fake_run)
_send_lemonade_warmup("dream-llama-server", "8080", "model.gguf", 0)
assert "http://dream-llama-server:8080/api/v1/chat/completions" in calls[0]
# --- _write_lemonade_config ---
class TestWriteLemonadeConfig:
def test_writes_correct_content(self, tmp_path):
litellm_dir = tmp_path / "config" / "litellm"
litellm_dir.mkdir(parents=True)
_write_lemonade_config(tmp_path, "Qwen3.5-9B-Q4_K_M.gguf")
content = (litellm_dir / "lemonade.yaml").read_text()
assert "model: openai/extra.Qwen3.5-9B-Q4_K_M.gguf" in content
assert "api_base: http://llama-server:8080/api/v1" in content
assert "api_key: sk-lemonade" in content
assert 'model_name: "*"' in content
assert "drop_params: true" in content
def test_overwrites_previous(self, tmp_path):
litellm_dir = tmp_path / "config" / "litellm"
litellm_dir.mkdir(parents=True)
_write_lemonade_config(tmp_path, "old-model.gguf")
_write_lemonade_config(tmp_path, "new-model.gguf")
content = (litellm_dir / "lemonade.yaml").read_text()
assert "old-model.gguf" not in content
assert "model: openai/extra.new-model.gguf" in content
def test_file_path(self, tmp_path):
litellm_dir = tmp_path / "config" / "litellm"
litellm_dir.mkdir(parents=True)
_write_lemonade_config(tmp_path, "model.gguf")
assert (litellm_dir / "lemonade.yaml").exists()
class TestComposeRestartLlamaServer:
def test_amd_uses_stop_then_up(self, monkeypatch, tmp_path):
calls = []
def fake_run(cmd, **kwargs):
calls.append(cmd)
return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")
monkeypatch.setattr(_mod, "INSTALL_DIR", tmp_path)
monkeypatch.setattr(
_mod,
"resolve_compose_flags",
lambda: ["--env-file", ".env", "-f", "docker-compose.base.yml"],
)
monkeypatch.setattr(subprocess, "run", fake_run)
_compose_restart_llama_server({"GPU_BACKEND": "amd"})
assert calls == [
[
"docker", "compose", "--env-file", ".env", "-f",
"docker-compose.base.yml", "stop", "llama-server",
],
[
"docker", "compose", "--env-file", ".env", "-f",
"docker-compose.base.yml", "up", "-d", "llama-server",
],
]
class TestLaunchNativeLlamaServer:
def test_reads_env_and_writes_pid(self, monkeypatch, tmp_path):
env_path = tmp_path / ".env"
env_path.write_text(
"GGUF_FILE=test-model.gguf\nCTX_SIZE=8192\nLLAMA_REASONING=on\n",
encoding="utf-8",
)
(tmp_path / "data" / "models").mkdir(parents=True)
(tmp_path / "data").mkdir(exist_ok=True)
llama_bin = tmp_path / "bin" / "llama-server"
llama_bin.parent.mkdir(parents=True)
llama_bin.write_text("", encoding="utf-8")
llama_log = tmp_path / "data" / "llama-server.log"
pid_file = tmp_path / "data" / ".llama-server.pid"
calls = []
class _FakeProc:
pid = 4321
def fake_popen(cmd, **kwargs):
calls.append((cmd, kwargs))
return _FakeProc()
monkeypatch.setattr(_mod, "INSTALL_DIR", tmp_path)
monkeypatch.setattr(subprocess, "Popen", fake_popen)
_launch_native_llama_server(env_path, llama_bin, llama_log, pid_file)
assert pid_file.read_text(encoding="utf-8") == "4321"
cmd, _kwargs = calls[0]
assert cmd[0] == str(llama_bin)
assert "--model" in cmd
assert str(tmp_path / "data" / "models" / "test-model.gguf") in cmd
assert "--ctx-size" in cmd
assert "8192" in cmd
assert "--reasoning-format" in cmd
assert "deepseek" in cmd
# --- Rollback integration ---
class TestLemonadeYamlRollback:
"""Verify that lemonade.yaml is backed up and restored on rollback.
We don't spin up the full HTTP server — instead, we test the backup/restore
logic by checking that the pattern in _do_model_activate is correct.
"""
def test_backup_sentinel_none_when_missing(self, tmp_path):
"""When lemonade.yaml doesn't exist, backup should be None."""
yaml_path = tmp_path / "config" / "litellm" / "lemonade.yaml"
# File doesn't exist
backup = yaml_path.read_text(encoding="utf-8") if yaml_path.exists() else None
assert backup is None
def test_backup_preserves_content(self, tmp_path):
"""When lemonade.yaml exists, backup should capture content."""
litellm_dir = tmp_path / "config" / "litellm"
litellm_dir.mkdir(parents=True)
yaml_path = litellm_dir / "lemonade.yaml"
yaml_path.write_text("original content", encoding="utf-8")
backup = yaml_path.read_text(encoding="utf-8") if yaml_path.exists() else None
assert backup == "original content"
# Simulate overwrite + rollback
_write_lemonade_config(tmp_path, "new-model.gguf")
assert "new-model.gguf" in yaml_path.read_text()
# Restore
if backup is not None:
yaml_path.write_text(backup, encoding="utf-8")
assert yaml_path.read_text() == "original content"
def test_no_restore_when_backup_is_none(self, tmp_path):
"""When backup is None, rollback should not create the file."""
litellm_dir = tmp_path / "config" / "litellm"
litellm_dir.mkdir(parents=True)
yaml_path = litellm_dir / "lemonade.yaml"
backup = None # File didn't exist at backup time
# Rollback should NOT create the file
if backup is not None:
yaml_path.write_text(backup, encoding="utf-8")
assert not yaml_path.exists()
# --- NVIDIA regression guard ---
class TestNvidiaHealthUnchanged:
"""Ensure the NVIDIA health check still uses the simple '"ok"' check."""
def test_ok_response_is_healthy(self):
"""llama.cpp health response contains "ok" — should be detected."""
body = '{"status": "ok"}'
# The NVIDIA path checks: '"ok"' in body
assert '"ok"' in body
def test_model_loaded_not_needed_for_nvidia(self):
"""NVIDIA doesn't need model_loaded — just "ok" is sufficient."""
# This response has "ok" but no model_loaded — fine for NVIDIA
body = '{"status": "ok"}'
assert '"ok"' in body
# But Lemonade check would fail (no model_loaded key)
assert _check_lemonade_health(body) is False