ducklm/tests/smoke/test_llama_service_script.py

109 lines
3.5 KiB
Python

import os
import subprocess
import textwrap
import time
from pathlib import Path
def test_start_main_script_manages_pid_status_stop_and_logs(tmp_path):
fake_bin = tmp_path / "llama-server"
fake_bin.write_text(
textwrap.dedent(
"""\
#!/usr/bin/env bash
echo "fake llama-server $*" >&2
trap 'exit 0' TERM INT
while true; do sleep 1; done
"""
)
)
fake_bin.chmod(0o755)
pid_file = tmp_path / "llama.pid"
log_file = tmp_path / "llama.log"
env = {
**os.environ,
"DUCK_LLAMA_SERVER_BIN": str(fake_bin),
"DUCK_MAIN_MODEL_PATH": str(tmp_path / "model.gguf"),
"DUCK_LLAMA_PID_FILE": str(pid_file),
"DUCK_LLAMA_LOG_FILE": str(log_file),
"DUCK_MAIN_PORT": "18081",
}
Path(env["DUCK_MAIN_MODEL_PATH"]).write_text("fake")
script = "scripts/llama/start_main.sh"
stopped = subprocess.run([script, "status"], env=env, text=True, capture_output=True)
assert stopped.returncode == 3
assert "not running" in stopped.stdout
started = subprocess.run([script, "start"], env=env, text=True, capture_output=True)
assert started.returncode == 0
assert pid_file.exists()
try:
running = subprocess.run([script, "status"], env=env, text=True, capture_output=True)
assert running.returncode == 0
assert "running" in running.stdout
time.sleep(0.2)
logs = subprocess.run(
[script, "logs", "--lines", "20"], env=env, text=True, capture_output=True
)
assert logs.returncode == 0
assert "--alias local-main" in logs.stdout
finally:
stopped = subprocess.run([script, "stop"], env=env, text=True, capture_output=True)
assert stopped.returncode == 0
assert not pid_file.exists()
def test_start_main_script_sets_llama_bin_dir_library_path(tmp_path):
bin_dir = tmp_path / "build" / "bin"
bin_dir.mkdir(parents=True)
fake_bin = bin_dir / "llama-server"
fake_bin.write_text(
textwrap.dedent(
"""\
#!/usr/bin/env bash
case ":${LD_LIBRARY_PATH:-}:" in
*":$(dirname "$0"):"*) ;;
*)
echo "error while loading shared libraries: libllama-common.so.0" >&2
exit 127
;;
esac
echo "fake llama-server $*" >&2
trap 'exit 0' TERM INT
while true; do sleep 1; done
"""
)
)
fake_bin.chmod(0o755)
model_path = tmp_path / "model.gguf"
model_path.write_text("fake")
pid_file = tmp_path / "llama.pid"
log_file = tmp_path / "llama.log"
env = {
**os.environ,
"LD_LIBRARY_PATH": "",
"DUCK_LLAMA_SERVER_BIN": str(fake_bin),
"DUCK_MAIN_MODEL_PATH": str(model_path),
"DUCK_LLAMA_PID_FILE": str(pid_file),
"DUCK_LLAMA_LOG_FILE": str(log_file),
"DUCK_MAIN_PORT": "18081",
}
script = "scripts/llama/start_main.sh"
started = subprocess.run([script, "start"], env=env, text=True, capture_output=True)
assert started.returncode == 0
try:
logs = subprocess.run(
[script, "logs", "--lines", "20"], env=env, text=True, capture_output=True
)
assert logs.returncode == 0
assert "--alias local-main" in logs.stdout
assert "error while loading shared libraries" not in logs.stdout
finally:
subprocess.run([script, "stop"], env=env, text=True, capture_output=True)