Skip to content

Commit 7d79d76

Browse files
committed
added tests for cleanup in api and cli, updated var names in utils.
1 parent 28401a2 commit 7d79d76

File tree

3 files changed

+192
-47
lines changed

3 files changed

+192
-47
lines changed

tests/vec_inf/cli/test_cli.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,3 +531,77 @@ def test_metrics_command_request_failed(
531531
in result.output
532532
)
533533
assert "Connection refused" in result.output
534+
535+
536+
def test_cli_cleanup_logs_dry_run(runner, tmp_path):
537+
"""Test CLI cleanup command in dry-run mode."""
538+
model_dir = tmp_path / "fam_a" / "model_a.123"
539+
model_dir.mkdir(parents=True)
540+
541+
result = runner.invoke(
542+
cli,
543+
[
544+
"cleanup",
545+
"--log-dir",
546+
str(tmp_path),
547+
"--model-family",
548+
"fam_a",
549+
"--model-name",
550+
"model_a",
551+
"--dry-run",
552+
],
553+
)
554+
555+
assert result.exit_code == 0
556+
assert "would be deleted" in result.output
557+
assert "model_a.123" in result.output
558+
559+
560+
def test_cli_cleanup_logs_delete(tmp_path):
561+
"""Test cleanup_logs CLI deletes matching directories when not in dry-run mode."""
562+
fam_dir = tmp_path / "fam_a"
563+
fam_dir.mkdir()
564+
(fam_dir / "model_a.1").mkdir()
565+
566+
runner = CliRunner()
567+
result = runner.invoke(
568+
cli,
569+
[
570+
"cleanup",
571+
"--log-dir",
572+
str(tmp_path),
573+
"--model-family",
574+
"fam_a",
575+
"--model-name",
576+
"model_a",
577+
"--job-id",
578+
"1",
579+
],
580+
)
581+
582+
assert result.exit_code == 0
583+
assert "Deleted 1 log directory" in result.output
584+
assert not (fam_dir / "model_a.1").exists()
585+
586+
587+
def test_cli_cleanup_logs_no_match(tmp_path):
588+
"""Test cleanup_logs CLI when no directories match the filters."""
589+
fam_dir = tmp_path / "fam_a"
590+
fam_dir.mkdir()
591+
(fam_dir / "model_a.1").mkdir()
592+
593+
runner = CliRunner()
594+
result = runner.invoke(
595+
cli,
596+
[
597+
"cleanup",
598+
"--log-dir",
599+
str(tmp_path),
600+
"--model-family",
601+
"fam_b",
602+
],
603+
)
604+
605+
assert result.exit_code == 0
606+
assert "No matching log directories were deleted." in result.output
607+
assert (fam_dir / "model_a.1").exists()

tests/vec_inf/client/test_api.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,74 @@ def test_wait_until_ready():
128128
assert result.server_status == ModelStatus.READY
129129
assert result.base_url == "http://gpu123:8080/v1"
130130
assert mock_status.call_count == 2
131+
132+
133+
def test_cleanup_logs_no_match(tmp_path):
134+
"""Test when cleanup_logs returns empty list."""
135+
fam_a = tmp_path / "fam_a"
136+
model_a = fam_a / "model_a.999"
137+
model_a.mkdir(parents=True)
138+
139+
client = VecInfClient()
140+
deleted = client.cleanup_logs(
141+
log_dir=tmp_path,
142+
model_family="fam_b",
143+
dry_run=False,
144+
)
145+
146+
assert deleted == []
147+
assert fam_a.exists()
148+
assert model_a.exists()
149+
150+
151+
def test_cleanup_logs_deletes_matching_dirs(tmp_path):
152+
"""Test that cleanup_logs deletes model directories matching filters."""
153+
fam_a = tmp_path / "fam_a"
154+
fam_a.mkdir()
155+
156+
model_a_1 = fam_a / "model_a.10"
157+
model_a_2 = fam_a / "model_a.20"
158+
model_b = fam_a / "model_b.30"
159+
160+
model_a_1.mkdir()
161+
model_a_2.mkdir()
162+
model_b.mkdir()
163+
164+
client = VecInfClient()
165+
deleted = client.cleanup_logs(
166+
log_dir=tmp_path,
167+
model_family="fam_a",
168+
model_name="model_a",
169+
before_job_id=15,
170+
dry_run=False,
171+
)
172+
173+
assert deleted == [model_a_1]
174+
assert not model_a_1.exists()
175+
assert model_a_2.exists()
176+
assert model_b.exists()
177+
178+
179+
def test_cleanup_logs_matching_dirs_dry_run(tmp_path):
180+
"""Test that cleanup_logs find model directories matching filters."""
181+
fam_a = tmp_path / "fam_a"
182+
fam_a.mkdir()
183+
184+
model_a_1 = fam_a / "model_a.10"
185+
model_a_2 = fam_a / "model_a.20"
186+
187+
model_a_1.mkdir()
188+
model_a_2.mkdir()
189+
190+
client = VecInfClient()
191+
deleted = client.cleanup_logs(
192+
log_dir=tmp_path,
193+
model_family="fam_a",
194+
model_name="model_a",
195+
before_job_id=15,
196+
dry_run=True,
197+
)
198+
199+
assert deleted == [model_a_1]
200+
assert model_a_1.exists()
201+
assert model_a_2.exists()

tests/vec_inf/client/test_utils.py

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -213,28 +213,28 @@ def test_load_config_invalid_user_model(tmp_path):
213213

214214
def test_find_matching_dirs_only_model_family(tmp_path):
215215
"""Return model_family directory when only model_family is provided."""
216-
fam_dir = tmp_path / "famA"
216+
fam_dir = tmp_path / "fam_a"
217217
fam_dir.mkdir()
218-
(fam_dir / "modelA.1").mkdir()
219-
(fam_dir / "modelB.2").mkdir()
218+
(fam_dir / "model_a.1").mkdir()
219+
(fam_dir / "model_b.2").mkdir()
220220

221-
other_dir = tmp_path / "famB"
221+
other_dir = tmp_path / "fam_b"
222222
other_dir.mkdir()
223-
(other_dir / "modelC.3").mkdir()
223+
(other_dir / "model_c.3").mkdir()
224224

225-
matches = find_matching_dirs(log_dir=tmp_path, model_family="famA")
225+
matches = find_matching_dirs(log_dir=tmp_path, model_family="fam_a")
226226
assert len(matches) == 1
227-
assert matches[0].name == "famA"
227+
assert matches[0].name == "fam_a"
228228

229229

230230
def test_find_matching_dirs_only_model_name(tmp_path):
231231
"""Return directories matching when only model_name is provided."""
232-
fam_a = tmp_path / "famA"
232+
fam_a = tmp_path / "fam_a"
233233
fam_a.mkdir()
234234
(fam_a / "target.1").mkdir()
235235
(fam_a / "other.2").mkdir()
236236

237-
fam_b = tmp_path / "famB"
237+
fam_b = tmp_path / "fam_b"
238238
fam_b.mkdir()
239239
(fam_b / "different.3").mkdir()
240240

@@ -250,53 +250,53 @@ def test_find_matching_dirs_only_job_id(tmp_path):
250250
"""Return directories matching exact job_id."""
251251
fam_dir = tmp_path / "fam"
252252
fam_dir.mkdir()
253-
(fam_dir / "modelA.10").mkdir()
254-
(fam_dir / "modelB.20").mkdir()
255-
(fam_dir / "modelC.30").mkdir()
253+
(fam_dir / "model_a.10").mkdir()
254+
(fam_dir / "model_b.20").mkdir()
255+
(fam_dir / "model_c.30").mkdir()
256256

257257
matches = find_matching_dirs(log_dir=tmp_path, job_id=10)
258258
result_names = [p.name for p in matches]
259259

260-
assert "modelA.10" in result_names
261-
assert "modelB.20" not in result_names
262-
assert "modelC.30" not in result_names
260+
assert "model_a.10" in result_names
261+
assert "model_b.20" not in result_names
262+
assert "model_c.30" not in result_names
263263

264264

265265
def test_find_matching_dirs_only_before_job_id(tmp_path):
266266
"""Return directories with job_id < before_job_id."""
267-
fam_dir = tmp_path / "famA"
267+
fam_dir = tmp_path / "fam_a"
268268
fam_dir.mkdir()
269-
(fam_dir / "modelA.1").mkdir()
270-
(fam_dir / "modelA.5").mkdir()
271-
(fam_dir / "modelA.100").mkdir()
269+
(fam_dir / "model_a.1").mkdir()
270+
(fam_dir / "model_a.5").mkdir()
271+
(fam_dir / "model_a.100").mkdir()
272272

273-
fam_dir = tmp_path / "famB"
273+
fam_dir = tmp_path / "fam_b"
274274
fam_dir.mkdir()
275-
(fam_dir / "modelB.30").mkdir()
275+
(fam_dir / "model_b.30").mkdir()
276276

277277
matches = find_matching_dirs(log_dir=tmp_path, before_job_id=50)
278278
result_names = [p.name for p in matches]
279279

280-
assert "modelA.1" in result_names
281-
assert "modelA.5" in result_names
282-
assert "modelA.100" not in result_names
283-
assert "modelB.30" in result_names
280+
assert "model_a.1" in result_names
281+
assert "model_a.5" in result_names
282+
assert "model_a.100" not in result_names
283+
assert "model_b.30" in result_names
284284

285285

286286
def test_find_matching_dirs_family_and_before_job_id(tmp_path):
287287
"""Return directories under a given family with job IDs less than before_job_id."""
288288
fam_dir = tmp_path / "targetfam"
289289
fam_dir.mkdir()
290-
(fam_dir / "modelA.10").mkdir()
291-
(fam_dir / "modelA.20").mkdir()
292-
(fam_dir / "modelA.99").mkdir()
293-
(fam_dir / "modelA.150").mkdir()
290+
(fam_dir / "model_a.10").mkdir()
291+
(fam_dir / "model_a.20").mkdir()
292+
(fam_dir / "model_a.99").mkdir()
293+
(fam_dir / "model_a.150").mkdir()
294294

295295
other_fam = tmp_path / "otherfam"
296296
other_fam.mkdir()
297-
(other_fam / "modelB.5").mkdir()
298-
(other_fam / "modelB.10").mkdir()
299-
(other_fam / "modelB.100").mkdir()
297+
(other_fam / "model_b.5").mkdir()
298+
(other_fam / "model_b.10").mkdir()
299+
(other_fam / "model_b.100").mkdir()
300300

301301
matches = find_matching_dirs(
302302
log_dir=tmp_path,
@@ -306,38 +306,38 @@ def test_find_matching_dirs_family_and_before_job_id(tmp_path):
306306

307307
result_names = [p.name for p in matches]
308308

309-
assert "modelA.10" in result_names
310-
assert "modelA.20" in result_names
311-
assert "modelA.99" in result_names
312-
assert "modelA.150" not in result_names
309+
assert "model_a.10" in result_names
310+
assert "model_a.20" in result_names
311+
assert "model_a.99" in result_names
312+
assert "model_a.150" not in result_names
313313
assert all("otherfam" not in str(p) for p in matches)
314314

315315

316316
def test_find_matching_dirs_with_family_model_name_and_before_job_id(tmp_path):
317317
"""Return matching dirs with model_family, model_name, and before_job_id filters."""
318318
fam_dir = tmp_path / "targetfam"
319319
fam_dir.mkdir()
320-
(fam_dir / "modelA.1").mkdir()
321-
(fam_dir / "modelA.50").mkdir()
322-
(fam_dir / "modelA.150").mkdir()
323-
(fam_dir / "modelB.40").mkdir()
320+
(fam_dir / "model_a.1").mkdir()
321+
(fam_dir / "model_a.50").mkdir()
322+
(fam_dir / "model_a.150").mkdir()
323+
(fam_dir / "model_b.40").mkdir()
324324

325325
other_fam = tmp_path / "otherfam"
326326
other_fam.mkdir()
327-
(other_fam / "modelC.20").mkdir()
327+
(other_fam / "model_c.20").mkdir()
328328

329329
matches = find_matching_dirs(
330330
log_dir=tmp_path,
331331
model_family="targetfam",
332-
model_name="modelA",
332+
model_name="model_a",
333333
before_job_id=100,
334334
)
335335

336336
result_names = [p.name for p in matches]
337337

338-
assert "modelA.1" in result_names
339-
assert "modelA.50" in result_names
340-
assert "modelA.150" not in result_names
341-
assert "modelB.40" not in result_names
342-
assert all("modelB" not in p for p in result_names)
338+
assert "model_a.1" in result_names
339+
assert "model_a.50" in result_names
340+
assert "model_a.150" not in result_names
341+
assert "model_b.40" not in result_names
342+
assert all("model_b" not in p for p in result_names)
343343
assert all("otherfam" not in str(p) for p in matches)

0 commit comments

Comments
 (0)