...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/kernelchaos_webhook_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/v1alpha1

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package v1alpha1
    17  
    18  import (
    19  	. "github.com/onsi/ginkgo/v2"
    20  	. "github.com/onsi/gomega"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  )
    23  
    24  var _ = Describe("kernelchaos_webhook", func() {
    25  	Context("Defaulter", func() {
    26  		It("set default namespace selector", func() {
    27  			kernelchaos := &KernelChaos{
    28  				ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault},
    29  			}
    30  			kernelchaos.Default()
    31  			Expect(kernelchaos.Spec.Selector.Namespaces[0]).To(Equal(metav1.NamespaceDefault))
    32  		})
    33  	})
    34  	Context("webhook.Validator of kernelchaos", func() {
    35  		It("Validate", func() {
    36  
    37  			type TestCase struct {
    38  				name    string
    39  				chaos   KernelChaos
    40  				execute func(chaos *KernelChaos) error
    41  				expect  string
    42  			}
    43  			tcs := []TestCase{
    44  				{
    45  					name: "simple ValidateCreate",
    46  					chaos: KernelChaos{
    47  						ObjectMeta: metav1.ObjectMeta{
    48  							Namespace: metav1.NamespaceDefault,
    49  							Name:      "foo1",
    50  						},
    51  					},
    52  					execute: func(chaos *KernelChaos) error {
    53  						_, err := chaos.ValidateCreate()
    54  						return err
    55  					},
    56  					expect: "",
    57  				},
    58  				{
    59  					name: "simple ValidateUpdate",
    60  					chaos: KernelChaos{
    61  						ObjectMeta: metav1.ObjectMeta{
    62  							Namespace: metav1.NamespaceDefault,
    63  							Name:      "foo2",
    64  						},
    65  					},
    66  					execute: func(chaos *KernelChaos) error {
    67  						_, err := chaos.ValidateUpdate(chaos)
    68  						return err
    69  					},
    70  					expect: "",
    71  				},
    72  				{
    73  					name: "simple ValidateDelete",
    74  					chaos: KernelChaos{
    75  						ObjectMeta: metav1.ObjectMeta{
    76  							Namespace: metav1.NamespaceDefault,
    77  							Name:      "foo3",
    78  						},
    79  					},
    80  					execute: func(chaos *KernelChaos) error {
    81  						_, err := chaos.ValidateDelete()
    82  						return err
    83  					},
    84  					expect: "",
    85  				},
    86  			}
    87  
    88  			for _, tc := range tcs {
    89  				err := tc.execute(&tc.chaos)
    90  				if tc.expect == "error" {
    91  					Expect(err).To(HaveOccurred())
    92  				} else {
    93  					Expect(err).NotTo(HaveOccurred())
    94  				}
    95  			}
    96  		})
    97  	})
    98  })
    99