package agent import ( "log/slog" "path/filepath" "slices" "testing" llmgwv1 "gitea.mynodes.xyz/ilker/llmgw/api/gen/llmgw/v1" "gitea.mynodes.xyz/ilker/llmgw/internal/tlsconf" ) func TestAControlPlaneTheLeaderNamesIsDialledAndRemembered(t *testing.T) { old := controlPlanesFile controlPlanesFile = filepath.Join(t.TempDir(), "control-planes.json") t.Cleanup(func() { controlPlanesFile = old }) configured := []string{"https://10.0.0.140:7443"} // TLS on, as every onboarded node has it: an agent without it refuses // https:// addresses, and the leader's are https://. a := &Agent{cfg: Config{Servers: configured, TLS: tlsconf.Config{CA: "/etc/llmgw/ca.pem"}}, log: slog.New(slog.DiscardHandler)} if got := a.servers(); !slices.Equal(got, configured) { t.Fatalf("before anything is named: %v", got) } // The configured one stays first; the new one is added after it, once. a.setControlPlanes(&llmgwv1.ControlPlanes{Servers: []string{"https://10.0.0.140:7443", "https://10.0.0.150:7443"}}) want := []string{"https://10.0.0.140:7443", "https://10.0.0.150:7443"} if got := a.servers(); !slices.Equal(got, want) { t.Errorf("after the leader named two: %v", got) } // And after a restart, before the leader says anything. if got := loadControlPlanes(configured, true); !slices.Equal(got, want) { t.Errorf("after a restart: %v", got) } // A list that drops the configured one does not drop it: the // configuration is the operator's. a.setControlPlanes(&llmgwv1.ControlPlanes{Servers: []string{"https://10.0.0.150:7443"}}) if got := a.servers(); got[0] != "https://10.0.0.140:7443" { t.Errorf("the configured server was dropped: %v", got) } }