generated from mathias/template-go-web
MergeSubstrate overlays live node specs onto the authored machine list by name: cluster nodes (koala) get fresh live specs, non-cluster machines (iguana/flamingo/ piguard) stay authored, new live nodes are appended. Test-first. Restores the full homelab machine list while keeping cluster nodes truthful. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
70 lines
2.3 KiB
Go
70 lines
2.3 KiB
Go
package atlas_test
|
||
|
||
import (
|
||
"reflect"
|
||
"testing"
|
||
|
||
"git.d-ma.be/mathias/cad-atlas/internal/atlas"
|
||
)
|
||
|
||
func TestHostsFromNodes_DerivesSpecFromNodeStatus(t *testing.T) {
|
||
nodes := []byte(`{"items":[
|
||
{"metadata":{"name":"koala"},
|
||
"status":{"capacity":{"cpu":"16","memory":"67108864Ki","nvidia.com/gpu":"1"},
|
||
"nodeInfo":{"architecture":"amd64","kubeletVersion":"v1.31.4+k3s1"}}},
|
||
{"metadata":{"name":"worker2"},
|
||
"status":{"capacity":{"cpu":"8","memory":"33554432Ki"},
|
||
"nodeInfo":{"architecture":"arm64","kubeletVersion":"v1.30.0+k3s1"}}}
|
||
]}`)
|
||
|
||
hosts, err := atlas.HostsFromNodes(nodes)
|
||
if err != nil {
|
||
t.Fatalf("HostsFromNodes: %v", err)
|
||
}
|
||
|
||
want := []atlas.Host{
|
||
{Name: "koala", Spec: "amd64 · 16 cpu · 64Gi · 1× GPU · k3s v1.31.4"},
|
||
{Name: "worker2", Spec: "arm64 · 8 cpu · 32Gi · k3s v1.30.0"},
|
||
}
|
||
if !reflect.DeepEqual(hosts, want) {
|
||
t.Fatalf("hosts = %+v\nwant %+v", hosts, want)
|
||
}
|
||
}
|
||
|
||
func TestMergeSubstrate_LiveOverridesAuthoredKeepsRestAppendsNew(t *testing.T) {
|
||
authored := []atlas.Host{
|
||
{Name: "koala", Spec: "RTX 5070 · authored"},
|
||
{Name: "iguana", Spec: "M2 Ultra · Ollama / mlx"},
|
||
{Name: "piguard", Spec: "NGINX reverse-proxy · ntfy"},
|
||
}
|
||
live := []atlas.Host{
|
||
{Name: "koala", Spec: "amd64 · 16 cpu · 60Gi · 1× GPU · k3s v1.34.5"},
|
||
{Name: "worker9", Spec: "arm64 · 4 cpu"},
|
||
}
|
||
|
||
got := atlas.MergeSubstrate(authored, live)
|
||
want := []atlas.Host{
|
||
{Name: "koala", Spec: "amd64 · 16 cpu · 60Gi · 1× GPU · k3s v1.34.5"}, // live wins
|
||
{Name: "iguana", Spec: "M2 Ultra · Ollama / mlx"}, // authored kept
|
||
{Name: "piguard", Spec: "NGINX reverse-proxy · ntfy"}, // authored kept
|
||
{Name: "worker9", Spec: "arm64 · 4 cpu"}, // live-only appended
|
||
}
|
||
if !reflect.DeepEqual(got, want) {
|
||
t.Fatalf("merge = %+v\nwant %+v", got, want)
|
||
}
|
||
}
|
||
|
||
func TestMergeSubstrate_NoLiveReturnsAuthored(t *testing.T) {
|
||
authored := []atlas.Host{{Name: "koala", Spec: "authored"}}
|
||
got := atlas.MergeSubstrate(authored, nil)
|
||
if !reflect.DeepEqual(got, authored) {
|
||
t.Fatalf("merge with no live = %+v, want %+v", got, authored)
|
||
}
|
||
}
|
||
|
||
func TestHostsFromNodes_ErrorsOnBadJSON(t *testing.T) {
|
||
if _, err := atlas.HostsFromNodes([]byte("{not json")); err == nil {
|
||
t.Fatal("expected error on bad JSON, got nil")
|
||
}
|
||
}
|