From 4f9d16c314d8cb949bec62eeaba8165a21479ff3 Mon Sep 17 00:00:00 2001 From: Ivo Meixner Date: Fri, 8 Oct 2021 11:00:12 +0200 Subject: [PATCH] Add tests for unhealthy pod logs cond. gatherer --- .../conditional/conditional_gatherer_test.go | 2 +- .../gather_logs_of_unhealthy_pods_test.go | 144 ++++++++++++++++++ 2 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 pkg/gatherers/conditional/gather_logs_of_unhealthy_pods_test.go diff --git a/pkg/gatherers/conditional/conditional_gatherer_test.go b/pkg/gatherers/conditional/conditional_gatherer_test.go index d0ca4eabf0..c56ab442b0 100644 --- a/pkg/gatherers/conditional/conditional_gatherer_test.go +++ b/pkg/gatherers/conditional/conditional_gatherer_test.go @@ -166,7 +166,7 @@ func Test_Gatherer_GatherConditionalGathererRules(t *testing.T) { err = json.Unmarshal(item, &gotGatheringRules) assert.NoError(t, err) - assert.Len(t, gotGatheringRules, 2) + assert.Len(t, gotGatheringRules, 4) } func newFakeClientWithMetrics(metrics string) *fake.RESTClient { diff --git a/pkg/gatherers/conditional/gather_logs_of_unhealthy_pods_test.go b/pkg/gatherers/conditional/gather_logs_of_unhealthy_pods_test.go new file mode 100644 index 0000000000..8dd3fa023a --- /dev/null +++ b/pkg/gatherers/conditional/gather_logs_of_unhealthy_pods_test.go @@ -0,0 +1,144 @@ +package conditional + +import ( + "context" + "testing" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + kubefake "k8s.io/client-go/kubernetes/fake" +) + +func Test_GatherLogsOfUnhealthyPods(t *testing.T) { + gatherer := Gatherer{ + firingAlerts: map[string][]AlertLabels{ + "test-alert-current": { + { + "namespace": "test-namespace", + "pod": "test-pod", + }, + }, + "test-alert-previous": { + { + "namespace": "test-namespace", + "pod": "test-pod", + }, + }, + }, + } + + ctx := context.Background() + + coreClient := kubefake.NewSimpleClientset().CoreV1() + _, err := coreClient.Pods("test-namespace").Create(ctx, + &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-pod", + Namespace: "test-namespace", + }, + Status: corev1.PodStatus{ + Phase: corev1.PodRunning, + ContainerStatuses: []corev1.ContainerStatus{ + {Name: "test-container"}, + }, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + {Name: "test-container"}, + }, + }, + }, + metav1.CreateOptions{}) + if err != nil { + t.Fatalf("unable to create fake pod: %v", err) + } + + rec, errs := gatherer.gatherLogsOfUnhealthyPods(ctx, coreClient, GatherLogsOfUnhealthyPodsParams{ + AlertsCurrent: []string{"test-alert-current"}, + AlertsPrevious: []string{"test-alert-previous"}, + TailLinesCurrent: 100, + TailLinesPrevious: 10, + }) + + if len(errs) > 0 { + t.Fatalf("unexpected error(s) returned by the log gathering function: %v", errs) + } + if len(rec) != 2 { + t.Fatalf("unexpected number of records (expected: 2, actual: %d)", len(rec)) + } + + // The order, in which the logs are gathered is fixed, so the current log + // should be the first record and the previous log should be the second. + if rec[0].Name != "conditional/unhealthy_logs/test-namespace/test-pod/test-container/current.log" { + t.Fatalf("unexpected 'Name' of the first log record: %q", rec[0].Name) + } + if rec[1].Name != "conditional/unhealthy_logs/test-namespace/test-pod/test-container/previous.log" { + t.Fatalf("unexpected 'Name' of the second log record: %q", rec[1].Name) + } +} + +func Test_GatherLogsOfUnhealthyPods_MissingNamespace(t *testing.T) { + gatherer := Gatherer{ + firingAlerts: map[string][]AlertLabels{ + "test-alert": { + { + "pod": "test-pod", + }, + }, + }, + } + + ctx := context.Background() + coreClient := kubefake.NewSimpleClientset().CoreV1() + + rec, errs := gatherer.gatherLogsOfUnhealthyPods(ctx, coreClient, GatherLogsOfUnhealthyPodsParams{ + AlertsCurrent: []string{"test-alert"}, + AlertsPrevious: []string{}, + TailLinesCurrent: 100, + TailLinesPrevious: 10, + }) + + if len(rec) != 0 { + t.Fatalf("unexpected number of records (expected: 0, actual: %d)", len(rec)) + } + if len(errs) != 1 { + t.Fatalf("expected 1 error, actual errors returned: %v", errs) + } + + if errs[0].Error() != "alert is missing 'namespace' label" { + t.Fatalf("unexpected error message on missing 'namespace' alert label: %q", errs[0].Error()) + } +} + +func Test_GatherLogsOfUnhealthyPods_MissingPod(t *testing.T) { + gatherer := Gatherer{ + firingAlerts: map[string][]AlertLabels{ + "test-alert": { + { + "namespace": "test-namespace", + }, + }, + }, + } + + ctx := context.Background() + coreClient := kubefake.NewSimpleClientset().CoreV1() + + rec, errs := gatherer.gatherLogsOfUnhealthyPods(ctx, coreClient, GatherLogsOfUnhealthyPodsParams{ + AlertsCurrent: []string{"test-alert"}, + AlertsPrevious: []string{}, + TailLinesCurrent: 100, + TailLinesPrevious: 10, + }) + + if len(rec) != 0 { + t.Fatalf("unexpected number of records (expected: 0, actual: %d)", len(rec)) + } + if len(errs) != 1 { + t.Fatalf("expected 1 error, actual errors returned: %v", errs) + } + + if errs[0].Error() != "alert is missing 'pod' label" { + t.Fatalf("unexpected error message on missing 'pod' alert label: %q", errs[0].Error()) + } +}