Răsfoiți Sursa

Add fake functions to mock SMInterface

When create secret is called, the fake associated with the client struct is called.
This means our mocks on SMInterface will likely have to be moved into client.(You were right Lilly and Marcus lol)
Additional functionality will also be needed as to not break the existing createSecret fake.

Signed-off-by: William Young <will.young@engineerbetter.com>
Co-authored-by: Marcus Dantas <marcus.dantas@engineerbetter.com>
William Young 3 ani în urmă
părinte
comite
d9d16ab432

+ 67 - 0
pkg/provider/aws/secretsmanager/fake/fake.go

@@ -29,6 +29,46 @@ type Client struct {
 	valFn            map[string]func(*awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error)
 }
 
+type GetSecretValueFn func(*awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error)
+type ListSecretsFn func(*awssm.ListSecretsInput) (*awssm.ListSecretsOutput, error)
+type CreateSecretWithContextFn func(aws.Context, *awssm.CreateSecretInput, ...request.Option) (*awssm.CreateSecretOutput, error)
+
+type SMInterface struct {
+	GetSecretValueFn          GetSecretValueFn
+	ListSecretsFn             ListSecretsFn
+	CreateSecretWithContextFn CreateSecretWithContextFn
+}
+
+func (sm SMInterface) CreateSecretWithContext(ctx aws.Context, input *awssm.CreateSecretInput, options ...request.Option) (*awssm.CreateSecretOutput, error) {
+	return sm.CreateSecretWithContextFn(ctx, input, options...)
+}
+
+func NewCreateSecretWithContextFn(output *awssm.CreateSecretOutput, err error) CreateSecretWithContextFn {
+	return func(ctx aws.Context, input *awssm.CreateSecretInput, options ...request.Option) (*awssm.CreateSecretOutput, error) {
+		return output, err
+	}
+}
+
+func (sm SMInterface) GetSecretValue(input *awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error) {
+	return sm.GetSecretValueFn(input)
+}
+
+func NewGetSecretValueFn(output *awssm.GetSecretValueOutput, err error) GetSecretValueFn {
+	return func(*awssm.GetSecretValueInput) (*awssm.GetSecretValueOutput, error) {
+		return output, err
+	}
+}
+
+func (sm SMInterface) ListSecrets(input *awssm.ListSecretsInput) (*awssm.ListSecretsOutput, error) {
+	return sm.ListSecretsFn(input)
+}
+
+func NewListSecretsFn(listOutput *awssm.ListSecretsOutput, err error) ListSecretsFn {
+	return func(*awssm.ListSecretsInput) (*awssm.ListSecretsOutput, error) {
+		return listOutput, err
+	}
+}
+
 // NewClient init a new fake client.
 func NewClient() *Client {
 	return &Client{
@@ -75,3 +115,30 @@ func (sm *Client) WithValue(in *awssm.GetSecretValueInput, val *awssm.GetSecretV
 		return val, err
 	}
 }
+
+// func makeValidSecretStoreWithVersion(v esv1beta1.VaultKVStoreVersion) *esv1beta1.SecretStore {
+// 	return &esv1beta1.SecretStore{
+// 		ObjectMeta: metav1.ObjectMeta{
+// 			Name:      "vault-store",
+// 			Namespace: "default",
+// 		},
+// 		Spec: esv1beta1.SecretStoreSpec{
+// 			Provider: &esv1beta1.SecretStoreProvider{
+// 				Vault: &esv1beta1.VaultProvider{
+// 					Server:  "vault.example.com",
+// 					Path:    &secretStorePath,
+// 					Version: v,
+// 					Auth: esv1beta1.VaultAuth{
+// 						Kubernetes: &esv1beta1.VaultKubernetesAuth{
+// 							Path: "kubernetes",
+// 							Role: "kubernetes-auth-role",
+// 							ServiceAccountRef: &esmeta.ServiceAccountSelector{
+// 								Name: "example-sa",
+// 							},
+// 						},
+// 					},
+// 				},
+// 			},
+// 		},
+// 	}
+// }

+ 80 - 0
pkg/provider/aws/secretsmanager/secretsmanager_test.go

@@ -16,6 +16,7 @@ package secretsmanager
 
 import (
 	"context"
+	"errors"
 	"fmt"
 	"strings"
 	"testing"
@@ -335,3 +336,82 @@ func TestSetSecret(t *testing.T) {
 
 	assert.Equal(t, err, nil)
 }
+
+// func TestSetSecretCreateError(t *testing.T) {
+// 	ref := fakeRef{key: "I'm a key"}
+// 	fakeClient := fakesm.NewClient()
+// 	createSecretFails := func(smtc *secretsManagerTestCase) {
+// 		smtc.apiOutput.SecretString = aws.String(`{"foo":"bar", "bar":"vodka"}`)
+// 		smtc.remoteRef.Property = "foo"
+// 		smtc.expectedSecret = "bar"
+// 		smtc.apiErr = errors.New("api err")
+// 		smtc.expectError = "api err"
+// 		smtc.fakeClient = fakeClient
+// 		smtc.remoteRef.Key = ref.key
+// 	}
+// 	successCases := []*secretsManagerTestCase{
+// 		makeValidSecretsManagerTestCaseCustom(createSecretFails),
+// 	}
+
+// 	for k, v := range successCases {
+// 		sm := SecretsManager{
+// 			cache:  make(map[string]*awssm.GetSecretValueOutput),
+// 			client: v.fakeClient,
+// 		}
+// 		sm.client.CreateSecretWithContext(context.Background(), &awssm.CreateSecretInput{})
+// 		err := sm.SetSecret(context.Background(), []byte("hi"), ref)
+// 		if !ErrorContains(err, v.expectError) {
+// 			t.Errorf(unexpectedErrorString, k, err.Error(), v.expectError)
+// 		}
+// 	}
+// }
+
+func TestSetSecret2(t *testing.T) {
+	noPermission := errors.New("no permission")
+
+	type args struct {
+		store       *esv1beta1.AWSProvider
+		SMInterface SMInterface
+	}
+
+	type want struct {
+		err error
+	}
+	tests := map[string]struct {
+		reason string
+		args   args
+		want   want
+	}{
+		"SetSecret": {
+			reason: "secret is successfully set, with no existing vault secret",
+			args:   args{
+				// store: makeValidSecretStoreWithVersion(esv1beta1.VaultKVStoreV2).Spec.Provider.Vault,
+				// SMInterface: fakesm.SMInterface{
+				// 	CreateSecretWithContextFn: fakesm.NewCreateSecretWithContextFn(nil, noPermission),
+
+				// 	// Run the debugger and step into the createsecret function.
+				// 	// You will notice that the above mock isn't called and the one associated with the client struct is instead.
+				// },
+			},
+			want: want{
+				err: noPermission,
+			},
+		},
+	}
+
+	for name, tc := range tests {
+		t.Run(name, func(t *testing.T) {
+			ref := fakeRef{key: "fake-key"}
+			sm := SecretsManager{
+				client: fakesm.NewClient(),
+			}
+			err := sm.SetSecret(context.Background(), []byte("fake-value"), ref)
+
+			// if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
+			// 	t.Errorf("\nTesting SetSecret:\nName: %v\nReason: %v\nWant error: %v\nGot error: %v", name, tc.reason, tc.want.err, diff)
+			// }
+
+			assert.Equal(t, err, tc.want.err)
+		})
+	}
+}