|
9 | 9 | "fmt" |
10 | 10 | "net/http" |
11 | 11 | "net/http/httptest" |
| 12 | + "reflect" |
| 13 | + "regexp" |
12 | 14 | "strings" |
13 | 15 | "testing" |
14 | 16 | "time" |
@@ -79,6 +81,13 @@ func createHelmServer() *httptest.Server { |
79 | 81 | })) |
80 | 82 | } |
81 | 83 |
|
| 84 | +func createHelmServerWithErrorHTML() *httptest.Server { |
| 85 | + return httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 86 | + w.WriteHeader(http.StatusNotFound) |
| 87 | + fmt.Fprint(w, `<!DOCTYPE html><html><head><meta charset=\"utf-8\"><body><p>Some irrelevant content here</p></body></html>`) |
| 88 | + })) |
| 89 | +} |
| 90 | + |
82 | 91 | func getCondition(helmop *fleet.HelmOp, condType string) (genericcondition.GenericCondition, bool) { |
83 | 92 | for _, cond := range helmop.Status.Conditions { |
84 | 93 | if cond.Type == condType { |
@@ -358,75 +367,170 @@ func TestReconcile_Validate(t *testing.T) { |
358 | 367 | } |
359 | 368 |
|
360 | 369 | func TestReconcile_ErrorCreatingBundleIsShownInStatus(t *testing.T) { |
361 | | - mockCtrl := gomock.NewController(t) |
362 | | - defer mockCtrl.Finish() |
363 | | - scheme := runtime.NewScheme() |
364 | | - utilruntime.Must(batchv1.AddToScheme(scheme)) |
365 | | - helmop := fleet.HelmOp{ |
366 | | - ObjectMeta: metav1.ObjectMeta{ |
367 | | - Name: "helmop", |
368 | | - Namespace: "default", |
369 | | - }, |
370 | | - } |
371 | | - namespacedName := types.NamespacedName{Name: helmop.Name, Namespace: helmop.Namespace} |
372 | | - client := mocks.NewMockK8sClient(mockCtrl) |
373 | | - client.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(1).DoAndReturn( |
374 | | - func(ctx context.Context, req types.NamespacedName, fh *fleet.HelmOp, opts ...interface{}) error { |
375 | | - fh.Name = helmop.Name |
376 | | - fh.Namespace = helmop.Namespace |
377 | | - fh.Spec.Helm = &fleet.HelmOptions{ |
378 | | - Chart: "chart.tgz", |
379 | | - } |
380 | | - fh.Status = fleet.HelmOpStatus{} |
| 370 | + t.Run("propagating error seen when getting the bundle", func(t *testing.T) { |
| 371 | + mockCtrl := gomock.NewController(t) |
| 372 | + defer mockCtrl.Finish() |
| 373 | + scheme := runtime.NewScheme() |
| 374 | + utilruntime.Must(batchv1.AddToScheme(scheme)) |
| 375 | + helmop := fleet.HelmOp{ |
| 376 | + ObjectMeta: metav1.ObjectMeta{ |
| 377 | + Name: "helmop", |
| 378 | + Namespace: "default", |
| 379 | + }, |
| 380 | + } |
| 381 | + namespacedName := types.NamespacedName{Name: helmop.Name, Namespace: helmop.Namespace} |
| 382 | + client := mocks.NewMockK8sClient(mockCtrl) |
| 383 | + client.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(1).DoAndReturn( |
| 384 | + func(ctx context.Context, req types.NamespacedName, fh *fleet.HelmOp, opts ...interface{}) error { |
| 385 | + fh.Name = helmop.Name |
| 386 | + fh.Namespace = helmop.Namespace |
| 387 | + fh.Spec.Helm = &fleet.HelmOptions{ |
| 388 | + Chart: "chart.tgz", |
| 389 | + } |
| 390 | + fh.Status = fleet.HelmOpStatus{} |
| 391 | + |
| 392 | + controllerutil.AddFinalizer(fh, finalize.HelmOpFinalizer) |
| 393 | + return nil |
| 394 | + }, |
| 395 | + ) |
381 | 396 |
|
382 | | - controllerutil.AddFinalizer(fh, finalize.HelmOpFinalizer) |
383 | | - return nil |
384 | | - }, |
385 | | - ) |
| 397 | + client.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(1).DoAndReturn( |
| 398 | + func(ctx context.Context, req types.NamespacedName, bundle *fleet.Bundle, opts ...interface{}) error { |
| 399 | + return fmt.Errorf("this is a test error") |
| 400 | + }, |
| 401 | + ) |
386 | 402 |
|
387 | | - client.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(1).DoAndReturn( |
388 | | - func(ctx context.Context, req types.NamespacedName, bundle *fleet.Bundle, opts ...interface{}) error { |
389 | | - return fmt.Errorf("this is a test error") |
390 | | - }, |
391 | | - ) |
| 403 | + client.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(1).DoAndReturn( |
| 404 | + func(ctx context.Context, req types.NamespacedName, bundle *fleet.HelmOp, opts ...interface{}) error { |
| 405 | + return nil |
| 406 | + }, |
| 407 | + ) |
392 | 408 |
|
393 | | - client.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(1).DoAndReturn( |
394 | | - func(ctx context.Context, req types.NamespacedName, bundle *fleet.HelmOp, opts ...interface{}) error { |
395 | | - return nil |
396 | | - }, |
397 | | - ) |
| 409 | + statusClient := mocks.NewMockSubResourceWriter(mockCtrl) |
| 410 | + client.EXPECT().Status().Return(statusClient).Times(1) |
| 411 | + statusClient.EXPECT().Patch(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Do( |
| 412 | + func(ctx context.Context, helmop *fleet.HelmOp, patch crclient.Patch, opts ...interface{}) { |
| 413 | + c, found := getCondition(helmop, fleet.HelmOpAcceptedCondition) |
| 414 | + if !found { |
| 415 | + t.Errorf("expecting to find the %s condition and could not find it.", fleet.HelmOpAcceptedCondition) |
| 416 | + } |
| 417 | + if c.Message != "this is a test error" { |
| 418 | + t.Errorf("expecting message [this is a test error] in condition, got [%s]", c.Message) |
| 419 | + } |
| 420 | + }, |
| 421 | + ).Times(1) |
398 | 422 |
|
399 | | - statusClient := mocks.NewMockSubResourceWriter(mockCtrl) |
400 | | - client.EXPECT().Status().Return(statusClient).Times(1) |
401 | | - statusClient.EXPECT().Patch(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Do( |
402 | | - func(ctx context.Context, helmop *fleet.HelmOp, patch crclient.Patch, opts ...interface{}) { |
403 | | - c, found := getCondition(helmop, fleet.HelmOpAcceptedCondition) |
404 | | - if !found { |
405 | | - t.Errorf("expecting to find the %s condition and could not find it.", fleet.HelmOpAcceptedCondition) |
406 | | - } |
407 | | - if c.Message != "this is a test error" { |
408 | | - t.Errorf("expecting message [this is a test error] in condition, got [%s]", c.Message) |
409 | | - } |
410 | | - }, |
411 | | - ).Times(1) |
| 423 | + r := HelmOpReconciler{ |
| 424 | + Client: client, |
| 425 | + Scheme: scheme, |
| 426 | + } |
412 | 427 |
|
413 | | - r := HelmOpReconciler{ |
414 | | - Client: client, |
415 | | - Scheme: scheme, |
416 | | - } |
| 428 | + ctx := context.TODO() |
417 | 429 |
|
418 | | - ctx := context.TODO() |
| 430 | + res, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: namespacedName}) |
| 431 | + if err == nil { |
| 432 | + t.Errorf("expecting error, got nil") |
| 433 | + } |
| 434 | + if err.Error() != "this is a test error" { |
| 435 | + t.Errorf("expecting error: [this is a test error], got %v", err.Error()) |
| 436 | + } |
| 437 | + if res.RequeueAfter != 0 { |
| 438 | + t.Errorf("expecting no requeue when there's an error, but got RequeueAfter: %v", res.RequeueAfter) |
| 439 | + } |
| 440 | + }) |
| 441 | + |
| 442 | + t.Run("propagating error from Helm registry without HTML response body", func(t *testing.T) { |
| 443 | + svr := createHelmServerWithErrorHTML() |
| 444 | + defer svr.Close() |
| 445 | + |
| 446 | + mockCtrl := gomock.NewController(t) |
| 447 | + defer mockCtrl.Finish() |
| 448 | + scheme := runtime.NewScheme() |
| 449 | + utilruntime.Must(batchv1.AddToScheme(scheme)) |
| 450 | + helmop := fleet.HelmOp{ |
| 451 | + ObjectMeta: metav1.ObjectMeta{ |
| 452 | + Name: "helmop", |
| 453 | + Namespace: "default", |
| 454 | + }, |
| 455 | + Spec: fleet.HelmOpSpec{ |
| 456 | + BundleSpec: fleet.BundleSpec{ |
| 457 | + BundleDeploymentOptions: fleet.BundleDeploymentOptions{ |
| 458 | + Helm: &fleet.HelmOptions{ |
| 459 | + Repo: svr.URL, |
| 460 | + Chart: "alpine", |
| 461 | + Version: "0.1.0", // static version |
| 462 | + }, |
| 463 | + }, |
| 464 | + }, |
| 465 | + InsecureSkipTLSverify: true, |
| 466 | + }, |
| 467 | + } |
419 | 468 |
|
420 | | - res, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: namespacedName}) |
421 | | - if err == nil { |
422 | | - t.Errorf("expecting error, got nil") |
423 | | - } |
424 | | - if err.Error() != "this is a test error" { |
425 | | - t.Errorf("expecting error: [this is a test error], got %v", err.Error()) |
426 | | - } |
427 | | - if res.RequeueAfter != 0 { |
428 | | - t.Errorf("expecting no requeue when there's an error, but got RequeueAfter: %v", res.RequeueAfter) |
429 | | - } |
| 469 | + namespacedName := types.NamespacedName{Name: helmop.Name, Namespace: helmop.Namespace} |
| 470 | + client := mocks.NewMockK8sClient(mockCtrl) |
| 471 | + client.EXPECT().Get(gomock.Any(), gomock.Any(), OfType(&fleet.HelmOp{}), gomock.Any()).Times(1).DoAndReturn( |
| 472 | + func(ctx context.Context, req types.NamespacedName, fh *fleet.HelmOp, opts ...interface{}) error { |
| 473 | + fh.Name = helmop.Name |
| 474 | + fh.Namespace = helmop.Namespace |
| 475 | + fh.Spec = helmop.Spec |
| 476 | + fh.Status = fleet.HelmOpStatus{} |
| 477 | + |
| 478 | + controllerutil.AddFinalizer(fh, finalize.HelmOpFinalizer) |
| 479 | + return nil |
| 480 | + }, |
| 481 | + ) |
| 482 | + |
| 483 | + client.EXPECT().Get(gomock.Any(), gomock.Any(), OfType(&fleet.Bundle{}), gomock.Any()).AnyTimes().DoAndReturn( |
| 484 | + func(ctx context.Context, req types.NamespacedName, bundle *fleet.Bundle, opts ...interface{}) error { |
| 485 | + bundle.Spec.HelmOpOptions = &fleet.BundleHelmOptions{} |
| 486 | + return nil |
| 487 | + }, |
| 488 | + ) |
| 489 | + |
| 490 | + client.EXPECT().Get(gomock.Any(), gomock.Any(), OfType(&fleet.HelmOp{}), gomock.Any()).AnyTimes().DoAndReturn( |
| 491 | + func(ctx context.Context, req types.NamespacedName, helmOp *fleet.HelmOp, opts ...interface{}) error { |
| 492 | + return nil |
| 493 | + }, |
| 494 | + ) |
| 495 | + |
| 496 | + statusClient := mocks.NewMockSubResourceWriter(mockCtrl) |
| 497 | + client.EXPECT().Status().Return(statusClient).Times(1) |
| 498 | + statusClient.EXPECT().Patch(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Do( |
| 499 | + func(ctx context.Context, helmop *fleet.HelmOp, patch crclient.Patch, opts ...interface{}) { |
| 500 | + c, found := getCondition(helmop, fleet.HelmOpAcceptedCondition) |
| 501 | + if !found { |
| 502 | + t.Errorf("expecting to find the %s condition and could not find it.", fleet.HelmOpAcceptedCondition) |
| 503 | + } |
| 504 | + if !strings.Contains(c.Message, "404") { |
| 505 | + t.Errorf("expecting message to contain [404] in condition, got [%s]", c.Message) |
| 506 | + } |
| 507 | + }, |
| 508 | + ).Times(1) |
| 509 | + |
| 510 | + r := HelmOpReconciler{ |
| 511 | + Client: client, |
| 512 | + Scheme: scheme, |
| 513 | + } |
| 514 | + |
| 515 | + ctx := context.TODO() |
| 516 | + |
| 517 | + res, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: namespacedName}) |
| 518 | + if err == nil { |
| 519 | + t.Errorf("expecting error, got nil") |
| 520 | + } |
| 521 | + |
| 522 | + errRegex := "could not get a chart version.*error code: 404" |
| 523 | + match, rErr := regexp.Match(errRegex, []byte(err.Error())) |
| 524 | + if rErr != nil { |
| 525 | + t.Errorf("something went wrong when compiling the regex: %v", rErr) |
| 526 | + } |
| 527 | + if !match { |
| 528 | + t.Errorf("expecting error matching %q, got %v", errRegex, err) |
| 529 | + } |
| 530 | + if res.RequeueAfter != 0 { |
| 531 | + t.Errorf("expecting no requeue when there's an error, but got RequeueAfter: %v", res.RequeueAfter) |
| 532 | + } |
| 533 | + }) |
430 | 534 | } |
431 | 535 |
|
432 | 536 | // Validates that the HelmOps reconciler will not create a bundle if another bundle exists with the same name, for |
@@ -1264,3 +1368,17 @@ func (s *scheduledJobMatcher) Matches(x interface{}) bool { |
1264 | 1368 | func (s *scheduledJobMatcher) String() string { |
1265 | 1369 | return fmt.Sprintf("matches replace %t and job key %s", s.replaceExisting, s.key) |
1266 | 1370 | } |
| 1371 | + |
| 1372 | +type typeMatcher struct{ t interface{} } |
| 1373 | + |
| 1374 | +func OfType(t interface{}) gomock.Matcher { |
| 1375 | + return &typeMatcher{t} |
| 1376 | +} |
| 1377 | + |
| 1378 | +func (tm *typeMatcher) Matches(x interface{}) bool { |
| 1379 | + return reflect.TypeOf(x) == reflect.TypeOf(tm.t) |
| 1380 | +} |
| 1381 | + |
| 1382 | +func (tm *typeMatcher) String() string { |
| 1383 | + return "is of type " + reflect.TypeOf(tm.t).String() |
| 1384 | +} |
0 commit comments