secretstore_validator_test.go 3.9 KB

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