fake.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 fake implements mocks for AWS Parameter Store service clients.
  14. package fake
  15. import (
  16. "context"
  17. "errors"
  18. "github.com/aws/aws-sdk-go-v2/service/ssm"
  19. "github.com/google/go-cmp/cmp"
  20. "github.com/google/go-cmp/cmp/cmpopts"
  21. )
  22. // Client implements the aws parameterstore interface.
  23. type Client struct {
  24. GetParameterFn GetParameterFn
  25. GetParametersByPathFn GetParametersByPathFn
  26. PutParameterFn PutParameterFn
  27. PutParameterCalledN int
  28. PutParameterFnCalledWith [][]*ssm.PutParameterInput
  29. DeleteParameterFn DeleteParameterFn
  30. DescribeParametersFn DescribeParametersFn
  31. ListTagsForResourceFn ListTagsForResourceFn
  32. RemoveTagsFromResourceFn RemoveTagsFromResourceFn
  33. AddTagsToResourceFn AddTagsToResourceFn
  34. }
  35. // GetParameterFn defines a function type for mocking GetParameter API.
  36. type GetParameterFn func(context.Context, *ssm.GetParameterInput, ...func(*ssm.Options)) (*ssm.GetParameterOutput, error)
  37. // GetParametersByPathFn defines a function type for mocking GetParametersByPath API.
  38. type GetParametersByPathFn func(context.Context, *ssm.GetParametersByPathInput, ...func(*ssm.Options)) (*ssm.GetParametersByPathOutput, error)
  39. // PutParameterFn defines a function type for mocking PutParameter API.
  40. type PutParameterFn func(context.Context, *ssm.PutParameterInput, ...func(*ssm.Options)) (*ssm.PutParameterOutput, error)
  41. // DescribeParametersFn defines a function type for mocking DescribeParameters API.
  42. type DescribeParametersFn func(context.Context, *ssm.DescribeParametersInput, ...func(*ssm.Options)) (*ssm.DescribeParametersOutput, error)
  43. // ListTagsForResourceFn defines a function type for mocking ListTagsForResource API.
  44. type ListTagsForResourceFn func(context.Context, *ssm.ListTagsForResourceInput, ...func(*ssm.Options)) (*ssm.ListTagsForResourceOutput, error)
  45. // DeleteParameterFn defines a function type for mocking DeleteParameter API.
  46. type DeleteParameterFn func(ctx context.Context, input *ssm.DeleteParameterInput, opts ...func(*ssm.Options)) (*ssm.DeleteParameterOutput, error)
  47. // RemoveTagsFromResourceFn defines a function type for mocking RemoveTagsFromResource API.
  48. type RemoveTagsFromResourceFn func(ctx context.Context, params *ssm.RemoveTagsFromResourceInput, optFns ...func(*ssm.Options)) (*ssm.RemoveTagsFromResourceOutput, error)
  49. // AddTagsToResourceFn defines a function type for mocking AddTagsToResource API.
  50. type AddTagsToResourceFn func(ctx context.Context, params *ssm.AddTagsToResourceInput, optFns ...func(*ssm.Options)) (*ssm.AddTagsToResourceOutput, error)
  51. // ListTagsForResource executes the mocked ListTagsForResourceFn.
  52. func (sm *Client) ListTagsForResource(ctx context.Context, input *ssm.ListTagsForResourceInput, options ...func(*ssm.Options)) (*ssm.ListTagsForResourceOutput, error) {
  53. return sm.ListTagsForResourceFn(ctx, input, options...)
  54. }
  55. // NewListTagsForResourceFn creates a new mock function for ListTagsForResource.
  56. func NewListTagsForResourceFn(output *ssm.ListTagsForResourceOutput, err error, aFunc ...func(input *ssm.ListTagsForResourceInput)) ListTagsForResourceFn {
  57. return func(_ context.Context, params *ssm.ListTagsForResourceInput, _ ...func(*ssm.Options)) (*ssm.ListTagsForResourceOutput, error) {
  58. if len(aFunc) > 0 {
  59. for _, f := range aFunc {
  60. f(params)
  61. }
  62. }
  63. return output, err
  64. }
  65. }
  66. // DeleteParameter executes the mocked DeleteParameterFn.
  67. func (sm *Client) DeleteParameter(ctx context.Context, input *ssm.DeleteParameterInput, opts ...func(*ssm.Options)) (*ssm.DeleteParameterOutput, error) {
  68. return sm.DeleteParameterFn(ctx, input, opts...)
  69. }
  70. // NewDeleteParameterFn creates a new mock function for DeleteParameter.
  71. func NewDeleteParameterFn(output *ssm.DeleteParameterOutput, err error) DeleteParameterFn {
  72. return func(context.Context, *ssm.DeleteParameterInput, ...func(*ssm.Options)) (*ssm.DeleteParameterOutput, error) {
  73. return output, err
  74. }
  75. }
  76. // GetParameter executes the mocked GetParameterFn.
  77. func (sm *Client) GetParameter(ctx context.Context, input *ssm.GetParameterInput, options ...func(*ssm.Options)) (*ssm.GetParameterOutput, error) {
  78. return sm.GetParameterFn(ctx, input, options...)
  79. }
  80. // GetParametersByPath executes the mocked GetParametersByPathFn.
  81. func (sm *Client) GetParametersByPath(ctx context.Context, input *ssm.GetParametersByPathInput, options ...func(*ssm.Options)) (*ssm.GetParametersByPathOutput, error) {
  82. return sm.GetParametersByPathFn(ctx, input, options...)
  83. }
  84. // NewGetParameterFn creates a new mock function for GetParameter.
  85. func NewGetParameterFn(output *ssm.GetParameterOutput, err error) GetParameterFn {
  86. return func(context.Context, *ssm.GetParameterInput, ...func(*ssm.Options)) (*ssm.GetParameterOutput, error) {
  87. return output, err
  88. }
  89. }
  90. // DescribeParameters executes the mocked DescribeParametersFn.
  91. func (sm *Client) DescribeParameters(ctx context.Context, input *ssm.DescribeParametersInput, options ...func(*ssm.Options)) (*ssm.DescribeParametersOutput, error) {
  92. return sm.DescribeParametersFn(ctx, input, options...)
  93. }
  94. // NewDescribeParametersFn creates a new mock function for DescribeParameters.
  95. func NewDescribeParametersFn(output *ssm.DescribeParametersOutput, err error) DescribeParametersFn {
  96. return func(context.Context, *ssm.DescribeParametersInput, ...func(*ssm.Options)) (*ssm.DescribeParametersOutput, error) {
  97. return output, err
  98. }
  99. }
  100. // PutParameter executes the mocked PutParameterFn and tracks call metadata.
  101. func (sm *Client) PutParameter(ctx context.Context, input *ssm.PutParameterInput, options ...func(*ssm.Options)) (*ssm.PutParameterOutput, error) {
  102. sm.PutParameterCalledN++
  103. sm.PutParameterFnCalledWith = append(sm.PutParameterFnCalledWith, []*ssm.PutParameterInput{input})
  104. return sm.PutParameterFn(ctx, input, options...)
  105. }
  106. // NewPutParameterFn creates a new mock function for PutParameter.
  107. func NewPutParameterFn(output *ssm.PutParameterOutput, err error, aFunc ...func(input *ssm.PutParameterInput)) PutParameterFn {
  108. return func(_ context.Context, params *ssm.PutParameterInput, _ ...func(*ssm.Options)) (*ssm.PutParameterOutput, error) {
  109. if len(aFunc) > 0 {
  110. for _, f := range aFunc {
  111. f(params)
  112. }
  113. }
  114. return output, err
  115. }
  116. }
  117. // WithValue configures the GetParameterFn with specific input and output.
  118. func (sm *Client) WithValue(in *ssm.GetParameterInput, val *ssm.GetParameterOutput, err error) {
  119. sm.GetParameterFn = func(_ context.Context, paramIn *ssm.GetParameterInput, _ ...func(*ssm.Options)) (*ssm.GetParameterOutput, error) {
  120. if !cmp.Equal(paramIn, in, cmpopts.IgnoreUnexported(ssm.GetParameterInput{})) {
  121. return nil, errors.New("unexpected test argument")
  122. }
  123. return val, err
  124. }
  125. }
  126. // RemoveTagsFromResource executes the mocked RemoveTagsFromResourceFn.
  127. func (sm *Client) RemoveTagsFromResource(ctx context.Context, params *ssm.RemoveTagsFromResourceInput, optFns ...func(*ssm.Options)) (*ssm.RemoveTagsFromResourceOutput, error) {
  128. return sm.RemoveTagsFromResourceFn(ctx, params, optFns...)
  129. }
  130. // NewRemoveTagsFromResourceFn creates a new mock function for RemoveTagsFromResource.
  131. func NewRemoveTagsFromResourceFn(output *ssm.RemoveTagsFromResourceOutput, err error, aFunc ...func(input *ssm.RemoveTagsFromResourceInput)) RemoveTagsFromResourceFn {
  132. return func(_ context.Context, params *ssm.RemoveTagsFromResourceInput, _ ...func(*ssm.Options)) (*ssm.RemoveTagsFromResourceOutput, error) {
  133. if len(aFunc) > 0 {
  134. for _, f := range aFunc {
  135. f(params)
  136. }
  137. }
  138. return output, err
  139. }
  140. }
  141. // AddTagsToResource executes the mocked AddTagsToResourceFn.
  142. func (sm *Client) AddTagsToResource(ctx context.Context, params *ssm.AddTagsToResourceInput, optFns ...func(*ssm.Options)) (*ssm.AddTagsToResourceOutput, error) {
  143. return sm.AddTagsToResourceFn(ctx, params, optFns...)
  144. }
  145. // NewAddTagsToResourceFn creates a new mock function for AddTagsToResource.
  146. func NewAddTagsToResourceFn(output *ssm.AddTagsToResourceOutput, err error, aFunc ...func(input *ssm.AddTagsToResourceInput)) AddTagsToResourceFn {
  147. return func(_ context.Context, params *ssm.AddTagsToResourceInput, _ ...func(*ssm.Options)) (*ssm.AddTagsToResourceOutput, error) {
  148. if len(aFunc) > 0 {
  149. for _, f := range aFunc {
  150. f(params)
  151. }
  152. }
  153. return output, err
  154. }
  155. }