fake.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 fake
  13. import (
  14. "fmt"
  15. awssm "github.com/aws/aws-sdk-go/service/secretsmanager"
  16. "github.com/google/go-cmp/cmp"
  17. )
  18. // Client implements the aws secretsmanager interface.
  19. type Client struct {
  20. ExecutionCounter int
  21. valFn map[string]func(*awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error)
  22. }
  23. // NewClient init a new fake client.
  24. func NewClient() *Client {
  25. return &Client{
  26. valFn: make(map[string]func(*awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error)),
  27. }
  28. }
  29. func (sm *Client) GetSecretValue(in *awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error) {
  30. sm.ExecutionCounter++
  31. if entry, found := sm.valFn[sm.cacheKeyForInput(in)]; found {
  32. return entry(in)
  33. }
  34. return nil, fmt.Errorf("test case not found")
  35. }
  36. func (sm *Client) ListSecrets(*awssm.ListSecretsInput) (*awssm.ListSecretsOutput, error) {
  37. return nil, nil
  38. }
  39. func (sm *Client) cacheKeyForInput(in *awssm.GetSecretValueInput) string {
  40. var secretID, versionID string
  41. if in.SecretId != nil {
  42. secretID = *in.SecretId
  43. }
  44. if in.VersionId != nil {
  45. versionID = *in.VersionId
  46. }
  47. return fmt.Sprintf("%s#%s", secretID, versionID)
  48. }
  49. func (sm *Client) WithValue(in *awssm.GetSecretValueInput, val *awssm.GetSecretValueOutput, err error) {
  50. sm.valFn[sm.cacheKeyForInput(in)] = func(paramIn *awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error) {
  51. if !cmp.Equal(paramIn, in) {
  52. return nil, fmt.Errorf("unexpected test argument")
  53. }
  54. return val, err
  55. }
  56. }