Skip to content

Commit 6ef73be

Browse files
author
Ricardo Lüders
committed
chore: unit tests to alertmanager logs gather
1 parent 4e2c994 commit 6ef73be

File tree

2 files changed

+186
-10
lines changed

2 files changed

+186
-10
lines changed

pkg/gatherers/conditional/gather_alertmanager_logs.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"strings"
77

8+
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
89
"k8s.io/klog/v2"
910

1011
"k8s.io/client-go/kubernetes"
@@ -31,29 +32,32 @@ func (g *Gatherer) BuildGatherAlertmanagerLogs(paramsInterface interface{}) (gat
3132

3233
return gatherers.GatheringClosure{
3334
Run: func(ctx context.Context) ([]record.Record, []error) {
34-
records, err := g.gatherAlertmanagerLogs(ctx, params)
35+
kubeClient, err := kubernetes.NewForConfig(g.gatherProtoKubeConfig)
3536
if err != nil {
36-
return records, err
37+
return nil, []error{err}
38+
}
39+
40+
coreClient := kubeClient.CoreV1()
41+
42+
records, errs := g.gatherAlertmanagerLogs(ctx, params, coreClient)
43+
if errs != nil {
44+
return records, errs
3745
}
3846
return records, nil
3947
},
4048
CanFail: canConditionalGathererFail,
4149
}, nil
4250
}
4351

44-
func (g *Gatherer) gatherAlertmanagerLogs(ctx context.Context, params GatherAlertmanagerLogsParams) ([]record.Record, []error) {
52+
func (g *Gatherer) gatherAlertmanagerLogs(
53+
ctx context.Context,
54+
params GatherAlertmanagerLogsParams,
55+
coreClient corev1client.CoreV1Interface) ([]record.Record, []error) {
4556
alertInstances, ok := g.firingAlerts[params.AlertName]
4657
if !ok {
4758
return nil, []error{fmt.Errorf("conditional gatherer triggered, but specified alert %q is not firing", params.AlertName)}
4859
}
4960

50-
kubeClient, err := kubernetes.NewForConfig(g.gatherProtoKubeConfig)
51-
if err != nil {
52-
return nil, []error{err}
53-
}
54-
55-
coreClient := kubeClient.CoreV1()
56-
5761
var errs []error
5862
var records []record.Record
5963

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package conditional
2+
3+
import (
4+
"context"
5+
"reflect"
6+
"testing"
7+
"time"
8+
9+
corev1 "k8s.io/api/core/v1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
kubefake "k8s.io/client-go/kubernetes/fake"
12+
13+
"github.com/openshift/insights-operator/pkg/record"
14+
"github.com/openshift/insights-operator/pkg/utils/marshal"
15+
)
16+
17+
var testAlertManagerFiringAlerts = map[string][]AlertLabels{
18+
"AlertmanagerFailedToSendAlerts": {
19+
{
20+
"pod": "alertmanager-main-0",
21+
"namespace": "openshift-monitoring",
22+
},
23+
},
24+
}
25+
26+
func TestGatherer_BuildGatherAlertmanagerLogs(t *testing.T) {
27+
g := &Gatherer{firingAlerts: testAlertManagerFiringAlerts}
28+
gather, err := g.BuildGatherAlertmanagerLogs(GatherAlertmanagerLogsParams{
29+
AlertName: "AlertmanagerFailedToSendAlerts",
30+
TailLines: 100,
31+
})
32+
33+
if err != nil {
34+
t.Errorf("BuildGatherAlertmanagerLogs() error = %v, it must not fail to be build", err)
35+
return
36+
}
37+
38+
if gather.CanFail != canConditionalGathererFail {
39+
t.Errorf("BuildGatherAlertmanagerLogs() got = %v, want %v", gather.CanFail, canConditionalGathererFail)
40+
}
41+
}
42+
43+
func TestGatherer_gatherAlertmanagerLogs(t *testing.T) {
44+
ctx := context.TODO()
45+
testPod := &corev1.Pod{
46+
ObjectMeta: metav1.ObjectMeta{
47+
Name: "alertmanager-main-0",
48+
Namespace: "openshift-monitoring",
49+
},
50+
Status: corev1.PodStatus{
51+
Phase: corev1.PodRunning,
52+
ContainerStatuses: []corev1.ContainerStatus{
53+
{Name: "alertmanager"},
54+
},
55+
},
56+
Spec: corev1.PodSpec{
57+
Containers: []corev1.Container{
58+
{Name: "alertmanager"},
59+
},
60+
},
61+
}
62+
63+
coreClient := kubefake.NewSimpleClientset().CoreV1()
64+
_, err := coreClient.Pods("openshift-monitoring").Create(ctx, testPod, metav1.CreateOptions{})
65+
if err != nil {
66+
t.Fatalf("unable to create fake pod: %v", err)
67+
}
68+
69+
tests := []struct {
70+
name string
71+
params GatherAlertmanagerLogsParams
72+
want []record.Record
73+
wantErr []error
74+
}{
75+
{
76+
name: "Can record logs",
77+
params: GatherAlertmanagerLogsParams{
78+
AlertName: "AlertmanagerFailedToSendAlerts",
79+
TailLines: 100,
80+
},
81+
want: []record.Record{{
82+
// nolint:lll
83+
Name: "conditional/namespaces/openshift-monitoring/pods/alertmanager-main-0/containers/logs/alertmanager-alertmanagerfailedtosendalerts.log",
84+
Captured: time.Time{},
85+
Item: marshal.Raw{Str: "fake logs"},
86+
}},
87+
wantErr: nil,
88+
},
89+
}
90+
for _, tt := range tests {
91+
t.Run(tt.name, func(t *testing.T) {
92+
g := &Gatherer{firingAlerts: testAlertManagerFiringAlerts}
93+
got, gotErr := g.gatherAlertmanagerLogs(ctx, tt.params, coreClient)
94+
if !reflect.DeepEqual(got, tt.want) {
95+
t.Errorf("gatherAlertmanagerLogs() got = %v, want %v", got, tt.want)
96+
}
97+
if !reflect.DeepEqual(gotErr, tt.wantErr) {
98+
t.Errorf("gatherAlertmanagerLogs() gotErr = %v, want %v", gotErr, tt.wantErr)
99+
}
100+
})
101+
}
102+
}
103+
104+
// nolint:dupl
105+
func Test_getAlertPodName(t *testing.T) {
106+
tests := []struct {
107+
name string
108+
labels AlertLabels
109+
want string
110+
wantErr bool
111+
}{
112+
{
113+
name: "Pod name exists",
114+
labels: AlertLabels{"pod": "test-name"},
115+
want: "test-name",
116+
wantErr: false,
117+
},
118+
{
119+
name: "Pod name does not exists",
120+
labels: AlertLabels{},
121+
want: "",
122+
wantErr: true,
123+
},
124+
}
125+
for _, tt := range tests {
126+
t.Run(tt.name, func(t *testing.T) {
127+
got, err := getAlertPodName(tt.labels)
128+
if (err != nil) != tt.wantErr {
129+
t.Errorf("getAlertPodName() error = %v, wantErr %v", err, tt.wantErr)
130+
return
131+
}
132+
if got != tt.want {
133+
t.Errorf("getAlertPodName() got = %v, want %v", got, tt.want)
134+
}
135+
})
136+
}
137+
}
138+
139+
// nolint:dupl
140+
func Test_getAlertPodNamespace(t *testing.T) {
141+
tests := []struct {
142+
name string
143+
labels AlertLabels
144+
want string
145+
wantErr bool
146+
}{
147+
{
148+
name: "Pod namemespace exists",
149+
labels: AlertLabels{"namespace": "test-namespace"},
150+
want: "test-namespace",
151+
wantErr: false,
152+
},
153+
{
154+
name: "Pod namespace does not exists",
155+
labels: AlertLabels{},
156+
want: "",
157+
wantErr: true,
158+
},
159+
}
160+
for _, tt := range tests {
161+
t.Run(tt.name, func(t *testing.T) {
162+
got, err := getAlertPodNamespace(tt.labels)
163+
if (err != nil) != tt.wantErr {
164+
t.Errorf("getAlertPodNamespace() error = %v, wantErr %v", err, tt.wantErr)
165+
return
166+
}
167+
if got != tt.want {
168+
t.Errorf("getAlertPodNamespace() got = %v, want %v", got, tt.want)
169+
}
170+
})
171+
}
172+
}

0 commit comments

Comments
 (0)