provider_schema_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. "context"
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. "sigs.k8s.io/controller-runtime/pkg/client"
  18. )
  19. type PP struct{}
  20. const shouldBeRegistered = "provider should be registered"
  21. // New constructs a SecretsManager Provider.
  22. func (p *PP) NewClient(ctx context.Context, store GenericStore, kube client.Client, namespace string) (SecretsClient, error) {
  23. return p, nil
  24. }
  25. // GetSecret returns a single secret from the provider.
  26. func (p *PP) GetSecret(ctx context.Context, ref ExternalSecretDataRemoteRef) ([]byte, error) {
  27. return []byte("NOOP"), nil
  28. }
  29. // GetSecretMap returns multiple k/v pairs from the provider.
  30. func (p *PP) GetSecretMap(ctx context.Context, ref ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  31. return map[string][]byte{}, nil
  32. }
  33. // Empty GetAllSecrets.
  34. func (p *PP) GetAllSecrets(ctx context.Context, ref ExternalSecretFind) (map[string][]byte, error) {
  35. // TO be implemented
  36. return map[string][]byte{}, nil
  37. }
  38. func (p *PP) Close(ctx context.Context) error {
  39. return nil
  40. }
  41. func (p *PP) Validate() error {
  42. return nil
  43. }
  44. func (p *PP) ValidateStore(store GenericStore) error {
  45. return nil
  46. }
  47. // TestRegister tests if the Register function
  48. // (1) panics if it tries to register something invalid
  49. // (2) stores the correct provider.
  50. func TestRegister(t *testing.T) {
  51. tbl := []struct {
  52. test string
  53. name string
  54. expPanic bool
  55. expExists bool
  56. provider *SecretStoreProvider
  57. }{
  58. {
  59. test: "should panic when given an invalid provider",
  60. name: "aws",
  61. expPanic: true,
  62. expExists: false,
  63. provider: &SecretStoreProvider{},
  64. },
  65. {
  66. test: "should register an correct provider",
  67. name: "aws",
  68. expExists: false,
  69. provider: &SecretStoreProvider{
  70. AWS: &AWSProvider{
  71. Service: AWSServiceSecretsManager,
  72. },
  73. },
  74. },
  75. {
  76. test: "should panic if already exists",
  77. name: "aws",
  78. expPanic: true,
  79. expExists: true,
  80. provider: &SecretStoreProvider{
  81. AWS: &AWSProvider{
  82. Service: AWSServiceSecretsManager,
  83. },
  84. },
  85. },
  86. }
  87. for i := range tbl {
  88. row := tbl[i]
  89. t.Run(row.test, func(t *testing.T) {
  90. runTest(t,
  91. row.name,
  92. row.provider,
  93. row.expPanic,
  94. )
  95. })
  96. }
  97. }
  98. func runTest(t *testing.T, name string, provider *SecretStoreProvider, expPanic bool) {
  99. testProvider := &PP{}
  100. secretStore := &SecretStore{
  101. Spec: SecretStoreSpec{
  102. Provider: provider,
  103. },
  104. }
  105. if expPanic {
  106. defer func() {
  107. if r := recover(); r == nil {
  108. t.Errorf("Register should panic")
  109. }
  110. }()
  111. }
  112. Register(testProvider, secretStore.Spec.Provider)
  113. p1, ok := GetProviderByName(name)
  114. assert.True(t, ok, shouldBeRegistered)
  115. assert.Equal(t, testProvider, p1)
  116. p2, err := GetProvider(secretStore)
  117. assert.Nil(t, err)
  118. assert.Equal(t, testProvider, p2)
  119. }
  120. // ForceRegister is used by other tests, we should ensure it works as expected.
  121. func TestForceRegister(t *testing.T) {
  122. testProvider := &PP{}
  123. provider := &SecretStoreProvider{
  124. AWS: &AWSProvider{
  125. Service: AWSServiceParameterStore,
  126. },
  127. }
  128. secretStore := &SecretStore{
  129. Spec: SecretStoreSpec{
  130. Provider: provider,
  131. },
  132. }
  133. ForceRegister(testProvider, &SecretStoreProvider{
  134. AWS: &AWSProvider{
  135. Service: AWSServiceParameterStore,
  136. },
  137. })
  138. p1, ok := GetProviderByName("aws")
  139. assert.True(t, ok, shouldBeRegistered)
  140. assert.Equal(t, testProvider, p1)
  141. p2, err := GetProvider(secretStore)
  142. assert.Nil(t, err)
  143. assert.Equal(t, testProvider, p2)
  144. }
  145. func TestRegisterGCP(t *testing.T) {
  146. p, ok := GetProviderByName("gcpsm")
  147. assert.Nil(t, p)
  148. assert.False(t, ok, "provider should not be registered")
  149. testProvider := &PP{}
  150. secretStore := &SecretStore{
  151. Spec: SecretStoreSpec{
  152. Provider: &SecretStoreProvider{
  153. GCPSM: &GCPSMProvider{},
  154. },
  155. },
  156. }
  157. ForceRegister(testProvider, secretStore.Spec.Provider)
  158. p1, ok := GetProviderByName("gcpsm")
  159. assert.True(t, ok, shouldBeRegistered)
  160. assert.Equal(t, testProvider, p1)
  161. p2, err := GetProvider(secretStore)
  162. assert.Nil(t, err)
  163. assert.Equal(t, testProvider, p2)
  164. }