fake.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 provides mock implementations for Akeyless provider testing.
  14. package fake
  15. import (
  16. "context"
  17. akeyless "github.com/akeylesslabs/akeyless-go/v4"
  18. )
  19. // AkeylessMockClient implements a mock client for Akeyless API operations.
  20. type AkeylessMockClient struct {
  21. getSecret func(secretName string, version int32) (string, error)
  22. createSecret func(ctx context.Context, remoteKey, data string) error
  23. updateSecret func(ctx context.Context, remoteKey, data string) error
  24. deleteSecret func(ctx context.Context, remoteKey string) error
  25. describeItem func(ctx context.Context, itemName string) (*akeyless.Item, error)
  26. }
  27. // New creates and returns a new AkeylessMockClient.
  28. func New() *AkeylessMockClient {
  29. return &AkeylessMockClient{}
  30. }
  31. // SetGetSecretFn sets the function to be called when GetSecret is invoked.
  32. func (mc *AkeylessMockClient) SetGetSecretFn(f func(secretName string, version int32) (string, error)) *AkeylessMockClient {
  33. mc.getSecret = f
  34. return mc
  35. }
  36. // SetCreateSecretFn sets the function to be called when CreateSecret is invoked.
  37. func (mc *AkeylessMockClient) SetCreateSecretFn(f func(ctx context.Context, remoteKey, data string) error) *AkeylessMockClient {
  38. mc.createSecret = f
  39. return mc
  40. }
  41. // SetUpdateSecretFn sets the function to be called when UpdateSecret is invoked.
  42. func (mc *AkeylessMockClient) SetUpdateSecretFn(f func(ctx context.Context, remoteKey, data string) error) *AkeylessMockClient {
  43. mc.updateSecret = f
  44. return mc
  45. }
  46. // SetDeleteSecretFn sets the function to be called when DeleteSecret is invoked.
  47. func (mc *AkeylessMockClient) SetDeleteSecretFn(f func(ctx context.Context, remoteKey string) error) *AkeylessMockClient {
  48. mc.deleteSecret = f
  49. return mc
  50. }
  51. // SetDescribeItemFn sets the function to be called when DescribeItem is invoked.
  52. func (mc *AkeylessMockClient) SetDescribeItemFn(f func(ctx context.Context, itemName string) (*akeyless.Item, error)) *AkeylessMockClient {
  53. mc.describeItem = f
  54. return mc
  55. }
  56. // CreateSecret creates a new secret in the mock Akeyless client.
  57. func (mc *AkeylessMockClient) CreateSecret(ctx context.Context, remoteKey, data string) error {
  58. return mc.createSecret(ctx, remoteKey, data)
  59. }
  60. // DeleteSecret deletes a secret from the mock Akeyless client.
  61. func (mc *AkeylessMockClient) DeleteSecret(ctx context.Context, remoteKey string) error {
  62. return mc.deleteSecret(ctx, remoteKey)
  63. }
  64. // DescribeItem retrieves an item description from the mock Akeyless client.
  65. func (mc *AkeylessMockClient) DescribeItem(ctx context.Context, itemName string) (*akeyless.Item, error) {
  66. return mc.describeItem(ctx, itemName)
  67. }
  68. // UpdateSecret updates an existing secret in the mock Akeyless client.
  69. func (mc *AkeylessMockClient) UpdateSecret(ctx context.Context, remoteKey, data string) error {
  70. return mc.updateSecret(ctx, remoteKey, data)
  71. }
  72. // TokenFromSecretRef returns a new token for the mock Akeyless client.
  73. func (mc *AkeylessMockClient) TokenFromSecretRef(_ context.Context) (string, error) {
  74. return "newToken", nil
  75. }
  76. // GetSecretByType retrieves a secret by its type from the mock Akeyless client.
  77. func (mc *AkeylessMockClient) GetSecretByType(_ context.Context, secretName string, version int32) (string, error) {
  78. return mc.getSecret(secretName, version)
  79. }
  80. // ListSecrets lists secrets from the mock Akeyless client.
  81. func (mc *AkeylessMockClient) ListSecrets(_ context.Context, _, _ string) ([]string, error) {
  82. return nil, nil
  83. }
  84. // WithValue sets the behavior of the mock client based on input and output values.
  85. func (mc *AkeylessMockClient) WithValue(_ *Input, out *Output) {
  86. if mc != nil {
  87. mc.getSecret = func(_ string, _ int32) (string, error) {
  88. return out.Value, out.Err
  89. }
  90. }
  91. }
  92. // Input represents the input parameters for the mock client functions.
  93. type Input struct {
  94. SecretName string
  95. Token string
  96. Version int32
  97. }
  98. // Output represents the output values for the mock client functions.
  99. type Output struct {
  100. Value string
  101. Err error
  102. }