@@ -4,75 +4,74 @@ import (
44 "context"
55 "fmt"
66
7- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8- "k8s.io/apimachinery/pkg/runtime"
9- "k8s.io/apimachinery/pkg/runtime/schema"
10- dynamicfake "k8s.io/client-go/dynamic/fake"
11- "k8s.io/client-go/testing"
127 "sigs.k8s.io/controller-runtime/pkg/client"
138 "sigs.k8s.io/controller-runtime/pkg/client/fake"
149 "sigs.k8s.io/controller-runtime/pkg/client/interceptor"
1510
1611 v1 "k8s.io/api/core/v1"
12+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1713 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1814
1915 . "github.com/onsi/ginkgo/v2"
2016 . "github.com/onsi/gomega"
2117)
2218
2319var _ = Describe ("HaveACMHub" , func () {
20+
2421 var (
2522 patternReconciler * PatternReconciler
26- dynamicClient * dynamicfake.FakeDynamicClient
27- gvrMCH schema.GroupVersionResource
2823 fakeClient client.Client
24+ configMap * v1.ConfigMap
25+ hub * unstructured.Unstructured
2926 )
30-
3127 BeforeEach (func () {
32- gvrMCH = schema.GroupVersionResource {Group : "operator.open-cluster-management.io" , Version : "v1" , Resource : "multiclusterhubs" }
28+ configMap = & v1.ConfigMap {
29+ ObjectMeta : metav1.ObjectMeta {
30+ Name : "test-configmap" ,
31+ Namespace : "default" ,
32+ Labels : map [string ]string {
33+ "ocm-configmap-type" : "image-manifest" ,
34+ },
35+ },
36+ }
37+ hub = & unstructured.Unstructured {
38+ Object : map [string ]any {
39+ "apiVersion" : "operator.open-cluster-management.io/v1" ,
40+ "kind" : "MultiClusterHub" ,
41+ "metadata" : map [string ]any {
42+ "name" : "test-hub" ,
43+ "namespace" : "default" ,
44+ },
45+ },
46+ }
47+ hub .SetGroupVersionKind (mchGVK )
48+ })
3349
34- dynamicClient = dynamicfake .NewSimpleDynamicClientWithCustomListKinds (runtime .NewScheme (), map [schema.GroupVersionResource ]string {
35- gvrMCH : "MultiClusterHubList" ,
36- })
50+ Context ("when the ACM Hub exists in same ns as configmap" , func () {
51+ It ("should return true" , func () {
52+ fakeClient = fake .NewClientBuilder ().WithScheme (testEnv .Scheme ).
53+ WithRuntimeObjects (configMap , hub ).Build ()
54+ patternReconciler = & PatternReconciler {
55+ Client : fakeClient ,
56+ }
3757
58+ result := haveACMHub (patternReconciler )
59+ Expect (result ).To (BeTrue ())
60+ })
3861 })
3962
40- Context ("when the ACM Hub exists" , func () {
41- BeforeEach (func () {
42- configMap := & v1.ConfigMap {
43- ObjectMeta : metav1.ObjectMeta {
44- Name : "test-configmap" ,
45- Namespace : "default" ,
46- Labels : map [string ]string {
47- "ocm-configmap-type" : "image-manifest" ,
48- },
49- },
50- }
63+ Context ("when the ACM Hub exists different ns as configmap" , func () {
64+ It ("should return false" , func () {
65+ hub .SetNamespace ("different" )
5166
5267 fakeClient = fake .NewClientBuilder ().WithScheme (testEnv .Scheme ).
53- WithRuntimeObjects (configMap ).Build ()
68+ WithRuntimeObjects (configMap , hub ).Build ()
5469 patternReconciler = & PatternReconciler {
55- Client : fakeClient ,
56- dynamicClient : dynamicClient ,
70+ Client : fakeClient ,
5771 }
5872
59- hub := & unstructured.Unstructured {
60- Object : map [string ]any {
61- "apiVersion" : "operator.open-cluster-management.io/v1" ,
62- "kind" : "MultiClusterHub" ,
63- "metadata" : map [string ]any {
64- "name" : "test-hub" ,
65- "namespace" : "default" ,
66- },
67- },
68- }
69- _ , err := dynamicClient .Resource (gvrMCH ).Namespace ("default" ).Create (context .Background (), hub , metav1.CreateOptions {})
70- Expect (err ).ToNot (HaveOccurred ())
71- })
72-
73- It ("should return true" , func () {
7473 result := haveACMHub (patternReconciler )
75- Expect (result ).To (BeTrue ())
74+ Expect (result ).To (BeFalse ())
7675 })
7776 })
7877
@@ -81,8 +80,7 @@ var _ = Describe("HaveACMHub", func() {
8180 fakeClient = fake .NewClientBuilder ().WithScheme (testEnv .Scheme ).
8281 WithRuntimeObjects ().Build ()
8382 patternReconciler = & PatternReconciler {
84- Client : fakeClient ,
85- dynamicClient : dynamicClient ,
83+ Client : fakeClient ,
8684 }
8785
8886 result := haveACMHub (patternReconciler )
@@ -91,47 +89,32 @@ var _ = Describe("HaveACMHub", func() {
9189 })
9290
9391 Context ("when there is an error listing ConfigMaps" , func () {
94- BeforeEach ( func () {
92+ It ( "should return false and log the error" , func () {
9593 fakeClient = fake .NewClientBuilder ().WithInterceptorFuncs (
9694 interceptor.Funcs {List : func (ctx context.Context , client client.WithWatch , obj client.ObjectList , opts ... client.ListOption ) error {
9795 return fmt .Errorf ("list error" )
9896 }}).WithScheme (testEnv .Scheme ).Build ()
97+
9998 patternReconciler = & PatternReconciler {
100- Client : fakeClient ,
101- dynamicClient : dynamicClient ,
99+ Client : fakeClient ,
102100 }
103- })
104101
105- It ("should return false and log the error" , func () {
106102 result := haveACMHub (patternReconciler )
107103 Expect (result ).To (BeFalse ())
108104 })
109105 })
110106
111107 Context ("when there is an error listing the MultiClusterHubs" , func () {
112- BeforeEach (func () {
113- configMap := & v1.ConfigMap {
114- ObjectMeta : metav1.ObjectMeta {
115- Name : "test-configmap" ,
116- Namespace : "default" ,
117- Labels : map [string ]string {
118- "ocm-configmap-type" : "image-manifest" ,
119- },
120- },
121- }
122- fakeClient = fake .NewClientBuilder ().WithScheme (testEnv .Scheme ).
123- WithRuntimeObjects (configMap ).Build ()
108+ It ("should return false and log the error" , func () {
109+ fakeClient = fake .NewClientBuilder ().WithInterceptorFuncs (
110+ interceptor.Funcs {List : func (ctx context.Context , client client.WithWatch , list client.ObjectList , opts ... client.ListOption ) error {
111+ return fmt .Errorf ("list error" )
112+ }}).WithScheme (testEnv .Scheme ).WithRuntimeObjects (configMap ).Build ()
124113
125- dynamicClient .PrependReactor ("list" , "multiclusterhubs" , func (testing.Action ) (handled bool , ret runtime.Object , err error ) {
126- return true , nil , fmt .Errorf ("multiclusterhub error" )
127- })
128114 patternReconciler = & PatternReconciler {
129- Client : fakeClient ,
130- dynamicClient : dynamicClient ,
115+ Client : fakeClient ,
131116 }
132- })
133117
134- It ("should return false and log the error" , func () {
135118 result := haveACMHub (patternReconciler )
136119 Expect (result ).To (BeFalse ())
137120 })
0 commit comments