@@ -8,8 +8,10 @@ import (
88 "k8s.io/apimachinery/pkg/runtime"
99 "k8s.io/apimachinery/pkg/runtime/schema"
1010 dynamicfake "k8s.io/client-go/dynamic/fake"
11- "k8s.io/client-go/kubernetes/fake"
1211 "k8s.io/client-go/testing"
12+ "sigs.k8s.io/controller-runtime/pkg/client"
13+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
14+ "sigs.k8s.io/controller-runtime/pkg/client/interceptor"
1315
1416 v1 "k8s.io/api/core/v1"
1517 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -21,24 +23,18 @@ import (
2123var _ = Describe ("HaveACMHub" , func () {
2224 var (
2325 patternReconciler * PatternReconciler
24- kubeClient * fake.Clientset
2526 dynamicClient * dynamicfake.FakeDynamicClient
2627 gvrMCH schema.GroupVersionResource
28+ fakeClient client.Client
2729 )
2830
2931 BeforeEach (func () {
30- kubeClient = fake .NewSimpleClientset ()
3132 gvrMCH = schema.GroupVersionResource {Group : "operator.open-cluster-management.io" , Version : "v1" , Resource : "multiclusterhubs" }
3233
3334 dynamicClient = dynamicfake .NewSimpleDynamicClientWithCustomListKinds (runtime .NewScheme (), map [schema.GroupVersionResource ]string {
3435 gvrMCH : "MultiClusterHubList" ,
3536 })
3637
37- patternReconciler = & PatternReconciler {
38- fullClient : kubeClient ,
39- dynamicClient : dynamicClient ,
40- }
41-
4238 })
4339
4440 Context ("when the ACM Hub exists" , func () {
@@ -52,8 +48,13 @@ var _ = Describe("HaveACMHub", func() {
5248 },
5349 },
5450 }
55- _ , err := kubeClient .CoreV1 ().ConfigMaps ("default" ).Create (context .Background (), configMap , metav1.CreateOptions {})
56- Expect (err ).ToNot (HaveOccurred ())
51+
52+ fakeClient = fake .NewClientBuilder ().WithScheme (testEnv .Scheme ).
53+ WithRuntimeObjects (configMap ).Build ()
54+ patternReconciler = & PatternReconciler {
55+ Client : fakeClient ,
56+ dynamicClient : dynamicClient ,
57+ }
5758
5859 hub := & unstructured.Unstructured {
5960 Object : map [string ]any {
@@ -65,7 +66,7 @@ var _ = Describe("HaveACMHub", func() {
6566 },
6667 },
6768 }
68- _ , err = dynamicClient .Resource (gvrMCH ).Namespace ("default" ).Create (context .Background (), hub , metav1.CreateOptions {})
69+ _ , err : = dynamicClient .Resource (gvrMCH ).Namespace ("default" ).Create (context .Background (), hub , metav1.CreateOptions {})
6970 Expect (err ).ToNot (HaveOccurred ())
7071 })
7172
@@ -77,16 +78,28 @@ var _ = Describe("HaveACMHub", func() {
7778
7879 Context ("when the ACM Hub does not exist" , func () {
7980 It ("should return false" , func () {
81+ fakeClient = fake .NewClientBuilder ().WithScheme (testEnv .Scheme ).
82+ WithRuntimeObjects ().Build ()
83+ patternReconciler = & PatternReconciler {
84+ Client : fakeClient ,
85+ dynamicClient : dynamicClient ,
86+ }
87+
8088 result := haveACMHub (patternReconciler )
8189 Expect (result ).To (BeFalse ())
8290 })
8391 })
8492
8593 Context ("when there is an error listing ConfigMaps" , func () {
8694 BeforeEach (func () {
87- kubeClient .PrependReactor ("list" , "configmaps" , func (testing.Action ) (handled bool , ret runtime.Object , err error ) {
88- return true , nil , fmt .Errorf ("config map error" )
89- })
95+ fakeClient = fake .NewClientBuilder ().WithInterceptorFuncs (
96+ interceptor.Funcs {List : func (ctx context.Context , client client.WithWatch , obj client.ObjectList , opts ... client.ListOption ) error {
97+ return fmt .Errorf ("list error" )
98+ }}).WithScheme (testEnv .Scheme ).Build ()
99+ patternReconciler = & PatternReconciler {
100+ Client : fakeClient ,
101+ dynamicClient : dynamicClient ,
102+ }
90103 })
91104
92105 It ("should return false and log the error" , func () {
@@ -106,12 +119,16 @@ var _ = Describe("HaveACMHub", func() {
106119 },
107120 },
108121 }
109- _ , err := kubeClient . CoreV1 ().ConfigMaps ( "default" ). Create ( context . Background (), configMap , metav1. CreateOptions {})
110- Expect ( err ). ToNot ( HaveOccurred () )
122+ fakeClient = fake . NewClientBuilder ().WithScheme ( testEnv . Scheme ).
123+ WithRuntimeObjects ( configMap ). Build ( )
111124
112125 dynamicClient .PrependReactor ("list" , "multiclusterhubs" , func (testing.Action ) (handled bool , ret runtime.Object , err error ) {
113126 return true , nil , fmt .Errorf ("multiclusterhub error" )
114127 })
128+ patternReconciler = & PatternReconciler {
129+ Client : fakeClient ,
130+ dynamicClient : dynamicClient ,
131+ }
115132 })
116133
117134 It ("should return false and log the error" , func () {
0 commit comments