|
| 1 | +package podlogs |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + "k8s.io/client-go/kubernetes" |
| 13 | + "k8s.io/client-go/rest" |
| 14 | + |
| 15 | + "github.com/skyhook-io/radar/pkg/k8score" |
| 16 | +) |
| 17 | + |
| 18 | +// readAnswer returns the error a log read gets when the apiserver answers with |
| 19 | +// the given 400 message, the way it relays the kubelet's refusal. |
| 20 | +func readAnswer(t *testing.T, message string) error { |
| 21 | + t.Helper() |
| 22 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 23 | + w.Header().Set("Content-Type", "application/json") |
| 24 | + w.WriteHeader(http.StatusBadRequest) |
| 25 | + _ = json.NewEncoder(w).Encode(map[string]any{"kind": "Status", "apiVersion": "v1", "status": "Failure", "message": message, "reason": "BadRequest", "code": 400}) |
| 26 | + })) |
| 27 | + t.Cleanup(srv.Close) |
| 28 | + client, err := kubernetes.NewForConfig(&rest.Config{Host: srv.URL}) |
| 29 | + if err != nil { |
| 30 | + t.Fatal(err) |
| 31 | + } |
| 32 | + _, err = k8score.GetContainerLogs(context.Background(), client, "default", "api-0", "app", k8score.LogOptions{}) |
| 33 | + return err |
| 34 | +} |
| 35 | + |
| 36 | +func TestIsNoPreviousRun(t *testing.T) { |
| 37 | + if !IsNoPreviousRun(readAnswer(t, `previous terminated container "app" in pod "api-0" not found`)) { |
| 38 | + t.Error("the apiserver's no-earlier-run answer should be recognised") |
| 39 | + } |
| 40 | + if IsNoPreviousRun(errors.New(`previous terminated container "app" in pod "api-0" not found`)) { |
| 41 | + t.Error("only the apiserver's 400 answer should be recognised, not any error with the same words") |
| 42 | + } |
| 43 | + if IsNoPreviousRun(errors.New("connection refused")) || IsNoPreviousRun(nil) { |
| 44 | + t.Error("an unrelated failure or no error is not a missing earlier run") |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// The messages are the kubelet's, captured from a kind cluster. |
| 49 | +func TestIsContainerNotStarted(t *testing.T) { |
| 50 | + for _, message := range []string{ |
| 51 | + `container "app" in pod "api-0" is waiting to start: ContainerCreating`, |
| 52 | + `container "app" in pod "api-0" is waiting to start: image can't be pulled`, |
| 53 | + `container "app" in pod "api-0" is waiting to start: trying and failing to pull image`, |
| 54 | + `container "app" in pod "api-0" is waiting to start: CreateContainerConfigError`, |
| 55 | + `container "app" in pod "api-0" is waiting to start - no logs yet`, |
| 56 | + } { |
| 57 | + if !IsContainerNotStarted(readAnswer(t, message)) { |
| 58 | + t.Errorf("not recognised: %s", message) |
| 59 | + } |
| 60 | + } |
| 61 | + if IsContainerNotStarted(readAnswer(t, `previous terminated container "app" in pod "api-0" not found`)) { |
| 62 | + t.Error("a missing earlier run is a different answer") |
| 63 | + } |
| 64 | + if IsContainerNotStarted(errors.New(`container "app" in pod "api-0" is waiting to start: ContainerCreating`)) { |
| 65 | + t.Error("only the apiserver's 400 answer should be recognised") |
| 66 | + } |
| 67 | + if IsContainerNotStarted(nil) { |
| 68 | + t.Error("no error is not a container that has not started") |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestEarlierRun(t *testing.T) { |
| 73 | + terminated := corev1.ContainerState{Terminated: &corev1.ContainerStateTerminated{ExitCode: 1}} |
| 74 | + for _, tc := range []struct { |
| 75 | + name string |
| 76 | + pod *corev1.Pod |
| 77 | + had, known bool |
| 78 | + }{ |
| 79 | + {"never restarted", &corev1.Pod{Status: corev1.PodStatus{ContainerStatuses: []corev1.ContainerStatus{{Name: "app"}}}}, false, true}, |
| 80 | + {"current run ended is not an earlier run", &corev1.Pod{Status: corev1.PodStatus{ContainerStatuses: []corev1.ContainerStatus{{Name: "app", State: terminated}}}}, false, true}, |
| 81 | + {"restart count", &corev1.Pod{Status: corev1.PodStatus{ContainerStatuses: []corev1.ContainerStatus{{Name: "app", RestartCount: 1}}}}, true, true}, |
| 82 | + {"last termination without a restart count", &corev1.Pod{Status: corev1.PodStatus{ContainerStatuses: []corev1.ContainerStatus{{Name: "app", LastTerminationState: terminated}}}}, true, true}, |
| 83 | + {"init container", &corev1.Pod{Status: corev1.PodStatus{InitContainerStatuses: []corev1.ContainerStatus{{Name: "app", RestartCount: 2}}}}, true, true}, |
| 84 | + {"no status yet", &corev1.Pod{}, false, false}, |
| 85 | + {"no pod", nil, false, false}, |
| 86 | + } { |
| 87 | + t.Run(tc.name, func(t *testing.T) { |
| 88 | + had, known := EarlierRun(tc.pod, "app") |
| 89 | + if had != tc.had || known != tc.known { |
| 90 | + t.Fatalf("EarlierRun = (%t, %t), want (%t, %t)", had, known, tc.had, tc.known) |
| 91 | + } |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestNotStartedMessage(t *testing.T) { |
| 97 | + waiting := func(reason, message string) *corev1.Pod { |
| 98 | + return &corev1.Pod{Status: corev1.PodStatus{ContainerStatuses: []corev1.ContainerStatus{{ |
| 99 | + Name: "app", State: corev1.ContainerState{Waiting: &corev1.ContainerStateWaiting{Reason: reason, Message: message}}, |
| 100 | + }}}} |
| 101 | + } |
| 102 | + answer := errors.New(`failed to stream logs for default/api-0/app: container "app" in pod "api-0" is waiting to start: trying and failing to pull image`) |
| 103 | + for _, tc := range []struct { |
| 104 | + name string |
| 105 | + pod *corev1.Pod |
| 106 | + err error |
| 107 | + want string |
| 108 | + }{ |
| 109 | + {"status reason and message", waiting("CreateContainerConfigError", `configmap "missing-config" not found`), answer, |
| 110 | + `This container has not started yet, so it has no logs. Kubernetes reports CreateContainerConfigError: configmap "missing-config" not found.`}, |
| 111 | + {"status reason only", waiting("ContainerCreating", ""), answer, |
| 112 | + "This container has not started yet, so it has no logs. Kubernetes reports ContainerCreating."}, |
| 113 | + {"no status falls back to the apiserver's answer", nil, answer, |
| 114 | + "This container has not started yet, so it has no logs. Kubernetes reports trying and failing to pull image."}, |
| 115 | + {"nothing to add", nil, errors.New(`container "app" in pod "api-0" is waiting to start - no logs yet`), |
| 116 | + "This container has not started yet, so it has no logs."}, |
| 117 | + } { |
| 118 | + t.Run(tc.name, func(t *testing.T) { |
| 119 | + if got := NotStartedMessage(tc.pod, "app", tc.err); got != tc.want { |
| 120 | + t.Fatalf("got %q\nwant %q", got, tc.want) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments