|
| 1 | +package handlers_test |
| 2 | + |
| 3 | +// logs_resourcelogs_twinlogs_test.go — covers the error/edge arms of |
| 4 | +// LogsHandler.ResourceLogs (logs.go) that logs_coverage_test.go leaves open: |
| 5 | +// |
| 6 | +// logs.go:157-158 — lookup_failed: GetResourceByToken returns a non-NotFound |
| 7 | +// error (driven with a closed DB). |
| 8 | +// logs.go:194-196 — tail clamp: ?tail=0 (n<1) clamps up to 1. |
| 9 | +// logs.go:206-211 — pods_unavailable: the pod List call returns an error |
| 10 | +// (driven with a PrependReactor on the fake clientset). |
| 11 | +// |
| 12 | +// The clientset is the in-memory k8s fake (SetClientset seam), so these run |
| 13 | +// under CI's postgres-only matrix without a live cluster. |
| 14 | + |
| 15 | +import ( |
| 16 | + "errors" |
| 17 | + "net/http" |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | + corev1 "k8s.io/api/core/v1" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/apimachinery/pkg/runtime" |
| 25 | + k8sfake "k8s.io/client-go/kubernetes/fake" |
| 26 | + k8stesting "k8s.io/client-go/testing" |
| 27 | + |
| 28 | + "instant.dev/internal/handlers" |
| 29 | + "instant.dev/internal/testhelpers" |
| 30 | +) |
| 31 | + |
| 32 | +// TestLogs_LookupFailed_503 drives logs.go:157-158: a DB error (not a |
| 33 | +// not-found) on GetResourceByToken returns 503 lookup_failed. We build the |
| 34 | +// handler against a CLOSED *sql.DB so the query fails with a driver error that |
| 35 | +// is NOT *models.ErrResourceNotFound. |
| 36 | +func TestLogs_LookupFailed_503(t *testing.T) { |
| 37 | + db, _ := testhelpers.SetupTestDB(t) |
| 38 | + h := handlers.NewLogsHandler(db) |
| 39 | + h.SetClientset(k8sfake.NewSimpleClientset()) |
| 40 | + // Close the DB now so GetResourceByToken's query returns a driver error |
| 41 | + // (sql.ErrConnDone) — NOT a *models.ErrResourceNotFound — driving the |
| 42 | + // lookup_failed 503 arm rather than the not_found 404 arm. |
| 43 | + require.NoError(t, db.Close()) |
| 44 | + |
| 45 | + app := logsTestApp(t, db, h) |
| 46 | + // A syntactically valid UUID so we pass the parse gate and reach the lookup. |
| 47 | + resp := logsGet(t, app, "11111111-1111-1111-1111-111111111111", "") |
| 48 | + defer resp.Body.Close() |
| 49 | + assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) |
| 50 | +} |
| 51 | + |
| 52 | +// TestLogs_TailClampLow_StreamsSSE drives logs.go:194-196: ?tail=0 (n<1) clamps |
| 53 | +// up to 1 and the happy path still streams. Needs a pod in the fake clientset. |
| 54 | +func TestLogs_TailClampLow_StreamsSSE(t *testing.T) { |
| 55 | + db, clean := testhelpers.SetupTestDB(t) |
| 56 | + defer clean() |
| 57 | + |
| 58 | + const ns = "ns-clamp-low" |
| 59 | + cs := k8sfake.NewSimpleClientset(&corev1.Pod{ |
| 60 | + ObjectMeta: metav1.ObjectMeta{ |
| 61 | + Name: "postgres-0", |
| 62 | + Namespace: ns, |
| 63 | + Labels: map[string]string{"app": "postgres"}, |
| 64 | + }, |
| 65 | + }) |
| 66 | + h := handlers.NewLogsHandler(db) |
| 67 | + h.SetClientset(cs) |
| 68 | + app := logsTestApp(t, db, h) |
| 69 | + |
| 70 | + token := seedLogsResource(t, db, "postgres", "growth", "active", ns) |
| 71 | + resp := logsGet(t, app, token, "tail=0") // n<1 → clamp to 1 |
| 72 | + defer resp.Body.Close() |
| 73 | + require.Equal(t, http.StatusOK, resp.StatusCode) |
| 74 | + assert.Equal(t, "text/event-stream", resp.Header.Get("Content-Type")) |
| 75 | +} |
| 76 | + |
| 77 | +// TestLogs_ListPodsError_503 drives logs.go:206-211: the pod List call errors. |
| 78 | +// A PrependReactor on the fake clientset makes List("pods") return an error so |
| 79 | +// the pods_unavailable arm runs (distinct from the empty-list pod_not_found arm |
| 80 | +// already covered). |
| 81 | +func TestLogs_ListPodsError_503(t *testing.T) { |
| 82 | + db, clean := testhelpers.SetupTestDB(t) |
| 83 | + defer clean() |
| 84 | + |
| 85 | + cs := k8sfake.NewSimpleClientset() |
| 86 | + cs.PrependReactor("list", "pods", |
| 87 | + func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) { |
| 88 | + return true, nil, errors.New("apiserver unreachable") |
| 89 | + }) |
| 90 | + h := handlers.NewLogsHandler(db) |
| 91 | + h.SetClientset(cs) |
| 92 | + app := logsTestApp(t, db, h) |
| 93 | + |
| 94 | + token := seedLogsResource(t, db, "postgres", "growth", "active", "ns-list-err") |
| 95 | + resp := logsGet(t, app, token, "") |
| 96 | + defer resp.Body.Close() |
| 97 | + assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) |
| 98 | +} |
0 commit comments