Files
llmgw/internal/config/config_test.go
T
Ilker ManapandClaude Opus 5.5 7d97c6b74f Install a node's engines on the node, instead of trusting what is there
A node ran whatever llama-server somebody had once unpacked on it. Onboarding
checked an engine only when told its path, installed none, and a node with
nothing on it registered as ready. The one time that went wrong it went wrong
invisibly: the binary was there, executable, and could not start for want of
libgomp1, while the node reported healthy.

The control plane now says which engines every node has, at which version —
`engines:` in server.yaml, defaulting to the llama.cpp build this release was
checked against — and each agent converges on it. What only the node can
know, the node works out: the build for its processor and driver (CUDA with
its runtime when an NVIDIA driver is loaded, CPU otherwise), the OS packages
that build needs from its own package sources, and whether the result runs.
Every archive is checked against the sha256 GitHub publishes for it and
unpacked refusing any path or link that leaves the install directory. ldd
names a missing library outright; running `--version` proves the rest. Only
then is a directory marked installed, and it is checked again every ten
minutes, because an unrelated upgrade can remove a library.

An engine is reported only once installed and seen to run, or when the
agent's configuration names it on purpose — never because something happened
to be on PATH. The agent now says when it has probed, so an empty list means
"has none" rather than "too old to say": a node rebuilt from nothing is no
longer a candidate for every model at the moment it can run none of them.
Placement's refusal carries the node's own words, "installing: downloading
llama-b10991-bin-ubuntu-x64.tar.gz (16 MB)", and so do `ctl nodes` and the
cluster page, along with a warning for a CPU without AVX2 — the VM CPU type
that makes everything five times slower and nothing look wrong.

Agents also report their OS, architecture, vector extensions and NVIDIA
driver. Onboarding installs llmgw itself from the CI release for the node's
architecture, checked against the release's checksums, rather than copying
whatever binary the operator's machine had.

vLLM and SGLang are not installed yet: they are Python environments of
several gigabytes, not an archive, and still have to be named in the agent's
configuration.

Also fixes a race on the weights fingerprint, which two heartbeat loops touch
during a failover.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 10:59:59 +02:00

202 lines
5.7 KiB
Go

package config
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog"
)
func write(t *testing.T, content string) string {
t.Helper()
p := filepath.Join(t.TempDir(), "server.yaml")
if err := os.WriteFile(p, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
return p
}
func TestLoadDefaultsWithoutFile(t *testing.T) {
cfg, err := Load("")
if err != nil {
t.Fatal(err)
}
if cfg.Listen != ":8080" || cfg.Router.DefaultProfile != "chat" {
t.Errorf("defaults not applied: %+v", cfg)
}
}
func TestLoadOverlaysOnDefaults(t *testing.T) {
// Specifying one field must not blank out its neighbours.
p := write(t, `
listen: ":9999"
endpoints:
- id: a
url: http://127.0.0.1:8000
models: [m]
`)
cfg, err := Load(p)
if err != nil {
t.Fatal(err)
}
if cfg.Listen != ":9999" {
t.Errorf("listen=%q", cfg.Listen)
}
if cfg.Router.ScrapeInterval != 2*time.Second {
t.Errorf("an unspecified default was lost: %v", cfg.Router.ScrapeInterval)
}
if len(cfg.Endpoints) != 1 || cfg.Endpoints[0].ID != "a" {
t.Errorf("endpoints=%+v", cfg.Endpoints)
}
}
func TestDurationsParse(t *testing.T) {
p := write(t, `
router:
scrape_interval: 500ms
timeouts:
idle: 45s
`)
cfg, err := Load(p)
if err != nil {
t.Fatal(err)
}
if cfg.Router.ScrapeInterval != 500*time.Millisecond {
t.Errorf("scrape_interval=%v", cfg.Router.ScrapeInterval)
}
if cfg.Router.Timeouts.Idle != 45*time.Second {
t.Errorf("timeouts.idle=%v", cfg.Router.Timeouts.Idle)
}
}
func TestEnvOverride(t *testing.T) {
t.Setenv("LLMGW_ROUTER__RETRIES", "7")
cfg, err := Load("")
if err != nil {
t.Fatal(err)
}
if cfg.Router.Retries != 7 {
t.Errorf("retries=%d, expected the environment to win", cfg.Router.Retries)
}
}
func TestMissingFileIsAnError(t *testing.T) {
if _, err := Load("/nonexistent/server.yaml"); err == nil {
t.Error("expected an error for a missing config file")
}
}
func TestValidateRejectsBadEndpoints(t *testing.T) {
cases := []struct {
name string
ep Endpoint
want string
}{
{"no id", Endpoint{URL: "http://x", Models: []string{"m"}}, "id is required"},
{"no url", Endpoint{ID: "a", Models: []string{"m"}}, "url is required"},
{"bad scheme", Endpoint{ID: "a", URL: "127.0.0.1:8000", Models: []string{"m"}}, "http://"},
{"no models", Endpoint{ID: "a", URL: "http://x"}, "at least one model"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
s := Default()
s.Endpoints = []Endpoint{c.ep}
err := s.Validate()
if err == nil {
t.Fatal("expected an error")
}
if !strings.Contains(err.Error(), c.want) {
t.Errorf("error %q does not mention %q", err, c.want)
}
})
}
}
func TestValidateRejectsDuplicateEndpointID(t *testing.T) {
s := Default()
s.Endpoints = []Endpoint{
{ID: "a", URL: "http://x", Models: []string{"m"}},
{ID: "a", URL: "http://y", Models: []string{"m"}},
}
if err := s.Validate(); err == nil || !strings.Contains(err.Error(), "twice") {
t.Errorf("duplicate id not rejected: %v", err)
}
}
func TestValidateRejectsAliasCollision(t *testing.T) {
// An alias shadowing another model would silently misroute every request
// for that model, so it has to fail at load time.
s := Default()
s.Models = []catalog.Model{
{Name: "llama-3.3-70b", Source: "/m", Aliases: []string{"qwen"},
Profiles: []catalog.Profile{{Name: "p", GPUClass: "h100-80g", TP: 1}}},
{Name: "qwen", Source: "/m",
Profiles: []catalog.Profile{{Name: "p", GPUClass: "h100-80g", TP: 1}}},
}
err := s.Validate()
if err == nil || !strings.Contains(err.Error(), "collides") {
t.Errorf("alias collision not rejected: %v", err)
}
}
func TestValidateAcceptsDistinctAliases(t *testing.T) {
s := Default()
s.Models = []catalog.Model{
{Name: "llama-3.3-70b", Source: "/m", Aliases: []string{"llama", "llama3"},
Profiles: []catalog.Profile{{Name: "p", GPUClass: "h100-80g", TP: 1}}},
{Name: "qwen2.5-32b", Source: "/m", Aliases: []string{"qwen"},
Profiles: []catalog.Profile{{Name: "p", GPUClass: "h100-80g", TP: 1}}},
}
if err := s.Validate(); err != nil {
t.Errorf("valid aliases rejected: %v", err)
}
}
func TestAControlPlaneCanBeAskedForWithNoModelsInTheFile(t *testing.T) {
// Without this, a deployment whose catalog lives entirely in the database
// cannot start: the mode is inferred from the file, an empty models block
// reads as "router only", and the process refuses to start for want of
// anything to serve — so the stored models it would have loaded are never
// read at all.
var s Server
if s.ControlPlaneMode() {
t.Fatal("an empty configuration is not a control plane")
}
s.Control.Enabled = true
if !s.ControlPlaneMode() {
t.Error("control.enabled should run the control plane with no models declared")
}
}
func TestAModelWithProfilesStillMakesAControlPlane(t *testing.T) {
// The inference stays, so no existing configuration has to add a flag.
var s Server
s.Models = []catalog.Model{{
Name: "m",
Profiles: []catalog.Profile{{Name: "p", Engine: "llamacpp"}},
}}
if !s.ControlPlaneMode() {
t.Error("a declared model with profiles is still a control plane")
}
}
func TestEnginesDefaultToTheCheckedVersions(t *testing.T) {
var s Server
if got := s.WantedEngines()["llamacpp"]; got != DefaultEngines["llamacpp"] {
t.Errorf("unset config wants llamacpp %q", got)
}
s.Engines = map[string]string{"llamacpp": "b11000", "vllm": "0.29.0"}
got := s.WantedEngines()
if got["llamacpp"] != "b11000" || got["vllm"] != "0.29.0" {
t.Errorf("configured versions not taken: %v", got)
}
// Empty leaves an engine to be installed by hand.
s.Engines = map[string]string{"llamacpp": ""}
if _, ok := s.WantedEngines()["llamacpp"]; ok {
t.Error("an emptied engine is still wanted")
}
}