sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/model/resource/webhooks_test.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package resource
    18  
    19  import (
    20  	. "github.com/onsi/ginkgo/v2"
    21  	. "github.com/onsi/gomega"
    22  )
    23  
    24  //nolint:dupl
    25  var _ = Describe("Webhooks", func() {
    26  	Context("Validate", func() {
    27  		It("should succeed for a valid Webhooks", func() {
    28  			Expect(Webhooks{WebhookVersion: v1}.Validate()).To(Succeed())
    29  		})
    30  
    31  		DescribeTable("should fail for invalid Webhooks",
    32  			func(webhooks Webhooks) { Expect(webhooks.Validate()).NotTo(Succeed()) },
    33  			// Ensure that the rest of the fields are valid to check each part
    34  			Entry("empty webhook version", Webhooks{}),
    35  			Entry("invalid webhook version", Webhooks{WebhookVersion: "1"}),
    36  		)
    37  	})
    38  
    39  	Context("Update", func() {
    40  		var webhook, other Webhooks
    41  
    42  		It("should do nothing if provided a nil pointer", func() {
    43  			webhook = Webhooks{}
    44  			Expect(webhook.Update(nil)).To(Succeed())
    45  			Expect(webhook.WebhookVersion).To(Equal(""))
    46  			Expect(webhook.Defaulting).To(BeFalse())
    47  			Expect(webhook.Validation).To(BeFalse())
    48  			Expect(webhook.Conversion).To(BeFalse())
    49  
    50  			webhook = Webhooks{
    51  				WebhookVersion: v1,
    52  				Defaulting:     true,
    53  				Validation:     true,
    54  				Conversion:     true,
    55  			}
    56  			Expect(webhook.Update(nil)).To(Succeed())
    57  			Expect(webhook.WebhookVersion).To(Equal(v1))
    58  			Expect(webhook.Defaulting).To(BeTrue())
    59  			Expect(webhook.Validation).To(BeTrue())
    60  			Expect(webhook.Conversion).To(BeTrue())
    61  		})
    62  
    63  		Context("webhooks version", func() {
    64  			It("should modify the webhooks version if provided and not previously set", func() {
    65  				webhook = Webhooks{}
    66  				other = Webhooks{WebhookVersion: v1}
    67  				Expect(webhook.Update(&other)).To(Succeed())
    68  				Expect(webhook.WebhookVersion).To(Equal(v1))
    69  			})
    70  
    71  			It("should keep the webhooks version if not provided", func() {
    72  				webhook = Webhooks{WebhookVersion: v1}
    73  				other = Webhooks{}
    74  				Expect(webhook.Update(&other)).To(Succeed())
    75  				Expect(webhook.WebhookVersion).To(Equal(v1))
    76  			})
    77  
    78  			It("should keep the webhooks version if provided the same as previously set", func() {
    79  				webhook = Webhooks{WebhookVersion: v1}
    80  				other = Webhooks{WebhookVersion: v1}
    81  				Expect(webhook.Update(&other)).To(Succeed())
    82  				Expect(webhook.WebhookVersion).To(Equal(v1))
    83  			})
    84  
    85  			It("should fail if previously set and provided webhooks versions do not match", func() {
    86  				webhook = Webhooks{WebhookVersion: v1}
    87  				other = Webhooks{WebhookVersion: "v1beta1"}
    88  				Expect(webhook.Update(&other)).NotTo(Succeed())
    89  			})
    90  		})
    91  
    92  		Context("Defaulting", func() {
    93  			It("should set the defaulting webhook if provided and not previously set", func() {
    94  				webhook = Webhooks{}
    95  				other = Webhooks{Defaulting: true}
    96  				Expect(webhook.Update(&other)).To(Succeed())
    97  				Expect(webhook.Defaulting).To(BeTrue())
    98  			})
    99  
   100  			It("should keep the defaulting webhook if previously set", func() {
   101  				webhook = Webhooks{Defaulting: true}
   102  
   103  				By("not providing it")
   104  				other = Webhooks{}
   105  				Expect(webhook.Update(&other)).To(Succeed())
   106  				Expect(webhook.Defaulting).To(BeTrue())
   107  
   108  				By("providing it")
   109  				other = Webhooks{Defaulting: true}
   110  				Expect(webhook.Update(&other)).To(Succeed())
   111  				Expect(webhook.Defaulting).To(BeTrue())
   112  			})
   113  
   114  			It("should not set the defaulting webhook if not provided and not previously set", func() {
   115  				webhook = Webhooks{}
   116  				other = Webhooks{}
   117  				Expect(webhook.Update(&other)).To(Succeed())
   118  				Expect(webhook.Defaulting).To(BeFalse())
   119  			})
   120  		})
   121  
   122  		Context("Validation", func() {
   123  			It("should set the validation webhook if provided and not previously set", func() {
   124  				webhook = Webhooks{}
   125  				other = Webhooks{Validation: true}
   126  				Expect(webhook.Update(&other)).To(Succeed())
   127  				Expect(webhook.Validation).To(BeTrue())
   128  			})
   129  
   130  			It("should keep the validation webhook if previously set", func() {
   131  				webhook = Webhooks{Validation: true}
   132  
   133  				By("not providing it")
   134  				other = Webhooks{}
   135  				Expect(webhook.Update(&other)).To(Succeed())
   136  				Expect(webhook.Validation).To(BeTrue())
   137  
   138  				By("providing it")
   139  				other = Webhooks{Validation: true}
   140  				Expect(webhook.Update(&other)).To(Succeed())
   141  				Expect(webhook.Validation).To(BeTrue())
   142  			})
   143  
   144  			It("should not set the validation webhook if not provided and not previously set", func() {
   145  				webhook = Webhooks{}
   146  				other = Webhooks{}
   147  				Expect(webhook.Update(&other)).To(Succeed())
   148  				Expect(webhook.Validation).To(BeFalse())
   149  			})
   150  		})
   151  
   152  		Context("Conversion", func() {
   153  			It("should set the conversion webhook if provided and not previously set", func() {
   154  				webhook = Webhooks{}
   155  				other = Webhooks{Conversion: true}
   156  				Expect(webhook.Update(&other)).To(Succeed())
   157  				Expect(webhook.Conversion).To(BeTrue())
   158  			})
   159  
   160  			It("should keep the conversion webhook if previously set", func() {
   161  				webhook = Webhooks{Conversion: true}
   162  
   163  				By("not providing it")
   164  				other = Webhooks{}
   165  				Expect(webhook.Update(&other)).To(Succeed())
   166  				Expect(webhook.Conversion).To(BeTrue())
   167  
   168  				By("providing it")
   169  				other = Webhooks{Conversion: true}
   170  				Expect(webhook.Update(&other)).To(Succeed())
   171  				Expect(webhook.Conversion).To(BeTrue())
   172  			})
   173  
   174  			It("should not set the conversion webhook if not provided and not previously set", func() {
   175  				webhook = Webhooks{}
   176  				other = Webhooks{}
   177  				Expect(webhook.Update(&other)).To(Succeed())
   178  				Expect(webhook.Conversion).To(BeFalse())
   179  			})
   180  		})
   181  	})
   182  
   183  	Context("IsEmpty", func() {
   184  		var (
   185  			none       = Webhooks{}
   186  			defaulting = Webhooks{
   187  				WebhookVersion: "v1",
   188  				Defaulting:     true,
   189  				Validation:     false,
   190  				Conversion:     false,
   191  			}
   192  			validation = Webhooks{
   193  				WebhookVersion: "v1",
   194  				Defaulting:     false,
   195  				Validation:     true,
   196  				Conversion:     false,
   197  			}
   198  			conversion = Webhooks{
   199  				WebhookVersion: "v1",
   200  				Defaulting:     false,
   201  				Validation:     false,
   202  				Conversion:     true,
   203  			}
   204  			defaultingAndValidation = Webhooks{
   205  				WebhookVersion: "v1",
   206  				Defaulting:     true,
   207  				Validation:     true,
   208  				Conversion:     false,
   209  			}
   210  			defaultingAndConversion = Webhooks{
   211  				WebhookVersion: "v1",
   212  				Defaulting:     true,
   213  				Validation:     false,
   214  				Conversion:     true,
   215  			}
   216  			validationAndConversion = Webhooks{
   217  				WebhookVersion: "v1",
   218  				Defaulting:     false,
   219  				Validation:     true,
   220  				Conversion:     true,
   221  			}
   222  			all = Webhooks{
   223  				WebhookVersion: "v1",
   224  				Defaulting:     true,
   225  				Validation:     true,
   226  				Conversion:     true,
   227  			}
   228  		)
   229  
   230  		It("should return true fo an empty object", func() {
   231  			Expect(none.IsEmpty()).To(BeTrue())
   232  		})
   233  
   234  		DescribeTable("should return false for non-empty objects",
   235  			func(webhooks Webhooks) { Expect(webhooks.IsEmpty()).To(BeFalse()) },
   236  			Entry("defaulting", defaulting),
   237  			Entry("validation", validation),
   238  			Entry("conversion", conversion),
   239  			Entry("defaulting and validation", defaultingAndValidation),
   240  			Entry("defaulting and conversion", defaultingAndConversion),
   241  			Entry("validation and conversion", validationAndConversion),
   242  			Entry("defaulting and validation and conversion", all),
   243  		)
   244  	})
   245  })