|
7 | 7 | "time"
|
8 | 8 |
|
9 | 9 | "github.com/stretchr/testify/assert"
|
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
10 | 11 |
|
| 12 | + v1 "github.com/openshift/api/operator/v1" |
11 | 13 | fakeOperatorCli "github.com/openshift/client-go/operator/clientset/versioned/fake"
|
12 | 14 | "github.com/openshift/insights-operator/pkg/config"
|
13 | 15 | "github.com/openshift/insights-operator/pkg/gather"
|
@@ -177,3 +179,138 @@ func getMocksForPeriodicTest(listGatherers []gatherers.Interface, interval time.
|
177 | 179 | return New(&mockConfigurator, &mockRecorder, listGatherers, nil, fakeInsightsOperatorCli), &mockRecorder
|
178 | 180 | }
|
179 | 181 |
|
| 182 | +func Test_createGathererStatus(t *testing.T) { //nolint: funlen |
| 183 | + tests := []struct { |
| 184 | + name string |
| 185 | + gfr gather.GathererFunctionReport |
| 186 | + expectedGs v1.GathererStatus |
| 187 | + }{ |
| 188 | + { |
| 189 | + name: "Data gathered OK", |
| 190 | + gfr: gather.GathererFunctionReport{ |
| 191 | + FuncName: "gatherer1/foo", |
| 192 | + Duration: 115000, |
| 193 | + RecordsCount: 5, |
| 194 | + }, |
| 195 | + expectedGs: v1.GathererStatus{ |
| 196 | + Name: "gatherer1/foo", |
| 197 | + LastGatherDuration: metav1.Duration{ |
| 198 | + Duration: 115000000000, |
| 199 | + }, |
| 200 | + Conditions: []metav1.Condition{ |
| 201 | + { |
| 202 | + Type: DataGatheredCondition, |
| 203 | + Status: metav1.ConditionTrue, |
| 204 | + Reason: GatherOKReason, |
| 205 | + Message: "Created 5 records in the archive.", |
| 206 | + }, |
| 207 | + }, |
| 208 | + }, |
| 209 | + }, |
| 210 | + { |
| 211 | + name: "No Data", |
| 212 | + gfr: gather.GathererFunctionReport{ |
| 213 | + FuncName: "gatherer2/baz", |
| 214 | + Duration: 0, |
| 215 | + RecordsCount: 0, |
| 216 | + }, |
| 217 | + expectedGs: v1.GathererStatus{ |
| 218 | + Name: "gatherer2/baz", |
| 219 | + LastGatherDuration: metav1.Duration{ |
| 220 | + Duration: 0, |
| 221 | + }, |
| 222 | + Conditions: []metav1.Condition{ |
| 223 | + { |
| 224 | + Type: DataGatheredCondition, |
| 225 | + Status: metav1.ConditionFalse, |
| 226 | + Reason: NoDataGatheredReason, |
| 227 | + }, |
| 228 | + }, |
| 229 | + }, |
| 230 | + }, |
| 231 | + { |
| 232 | + name: "Gatherer Error", |
| 233 | + gfr: gather.GathererFunctionReport{ |
| 234 | + FuncName: "gatherer3/bar", |
| 235 | + Duration: 0, |
| 236 | + RecordsCount: 0, |
| 237 | + Errors: []string{"unable to read the data"}, |
| 238 | + }, |
| 239 | + expectedGs: v1.GathererStatus{ |
| 240 | + Name: "gatherer3/bar", |
| 241 | + LastGatherDuration: metav1.Duration{ |
| 242 | + Duration: 0, |
| 243 | + }, |
| 244 | + Conditions: []metav1.Condition{ |
| 245 | + { |
| 246 | + Type: DataGatheredCondition, |
| 247 | + Status: metav1.ConditionFalse, |
| 248 | + Reason: GatherErrorReason, |
| 249 | + Message: "unable to read the data", |
| 250 | + }, |
| 251 | + }, |
| 252 | + }, |
| 253 | + }, |
| 254 | + { |
| 255 | + name: "Data gathered with an error", |
| 256 | + gfr: gather.GathererFunctionReport{ |
| 257 | + FuncName: "gatherer4/quz", |
| 258 | + Duration: 9000, |
| 259 | + RecordsCount: 2, |
| 260 | + Errors: []string{"didn't find xyz configmap"}, |
| 261 | + }, |
| 262 | + expectedGs: v1.GathererStatus{ |
| 263 | + Name: "gatherer4/quz", |
| 264 | + LastGatherDuration: metav1.Duration{ |
| 265 | + Duration: 9000000000, |
| 266 | + }, |
| 267 | + Conditions: []metav1.Condition{ |
| 268 | + { |
| 269 | + Type: DataGatheredCondition, |
| 270 | + Status: metav1.ConditionTrue, |
| 271 | + Reason: GatherWithErrorReason, |
| 272 | + Message: "Created 2 records in the archive. Error: didn't find xyz configmap", |
| 273 | + }, |
| 274 | + }, |
| 275 | + }, |
| 276 | + }, |
| 277 | + { |
| 278 | + name: "Gatherer panicked", |
| 279 | + gfr: gather.GathererFunctionReport{ |
| 280 | + FuncName: "gatherer5/quz", |
| 281 | + Duration: 0, |
| 282 | + RecordsCount: 0, |
| 283 | + Panic: "quz gatherer panicked", |
| 284 | + }, |
| 285 | + expectedGs: v1.GathererStatus{ |
| 286 | + Name: "gatherer5/quz", |
| 287 | + LastGatherDuration: metav1.Duration{ |
| 288 | + Duration: 0, |
| 289 | + }, |
| 290 | + Conditions: []metav1.Condition{ |
| 291 | + { |
| 292 | + Type: DataGatheredCondition, |
| 293 | + Status: metav1.ConditionFalse, |
| 294 | + Reason: GatherPanicReason, |
| 295 | + Message: "quz gatherer panicked", |
| 296 | + }, |
| 297 | + }, |
| 298 | + }, |
| 299 | + }, |
| 300 | + } |
| 301 | + |
| 302 | + for _, tt := range tests { |
| 303 | + t.Run(tt.name, func(t *testing.T) { |
| 304 | + gathererStatus := createGathererStatus(&tt.gfr) |
| 305 | + assert.Equal(t, tt.expectedGs.Name, gathererStatus.Name) |
| 306 | + assert.Equal(t, tt.expectedGs.LastGatherDuration, gathererStatus.LastGatherDuration) |
| 307 | + |
| 308 | + // more asserts since we can use simple equal because of the last transition time of the condition |
| 309 | + assert.Len(t, gathererStatus.Conditions, 1) |
| 310 | + assert.Equal(t, tt.expectedGs.Conditions[0].Type, gathererStatus.Conditions[0].Type) |
| 311 | + assert.Equal(t, tt.expectedGs.Conditions[0].Reason, gathererStatus.Conditions[0].Reason) |
| 312 | + assert.Equal(t, tt.expectedGs.Conditions[0].Status, gathererStatus.Conditions[0].Status) |
| 313 | + assert.Equal(t, tt.expectedGs.Conditions[0].Message, gathererStatus.Conditions[0].Message) |
| 314 | + }) |
| 315 | + } |
| 316 | +} |
0 commit comments