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