externalsecret_validator_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. expectedErr string
  22. }{
  23. {
  24. name: "nil",
  25. obj: nil,
  26. expectedErr: "unexpected type",
  27. },
  28. {
  29. name: "deletion policy delete",
  30. obj: &ExternalSecret{
  31. Spec: ExternalSecretSpec{
  32. Target: ExternalSecretTarget{
  33. DeletionPolicy: DeletionPolicyDelete,
  34. CreationPolicy: CreatePolicyMerge,
  35. },
  36. Data: []ExternalSecretData{
  37. {},
  38. },
  39. },
  40. },
  41. expectedErr: "deletionPolicy=Delete must not be used when the controller doesn't own the secret. Please set creationPolicy=Owner",
  42. },
  43. {
  44. name: "deletion policy merge",
  45. obj: &ExternalSecret{
  46. Spec: ExternalSecretSpec{
  47. Target: ExternalSecretTarget{
  48. DeletionPolicy: DeletionPolicyMerge,
  49. CreationPolicy: CreatePolicyNone,
  50. },
  51. Data: []ExternalSecretData{
  52. {},
  53. },
  54. },
  55. },
  56. expectedErr: "deletionPolicy=Merge must not be used with creationPolicy=None. There is no Secret to merge with",
  57. },
  58. {
  59. name: "both data and data_from are empty",
  60. obj: &ExternalSecret{
  61. Spec: ExternalSecretSpec{},
  62. },
  63. expectedErr: "either data or dataFrom should be specified",
  64. },
  65. {
  66. name: "generator with find",
  67. obj: &ExternalSecret{
  68. Spec: ExternalSecretSpec{
  69. DataFrom: []ExternalSecretDataFromRemoteRef{
  70. {
  71. Find: &ExternalSecretFind{},
  72. SourceRef: &StoreGeneratorSourceRef{
  73. GeneratorRef: &GeneratorRef{},
  74. },
  75. },
  76. },
  77. },
  78. },
  79. expectedErr: "generator can not be used with find or extract",
  80. },
  81. {
  82. name: "generator with extract",
  83. obj: &ExternalSecret{
  84. Spec: ExternalSecretSpec{
  85. DataFrom: []ExternalSecretDataFromRemoteRef{
  86. {
  87. Extract: &ExternalSecretDataRemoteRef{},
  88. SourceRef: &StoreGeneratorSourceRef{
  89. GeneratorRef: &GeneratorRef{},
  90. },
  91. },
  92. },
  93. },
  94. },
  95. expectedErr: "generator can not be used with find or extract",
  96. },
  97. {
  98. name: "multiple errors",
  99. obj: &ExternalSecret{
  100. Spec: ExternalSecretSpec{
  101. Target: ExternalSecretTarget{
  102. DeletionPolicy: DeletionPolicyMerge,
  103. CreationPolicy: CreatePolicyNone,
  104. },
  105. },
  106. },
  107. expectedErr: `deletionPolicy=Merge must not be used with creationPolicy=None. There is no Secret to merge with
  108. either data or dataFrom should be specified`,
  109. },
  110. {
  111. name: "valid",
  112. obj: &ExternalSecret{
  113. Spec: ExternalSecretSpec{
  114. DataFrom: []ExternalSecretDataFromRemoteRef{
  115. {},
  116. },
  117. },
  118. },
  119. },
  120. {
  121. name: "duplicate secretKeys",
  122. obj: &ExternalSecret{
  123. Spec: ExternalSecretSpec{
  124. Target: ExternalSecretTarget{
  125. DeletionPolicy: DeletionPolicyRetain,
  126. },
  127. Data: []ExternalSecretData{
  128. {SecretKey: "SERVICE_NAME"},
  129. {SecretKey: "SERVICE_NAME"},
  130. {SecretKey: "SERVICE_NAME-2"},
  131. {SecretKey: "SERVICE_NAME-2"},
  132. {SecretKey: "NOT_DUPLICATE"},
  133. },
  134. },
  135. },
  136. expectedErr: "duplicate secretKey found: SERVICE_NAME\nduplicate secretKey found: SERVICE_NAME-2",
  137. },
  138. {
  139. name: "duplicate secretKey",
  140. obj: &ExternalSecret{
  141. Spec: ExternalSecretSpec{
  142. Target: ExternalSecretTarget{
  143. DeletionPolicy: DeletionPolicyRetain,
  144. },
  145. Data: []ExternalSecretData{
  146. {SecretKey: "SERVICE_NAME"},
  147. {SecretKey: "SERVICE_NAME"},
  148. },
  149. },
  150. },
  151. expectedErr: "duplicate secretKey found: SERVICE_NAME",
  152. },
  153. }
  154. for _, tt := range tests {
  155. t.Run(tt.name, func(t *testing.T) {
  156. _, err := validateExternalSecret(tt.obj)
  157. if err != nil {
  158. if tt.expectedErr == "" {
  159. t.Fatalf("validateExternalSecret() returned an unexpected error: %v", err)
  160. }
  161. if err.Error() != tt.expectedErr {
  162. t.Fatalf("validateExternalSecret() returned an unexpected error: got: %v, expected: %v", err, tt.expectedErr)
  163. }
  164. return
  165. }
  166. if tt.expectedErr != "" {
  167. t.Errorf("validateExternalSecret() should have returned an error but got nil")
  168. }
  169. })
  170. }
  171. }