...

Source file src/sigs.k8s.io/controller-runtime/pkg/client/interceptor/intercept_test.go

Documentation: sigs.k8s.io/controller-runtime/pkg/client/interceptor

     1  package interceptor
     2  
     3  import (
     4  	"context"
     5  
     6  	"k8s.io/apimachinery/pkg/api/meta"
     7  	"k8s.io/apimachinery/pkg/runtime"
     8  	"k8s.io/apimachinery/pkg/runtime/schema"
     9  
    10  	. "github.com/onsi/ginkgo/v2"
    11  	. "github.com/onsi/gomega"
    12  	"k8s.io/apimachinery/pkg/types"
    13  	"k8s.io/apimachinery/pkg/watch"
    14  	"sigs.k8s.io/controller-runtime/pkg/client"
    15  )
    16  
    17  var _ = Describe("NewClient", func() {
    18  	wrappedClient := dummyClient{}
    19  	ctx := context.Background()
    20  	It("should call the provided Get function", func() {
    21  		var called bool
    22  		client := NewClient(wrappedClient, Funcs{
    23  			Get: func(ctx context.Context, client client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
    24  				called = true
    25  				return nil
    26  			},
    27  		})
    28  		_ = client.Get(ctx, types.NamespacedName{}, nil)
    29  		Expect(called).To(BeTrue())
    30  	})
    31  	It("should call the underlying client if the provided Get function is nil", func() {
    32  		var called bool
    33  		client1 := NewClient(wrappedClient, Funcs{
    34  			Get: func(ctx context.Context, client client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
    35  				called = true
    36  				return nil
    37  			},
    38  		})
    39  		client2 := NewClient(client1, Funcs{})
    40  		_ = client2.Get(ctx, types.NamespacedName{}, nil)
    41  		Expect(called).To(BeTrue())
    42  	})
    43  	It("should call the provided List function", func() {
    44  		var called bool
    45  		client := NewClient(wrappedClient, Funcs{
    46  			List: func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
    47  				called = true
    48  				return nil
    49  			},
    50  		})
    51  		_ = client.List(ctx, nil)
    52  		Expect(called).To(BeTrue())
    53  	})
    54  	It("should call the underlying client if the provided List function is nil", func() {
    55  		var called bool
    56  		client1 := NewClient(wrappedClient, Funcs{
    57  			List: func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
    58  				called = true
    59  				return nil
    60  			},
    61  		})
    62  		client2 := NewClient(client1, Funcs{})
    63  		_ = client2.List(ctx, nil)
    64  		Expect(called).To(BeTrue())
    65  	})
    66  	It("should call the provided Create function", func() {
    67  		var called bool
    68  		client := NewClient(wrappedClient, Funcs{
    69  			Create: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.CreateOption) error {
    70  				called = true
    71  				return nil
    72  			},
    73  		})
    74  		_ = client.Create(ctx, nil)
    75  		Expect(called).To(BeTrue())
    76  	})
    77  	It("should call the underlying client if the provided Create function is nil", func() {
    78  		var called bool
    79  		client1 := NewClient(wrappedClient, Funcs{
    80  			Create: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.CreateOption) error {
    81  				called = true
    82  				return nil
    83  			},
    84  		})
    85  		client2 := NewClient(client1, Funcs{})
    86  		_ = client2.Create(ctx, nil)
    87  		Expect(called).To(BeTrue())
    88  	})
    89  	It("should call the provided Delete function", func() {
    90  		var called bool
    91  		client := NewClient(wrappedClient, Funcs{
    92  			Delete: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteOption) error {
    93  				called = true
    94  				return nil
    95  			},
    96  		})
    97  		_ = client.Delete(ctx, nil)
    98  		Expect(called).To(BeTrue())
    99  	})
   100  	It("should call the underlying client if the provided Delete function is nil", func() {
   101  		var called bool
   102  		client1 := NewClient(wrappedClient, Funcs{
   103  			Delete: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteOption) error {
   104  				called = true
   105  				return nil
   106  			},
   107  		})
   108  		client2 := NewClient(client1, Funcs{})
   109  		_ = client2.Delete(ctx, nil)
   110  		Expect(called).To(BeTrue())
   111  	})
   112  	It("should call the provided DeleteAllOf function", func() {
   113  		var called bool
   114  		client := NewClient(wrappedClient, Funcs{
   115  			DeleteAllOf: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteAllOfOption) error {
   116  				called = true
   117  				return nil
   118  			},
   119  		})
   120  		_ = client.DeleteAllOf(ctx, nil)
   121  		Expect(called).To(BeTrue())
   122  	})
   123  	It("should call the underlying client if the provided DeleteAllOf function is nil", func() {
   124  		var called bool
   125  		client1 := NewClient(wrappedClient, Funcs{
   126  			DeleteAllOf: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteAllOfOption) error {
   127  				called = true
   128  				return nil
   129  			},
   130  		})
   131  		client2 := NewClient(client1, Funcs{})
   132  		_ = client2.DeleteAllOf(ctx, nil)
   133  		Expect(called).To(BeTrue())
   134  	})
   135  	It("should call the provided Update function", func() {
   136  		var called bool
   137  		client := NewClient(wrappedClient, Funcs{
   138  			Update: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.UpdateOption) error {
   139  				called = true
   140  				return nil
   141  			},
   142  		})
   143  		_ = client.Update(ctx, nil)
   144  		Expect(called).To(BeTrue())
   145  	})
   146  	It("should call the underlying client if the provided Update function is nil", func() {
   147  		var called bool
   148  		client1 := NewClient(wrappedClient, Funcs{
   149  			Update: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.UpdateOption) error {
   150  				called = true
   151  				return nil
   152  			},
   153  		})
   154  		client2 := NewClient(client1, Funcs{})
   155  		_ = client2.Update(ctx, nil)
   156  		Expect(called).To(BeTrue())
   157  	})
   158  	It("should call the provided Patch function", func() {
   159  		var called bool
   160  		client := NewClient(wrappedClient, Funcs{
   161  			Patch: func(ctx context.Context, client client.WithWatch, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
   162  				called = true
   163  				return nil
   164  			},
   165  		})
   166  		_ = client.Patch(ctx, nil, nil)
   167  		Expect(called).To(BeTrue())
   168  	})
   169  	It("should call the underlying client if the provided Patch function is nil", func() {
   170  		var called bool
   171  		client1 := NewClient(wrappedClient, Funcs{
   172  			Patch: func(ctx context.Context, client client.WithWatch, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
   173  				called = true
   174  				return nil
   175  			},
   176  		})
   177  		client2 := NewClient(client1, Funcs{})
   178  		_ = client2.Patch(ctx, nil, nil)
   179  		Expect(called).To(BeTrue())
   180  	})
   181  	It("should call the provided Watch function", func() {
   182  		var called bool
   183  		client := NewClient(wrappedClient, Funcs{
   184  			Watch: func(ctx context.Context, client client.WithWatch, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error) {
   185  				called = true
   186  				return nil, nil
   187  			},
   188  		})
   189  		_, _ = client.Watch(ctx, nil)
   190  		Expect(called).To(BeTrue())
   191  	})
   192  	It("should call the underlying client if the provided Watch function is nil", func() {
   193  		var called bool
   194  		client1 := NewClient(wrappedClient, Funcs{
   195  			Watch: func(ctx context.Context, client client.WithWatch, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error) {
   196  				called = true
   197  				return nil, nil
   198  			},
   199  		})
   200  		client2 := NewClient(client1, Funcs{})
   201  		_, _ = client2.Watch(ctx, nil)
   202  		Expect(called).To(BeTrue())
   203  	})
   204  	It("should call the provided SubResource function", func() {
   205  		var called bool
   206  		client := NewClient(wrappedClient, Funcs{
   207  			SubResource: func(client client.WithWatch, subResource string) client.SubResourceClient {
   208  				called = true
   209  				return nil
   210  			},
   211  		})
   212  		_ = client.SubResource("")
   213  		Expect(called).To(BeTrue())
   214  	})
   215  	It("should call the provided SubResource function with 'status' when calling Status()", func() {
   216  		var called bool
   217  		client := NewClient(wrappedClient, Funcs{
   218  			SubResource: func(client client.WithWatch, subResource string) client.SubResourceClient {
   219  				if subResource == "status" {
   220  					called = true
   221  				}
   222  				return nil
   223  			},
   224  		})
   225  		_ = client.Status()
   226  		Expect(called).To(BeTrue())
   227  	})
   228  })
   229  
   230  var _ = Describe("NewSubResourceClient", func() {
   231  	c := dummyClient{}
   232  	ctx := context.Background()
   233  	It("should call the provided Get function", func() {
   234  		var called bool
   235  		c := NewClient(c, Funcs{
   236  			SubResourceGet: func(_ context.Context, client client.Client, subResourceName string, obj, subResource client.Object, opts ...client.SubResourceGetOption) error {
   237  				called = true
   238  				Expect(subResourceName).To(BeEquivalentTo("foo"))
   239  				return nil
   240  			},
   241  		})
   242  		_ = c.SubResource("foo").Get(ctx, nil, nil)
   243  		Expect(called).To(BeTrue())
   244  	})
   245  	It("should call the underlying client if the provided Get function is nil", func() {
   246  		var called bool
   247  		client1 := NewClient(c, Funcs{
   248  			SubResourceGet: func(_ context.Context, client client.Client, subResourceName string, obj, subResource client.Object, opts ...client.SubResourceGetOption) error {
   249  				called = true
   250  				Expect(subResourceName).To(BeEquivalentTo("foo"))
   251  				return nil
   252  			},
   253  		})
   254  		client2 := NewClient(client1, Funcs{})
   255  		_ = client2.SubResource("foo").Get(ctx, nil, nil)
   256  		Expect(called).To(BeTrue())
   257  	})
   258  	It("should call the provided Update function", func() {
   259  		var called bool
   260  		client := NewClient(c, Funcs{
   261  			SubResourceUpdate: func(_ context.Context, client client.Client, subResourceName string, obj client.Object, opts ...client.SubResourceUpdateOption) error {
   262  				called = true
   263  				Expect(subResourceName).To(BeEquivalentTo("foo"))
   264  				return nil
   265  			},
   266  		})
   267  		_ = client.SubResource("foo").Update(ctx, nil, nil)
   268  		Expect(called).To(BeTrue())
   269  	})
   270  	It("should call the underlying client if the provided Update function is nil", func() {
   271  		var called bool
   272  		client1 := NewClient(c, Funcs{
   273  			SubResourceUpdate: func(_ context.Context, client client.Client, subResourceName string, obj client.Object, opts ...client.SubResourceUpdateOption) error {
   274  				called = true
   275  				Expect(subResourceName).To(BeEquivalentTo("foo"))
   276  				return nil
   277  			},
   278  		})
   279  		client2 := NewClient(client1, Funcs{})
   280  		_ = client2.SubResource("foo").Update(ctx, nil, nil)
   281  		Expect(called).To(BeTrue())
   282  	})
   283  	It("should call the provided Patch function", func() {
   284  		var called bool
   285  		client := NewClient(c, Funcs{
   286  			SubResourcePatch: func(_ context.Context, client client.Client, subResourceName string, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
   287  				called = true
   288  				Expect(subResourceName).To(BeEquivalentTo("foo"))
   289  				return nil
   290  			},
   291  		})
   292  		_ = client.SubResource("foo").Patch(ctx, nil, nil)
   293  		Expect(called).To(BeTrue())
   294  	})
   295  	It("should call the underlying client if the provided Patch function is nil", func() {
   296  		var called bool
   297  		client1 := NewClient(c, Funcs{
   298  			SubResourcePatch: func(ctx context.Context, client client.Client, subResourceName string, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
   299  				called = true
   300  				Expect(subResourceName).To(BeEquivalentTo("foo"))
   301  				return nil
   302  			},
   303  		})
   304  		client2 := NewClient(client1, Funcs{})
   305  		_ = client2.SubResource("foo").Patch(ctx, nil, nil)
   306  		Expect(called).To(BeTrue())
   307  	})
   308  	It("should call the provided Create function", func() {
   309  		var called bool
   310  		client := NewClient(c, Funcs{
   311  			SubResourceCreate: func(_ context.Context, client client.Client, subResourceName string, obj, subResource client.Object, opts ...client.SubResourceCreateOption) error {
   312  				called = true
   313  				Expect(subResourceName).To(BeEquivalentTo("foo"))
   314  				return nil
   315  			},
   316  		})
   317  		_ = client.SubResource("foo").Create(ctx, nil, nil)
   318  		Expect(called).To(BeTrue())
   319  	})
   320  	It("should call the underlying client if the provided Create function is nil", func() {
   321  		var called bool
   322  		client1 := NewClient(c, Funcs{
   323  			SubResourceCreate: func(_ context.Context, client client.Client, subResourceName string, obj, subResource client.Object, opts ...client.SubResourceCreateOption) error {
   324  				called = true
   325  				Expect(subResourceName).To(BeEquivalentTo("foo"))
   326  				return nil
   327  			},
   328  		})
   329  		client2 := NewClient(client1, Funcs{})
   330  		_ = client2.SubResource("foo").Create(ctx, nil, nil)
   331  		Expect(called).To(BeTrue())
   332  	})
   333  })
   334  
   335  type dummyClient struct{}
   336  
   337  var _ client.WithWatch = &dummyClient{}
   338  
   339  func (d dummyClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
   340  	return nil
   341  }
   342  
   343  func (d dummyClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
   344  	return nil
   345  }
   346  
   347  func (d dummyClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
   348  	return nil
   349  }
   350  
   351  func (d dummyClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
   352  	return nil
   353  }
   354  
   355  func (d dummyClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
   356  	return nil
   357  }
   358  
   359  func (d dummyClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
   360  	return nil
   361  }
   362  
   363  func (d dummyClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
   364  	return nil
   365  }
   366  
   367  func (d dummyClient) Status() client.SubResourceWriter {
   368  	return d.SubResource("status")
   369  }
   370  
   371  func (d dummyClient) SubResource(subResource string) client.SubResourceClient {
   372  	return nil
   373  }
   374  
   375  func (d dummyClient) Scheme() *runtime.Scheme {
   376  	return nil
   377  }
   378  
   379  func (d dummyClient) RESTMapper() meta.RESTMapper {
   380  	return nil
   381  }
   382  
   383  func (d dummyClient) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) {
   384  	return schema.GroupVersionKind{}, nil
   385  }
   386  
   387  func (d dummyClient) IsObjectNamespaced(obj runtime.Object) (bool, error) {
   388  	return false, nil
   389  }
   390  
   391  func (d dummyClient) Watch(ctx context.Context, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error) {
   392  	return nil, nil
   393  }
   394  

View as plain text