|
4 | 4 | "testing" |
5 | 5 |
|
6 | 6 | "github.com/stretchr/testify/assert" |
| 7 | + corev1 "k8s.io/api/core/v1" |
7 | 8 |
|
8 | 9 | metal3api "github.com/metal3-io/ironic-standalone-operator/api/v1alpha1" |
9 | 10 | ) |
@@ -493,3 +494,163 @@ func TestValidateCASettings(t *testing.T) { |
493 | 494 | }) |
494 | 495 | } |
495 | 496 | } |
| 497 | + |
| 498 | +func TestResourcesValidate(t *testing.T) { |
| 499 | + testCases := []struct { |
| 500 | + Scenario string |
| 501 | + Resources Resources |
| 502 | + ExpectedError string |
| 503 | + }{ |
| 504 | + { |
| 505 | + Scenario: "minimal valid resources", |
| 506 | + Resources: Resources{ |
| 507 | + Ironic: &metal3api.Ironic{}, |
| 508 | + }, |
| 509 | + }, |
| 510 | + { |
| 511 | + Scenario: "trustedCA secret with matching key", |
| 512 | + Resources: Resources{ |
| 513 | + Ironic: &metal3api.Ironic{ |
| 514 | + Spec: metal3api.IronicSpec{ |
| 515 | + TLS: metal3api.TLS{ |
| 516 | + TrustedCA: &metal3api.ResourceReferenceWithKey{ |
| 517 | + ResourceReference: metal3api.ResourceReference{ |
| 518 | + Name: "my-ca", |
| 519 | + Kind: metal3api.ResourceKindSecret, |
| 520 | + }, |
| 521 | + Key: "ca.crt", |
| 522 | + }, |
| 523 | + }, |
| 524 | + }, |
| 525 | + }, |
| 526 | + TrustedCASecret: &corev1.Secret{ |
| 527 | + Data: map[string][]byte{ |
| 528 | + "ca.crt": []byte("cert-data"), |
| 529 | + }, |
| 530 | + }, |
| 531 | + }, |
| 532 | + }, |
| 533 | + { |
| 534 | + Scenario: "trustedCA secret with missing key", |
| 535 | + Resources: Resources{ |
| 536 | + Ironic: &metal3api.Ironic{ |
| 537 | + Spec: metal3api.IronicSpec{ |
| 538 | + TLS: metal3api.TLS{ |
| 539 | + TrustedCA: &metal3api.ResourceReferenceWithKey{ |
| 540 | + ResourceReference: metal3api.ResourceReference{ |
| 541 | + Name: "my-ca", |
| 542 | + Kind: metal3api.ResourceKindSecret, |
| 543 | + }, |
| 544 | + Key: "missing-key", |
| 545 | + }, |
| 546 | + }, |
| 547 | + }, |
| 548 | + }, |
| 549 | + TrustedCASecret: &corev1.Secret{ |
| 550 | + Data: map[string][]byte{ |
| 551 | + "ca.crt": []byte("cert-data"), |
| 552 | + }, |
| 553 | + }, |
| 554 | + }, |
| 555 | + ExpectedError: "does not contain the required key missing-key", |
| 556 | + }, |
| 557 | + { |
| 558 | + Scenario: "trustedCA configmap with matching key", |
| 559 | + Resources: Resources{ |
| 560 | + Ironic: &metal3api.Ironic{ |
| 561 | + Spec: metal3api.IronicSpec{ |
| 562 | + TLS: metal3api.TLS{ |
| 563 | + TrustedCA: &metal3api.ResourceReferenceWithKey{ |
| 564 | + ResourceReference: metal3api.ResourceReference{ |
| 565 | + Name: "my-ca", |
| 566 | + Kind: metal3api.ResourceKindConfigMap, |
| 567 | + }, |
| 568 | + Key: "ca-bundle.crt", |
| 569 | + }, |
| 570 | + }, |
| 571 | + }, |
| 572 | + }, |
| 573 | + TrustedCAConfigMap: &corev1.ConfigMap{ |
| 574 | + Data: map[string]string{ |
| 575 | + "ca-bundle.crt": "cert-data", |
| 576 | + }, |
| 577 | + }, |
| 578 | + }, |
| 579 | + }, |
| 580 | + { |
| 581 | + Scenario: "trustedCA configmap with missing key", |
| 582 | + Resources: Resources{ |
| 583 | + Ironic: &metal3api.Ironic{ |
| 584 | + Spec: metal3api.IronicSpec{ |
| 585 | + TLS: metal3api.TLS{ |
| 586 | + TrustedCA: &metal3api.ResourceReferenceWithKey{ |
| 587 | + ResourceReference: metal3api.ResourceReference{ |
| 588 | + Name: "my-ca", |
| 589 | + Kind: metal3api.ResourceKindConfigMap, |
| 590 | + }, |
| 591 | + Key: "missing-key", |
| 592 | + }, |
| 593 | + }, |
| 594 | + }, |
| 595 | + }, |
| 596 | + TrustedCAConfigMap: &corev1.ConfigMap{ |
| 597 | + Data: map[string]string{ |
| 598 | + "ca-bundle.crt": "cert-data", |
| 599 | + }, |
| 600 | + }, |
| 601 | + }, |
| 602 | + ExpectedError: "does not contain the required key missing-key", |
| 603 | + }, |
| 604 | + { |
| 605 | + Scenario: "trustedCA with empty key skips key check", |
| 606 | + Resources: Resources{ |
| 607 | + Ironic: &metal3api.Ironic{ |
| 608 | + Spec: metal3api.IronicSpec{ |
| 609 | + TLS: metal3api.TLS{ |
| 610 | + TrustedCA: &metal3api.ResourceReferenceWithKey{ |
| 611 | + ResourceReference: metal3api.ResourceReference{ |
| 612 | + Name: "my-ca", |
| 613 | + Kind: metal3api.ResourceKindConfigMap, |
| 614 | + }, |
| 615 | + }, |
| 616 | + }, |
| 617 | + }, |
| 618 | + }, |
| 619 | + TrustedCAConfigMap: &corev1.ConfigMap{ |
| 620 | + Data: map[string]string{ |
| 621 | + "ca-bundle.crt": "cert-data", |
| 622 | + }, |
| 623 | + }, |
| 624 | + }, |
| 625 | + }, |
| 626 | + { |
| 627 | + Scenario: "trustedCA without resource defaults to valid", |
| 628 | + Resources: Resources{ |
| 629 | + Ironic: &metal3api.Ironic{ |
| 630 | + Spec: metal3api.IronicSpec{ |
| 631 | + TLS: metal3api.TLS{ |
| 632 | + TrustedCA: &metal3api.ResourceReferenceWithKey{ |
| 633 | + ResourceReference: metal3api.ResourceReference{ |
| 634 | + Name: "my-ca", |
| 635 | + Kind: metal3api.ResourceKindConfigMap, |
| 636 | + }, |
| 637 | + Key: "ca.crt", |
| 638 | + }, |
| 639 | + }, |
| 640 | + }, |
| 641 | + }, |
| 642 | + }, |
| 643 | + }, |
| 644 | + } |
| 645 | + |
| 646 | + for _, tc := range testCases { |
| 647 | + t.Run(tc.Scenario, func(t *testing.T) { |
| 648 | + err := tc.Resources.Validate() |
| 649 | + if tc.ExpectedError == "" { |
| 650 | + assert.NoError(t, err) |
| 651 | + } else { |
| 652 | + assert.ErrorContains(t, err, tc.ExpectedError) |
| 653 | + } |
| 654 | + }) |
| 655 | + } |
| 656 | +} |
0 commit comments