From 354b2de9a400f96a4864366b7a9b7d21ac3fdc17 Mon Sep 17 00:00:00 2001 From: jmnote Date: Sun, 11 Aug 2024 10:00:48 +0000 Subject: [PATCH] minor edit --- internal/controller/common_test.go | 16 ++++++++++++++++ internal/controller/configmap_controller_test.go | 13 ++----------- internal/controller/ingress_controller_test.go | 7 ++----- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/internal/controller/common_test.go b/internal/controller/common_test.go index e1ceae9..d51c0b4 100644 --- a/internal/controller/common_test.go +++ b/internal/controller/common_test.go @@ -7,8 +7,11 @@ import ( corev1 "k8s.io/api/core/v1" networkingv1 "k8s.io/api/networking/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/rest" + ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/manager" ) func newScheme() *runtime.Scheme { @@ -22,6 +25,19 @@ func newFakeClient(objs ...client.Object) client.Client { return fake.NewClientBuilder().WithScheme(newScheme()).WithObjects(objs...).Build() } +func newFakeManager() manager.Manager { + fakeConfig := &rest.Config{ + Host: "https://fake-api-server", + } + mgr, err := ctrl.NewManager(fakeConfig, ctrl.Options{ + Scheme: newScheme(), + }) + if err != nil { + panic(err) + } + return mgr +} + type BadClient1 struct { client.Client } diff --git a/internal/controller/configmap_controller_test.go b/internal/controller/configmap_controller_test.go index e8d3611..517ef38 100644 --- a/internal/controller/configmap_controller_test.go +++ b/internal/controller/configmap_controller_test.go @@ -9,7 +9,6 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/rest" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -84,9 +83,6 @@ func TestConfigMapReconciler_SetupWithManager(t *testing.T) { t.Log(222) fakeClient := newFakeClient(tc.objects...) - fakeClientFunc := func(config *rest.Config, options client.Options) (client.Client, error) { - return fakeClient, nil - } reconciler := &ConfigMapReconciler{ Client: fakeClient, Scheme: newScheme(), @@ -94,15 +90,10 @@ func TestConfigMapReconciler_SetupWithManager(t *testing.T) { } t.Log(333) - mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ - Scheme: newScheme(), - NewClient: fakeClientFunc, - }) - t.Log(444) - assert.NoError(t, err) + mgr := newFakeManager() t.Log(555) - err = reconciler.SetupWithManager(mgr) + err := reconciler.SetupWithManager(mgr) t.Log(666) if tc.wantError != "" { assert.ErrorContains(t, err, tc.wantError) diff --git a/internal/controller/ingress_controller_test.go b/internal/controller/ingress_controller_test.go index d4792d3..bf820e6 100644 --- a/internal/controller/ingress_controller_test.go +++ b/internal/controller/ingress_controller_test.go @@ -41,12 +41,9 @@ func TestSetupWithManager(t *testing.T) { Scheme: newScheme(), RulesStore: mockRulesStore, } - mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ - Scheme: newScheme(), - }) - assert.NoError(t, err) - err = reconciler.SetupWithManager(mgr) + mgr := newFakeManager() + err := reconciler.SetupWithManager(mgr) assert.NoError(t, err) }