| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- /*
- Copyright © The ESO Authors
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- https://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package externalsecret
- import (
- "context"
- "testing"
- "k8s.io/apimachinery/pkg/runtime"
- utilruntime "k8s.io/apimachinery/pkg/util/runtime"
- clientgoscheme "k8s.io/client-go/kubernetes/scheme"
- "sigs.k8s.io/controller-runtime/pkg/client"
- fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
- "sigs.k8s.io/controller-runtime/pkg/reconcile"
- esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
- esv2alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v2alpha1"
- )
- const testNamespaceTeamA = "team-a"
- func TestIndexExternalSecretV2StoreRefs(t *testing.T) {
- es := &esv1.ExternalSecret{}
- es.Namespace = testNamespaceTeamA
- es.Spec.SecretStoreRef = esv1.SecretStoreRef{
- Name: "namespaced-store",
- Kind: esv1.ProviderStoreKindStr,
- }
- es.Spec.Data = []esv1.ExternalSecretData{
- {},
- {
- SourceRef: &esv1.StoreSourceRef{
- SecretStoreRef: esv1.SecretStoreRef{
- Name: "cluster-store",
- Kind: esv1.ClusterProviderStoreKindStr,
- },
- },
- },
- }
- es.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
- {
- SourceRef: &esv1.StoreGeneratorSourceRef{
- SecretStoreRef: &esv1.SecretStoreRef{
- Name: "secondary-store",
- Kind: esv1.ProviderStoreKindStr,
- },
- },
- },
- {
- SourceRef: &esv1.StoreGeneratorSourceRef{
- SecretStoreRef: &esv1.SecretStoreRef{
- Name: "ignored-v1-store",
- Kind: esv1.SecretStoreKind,
- },
- },
- },
- }
- got := indexExternalSecretV2StoreRefs(es)
- want := map[string]struct{}{
- v2StoreRefIndexKey(esv1.ClusterProviderStoreKindStr, "", "cluster-store"): {},
- v2StoreRefIndexKey(esv1.ProviderStoreKindStr, testNamespaceTeamA, "namespaced-store"): {},
- v2StoreRefIndexKey(esv1.ProviderStoreKindStr, testNamespaceTeamA, "secondary-store"): {},
- }
- if len(got) != len(want) {
- t.Fatalf("expected %d index keys, got %d: %#v", len(want), len(got), got)
- }
- for _, key := range got {
- if _, ok := want[key]; !ok {
- t.Fatalf("unexpected index key %q in %#v", key, got)
- }
- delete(want, key)
- }
- if len(want) != 0 {
- t.Fatalf("missing expected index keys: %#v", want)
- }
- }
- func TestFindExternalSecretsForV2Store(t *testing.T) {
- scheme := runtime.NewScheme()
- utilruntime.Must(clientgoscheme.AddToScheme(scheme))
- utilruntime.Must(esv1.AddToScheme(scheme))
- utilruntime.Must(esv2alpha1.AddToScheme(scheme))
- matching := &esv1.ExternalSecret{}
- matching.Namespace = testNamespaceTeamA
- matching.Name = "matching"
- matching.Spec.SecretStoreRef = esv1.SecretStoreRef{
- Name: "aws-prod",
- Kind: esv1.ProviderStoreKindStr,
- }
- nonMatching := &esv1.ExternalSecret{}
- nonMatching.Namespace = testNamespaceTeamA
- nonMatching.Name = "non-matching"
- nonMatching.Spec.SecretStoreRef = esv1.SecretStoreRef{
- Name: "aws-other",
- Kind: esv1.ProviderStoreKindStr,
- }
- cl := fakeclient.NewClientBuilder().
- WithScheme(scheme).
- WithObjects(matching, nonMatching).
- WithIndex(&esv1.ExternalSecret{}, indexESV2StoreRefField, indexExternalSecretV2StoreRefs).
- Build()
- r := &Reconciler{Client: cl}
- store := &esv2alpha1.ProviderStore{}
- store.Namespace = testNamespaceTeamA
- store.Name = "aws-prod"
- got := r.findExternalSecretsForV2Store(context.Background(), store)
- want := []reconcile.Request{{
- NamespacedName: client.ObjectKeyFromObject(matching),
- }}
- if len(got) != len(want) {
- t.Fatalf("expected %d requests, got %d: %#v", len(want), len(got), got)
- }
- if got[0] != want[0] {
- t.Fatalf("expected requests %#v, got %#v", want, got)
- }
- }
- func TestFindExternalSecretsForClusterProviderStore(t *testing.T) {
- scheme := runtime.NewScheme()
- utilruntime.Must(clientgoscheme.AddToScheme(scheme))
- utilruntime.Must(esv1.AddToScheme(scheme))
- utilruntime.Must(esv2alpha1.AddToScheme(scheme))
- matching := &esv1.ExternalSecret{}
- matching.Namespace = testNamespaceTeamA
- matching.Name = "matching"
- matching.Spec.Data = []esv1.ExternalSecretData{
- {
- SourceRef: &esv1.StoreSourceRef{
- SecretStoreRef: esv1.SecretStoreRef{
- Name: "shared",
- Kind: esv1.ClusterProviderStoreKindStr,
- },
- },
- },
- }
- cl := fakeclient.NewClientBuilder().
- WithScheme(scheme).
- WithObjects(matching).
- WithIndex(&esv1.ExternalSecret{}, indexESV2StoreRefField, indexExternalSecretV2StoreRefs).
- Build()
- r := &Reconciler{Client: cl}
- store := &esv2alpha1.ClusterProviderStore{}
- store.Name = "shared"
- got := r.findExternalSecretsForV2Store(context.Background(), store)
- if len(got) != 1 || got[0].NamespacedName.Name != matching.Name || got[0].NamespacedName.Namespace != matching.Namespace {
- t.Fatalf("unexpected requests: %#v", got)
- }
- }
|