|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/acmore/okdev/internal/kube" |
| 13 | +) |
| 14 | + |
| 15 | +func TestFormatMeshSummary(t *testing.T) { |
| 16 | + if got := formatMeshSummary(nil); got != "no receivers" { |
| 17 | + t.Fatalf("expected 'no receivers', got %q", got) |
| 18 | + } |
| 19 | + |
| 20 | + summary := &meshSummary{Receivers: []meshReceiverStatus{}} |
| 21 | + if got := formatMeshSummary(summary); got != "0 receiver(s) connected and synced" { |
| 22 | + t.Fatalf("expected 0 connected, got %q", got) |
| 23 | + } |
| 24 | + |
| 25 | + summary = &meshSummary{Receivers: []meshReceiverStatus{ |
| 26 | + {Pod: "recv-1", Connected: true, Synced: true}, |
| 27 | + {Pod: "recv-2", Connected: true, Synced: true}, |
| 28 | + }} |
| 29 | + if got := formatMeshSummary(summary); got != "2 receiver(s) connected and synced" { |
| 30 | + t.Fatalf("expected 2 connected, got %q", got) |
| 31 | + } |
| 32 | + |
| 33 | + summary = &meshSummary{Receivers: []meshReceiverStatus{ |
| 34 | + {Pod: "recv-1", Connected: true, Synced: true}, |
| 35 | + {Pod: "recv-2", Err: errors.New("timeout")}, |
| 36 | + }} |
| 37 | + if got := formatMeshSummary(summary); got != "1/2 receiver(s) synced, 1 failed" { |
| 38 | + t.Fatalf("expected failed summary, got %q", got) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestMeshStatusLines(t *testing.T) { |
| 43 | + if got := meshStatusLines(nil); len(got) != 0 { |
| 44 | + t.Fatalf("expected empty lines for nil summary, got %v", got) |
| 45 | + } |
| 46 | + |
| 47 | + summary := &meshSummary{HubPod: "hub", Receivers: []meshReceiverStatus{ |
| 48 | + {Pod: "recv-1", Connected: true, Synced: true}, |
| 49 | + {Pod: "recv-2", Err: errors.New("timeout")}, |
| 50 | + }} |
| 51 | + lines := meshStatusLines(summary) |
| 52 | + |
| 53 | + joined := strings.Join(lines, "\n") |
| 54 | + if !strings.Contains(joined, "topology: hub-and-spoke") { |
| 55 | + t.Fatalf("expected topology, got %q", joined) |
| 56 | + } |
| 57 | + if !strings.Contains(joined, "hub: hub") { |
| 58 | + t.Fatalf("expected hub name, got %q", joined) |
| 59 | + } |
| 60 | + if !strings.Contains(joined, "receivers: 1/2 connected") { |
| 61 | + t.Fatalf("expected receiver summary, got %q", joined) |
| 62 | + } |
| 63 | + if !strings.Contains(joined, "recv-1: synced") { |
| 64 | + t.Fatalf("expected recv-1 status, got %q", joined) |
| 65 | + } |
| 66 | + if !strings.Contains(joined, "recv-2: error: timeout") { |
| 67 | + t.Fatalf("expected recv-2 error, got %q", joined) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestMeshReceiverCount(t *testing.T) { |
| 72 | + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 73 | + t.Logf("Got request: %s %s", r.Method, r.URL.String()) |
| 74 | + if r.URL.Path == "/api/v1/namespaces/demo/pods" { |
| 75 | + w.Header().Set("Content-Type", "application/json") |
| 76 | + _, _ = io.WriteString(w, `{"kind":"PodList","apiVersion":"v1","items":[ |
| 77 | + {"metadata":{"name":"pod-1"}}, |
| 78 | + {"metadata":{"name":"pod-2"}} |
| 79 | + ]}`) |
| 80 | + return |
| 81 | + } |
| 82 | + http.NotFound(w, r) |
| 83 | + })) |
| 84 | + defer server.Close() |
| 85 | + |
| 86 | + kubeconfig := writeCLITLSTestKubeconfig(t, server) |
| 87 | + t.Setenv("KUBECONFIG", kubeconfig) |
| 88 | + |
| 89 | + client := &kube.Client{} |
| 90 | + |
| 91 | + count, err := meshReceiverCount(context.Background(), client, "demo", map[string]string{"okdev.io/session": "foo"}) |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("unexpected error: %v", err) |
| 94 | + } |
| 95 | + if count != 2 { |
| 96 | + t.Fatalf("expected 2 receivers, got %d", count) |
| 97 | + } |
| 98 | +} |
0 commit comments