Skip to content

Commit 7b14f02

Browse files
authored
Merge pull request #512 from mbaldessari/test-coverage
Increase test coverage
2 parents 34cf002 + b8370b5 commit 7b14f02

10 files changed

Lines changed: 3961 additions & 0 deletions

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ linters:
119119
- mnd
120120
- revive
121121
- staticcheck
122+
- dupl
122123
path: _test\.go
123124
- path: pkg/golinters/errcheck.go
124125
text: 'SA1019: errCfg.Exclude is deprecated: use ExcludeFunctions instead'
@@ -144,6 +145,7 @@ linters:
144145
- third_party$
145146
- builtin$
146147
- examples$
148+
- console/node_modules
147149
formatters:
148150
enable:
149151
- gofmt
@@ -166,3 +168,4 @@ formatters:
166168
- third_party$
167169
- builtin$
168170
- examples$
171+
- console/node_modules

internal/controller/acm_test.go

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,208 @@ var _ = Describe("HaveACMHub", func() {
108108
})
109109
})
110110
})
111+
112+
var _ = Describe("ListManagedClusters", func() {
113+
var (
114+
patternReconciler *PatternReconciler
115+
dynamicClient *dynamicfake.FakeDynamicClient
116+
gvrMC schema.GroupVersionResource
117+
)
118+
119+
BeforeEach(func() {
120+
gvrMC = schema.GroupVersionResource{Group: "cluster.open-cluster-management.io", Version: "v1", Resource: "managedclusters"}
121+
122+
dynamicClient = dynamicfake.NewSimpleDynamicClientWithCustomListKinds(runtime.NewScheme(), map[schema.GroupVersionResource]string{
123+
gvrMC: "ManagedClusterList",
124+
})
125+
126+
patternReconciler = &PatternReconciler{
127+
dynamicClient: dynamicClient,
128+
}
129+
})
130+
131+
Context("when there are managed clusters", func() {
132+
BeforeEach(func() {
133+
for _, name := range []string{"local-cluster", "spoke-1", "spoke-2"} {
134+
mc := &unstructured.Unstructured{
135+
Object: map[string]any{
136+
"apiVersion": "cluster.open-cluster-management.io/v1",
137+
"kind": "ManagedCluster",
138+
"metadata": map[string]any{
139+
"name": name,
140+
},
141+
},
142+
}
143+
_, err := dynamicClient.Resource(gvrMC).Create(context.Background(), mc, metav1.CreateOptions{})
144+
Expect(err).ToNot(HaveOccurred())
145+
}
146+
})
147+
148+
It("should return all clusters except local-cluster", func() {
149+
clusters, err := patternReconciler.listManagedClusters(context.Background())
150+
Expect(err).ToNot(HaveOccurred())
151+
Expect(clusters).To(HaveLen(2))
152+
Expect(clusters).To(ContainElement("spoke-1"))
153+
Expect(clusters).To(ContainElement("spoke-2"))
154+
Expect(clusters).ToNot(ContainElement("local-cluster"))
155+
})
156+
})
157+
158+
Context("when there are no managed clusters", func() {
159+
It("should return empty list", func() {
160+
clusters, err := patternReconciler.listManagedClusters(context.Background())
161+
Expect(err).ToNot(HaveOccurred())
162+
Expect(clusters).To(BeEmpty())
163+
})
164+
})
165+
166+
Context("when only local-cluster exists", func() {
167+
BeforeEach(func() {
168+
mc := &unstructured.Unstructured{
169+
Object: map[string]any{
170+
"apiVersion": "cluster.open-cluster-management.io/v1",
171+
"kind": "ManagedCluster",
172+
"metadata": map[string]any{
173+
"name": "local-cluster",
174+
},
175+
},
176+
}
177+
_, err := dynamicClient.Resource(gvrMC).Create(context.Background(), mc, metav1.CreateOptions{})
178+
Expect(err).ToNot(HaveOccurred())
179+
})
180+
181+
It("should return empty list", func() {
182+
clusters, err := patternReconciler.listManagedClusters(context.Background())
183+
Expect(err).ToNot(HaveOccurred())
184+
Expect(clusters).To(BeEmpty())
185+
})
186+
})
187+
188+
Context("when there is an error listing", func() {
189+
BeforeEach(func() {
190+
dynamicClient.PrependReactor("list", "managedclusters", func(testing.Action) (handled bool, ret runtime.Object, err error) {
191+
return true, nil, fmt.Errorf("list error")
192+
})
193+
})
194+
195+
It("should return an error", func() {
196+
_, err := patternReconciler.listManagedClusters(context.Background())
197+
Expect(err).To(HaveOccurred())
198+
Expect(err.Error()).To(ContainSubstring("failed to list ManagedClusters"))
199+
})
200+
})
201+
})
202+
203+
var _ = Describe("DeleteManagedClusters", func() {
204+
var (
205+
patternReconciler *PatternReconciler
206+
dynamicClient *dynamicfake.FakeDynamicClient
207+
gvrMC schema.GroupVersionResource
208+
)
209+
210+
BeforeEach(func() {
211+
gvrMC = schema.GroupVersionResource{Group: "cluster.open-cluster-management.io", Version: "v1", Resource: "managedclusters"}
212+
213+
dynamicClient = dynamicfake.NewSimpleDynamicClientWithCustomListKinds(runtime.NewScheme(), map[schema.GroupVersionResource]string{
214+
gvrMC: "ManagedClusterList",
215+
})
216+
217+
patternReconciler = &PatternReconciler{
218+
dynamicClient: dynamicClient,
219+
}
220+
})
221+
222+
Context("when there are managed clusters to delete", func() {
223+
BeforeEach(func() {
224+
for _, name := range []string{"local-cluster", "spoke-1", "spoke-2"} {
225+
mc := &unstructured.Unstructured{
226+
Object: map[string]any{
227+
"apiVersion": "cluster.open-cluster-management.io/v1",
228+
"kind": "ManagedCluster",
229+
"metadata": map[string]any{
230+
"name": name,
231+
},
232+
},
233+
}
234+
_, err := dynamicClient.Resource(gvrMC).Create(context.Background(), mc, metav1.CreateOptions{})
235+
Expect(err).ToNot(HaveOccurred())
236+
}
237+
})
238+
239+
It("should delete all clusters except local-cluster", func() {
240+
count, err := patternReconciler.deleteManagedClusters(context.Background())
241+
Expect(err).ToNot(HaveOccurred())
242+
Expect(count).To(Equal(2))
243+
})
244+
})
245+
246+
Context("when there are no managed clusters", func() {
247+
It("should return 0", func() {
248+
count, err := patternReconciler.deleteManagedClusters(context.Background())
249+
Expect(err).ToNot(HaveOccurred())
250+
Expect(count).To(Equal(0))
251+
})
252+
})
253+
254+
Context("when only local-cluster exists", func() {
255+
BeforeEach(func() {
256+
mc := &unstructured.Unstructured{
257+
Object: map[string]any{
258+
"apiVersion": "cluster.open-cluster-management.io/v1",
259+
"kind": "ManagedCluster",
260+
"metadata": map[string]any{
261+
"name": "local-cluster",
262+
},
263+
},
264+
}
265+
_, err := dynamicClient.Resource(gvrMC).Create(context.Background(), mc, metav1.CreateOptions{})
266+
Expect(err).ToNot(HaveOccurred())
267+
})
268+
269+
It("should return 0", func() {
270+
count, err := patternReconciler.deleteManagedClusters(context.Background())
271+
Expect(err).ToNot(HaveOccurred())
272+
Expect(count).To(Equal(0))
273+
})
274+
})
275+
276+
Context("when listing fails", func() {
277+
BeforeEach(func() {
278+
dynamicClient.PrependReactor("list", "managedclusters", func(testing.Action) (handled bool, ret runtime.Object, err error) {
279+
return true, nil, fmt.Errorf("list error")
280+
})
281+
})
282+
283+
It("should return an error", func() {
284+
_, err := patternReconciler.deleteManagedClusters(context.Background())
285+
Expect(err).To(HaveOccurred())
286+
Expect(err.Error()).To(ContainSubstring("failed to list ManagedClusters"))
287+
})
288+
})
289+
290+
Context("when delete fails", func() {
291+
BeforeEach(func() {
292+
mc := &unstructured.Unstructured{
293+
Object: map[string]any{
294+
"apiVersion": "cluster.open-cluster-management.io/v1",
295+
"kind": "ManagedCluster",
296+
"metadata": map[string]any{
297+
"name": "spoke-1",
298+
},
299+
},
300+
}
301+
_, err := dynamicClient.Resource(gvrMC).Create(context.Background(), mc, metav1.CreateOptions{})
302+
Expect(err).ToNot(HaveOccurred())
303+
304+
dynamicClient.PrependReactor("delete", "managedclusters", func(testing.Action) (handled bool, ret runtime.Object, err error) {
305+
return true, nil, fmt.Errorf("delete error")
306+
})
307+
})
308+
309+
It("should return an error", func() {
310+
_, err := patternReconciler.deleteManagedClusters(context.Background())
311+
Expect(err).To(HaveOccurred())
312+
Expect(err.Error()).To(ContainSubstring("failed to delete ManagedCluster"))
313+
})
314+
})
315+
})

0 commit comments

Comments
 (0)