Skip to content

Commit

Permalink
Add tests for unhealthy pod logs cond. gatherer
Browse files Browse the repository at this point in the history
  • Loading branch information
natiiix committed Oct 8, 2021
1 parent 2212ef3 commit 4f9d16c
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/gatherers/conditional/conditional_gatherer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
144 changes: 144 additions & 0 deletions pkg/gatherers/conditional/gather_logs_of_unhealthy_pods_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}

0 comments on commit 4f9d16c

Please sign in to comment.