From ec14e8644599400456b6e9a8a4e548cb03f8dcff Mon Sep 17 00:00:00 2001 From: Tim Riffer Date: Thu, 22 Dec 2022 08:31:56 +0100 Subject: [PATCH] Add support for pod status.phase field selector in fake client to fix tests --- operator/pkg/lib/sidecars/pods/get_test.go | 6 +++++- .../sidecars/test/helpers/fake_client_indexer.go | 15 +++++++++++++++ operator/pkg/lib/sidecars/test/test_client.go | 14 +++++++++----- 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 operator/pkg/lib/sidecars/test/helpers/fake_client_indexer.go diff --git a/operator/pkg/lib/sidecars/pods/get_test.go b/operator/pkg/lib/sidecars/pods/get_test.go index 48fe594250..111b22eacb 100644 --- a/operator/pkg/lib/sidecars/pods/get_test.go +++ b/operator/pkg/lib/sidecars/pods/get_test.go @@ -23,7 +23,11 @@ func createClientSet(t *testing.T, objects ...client.Object) client.Client { err = v1.AddToScheme(scheme.Scheme) require.NoError(t, err) - fakeClient := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(objects...).Build() + fakeClient := fake.NewClientBuilder(). + WithScheme(scheme.Scheme). + WithObjects(objects...). + WithIndex(&v1.Pod{}, "status.phase", helpers.FakePodStatusPhaseIndexer). + Build() return fakeClient } diff --git a/operator/pkg/lib/sidecars/test/helpers/fake_client_indexer.go b/operator/pkg/lib/sidecars/test/helpers/fake_client_indexer.go new file mode 100644 index 0000000000..f421a33895 --- /dev/null +++ b/operator/pkg/lib/sidecars/test/helpers/fake_client_indexer.go @@ -0,0 +1,15 @@ +package helpers + +import ( + "fmt" + "k8s.io/api/core/v1" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func FakePodStatusPhaseIndexer(object client.Object) []string { + p, ok := object.(*v1.Pod) + if !ok { + panic(fmt.Errorf("indexer function for type %T's status.phase field received object of type %T", v1.Pod{}, object)) + } + return []string{string(p.Status.Phase)} +} diff --git a/operator/pkg/lib/sidecars/test/test_client.go b/operator/pkg/lib/sidecars/test/test_client.go index 74c6308f5d..7deff4ced5 100644 --- a/operator/pkg/lib/sidecars/test/test_client.go +++ b/operator/pkg/lib/sidecars/test/test_client.go @@ -55,11 +55,15 @@ func newScenario() (*scenario, error) { } return &scenario{ - Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects( - helpers.FixNamespaceWith(noAnnotationNamespace, nil), - helpers.FixNamespaceWith(sidecarEnabledNamespace, map[string]string{"istio-injection": "enabled"}), - helpers.FixNamespaceWith(sidecarDisabledNamespace, map[string]string{"istio-injection": "disabled"}), - ).Build(), + Client: fake.NewClientBuilder(). + WithScheme(scheme.Scheme). + WithObjects( + helpers.FixNamespaceWith(noAnnotationNamespace, nil), + helpers.FixNamespaceWith(sidecarEnabledNamespace, map[string]string{"istio-injection": "enabled"}), + helpers.FixNamespaceWith(sidecarDisabledNamespace, map[string]string{"istio-injection": "disabled"}), + ). + WithIndex(&corev1.Pod{}, "status.phase", helpers.FakePodStatusPhaseIndexer). + Build(), logger: logr.Discard(), injectionNamespaceSelector: SidecarEnabled, }, nil