secretstore_validator_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. "github.com/stretchr/testify/assert"
  16. "github.com/stretchr/testify/require"
  17. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  18. )
  19. // ValidationProvider is a simple provider that we can use without cyclic import.
  20. type ValidationProvider struct {
  21. Provider
  22. }
  23. func (v *ValidationProvider) ValidateStore(_ GenericStore) (admission.Warnings, error) {
  24. return nil, nil
  25. }
  26. func TestValidateSecretStore(t *testing.T) {
  27. tests := []struct {
  28. name string
  29. obj *SecretStore
  30. mock func()
  31. assertErr func(t *testing.T, err error)
  32. }{
  33. {
  34. name: "valid regex",
  35. obj: &SecretStore{
  36. Spec: SecretStoreSpec{
  37. Conditions: []ClusterSecretStoreCondition{
  38. {
  39. NamespaceRegexes: []string{`.*`},
  40. },
  41. },
  42. Provider: &SecretStoreProvider{
  43. AWS: &AWSProvider{},
  44. },
  45. },
  46. },
  47. mock: func() {
  48. ForceRegister(&ValidationProvider{}, &SecretStoreProvider{
  49. AWS: &AWSProvider{},
  50. })
  51. },
  52. assertErr: func(t *testing.T, err error) {
  53. require.NoError(t, err)
  54. },
  55. },
  56. {
  57. name: "invalid regex",
  58. obj: &SecretStore{
  59. Spec: SecretStoreSpec{
  60. Conditions: []ClusterSecretStoreCondition{
  61. {
  62. NamespaceRegexes: []string{`\1`},
  63. },
  64. },
  65. Provider: &SecretStoreProvider{
  66. AWS: &AWSProvider{},
  67. },
  68. },
  69. },
  70. mock: func() {
  71. ForceRegister(&ValidationProvider{}, &SecretStoreProvider{
  72. AWS: &AWSProvider{},
  73. })
  74. },
  75. assertErr: func(t *testing.T, err error) {
  76. assert.EqualError(t, err, "failed to compile 0th namespace regex in 0th condition: error parsing regexp: invalid escape sequence: `\\1`")
  77. },
  78. },
  79. {
  80. name: "multiple errors",
  81. obj: &SecretStore{
  82. Spec: SecretStoreSpec{
  83. Conditions: []ClusterSecretStoreCondition{
  84. {
  85. NamespaceRegexes: []string{`\1`, `\2`},
  86. },
  87. },
  88. Provider: &SecretStoreProvider{
  89. AWS: &AWSProvider{},
  90. },
  91. },
  92. },
  93. mock: func() {
  94. ForceRegister(&ValidationProvider{}, &SecretStoreProvider{
  95. AWS: &AWSProvider{},
  96. })
  97. },
  98. assertErr: func(t *testing.T, err error) {
  99. assert.EqualError(t, err, "failed to compile 0th namespace regex in 0th condition: error parsing regexp: invalid escape sequence: `\\1`\nfailed to compile 1th namespace regex in 0th condition: error parsing regexp: invalid escape sequence: `\\2`")
  100. },
  101. },
  102. {
  103. name: "secret store must have only a single backend",
  104. obj: &SecretStore{
  105. Spec: SecretStoreSpec{
  106. Provider: &SecretStoreProvider{
  107. AWS: &AWSProvider{},
  108. GCPSM: &GCPSMProvider{},
  109. },
  110. },
  111. },
  112. assertErr: func(t *testing.T, err error) {
  113. assert.EqualError(t, err, "store error for : secret stores must only have exactly one backend specified, found 2")
  114. },
  115. },
  116. {
  117. name: "no registered store backend",
  118. obj: &SecretStore{
  119. Spec: SecretStoreSpec{
  120. Conditions: []ClusterSecretStoreCondition{
  121. {
  122. Namespaces: []string{"default"},
  123. },
  124. },
  125. },
  126. },
  127. assertErr: func(t *testing.T, err error) {
  128. assert.EqualError(t, err, "store error for : secret stores must only have exactly one backend specified, found 0")
  129. },
  130. },
  131. }
  132. for _, tt := range tests {
  133. t.Run(tt.name, func(t *testing.T) {
  134. if tt.mock != nil {
  135. tt.mock()
  136. }
  137. _, err := validateStore(tt.obj)
  138. tt.assertErr(t, err)
  139. })
  140. }
  141. }