@@ -16,18 +16,20 @@ package client
1616
1717import (
1818 "context"
19+ "errors"
20+ "time"
1921
2022 . "github.com/onsi/ginkgo/v2"
2123 . "github.com/onsi/gomega"
2224 olmapiv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
2325 appsv1 "k8s.io/api/apps/v1"
2426 corev1 "k8s.io/api/core/v1"
27+ "k8s.io/apimachinery/pkg/api/meta"
2528 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26-
29+ "k8s.io/apimachinery/pkg/runtime"
2730 "k8s.io/apimachinery/pkg/types"
28-
2931 "sigs.k8s.io/controller-runtime/pkg/client"
30- fake "sigs.k8s.io/controller-runtime/pkg/client/fake"
32+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
3133)
3234
3335var _ = Describe ("Client" , func () {
@@ -258,4 +260,156 @@ var _ = Describe("Client", func() {
258260 })
259261 })
260262 })
263+
264+ Describe ("test DoCreate" , func () {
265+ var fakeClient client.Client
266+
267+ BeforeEach (func () {
268+ fakeClient = & errClient {cli : fake .NewClientBuilder ().Build ()}
269+ })
270+
271+ AfterEach (func () {
272+ fakeClient .(* errClient ).reset ()
273+ })
274+
275+ It ("should create all the resources successfully" , func () {
276+ cli := Client {KubeClient : fakeClient }
277+
278+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
279+ defer cancel ()
280+
281+ Expect (cli .DoCreate (ctx ,
282+ & corev1.Namespace {
283+ ObjectMeta : metav1.ObjectMeta {Name : "test-ns" },
284+ },
285+ & corev1.Pod {
286+ ObjectMeta : metav1.ObjectMeta {Name : "test-pod" , Namespace : "test-ns" },
287+ },
288+ )).To (Succeed ())
289+
290+ ns := & corev1.Namespace {}
291+ Expect (fakeClient .Get (context .Background (), client.ObjectKey {Name : "test-ns" }, ns )).To (Succeed ())
292+
293+ pod := & corev1.Pod {}
294+ Expect (fakeClient .Get (context .Background (), client.ObjectKey {Namespace : "test-ns" , Name : "test-pod" }, pod )).To (Succeed ())
295+ })
296+
297+ It ("should eventually create all the resources successfully" , func () {
298+ cli := Client {KubeClient : fakeClient }
299+
300+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
301+ defer cancel ()
302+
303+ Expect (cli .DoCreate (ctx ,
304+ & corev1.Namespace {
305+ ObjectMeta : metav1.ObjectMeta {Name : "test-ns" },
306+ },
307+ & corev1.Pod {
308+ ObjectMeta : metav1.ObjectMeta {Name : "eventually-match" , Namespace : "test-ns" },
309+ },
310+ )).To (Succeed ())
311+
312+ ns := & corev1.Namespace {}
313+ Expect (fakeClient .Get (context .Background (), client.ObjectKey {Name : "test-ns" }, ns )).To (Succeed ())
314+
315+ pod := & corev1.Pod {}
316+ Expect (fakeClient .Get (context .Background (), client.ObjectKey {Namespace : "test-ns" , Name : "eventually-match" }, pod )).To (Succeed ())
317+ })
318+
319+ It ("should fail with no-match error" , func () {
320+ cli := Client {KubeClient : fakeClient }
321+
322+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
323+ defer cancel ()
324+ Expect (cli .DoCreate (ctx ,
325+ & corev1.Namespace {
326+ ObjectMeta : metav1.ObjectMeta {Name : "test-ns" },
327+ },
328+ & corev1.Pod {
329+ ObjectMeta : metav1.ObjectMeta {Name : "no-match" , Namespace : "test-ns" },
330+ },
331+ )).ToNot (Succeed ())
332+ })
333+
334+ It ("should fail with unknown-error error" , func () {
335+ cli := Client {KubeClient : fakeClient }
336+
337+ Expect (cli .DoCreate (context .Background (),
338+ & corev1.Namespace {
339+ ObjectMeta : metav1.ObjectMeta {Name : "test-ns" },
340+ },
341+ & corev1.Pod {
342+ ObjectMeta : metav1.ObjectMeta {Name : "unknown-error" , Namespace : "test-ns" },
343+ },
344+ )).ToNot (Succeed ())
345+ })
346+ })
261347})
348+
349+ type errClient struct {
350+ cli client.Client
351+ noMatchCounter int
352+ }
353+
354+ func (c * errClient ) reset () {
355+ c .noMatchCounter = 0
356+ }
357+
358+ func (c * errClient ) Get (ctx context.Context , key client.ObjectKey , obj client.Object , opts ... client.GetOption ) error {
359+ return c .cli .Get (ctx , key , obj , opts ... )
360+ }
361+
362+ func (c * errClient ) List (ctx context.Context , list client.ObjectList , opts ... client.ListOption ) error {
363+ return c .cli .List (ctx , list , opts ... )
364+ }
365+ func (c * errClient ) Create (ctx context.Context , obj client.Object , opts ... client.CreateOption ) error {
366+ switch obj .GetName () {
367+ case "no-match" :
368+ return & meta.NoResourceMatchError {}
369+
370+ case "eventually-match" :
371+ if c .noMatchCounter >= 4 {
372+ return c .cli .Create (ctx , obj , opts ... )
373+ }
374+ c .noMatchCounter ++
375+ return & meta.NoResourceMatchError {}
376+
377+ case "unknown-error" :
378+ return errors .New ("fake error" )
379+
380+ default :
381+ return c .cli .Create (ctx , obj , opts ... )
382+ }
383+ }
384+
385+ func (c * errClient ) Delete (ctx context.Context , obj client.Object , opts ... client.DeleteOption ) error {
386+ return c .cli .Delete (ctx , obj , opts ... )
387+ }
388+
389+ func (c * errClient ) Update (ctx context.Context , obj client.Object , opts ... client.UpdateOption ) error {
390+ return c .cli .Update (ctx , obj , opts ... )
391+ }
392+
393+ func (c * errClient ) Patch (ctx context.Context , obj client.Object , patch client.Patch , opts ... client.PatchOption ) error {
394+ return c .cli .Patch (ctx , obj , patch , opts ... )
395+ }
396+
397+ func (c * errClient ) DeleteAllOf (ctx context.Context , obj client.Object , opts ... client.DeleteAllOfOption ) error {
398+ return c .cli .DeleteAllOf (ctx , obj , opts ... )
399+ }
400+
401+ func (c * errClient ) SubResource (subResource string ) client.SubResourceClient {
402+ return c .cli .SubResource (subResource )
403+ }
404+
405+ func (c * errClient ) Scheme () * runtime.Scheme {
406+ return c .cli .Scheme ()
407+ }
408+
409+ func (c * errClient ) RESTMapper () meta.RESTMapper {
410+ return c .cli .RESTMapper ()
411+ }
412+
413+ func (c * errClient ) Status () client.SubResourceWriter {
414+ return c .cli .Status ()
415+ }
0 commit comments