uninstall_eso_crds_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package addon
  14. import (
  15. "context"
  16. "testing"
  17. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "sigs.k8s.io/controller-runtime/pkg/client/fake"
  21. )
  22. func TestIsExternalSecretsCRDGroup(t *testing.T) {
  23. t.Helper()
  24. if !isExternalSecretsCRDGroup("external-secrets.io") {
  25. t.Fatal("expected external-secrets.io group to match")
  26. }
  27. if !isExternalSecretsCRDGroup("provider.external-secrets.io") {
  28. t.Fatal("expected provider.external-secrets.io group to match")
  29. }
  30. if isExternalSecretsCRDGroup("example.com") {
  31. t.Fatal("did not expect unrelated group to match")
  32. }
  33. }
  34. func TestListExternalSecretsCRDsFiltersByGroup(t *testing.T) {
  35. t.Helper()
  36. scheme := runtime.NewScheme()
  37. if err := apiextensionsv1.AddToScheme(scheme); err != nil {
  38. t.Fatalf("add apiextensions scheme: %v", err)
  39. }
  40. cfg := &Config{
  41. CRClient: fake.NewClientBuilder().WithScheme(scheme).WithObjects(
  42. &apiextensionsv1.CustomResourceDefinition{
  43. ObjectMeta: metav1.ObjectMeta{Name: "externalsecrets.external-secrets.io"},
  44. Spec: apiextensionsv1.CustomResourceDefinitionSpec{Group: "external-secrets.io"},
  45. },
  46. &apiextensionsv1.CustomResourceDefinition{
  47. ObjectMeta: metav1.ObjectMeta{Name: "providers.provider.external-secrets.io"},
  48. Spec: apiextensionsv1.CustomResourceDefinitionSpec{Group: "provider.external-secrets.io"},
  49. },
  50. &apiextensionsv1.CustomResourceDefinition{
  51. ObjectMeta: metav1.ObjectMeta{Name: "widgets.example.com"},
  52. Spec: apiextensionsv1.CustomResourceDefinitionSpec{Group: "example.com"},
  53. },
  54. ).Build(),
  55. }
  56. crds, err := listExternalSecretsCRDs(context.Background(), cfg)
  57. if err != nil {
  58. t.Fatalf("listExternalSecretsCRDs returned error: %v", err)
  59. }
  60. if len(crds) != 2 {
  61. t.Fatalf("expected 2 external-secrets CRDs, got %d", len(crds))
  62. }
  63. }