...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/podchaos_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("podchaos_webhook", func() {
    25  	Context("Defaulter", func() {
    26  		It("set default namespace selector", func() {
    27  			podchaos := &PodChaos{
    28  				ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault},
    29  			}
    30  			podchaos.Default()
    31  			Expect(podchaos.Spec.Selector.Namespaces[0]).To(Equal(metav1.NamespaceDefault))
    32  		})
    33  	})
    34  	Context("webhook.Validator of podchaos", func() {
    35  		It("Validate", func() {
    36  
    37  			type TestCase struct {
    38  				name    string
    39  				chaos   PodChaos
    40  				execute func(chaos *PodChaos) error
    41  				expect  string
    42  			}
    43  			tcs := []TestCase{
    44  				{
    45  					name: "simple ValidateCreate for ContainerKillAction",
    46  					chaos: PodChaos{
    47  						ObjectMeta: metav1.ObjectMeta{
    48  							Namespace: metav1.NamespaceDefault,
    49  							Name:      "foo1",
    50  						},
    51  						Spec: PodChaosSpec{
    52  							Action: ContainerKillAction,
    53  						},
    54  					},
    55  					execute: func(chaos *PodChaos) error {
    56  						_, err := chaos.ValidateCreate()
    57  						return err
    58  					},
    59  					expect: "error",
    60  				},
    61  				{
    62  					name: "simple ValidateDelete",
    63  					chaos: PodChaos{
    64  						ObjectMeta: metav1.ObjectMeta{
    65  							Namespace: metav1.NamespaceDefault,
    66  							Name:      "foo3",
    67  						},
    68  					},
    69  					execute: func(chaos *PodChaos) error {
    70  						_, err := chaos.ValidateDelete()
    71  						return err
    72  					},
    73  					expect: "",
    74  				},
    75  				{
    76  					name: "validate the ContainerNames",
    77  					chaos: PodChaos{
    78  						ObjectMeta: metav1.ObjectMeta{
    79  							Namespace: metav1.NamespaceDefault,
    80  							Name:      "foo7",
    81  						},
    82  						Spec: PodChaosSpec{
    83  							Action: ContainerKillAction,
    84  						},
    85  					},
    86  					execute: func(chaos *PodChaos) error {
    87  						_, err := chaos.ValidateCreate()
    88  						return err
    89  					},
    90  					expect: "error",
    91  				},
    92  			}
    93  
    94  			for _, tc := range tcs {
    95  				err := tc.execute(&tc.chaos)
    96  				if tc.expect == "error" {
    97  					Expect(err).To(HaveOccurred())
    98  				} else {
    99  					Expect(err).NotTo(HaveOccurred())
   100  				}
   101  			}
   102  		})
   103  	})
   104  })
   105