package config import ( "fmt" "strings" "testing" ) func TestValidateRoutingCheckTargets(t *testing.T) { source := strings.Replace(validConfig, " strategy:", " check:\n targets: [https://checkout.example/health]\n strategy:", 1) loaded, err := Load(strings.NewReader(source)) if err != nil || len(loaded.Routing[0].Check.Targets) != 1 || loaded.Routing[0].Check.Targets[0] != "https://checkout.example/health" { t.Fatalf("Load(routing check targets) = (%+v, %v)", loaded, err) } cfg := mustLoadValidConfig(t) route := cfg.Routing[0] route.Check.Targets = []string{"https://checkout.example/health"} cfg.Routing[0] = route if err := Validate(cfg); err != nil { t.Fatalf("Validate(routing check targets) = %v", err) } redacted := cfg.Redacted() redacted.Routing[0].Check.Targets[0] = "https://changed.example/health" if cfg.Routing[0].Check.Targets[0] == redacted.Routing[0].Check.Targets[0] { t.Fatal("Redacted() routing check targets alias source configuration") } } func TestValidateRejectsInvalidOrUnboundedRoutingCheckTargets(t *testing.T) { for _, targets := range [][]string{ {"https://checkout.example/health", "https://checkout.example/health"}, {"ftp://checkout.example/health"}, } { cfg := mustLoadValidConfig(t) route := cfg.Routing[0] route.Check.Targets = targets cfg.Routing[0] = route if err := Validate(cfg); err == nil || !strings.Contains(err.Error(), "check.targets") { t.Fatalf("Validate(targets=%v) error = %v, want check.targets", targets, err) } } cfg := mustLoadValidConfig(t) route := cfg.Routing[0] route.Name = "checkout route" route.Check.Targets = []string{"https://checkout.example/health"} cfg.Routing[0] = route if err := Validate(cfg); err == nil || !strings.Contains(err.Error(), "target profile identifier") { t.Fatalf("Validate(invalid target routing name) error = %v", err) } cfg = mustLoadValidConfig(t) route = cfg.Routing[0] route.Check.Targets = make([]string, MaximumCheckURLs+1) for index := range route.Check.Targets { route.Check.Targets[index] = fmt.Sprintf("https://target-%d.example/health", index) } cfg.Routing[0] = route if err := Validate(cfg); err == nil || !strings.Contains(err.Error(), "at most") { t.Fatalf("Validate(too many route targets) error = %v", err) } cfg = mustLoadValidConfig(t) route = cfg.Routing[0] route.Check.Targets = make([]string, MaximumCheckURLs) for index := range route.Check.Targets { route.Check.Targets[index] = fmt.Sprintf("https://target-%d.example/health", index) } cfg.Routing[0] = route for index := 0; index < 4; index++ { copyRoute := route copyRoute.Name = fmt.Sprintf("route-%d", index) cfg.Routing = append(cfg.Routing, copyRoute) } if err := Validate(cfg); err == nil || !strings.Contains(err.Error(), "routing target profiles") { t.Fatalf("Validate(unbounded target profiles) error = %v", err) } }