forked from openshift/insights-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgather_aggregated_instances_test.go
183 lines (168 loc) · 5.95 KB
/
gather_aggregated_instances_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package clusterconfig
import (
"context"
"testing"
"github.com/openshift/insights-operator/pkg/record"
v1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/fake"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
// Test_GatherAggregatedInstances provides unit tests for the correct output file structure
func Test_GatherAggregatedInstances(t *testing.T) {
testCases := []struct {
name string
proms []*v1.Prometheus
alertMgrs []*v1.Alertmanager
expected []record.Record
}{
{
name: "The function returns the name of the Prometheus instance in the correct field",
proms: []*v1.Prometheus{
{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test-namespace"}},
},
expected: []record.Record{{
Name: "aggregated/custom_prometheuses_alertmanagers",
Item: record.JSONMarshaller{Object: aggregatedInstances{
Prometheuses: []string{"test"}, Alertmanagers: []string{},
}}},
},
}, {
name: "The function returns the name of the AlertManager instance in the correct field",
alertMgrs: []*v1.Alertmanager{
{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test-namespace"}},
},
expected: []record.Record{{
Name: "aggregated/custom_prometheuses_alertmanagers",
Item: record.JSONMarshaller{Object: aggregatedInstances{
Alertmanagers: []string{"test"}, Prometheuses: []string{},
}}},
},
}, {
name: "The function returns the names of the mixed instances in the correct field",
alertMgrs: []*v1.Alertmanager{
{ObjectMeta: metav1.ObjectMeta{Name: "test-alertmanager", Namespace: "test-namespace"}},
},
proms: []*v1.Prometheus{
{ObjectMeta: metav1.ObjectMeta{Name: "test-prometheus", Namespace: "test-namespace"}},
},
expected: []record.Record{{
Name: "aggregated/custom_prometheuses_alertmanagers",
Item: record.JSONMarshaller{Object: aggregatedInstances{
Alertmanagers: []string{"test-alertmanager"}, Prometheuses: []string{"test-prometheus"},
}}},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Given
items := []runtime.Object{}
if len(tc.proms) > 0 || len(tc.alertMgrs) > 0 {
for i := range tc.proms {
items = append(items, tc.proms[i])
}
for i := range tc.alertMgrs {
items = append(items, tc.alertMgrs[i])
}
}
clientset := fake.NewSimpleClientset(items...)
// When
test, errs := aggregatedInstances{}.gather(context.Background(), clientset)
// Assert
assert.Empty(t, errs)
assert.EqualValues(t, tc.expected, test)
})
}
}
// Test_getOutcastedAlertManagers provides unit tests for the namespace filtering logic of AlertManager instances
func Test_getOutcastedAlertManagers(t *testing.T) {
testCases := []struct {
name string
alertMgrs []*v1.Alertmanager
expected []string
}{
{
name: "The function returns the name of the Prometheus outside the 'openshift-monitoring' namespace",
alertMgrs: []*v1.Alertmanager{
{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test-namespace"}},
},
expected: []string{"test"},
}, {
name: "The function ignores the name of the Prometheus inside the 'openshift-monitoring' namespace",
alertMgrs: []*v1.Alertmanager{
{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "openshift-monitoring"}},
},
expected: []string{},
}, {
name: "The function returns only items outside of the namespace on a mixed response from client",
alertMgrs: []*v1.Alertmanager{
{ObjectMeta: metav1.ObjectMeta{Name: "test1", Namespace: "test-namespace"}},
{ObjectMeta: metav1.ObjectMeta{Name: "ignore", Namespace: "openshift-monitoring"}},
{ObjectMeta: metav1.ObjectMeta{Name: "test2", Namespace: "test-namespace"}},
},
expected: []string{"test1", "test2"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Given
items := make([]runtime.Object, len(tc.alertMgrs))
for i := range tc.alertMgrs {
items[i] = tc.alertMgrs[i]
}
clientset := fake.NewSimpleClientset(items...)
// When
test, err := aggregatedInstances{}.getOutcastedAlertManagers(context.Background(), clientset)
// Assert
assert.NoError(t, err)
assert.EqualValues(t, tc.expected, test)
})
}
}
// Test_getOutcastedPrometheuses provides unit tests for the namespace filtering logic of Prometheus instances
func Test_getOutcastedPrometheuses(t *testing.T) {
testCases := []struct {
name string
proms []*v1.Prometheus
expected []string
}{
{
name: "The function returns the name of the Prometheus outside the 'openshift-monitoring' namespace",
proms: []*v1.Prometheus{
{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test-namespace"}},
},
expected: []string{"test"},
}, {
name: "The function ignores the name of the Prometheus inside the 'openshift-monitoring' namespace",
proms: []*v1.Prometheus{
{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "openshift-monitoring"}},
},
expected: []string{},
}, {
name: "The function returns only items outside of the namespace on a mixed response from client",
proms: []*v1.Prometheus{
{ObjectMeta: metav1.ObjectMeta{Name: "test1", Namespace: "test-namespace"}},
{ObjectMeta: metav1.ObjectMeta{Name: "ignore", Namespace: "openshift-monitoring"}},
{ObjectMeta: metav1.ObjectMeta{Name: "test2", Namespace: "test-namespace"}},
},
expected: []string{"test1", "test2"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Given
items := make([]runtime.Object, len(tc.proms))
for i := range tc.proms {
items[i] = tc.proms[i]
}
clientset := fake.NewSimpleClientset(items...)
// When
test, err := aggregatedInstances{}.getOutcastedPrometheuses(context.Background(), clientset)
// Assert
assert.NoError(t, err)
assert.EqualValues(t, tc.expected, test)
})
}
}