diff --git a/pkg/connection/store/kubernetes/store.go b/pkg/connection/store/kubernetes/store.go index 9994d21bd..72623efb7 100644 --- a/pkg/connection/store/kubernetes/store.go +++ b/pkg/connection/store/kubernetes/store.go @@ -93,7 +93,7 @@ func buildClient(ctx context.Context, local client.Client, cfg v1.SecretStoreCon // ReadKeyValues reads and returns key value pairs for a given Kubernetes Secret. func (ss *SecretStore) ReadKeyValues(ctx context.Context, n store.ScopedName, s *store.Secret) error { ks := &corev1.Secret{} - if err := ss.client.Get(ctx, types.NamespacedName{Name: n.Name, Namespace: ss.namespaceForSecret(n)}, ks); err != nil { + if err := ss.client.Get(ctx, types.NamespacedName{Name: n.Name, Namespace: ss.namespaceForSecret(n)}, ks); resource.IgnoreNotFound(err) != nil { return errors.Wrap(err, errGetSecret) } s.Data = ks.Data diff --git a/pkg/connection/store/kubernetes/store_test.go b/pkg/connection/store/kubernetes/store_test.go index 8735916a2..b5afd10e8 100644 --- a/pkg/connection/store/kubernetes/store_test.go +++ b/pkg/connection/store/kubernetes/store_test.go @@ -102,22 +102,39 @@ func TestSecretStoreReadKeyValues(t *testing.T) { args: args{ client: resource.ClientApplicator{ Client: &test.MockClient{ - MockGet: test.NewMockGetFn(nil, func(obj client.Object) error { + MockGet: func(ctx context.Context, key client.ObjectKey, obj client.Object) error { + if key.Name != fakeSecretName || key.Namespace != fakeSecretNamespace { + return errors.New("unexpected secret name or namespace to get the secret") + } *obj.(*corev1.Secret) = corev1.Secret{ Data: fakeKV(), } return nil - }), + }, }, }, n: store.ScopedName{ - Name: fakeSecretName, + Name: fakeSecretName, + Scope: fakeSecretNamespace, }, }, want: want{ result: store.KeyValues(fakeKV()), }, }, + "SecretNotFound": { + reason: "Should return nil as an error if secret is not found", + args: args{ + client: resource.ClientApplicator{ + Client: &test.MockClient{ + MockGet: test.NewMockGetFn(kerrors.NewNotFound(schema.GroupResource{}, "")), + }, + }, + }, + want: want{ + err: nil, + }, + }, } for name, tc := range cases { t.Run(name, func(t *testing.T) { @@ -126,6 +143,7 @@ func TestSecretStoreReadKeyValues(t *testing.T) { } s := &store.Secret{} + s.ScopedName = tc.args.n err := ss.ReadKeyValues(context.Background(), tc.args.n, s) if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { t.Errorf("\n%s\nss.ReadKeyValues(...): -want error, +got error:\n%s", tc.reason, diff)