package haproxy import ( "bytes" "strings" "testing" "git.netcell-it.de/projekte/edgeguard-native/internal/models" ) func renderView(t *testing.T, v View) string { t.Helper() var buf bytes.Buffer if err := tpl.Execute(&buf, v); err != nil { t.Fatalf("template execute: %v", err) } return buf.String() } func TestRender_BaselineHasFrontendsAndApiBackend(t *testing.T) { out := renderView(t, View{}) for _, w := range []string{ "frontend public_http", "frontend public_https", "frontend internal_stats", "backend api_backend", "server api1 127.0.0.1:9443 check", "bind :443 ssl crt /etc/edgeguard/tls/", "path_beg /.well-known/acme-challenge/", "http-request redirect scheme https", } { if !strings.Contains(out, w) { t.Errorf("missing %q in baseline output:\n%s", w, out) } } } func TestRender_DomainRoutesEmitUseBackend(t *testing.T) { v := View{ Backends: []models.Backend{ {ID: 1, Name: "app", Address: "10.0.0.10", Port: 8080, Active: true}, {ID: 2, Name: "api", Address: "10.0.0.20", Port: 9000, Active: true}, }, Domains: []DomainView{{ Domain: models.Domain{ID: 1, Name: "example.com", Active: true}, Routes: []RouteView{ {PathPrefix: "/", BackendID: 1}, {PathPrefix: "/api", BackendID: 2}, }, }}, } out := renderView(t, v) for _, w := range []string{ "backend eg_backend_1", "server app 10.0.0.10:8080", "backend eg_backend_2", "server api 10.0.0.20:9000", "use_backend eg_backend_1 if { hdr(host) -i example.com } { path_beg / }", "use_backend eg_backend_2 if { hdr(host) -i example.com } { path_beg /api }", } { if !strings.Contains(out, w) { t.Errorf("missing %q in output:\n%s", w, out) } } } func TestRender_HealthCheckPathAddsCheckInter(t *testing.T) { hcp := "/health" v := View{ Backends: []models.Backend{ {ID: 1, Name: "app", Address: "10.0.0.10", Port: 8080, Active: true, HealthCheckPath: &hcp}, }, } out := renderView(t, v) if !strings.Contains(out, "server app 10.0.0.10:8080 check inter 5s") { t.Errorf("expected `check inter 5s` for backend with health_check_path:\n%s", out) } }