Skip to content

Commit 169d353

Browse files
committed
test: add unit tests for mesh summary and fix wait timeout error
- Add mesh_test.go to test meshReceiverCount, formatMeshSummary, and meshStatusLines. - Fix context deadline bug in down.go where client rate limit errors were not treated as timeouts.
1 parent 517d63e commit 169d353

2 files changed

Lines changed: 100 additions & 2 deletions

File tree

internal/cli/down.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func waitForResourceDeletion(ctx context.Context, k downWaitClient, namespace, a
336336
for {
337337
exists, err := k.ResourceExists(ctx, namespace, apiVersion, kind, name)
338338
if err != nil {
339-
if ctx.Err() != nil {
339+
if ctx.Err() != nil || strings.Contains(err.Error(), "context deadline") {
340340
return downWaitContextError(ctx, timeoutMessage)
341341
}
342342
return fmt.Errorf("check %s/%s deletion: %w", kind, name, err)
@@ -359,7 +359,7 @@ func waitForSessionPodsDeleted(ctx context.Context, k downWaitClient, namespace,
359359
for {
360360
pods, err := k.ListPods(ctx, namespace, false, selector)
361361
if err != nil {
362-
if ctx.Err() != nil {
362+
if ctx.Err() != nil || strings.Contains(err.Error(), "context deadline") {
363363
return downWaitContextError(ctx, "timed out waiting for session pods to terminate")
364364
}
365365
return fmt.Errorf("list session pods while waiting for deletion: %w", err)

internal/cli/mesh_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)