pushsecret_crd_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 v1alpha1
  14. import (
  15. "os"
  16. "path/filepath"
  17. "testing"
  18. "sigs.k8s.io/yaml"
  19. )
  20. func TestPushSecretCRDDoesNotDefaultSecretStoreRefKind(t *testing.T) {
  21. crdPath := filepath.Join("..", "..", "..", "config", "crds", "bases", "external-secrets.io_pushsecrets.yaml")
  22. data, err := os.ReadFile(crdPath)
  23. if err != nil {
  24. t.Fatalf("read CRD: %v", err)
  25. }
  26. var crd map[string]any
  27. if err := yaml.Unmarshal(data, &crd); err != nil {
  28. t.Fatalf("unmarshal CRD: %v", err)
  29. }
  30. versions := asSlice(t, asMap(t, crd["spec"], "spec")["versions"], "spec.versions")
  31. var kindSchema map[string]any
  32. for _, version := range versions {
  33. versionMap := asMap(t, version, "spec.versions[]")
  34. if versionMap["name"] != "v1alpha1" {
  35. continue
  36. }
  37. schema := asMap(t, versionMap["schema"], "spec.versions[].schema")
  38. openAPIV3 := asMap(t, schema["openAPIV3Schema"], "spec.versions[].schema.openAPIV3Schema")
  39. properties := asMap(t, openAPIV3["properties"], "spec.versions[].schema.openAPIV3Schema.properties")
  40. specProperties := asMap(t, asMap(t, properties["spec"], "spec property")["properties"], "spec.properties")
  41. secretStoreRefs := asMap(t, specProperties["secretStoreRefs"], "spec.properties.secretStoreRefs")
  42. items := asMap(t, secretStoreRefs["items"], "spec.properties.secretStoreRefs.items")
  43. itemProperties := asMap(t, items["properties"], "spec.properties.secretStoreRefs.items.properties")
  44. kindSchema = asMap(t, itemProperties["kind"], "spec.properties.secretStoreRefs.items.properties.kind")
  45. break
  46. }
  47. if kindSchema == nil {
  48. t.Fatal("did not find v1alpha1 secretStoreRefs.kind schema")
  49. }
  50. if def, ok := kindSchema["default"]; ok {
  51. t.Fatalf("secretStoreRefs.kind must not define a CRD default, got %v", def)
  52. }
  53. }
  54. func asMap(t *testing.T, v any, path string) map[string]any {
  55. t.Helper()
  56. m, ok := v.(map[string]any)
  57. if !ok {
  58. t.Fatalf("%s is %T, want map[string]any", path, v)
  59. }
  60. return m
  61. }
  62. func asSlice(t *testing.T, v any, path string) []any {
  63. t.Helper()
  64. s, ok := v.([]any)
  65. if !ok {
  66. t.Fatalf("%s is %T, want []any", path, v)
  67. }
  68. return s
  69. }