package agent import ( "slices" "strings" "testing" "gitea.mynodes.xyz/ilker/llmgw/internal/tlsconf" ) func normalized(t *testing.T, c Config) Config { t.Helper() if err := c.normalizeServerURL(); err != nil { t.Fatalf("normalising %+v: %v", c, err) } return c } func TestOneServerStillWorksExactlyAsBefore(t *testing.T) { // Every agent configuration written before there could be a second control // plane names one address, and none of them should have to change. c := normalized(t, Config{Server: "10.0.0.140:7443"}) if !slices.Equal(c.Servers, []string{"http://10.0.0.140:7443"}) { t.Errorf("servers = %v", c.Servers) } if c.Server != "http://10.0.0.140:7443" { t.Errorf("server = %q", c.Server) } } func TestTheAddressAlreadyThereStaysPreferred(t *testing.T) { // An operator adding a standby to an existing file expects the control // plane that was already working to keep being tried first. c := normalized(t, Config{ Server: "first:7443", Servers: []string{"second:7443", "third:7443"}, }) want := []string{"http://first:7443", "http://second:7443", "http://third:7443"} if !slices.Equal(c.Servers, want) { t.Errorf("servers = %v, want %v", c.Servers, want) } } func TestADuplicateAddressIsNotTriedTwiceInARow(t *testing.T) { // Which would look exactly like the rotation not working. c := normalized(t, Config{ Server: "a:7443", Servers: []string{"http://a:7443", "b:7443"}, }) want := []string{"http://a:7443", "http://b:7443"} if !slices.Equal(c.Servers, want) { t.Errorf("servers = %v, want %v", c.Servers, want) } } func TestEveryAddressIsCheckedAgainstTheTLSSetting(t *testing.T) { // The check existed for one address and has to hold for all of them: a // standby reached over plaintext while the leader is authenticated would // be a way around mutual TLS, not a fallback. c := Config{ Servers: []string{"https://a:7443", "http://b:7443"}, TLS: tlsconf.Config{CA: "ca.pem", Cert: "c.pem", Key: "k.pem"}, } err := c.normalizeServerURL() if err == nil { t.Fatal("a plaintext standby was accepted alongside TLS") } if !strings.Contains(err.Error(), "b:7443") { t.Errorf("the error does not say which address: %v", err) } } func TestNoServerAtAllIsStillAnError(t *testing.T) { c := Config{} if err := c.normalizeServerURL(); err == nil { t.Error("an agent with no control plane configured was accepted") } } func TestSchemeIsAddedPerAddress(t *testing.T) { c := normalized(t, Config{ Servers: []string{"a:7443", "b:7443"}, TLS: tlsconf.Config{CA: "ca.pem", Cert: "c.pem", Key: "k.pem"}, }) for _, s := range c.Servers { if !strings.HasPrefix(s, "https://") { t.Errorf("%q did not get the secure scheme", s) } } }