externalsecret_validator_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package v1beta1
  13. import (
  14. "testing"
  15. "k8s.io/apimachinery/pkg/runtime"
  16. )
  17. func TestValidateExternalSecret(t *testing.T) {
  18. tests := []struct {
  19. name string
  20. obj runtime.Object
  21. wantErr bool
  22. }{
  23. {
  24. name: "nil",
  25. obj: nil,
  26. wantErr: true,
  27. },
  28. {
  29. name: "deletion policy delete",
  30. obj: &ExternalSecret{
  31. Spec: ExternalSecretSpec{
  32. Target: ExternalSecretTarget{
  33. DeletionPolicy: DeletionPolicyDelete,
  34. CreationPolicy: CreatePolicyMerge,
  35. },
  36. },
  37. },
  38. wantErr: true,
  39. },
  40. {
  41. name: "deletion policy merge",
  42. obj: &ExternalSecret{
  43. Spec: ExternalSecretSpec{
  44. Target: ExternalSecretTarget{
  45. DeletionPolicy: DeletionPolicyMerge,
  46. CreationPolicy: CreatePolicyNone,
  47. },
  48. },
  49. },
  50. wantErr: true,
  51. },
  52. {
  53. name: "generator with find",
  54. obj: &ExternalSecret{
  55. Spec: ExternalSecretSpec{
  56. DataFrom: []ExternalSecretDataFromRemoteRef{
  57. {
  58. Find: &ExternalSecretFind{},
  59. SourceRef: &StoreGeneratorSourceRef{
  60. GeneratorRef: &GeneratorRef{},
  61. },
  62. },
  63. },
  64. },
  65. },
  66. wantErr: true,
  67. },
  68. {
  69. name: "generator with extract",
  70. obj: &ExternalSecret{
  71. Spec: ExternalSecretSpec{
  72. DataFrom: []ExternalSecretDataFromRemoteRef{
  73. {
  74. Extract: &ExternalSecretDataRemoteRef{},
  75. SourceRef: &StoreGeneratorSourceRef{
  76. GeneratorRef: &GeneratorRef{},
  77. },
  78. },
  79. },
  80. },
  81. },
  82. wantErr: true,
  83. },
  84. {
  85. name: "valid",
  86. obj: &ExternalSecret{
  87. Spec: ExternalSecretSpec{
  88. DataFrom: []ExternalSecretDataFromRemoteRef{},
  89. },
  90. },
  91. },
  92. }
  93. for _, tt := range tests {
  94. t.Run(tt.name, func(t *testing.T) {
  95. if _, err := validateExternalSecret(tt.obj); (err != nil) != tt.wantErr {
  96. t.Errorf("validateExternalSecret() error = %v, wantErr %v", err, tt.wantErr)
  97. }
  98. })
  99. }
  100. }