|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 14 | + dynamicfake "k8s.io/client-go/dynamic/fake" |
| 15 | + |
| 16 | + "github.com/skyhook-io/radar/internal/k8s" |
| 17 | +) |
| 18 | + |
| 19 | +// End-to-end coverage of include=summary through the real handler path: |
| 20 | +// HTTP request → handleListResources → dynamic cache → applySummaryStrip → |
| 21 | +// JSON response. The unit tests in resource_summary_test.go pin the strip |
| 22 | +// profiles; this pins the wiring — the query-param parse, the strip actually |
| 23 | +// being applied at the writeJSON exit, and the informer-cache object |
| 24 | +// surviving unmutated. |
| 25 | + |
| 26 | +func argoAppE2EFixture() *unstructured.Unstructured { |
| 27 | + return &unstructured.Unstructured{Object: map[string]any{ |
| 28 | + "apiVersion": "argoproj.io/v1alpha1", |
| 29 | + "kind": "Application", |
| 30 | + "metadata": map[string]any{ |
| 31 | + "name": "checkout-service-production-us-east1", |
| 32 | + "namespace": "argocd", |
| 33 | + "labels": map[string]any{"team": "payments-platform"}, |
| 34 | + "annotations": map[string]any{ |
| 35 | + "argocd.argoproj.io/refresh": "normal", |
| 36 | + }, |
| 37 | + }, |
| 38 | + "spec": map[string]any{ |
| 39 | + "project": "shop-backend", |
| 40 | + "source": map[string]any{ |
| 41 | + "repoURL": "https://github.com/example-org/shop-backend-gitops", |
| 42 | + "targetRevision": "main", |
| 43 | + "path": "environments/production/checkout-service", |
| 44 | + }, |
| 45 | + "destination": map[string]any{ |
| 46 | + "server": "https://kubernetes.default.svc", |
| 47 | + "namespace": "shop-backend-production", |
| 48 | + }, |
| 49 | + "syncPolicy": map[string]any{"automated": map[string]any{"prune": true}}, |
| 50 | + }, |
| 51 | + "status": map[string]any{ |
| 52 | + "sync": map[string]any{"status": "Synced", "revision": "f3c9a1d7"}, |
| 53 | + "health": map[string]any{"status": "Healthy"}, |
| 54 | + "operationState": map[string]any{ |
| 55 | + "phase": "Succeeded", |
| 56 | + "finishedAt": "2026-06-10T10:00:12Z", |
| 57 | + "syncResult": map[string]any{ |
| 58 | + "resources": []any{map[string]any{"kind": "Deployment", "name": "checkout-service", "status": "Synced"}}, |
| 59 | + }, |
| 60 | + }, |
| 61 | + "reconciledAt": "2026-06-10T10:05:00Z", |
| 62 | + "history": []any{ |
| 63 | + map[string]any{"id": int64(0), "revision": "aaaa", "deployedAt": "2026-06-01T10:00:00Z"}, |
| 64 | + map[string]any{"id": int64(1), "revision": "bbbb", "deployedAt": "2026-06-10T10:00:00Z"}, |
| 65 | + }, |
| 66 | + "resources": []any{ |
| 67 | + map[string]any{"kind": "Deployment", "name": "checkout-service", "status": "Synced", "health": map[string]any{"status": "Healthy"}}, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }} |
| 71 | +} |
| 72 | + |
| 73 | +func setupArgoApplicationsDynamicCache(t *testing.T) { |
| 74 | + t.Helper() |
| 75 | + gvr := schema.GroupVersionResource{Group: "argoproj.io", Version: "v1alpha1", Resource: "applications"} |
| 76 | + dyn := dynamicfake.NewSimpleDynamicClientWithCustomListKinds( |
| 77 | + runtime.NewScheme(), |
| 78 | + map[schema.GroupVersionResource]string{gvr: "ApplicationList"}, |
| 79 | + argoAppE2EFixture(), |
| 80 | + ) |
| 81 | + if err := k8s.InitTestDynamicResourceCache(dyn, []k8s.APIResource{{ |
| 82 | + Group: "argoproj.io", |
| 83 | + Version: "v1alpha1", |
| 84 | + Kind: "Application", |
| 85 | + Name: "applications", |
| 86 | + Namespaced: true, |
| 87 | + Verbs: []string{"get", "list", "watch"}, |
| 88 | + }}); err != nil { |
| 89 | + t.Fatalf("InitTestDynamicResourceCache: %v", err) |
| 90 | + } |
| 91 | + t.Cleanup(k8s.ResetTestDynamicState) |
| 92 | + |
| 93 | + // The dynamic cache's List is non-blocking; poll until the informer has |
| 94 | + // synced the fixture so the HTTP assertions below see deterministic data. |
| 95 | + deadline := time.Now().Add(5 * time.Second) |
| 96 | + for { |
| 97 | + cached, err := k8s.GetResourceCache().ListDynamicWithGroup(context.Background(), "applications", "", "argoproj.io") |
| 98 | + if err == nil && len(cached) == 1 { |
| 99 | + return |
| 100 | + } |
| 101 | + if time.Now().After(deadline) { |
| 102 | + t.Fatalf("dynamic informer never synced: items=%d err=%v", len(cached), err) |
| 103 | + } |
| 104 | + time.Sleep(10 * time.Millisecond) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func listApplications(t *testing.T, query string) []map[string]any { |
| 109 | + t.Helper() |
| 110 | + var items []map[string]any |
| 111 | + assertOK(t, get(t, "/api/resources/applications?group=argoproj.io"+query), &items) |
| 112 | + if len(items) != 1 { |
| 113 | + t.Fatalf("got %d applications, want 1", len(items)) |
| 114 | + } |
| 115 | + return items |
| 116 | +} |
| 117 | + |
| 118 | +func TestListResourcesIncludeSummaryE2E(t *testing.T) { |
| 119 | + setupArgoApplicationsDynamicCache(t) |
| 120 | + |
| 121 | + summarized := listApplications(t, "&include=summary")[0] |
| 122 | + |
| 123 | + assertPathAbsent(t, summarized, "status", "resources") |
| 124 | + assertPathAbsent(t, summarized, "status", "operationState", "syncResult") |
| 125 | + if got := mustNested(t, summarized, "status", "sync", "status"); got != "Synced" { |
| 126 | + t.Errorf("status.sync.status = %v, want Synced", got) |
| 127 | + } |
| 128 | + if got := mustNested(t, summarized, "status", "health", "status"); got != "Healthy" { |
| 129 | + t.Errorf("status.health.status = %v, want Healthy", got) |
| 130 | + } |
| 131 | + if got := mustNested(t, summarized, "status", "operationState", "phase"); got != "Succeeded" { |
| 132 | + t.Errorf("status.operationState.phase = %v, want Succeeded", got) |
| 133 | + } |
| 134 | + history := mustNested(t, summarized, "status", "history").([]any) |
| 135 | + if len(history) != 1 { |
| 136 | + t.Fatalf("history should be trimmed to tail, got %d entries", len(history)) |
| 137 | + } |
| 138 | + tail, _ := history[0].(map[string]any) |
| 139 | + if len(tail) != 1 || tail["deployedAt"] != "2026-06-10T10:00:00Z" { |
| 140 | + t.Errorf("history tail should keep only deployedAt: %v", tail) |
| 141 | + } |
| 142 | + |
| 143 | + // The summary strip must have operated on a copy: the informer-cache |
| 144 | + // object behind the handler must still carry the heavy subtrees. |
| 145 | + cached, err := k8s.GetResourceCache().ListDynamicWithGroup(context.Background(), "applications", "", "argoproj.io") |
| 146 | + if err != nil { |
| 147 | + t.Fatalf("ListDynamicWithGroup: %v", err) |
| 148 | + } |
| 149 | + if len(cached) != 1 { |
| 150 | + t.Fatalf("got %d cached applications, want 1", len(cached)) |
| 151 | + } |
| 152 | + if _, found, _ := unstructured.NestedFieldNoCopy(cached[0].Object, "status", "resources"); !found { |
| 153 | + t.Fatal("cache object lost status.resources — summary request mutated the informer cache") |
| 154 | + } |
| 155 | + if hist, _, _ := unstructured.NestedSlice(cached[0].Object, "status", "history"); len(hist) != 2 { |
| 156 | + t.Fatalf("cache object history trimmed to %d entries — summary request mutated the informer cache", len(hist)) |
| 157 | + } |
| 158 | + |
| 159 | + // Without include the same GET returns the full objects. |
| 160 | + full := listApplications(t, "")[0] |
| 161 | + if _, found, _ := unstructured.NestedFieldNoCopy(full, "status", "resources"); !found { |
| 162 | + t.Error("raw response missing status.resources") |
| 163 | + } |
| 164 | + if _, found, _ := unstructured.NestedFieldNoCopy(full, "status", "operationState", "syncResult"); !found { |
| 165 | + t.Error("raw response missing status.operationState.syncResult") |
| 166 | + } |
| 167 | + if fullHistory := mustNested(t, full, "status", "history").([]any); len(fullHistory) != 2 { |
| 168 | + t.Errorf("raw response history = %d entries, want 2", len(fullHistory)) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func TestListResourcesUnknownIncludeRejected(t *testing.T) { |
| 173 | + setupArgoApplicationsDynamicCache(t) |
| 174 | + |
| 175 | + resp := get(t, "/api/resources/applications?group=argoproj.io&include=bogus") |
| 176 | + defer resp.Body.Close() |
| 177 | + if resp.StatusCode != http.StatusBadRequest { |
| 178 | + t.Fatalf("status = %d, want 400", resp.StatusCode) |
| 179 | + } |
| 180 | + body, _ := io.ReadAll(resp.Body) |
| 181 | + var payload map[string]any |
| 182 | + if err := json.Unmarshal(body, &payload); err != nil { |
| 183 | + t.Fatalf("non-JSON error body: %s", body) |
| 184 | + } |
| 185 | + msg, _ := payload["error"].(string) |
| 186 | + if msg != `unknown include="bogus" (want: summary, raw)` { |
| 187 | + t.Errorf("error = %q, want the accepted values named", msg) |
| 188 | + } |
| 189 | +} |
0 commit comments