From 6ade221621c1d27bb260318002ac469f114fdde0 Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Sun, 2 Aug 2026 00:51:28 +0200 Subject: [PATCH] server: build status payload from expanded machines StatusPayload was re-resolving grouped machines from the raw cfg.Groups configs, so glob hosts like a3-dott{1..12} showed up as a single machine with an unresolved pattern host and no probe state. Iterate the expanded cfg.Machines once, bucketing by group. Adds TestStatusPayloadExpandsGlobs regression test. --- server/config_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++ server/monitor.go | 9 +------- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/server/config_test.go b/server/config_test.go index b550f62..5e4e4b5 100644 --- a/server/config_test.go +++ b/server/config_test.go @@ -1,6 +1,8 @@ package main import ( + "os" + "path/filepath" "reflect" "strings" "testing" @@ -117,3 +119,49 @@ func TestExpandHostPattern(t *testing.T) { } } } + +func TestStatusPayloadExpandsGlobs(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + cfg := ` +groups: + - name: Aula 3 + machines: + - host: a3-dott{1..3}.cs.dm.unipi.it +machines: + - host: router.example.net +` + if err := os.WriteFile(path, []byte(cfg), 0o644); err != nil { + t.Fatal(err) + } + conf, err := LoadConfig(path) + if err != nil { + t.Fatal(err) + } + mon := NewMonitor(conf, nil) + p := mon.StatusPayload() + + if len(p.Groups) != 1 { + t.Fatalf("want 1 group, got %d", len(p.Groups)) + } + g := p.Groups[0] + want := []string{ + "a3-dott1.cs.dm.unipi.it", + "a3-dott2.cs.dm.unipi.it", + "a3-dott3.cs.dm.unipi.it", + } + if len(g.Machines) != len(want) { + t.Fatalf("group machines: want %d, got %d", len(want), len(g.Machines)) + } + for i, m := range g.Machines { + if m.Host != want[i] { + t.Errorf("machine %d: host %q, want %q", i, m.Host, want[i]) + } + } + if len(p.Machines) != 1 { + t.Fatalf("top-level machines: want 1, got %d", len(p.Machines)) + } + if p.Machines[0].Host != "router.example.net" { + t.Errorf("top-level host: %q", p.Machines[0].Host) + } +} diff --git a/server/monitor.go b/server/monitor.go index 6027cde..00d439b 100644 --- a/server/monitor.go +++ b/server/monitor.go @@ -658,15 +658,8 @@ func (m *Monitor) StatusPayload() statusPayload { grouped[group] = append(grouped[group], ms) } - for _, g := range m.cfg.Groups { - for _, mc := range g.Machines { - appendStatus(resolveMachine(mc, g.Name, m.cfg), g.Name) - } - } for _, mc := range m.cfg.Machines { - if mc.Group == "" { - appendStatus(mc, "") - } + appendStatus(mc, mc.Group) } for _, name := range order {