Files
orchestra/internal/registry/registry_test.go
T
2026-07-30 14:57:25 +04:00

47 lines
1.7 KiB
Go

package registry
import (
"errors"
"testing"
"time"
)
type reach map[string]bool
func (r reach) Reachable(a string, _ time.Duration) bool { return r[a] }
func TestNewValidatesTopologyAndResolvesHardAffinity(t *testing.T) {
r, err := New(Config{
Projects: []Project{{ID: "work", MachineAffinity: []string{"pc"}}},
Machines: []Machine{{ID: "server", Address: "server:1"}, {ID: "pc", Address: "pc:1"}},
Herdrs: []Herdr{{ID: "offline", MachineID: "pc", Address: "off:1"}, {ID: "online", MachineID: "pc", Address: "on:1"}, {ID: "wrong", MachineID: "server", Address: "server:1"}},
})
if err != nil {
t.Fatal(err)
}
got, err := r.Candidates("work", reach{"off:1": false, "on:1": true, "server:1": true}, time.Second)
if err != nil || len(got) != 1 || got[0].ID != "online" {
t.Fatalf("candidates=%v err=%v", got, err)
}
if _, err = r.Candidates("missing", nil, time.Second); !errors.Is(err, ErrUnknownProject) {
t.Fatalf("err=%v", err)
}
}
func TestNewRejectsBrokenReferences(t *testing.T) {
_, err := New(Config{Projects: []Project{{ID: "p", MachineAffinity: []string{"missing"}}}})
if !errors.Is(err, ErrUnknownMachine) {
t.Fatalf("err=%v", err)
}
}
func TestProjectSafeOperationsAreNarrowAndAudited(t *testing.T) {
_, err := New(Config{Projects: []Project{{ID: "p", MachineAffinity: []string{"m"}, SafeOperations: []string{"read", "network"}}}, Machines: []Machine{{ID: "m", Address: "m:1"}}})
if err == nil {
t.Fatal("network operation was accepted into no-grant policy")
}
if _, err := New(Config{Projects: []Project{{ID: "p", MachineAffinity: []string{"m"}, SafeOperations: []string{"read", "edit", "test", "git"}}}, Machines: []Machine{{ID: "m", Address: "m:1"}}}); err != nil {
t.Fatalf("safe policy rejected: %v", err)
}
}