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.
This commit is contained in:
2026-08-02 00:51:28 +02:00
parent 726800a366
commit 6ade221621
2 changed files with 49 additions and 8 deletions
+48
View File
@@ -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)
}
}
+1 -8
View File
@@ -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 {