Files
ThothII/scripts/test-internal-semantic-compose.sh

149 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT HUP INT TERM
docker compose --env-file deploy/env/local.env.example \
-f compose.yaml -f deploy/compose.local.yaml -f deploy/compose.embedding-gpu.yaml \
config --format json >"$tmp/compose-gpu.json"
node - "$tmp/compose-gpu.json" <<'NODE'
const fs = require("fs");
const config = JSON.parse(fs.readFileSync(process.argv[2], "utf8"));
const devices = config.services.embedding?.deploy?.resources?.reservations?.devices;
if (!Array.isArray(devices) || devices.length !== 1) {
throw new Error("GPU override must add one embedding device reservation");
}
const [device] = devices;
if (JSON.stringify(device.capabilities) !== JSON.stringify(["gpu"])) {
throw new Error("GPU override must request gpu capability only");
}
NODE
mock_bin="$tmp/mock-bin"
mkdir -p "$mock_bin"
cat >"$mock_bin/ollama" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
state_dir=${MOCK_STATE_DIR:?}
printf '%s\n' "$*" >>"$state_dir/ollama-calls"
if [[ "$1" != "pull" ]]; then
echo "unexpected ollama command: $*" >&2
exit 1
fi
cat >"$state_dir/tags.json" <<JSON
{"models":[{"name":"${2}"}]}
JSON
EOF
chmod +x "$mock_bin/ollama"
cat >"$tmp/mock-tags-server.py" <<'PY'
import http.server
import os
from pathlib import Path
state_dir = Path(os.environ["MOCK_STATE_DIR"])
port_file = Path(os.environ["MOCK_PORT_FILE"])
class Handler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path != "/api/tags":
self.send_response(404)
self.end_headers()
return
count_file = state_dir / "curl-count"
count = int(count_file.read_text() or "0") if count_file.exists() else 0
count += 1
count_file.write_text(str(count))
fail_until = int((state_dir / "fail-until").read_text()) if (state_dir / "fail-until").exists() else 0
if count <= fail_until:
self.send_response(503)
self.end_headers()
self.wfile.write(b'{"models":[]}')
return
payload = (state_dir / "tags.json").read_bytes()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
def log_message(self, format, *args):
return
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler)
port_file.write_text(str(server.server_address[1]))
server.serve_forever()
PY
start_server() {
local state_dir=$1
local port_file="$state_dir/port"
MOCK_STATE_DIR="$state_dir" MOCK_PORT_FILE="$port_file" \
python3 "$tmp/mock-tags-server.py" >/dev/null 2>&1 &
local server_pid=$!
for _ in $(seq 1 50); do
[[ -f "$port_file" ]] && break
sleep 0.1
done
[[ -f "$port_file" ]] || {
echo "mock tags server did not start" >&2
kill "$server_pid" >/dev/null 2>&1 || true
exit 1
}
printf '%s %s\n' "$server_pid" "$(cat "$port_file")"
}
run_cached() {
local state_dir="$tmp/cached"
mkdir -p "$state_dir"
cat >"$state_dir/tags.json" <<'JSON'
{"models":[{"name":"qwen3-embedding:0.6b"}]}
JSON
read -r server_pid port < <(start_server "$state_dir")
PATH="$mock_bin:$PATH" \
MOCK_STATE_DIR="$state_dir" \
OLLAMA_BASE_URL="http://127.0.0.1:$port" \
OLLAMA_MODEL="qwen3-embedding:0.6b" \
OLLAMA_WAIT_TIMEOUT_SEC=2 \
./docker/embedding-model-init.sh
kill "$server_pid" >/dev/null 2>&1 || true
wait "$server_pid" 2>/dev/null || true
if [[ -e "$state_dir/ollama-calls" ]]; then
echo "cached bootstrap must not call ollama pull" >&2
exit 1
fi
}
run_pull() {
local state_dir="$tmp/pull"
mkdir -p "$state_dir"
cat >"$state_dir/tags.json" <<'JSON'
{"models":[]}
JSON
printf '2' >"$state_dir/fail-until"
read -r server_pid port < <(start_server "$state_dir")
PATH="$mock_bin:$PATH" \
MOCK_STATE_DIR="$state_dir" \
OLLAMA_BASE_URL="http://127.0.0.1:$port" \
OLLAMA_MODEL="qwen3-embedding:0.6b" \
OLLAMA_WAIT_TIMEOUT_SEC=5 \
./docker/embedding-model-init.sh
kill "$server_pid" >/dev/null 2>&1 || true
wait "$server_pid" 2>/dev/null || true
grep -qx 'pull qwen3-embedding:0.6b' "$state_dir/ollama-calls"
}
run_cached
run_pull
echo "internal semantic Compose/script contracts passed."