externalsecret_crd_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 v1
  14. import (
  15. "os"
  16. "path/filepath"
  17. "slices"
  18. "testing"
  19. "sigs.k8s.io/yaml"
  20. )
  21. func TestExternalSecretCRDSecretStoreRefKindOmitsProviderKinds(t *testing.T) {
  22. kindEnum := externalSecretCRDKindEnum(t, "v1")
  23. assertContains := func(value string) {
  24. t.Helper()
  25. if slices.Contains(kindEnum, value) {
  26. return
  27. }
  28. t.Fatalf("kind enum does not contain %q: %v", value, kindEnum)
  29. }
  30. assertNotContains := func(value string) {
  31. t.Helper()
  32. if slices.Contains(kindEnum, value) {
  33. t.Fatalf("kind enum unexpectedly contains %q: %v", value, kindEnum)
  34. }
  35. }
  36. assertContains("ProviderStore")
  37. assertContains("ClusterProviderStore")
  38. assertNotContains("Provider")
  39. assertNotContains("ClusterProvider")
  40. }
  41. func externalSecretCRDKindEnum(t *testing.T, versionName string) []string {
  42. t.Helper()
  43. crdPath := filepath.Join("..", "..", "..", "config", "crds", "bases", "external-secrets.io_externalsecrets.yaml")
  44. data, err := os.ReadFile(crdPath)
  45. if err != nil {
  46. t.Fatalf("read CRD: %v", err)
  47. }
  48. var crd map[string]any
  49. if err := yaml.Unmarshal(data, &crd); err != nil {
  50. t.Fatalf("unmarshal CRD: %v", err)
  51. }
  52. versions := asSlice(t, asMap(t, crd["spec"], "spec")["versions"], "spec.versions")
  53. for _, version := range versions {
  54. versionMap := asMap(t, version, "spec.versions[]")
  55. if versionMap["name"] != versionName {
  56. continue
  57. }
  58. schema := asMap(t, versionMap["schema"], "spec.versions[].schema")
  59. openAPIV3 := asMap(t, schema["openAPIV3Schema"], "spec.versions[].schema.openAPIV3Schema")
  60. properties := asMap(t, openAPIV3["properties"], "spec.versions[].schema.openAPIV3Schema.properties")
  61. specProperties := asMap(t, asMap(t, properties["spec"], "spec property")["properties"], "spec.properties")
  62. secretStoreRef := asMap(t, asMap(t, specProperties["secretStoreRef"], "spec.properties.secretStoreRef")["properties"], "spec.properties.secretStoreRef.properties")
  63. kindSchema := asMap(t, secretStoreRef["kind"], "spec.properties.secretStoreRef.properties.kind")
  64. return asStringSlice(t, kindSchema["enum"], "spec.properties.secretStoreRef.properties.kind.enum")
  65. }
  66. t.Fatalf("did not find %s secretStoreRef.kind enum", versionName)
  67. return nil
  68. }
  69. func asMap(t *testing.T, v any, path string) map[string]any {
  70. t.Helper()
  71. m, ok := v.(map[string]any)
  72. if !ok {
  73. t.Fatalf("%s is %T, want map[string]any", path, v)
  74. }
  75. return m
  76. }
  77. func asSlice(t *testing.T, v any, path string) []any {
  78. t.Helper()
  79. s, ok := v.([]any)
  80. if !ok {
  81. t.Fatalf("%s is %T, want []any", path, v)
  82. }
  83. return s
  84. }
  85. func asStringSlice(t *testing.T, v any, path string) []string {
  86. t.Helper()
  87. s := asSlice(t, v, path)
  88. out := make([]string, 0, len(s))
  89. for i, entry := range s {
  90. str, ok := entry.(string)
  91. if !ok {
  92. t.Fatalf("%s[%d] is %T, want string", path, i, entry)
  93. }
  94. out = append(out, str)
  95. }
  96. return out
  97. }