package controller import ( "reflect" "testing" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" v1 "gitea.renner.uno/konairius/homelab/kuma-sync/api/v1alpha1" "gitea.renner.uno/konairius/homelab/kuma-sync/internal/kumaclient" ) func TestMappingAllTypes(t *testing.T) { i32 := func(v int32) *int32 { return &v } base := func(typ string) v1.KumaMonitorSpec { return v1.KumaMonitorSpec{ Type: typ, DisplayName: "dn", Description: "desc", Interval: 120, RetryInterval: 30, MaxRetries: 2, } } httpSpec := &v1.HTTPSpec{ URL: "https://e.com", Method: "POST", AcceptedStatusCodes: []string{"200"}, Headers: map[string]string{"a": "b"}, Body: "x", MaxRedirects: i32(3), IgnoreTLS: true, ExpiryNotification: true, } mk := func(s v1.KumaMonitorSpec) *v1.KumaMonitor { return &v1.KumaMonitor{ObjectMeta: metav1.ObjectMeta{Namespace: "ns", Name: "n"}, Spec: s} } common := func(typ string) kumaclient.Monitor { return kumaclient.Monitor{ Type: typ, Name: "dn", Description: "desc", Active: true, Interval: 120, RetryInterval: 30, MaxRetries: 2, OwnerKey: "ns/n", } } withHTTP := func(m kumaclient.Monitor) kumaclient.Monitor { m.URL, m.Method, m.AcceptedStatusCodes = "https://e.com", "POST", []string{"200"} m.Headers, m.Body, m.MaxRedirects = map[string]string{"a": "b"}, "x", 3 m.IgnoreTLS, m.ExpiryNotification, m.Timeout = true, true, 15 return m } group := base("group") httpS := base("http") httpS.HTTP, httpS.Timeout = httpSpec, i32(15) kw := base("keyword") kw.HTTP, kw.Timeout = httpSpec, i32(15) kw.Keyword = &v1.KeywordSpec{Keyword: "ok", Invert: true} jq := base("json-query") jq.HTTP, jq.Timeout = httpSpec, i32(15) jq.JSONQuery = &v1.JSONQuerySpec{Expression: "a.b", Operator: "contains", Expected: "z"} dns := base("dns") dns.DNS = &v1.DNSSpec{Hostname: "h.com", ResolveType: "AAAA", ResolveServer: "9.9.9.9", Port: 5353} port := base("port") port.Port = &v1.PortSpec{Hostname: "p.com", Port: 443} paused := base("group") paused.Paused = true wantKW := withHTTP(common("keyword")) wantKW.Keyword, wantKW.InvertKeyword = "ok", true wantJQ := withHTTP(common("json-query")) wantJQ.JSONPath, wantJQ.JSONPathOperator, wantJQ.ExpectedValue = "a.b", "contains", "z" wantDNS := common("dns") wantDNS.Hostname, wantDNS.DNSResolveType, wantDNS.DNSResolveServer, wantDNS.Port = "h.com", "AAAA", "9.9.9.9", 5353 wantPort := common("port") wantPort.Hostname, wantPort.Port = "p.com", 443 wantPaused := common("group") wantPaused.Active = false cases := []struct { name string spec v1.KumaMonitorSpec want kumaclient.Monitor }{ {"group", group, common("group")}, {"http", httpS, withHTTP(common("http"))}, {"keyword", kw, wantKW}, {"json-query", jq, wantJQ}, {"dns", dns, wantDNS}, {"port", port, wantPort}, {"paused maps to inactive", paused, wantPaused}, } for _, c := range cases { got := Desired(mk(c.spec)) if !reflect.DeepEqual(got, c.want) { t.Errorf("%s:\n got %+v\n want %+v", c.name, got, c.want) } } } func TestMappingMaxRedirectsDefault(t *testing.T) { km := &v1.KumaMonitor{ ObjectMeta: metav1.ObjectMeta{Namespace: "ns", Name: "n"}, Spec: v1.KumaMonitorSpec{Type: "http", DisplayName: "d", HTTP: &v1.HTTPSpec{URL: "https://e.com"}}, } if got := Desired(km).MaxRedirects; got != 10 { t.Errorf("MaxRedirects = %d, want Kuma's default 10", got) } }