...

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"
    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  						return chaos.ValidateCreate()
    57  					},
    58  					expect: "error",
    59  				},
    60  				{
    61  					name: "simple ValidateDelete",
    62  					chaos: PodChaos{
    63  						ObjectMeta: metav1.ObjectMeta{
    64  							Namespace: metav1.NamespaceDefault,
    65  							Name:      "foo3",
    66  						},
    67  					},
    68  					execute: func(chaos *PodChaos) error {
    69  						return chaos.ValidateDelete()
    70  					},
    71  					expect: "",
    72  				},
    73  				{
    74  					name: "validate the ContainerNames",
    75  					chaos: PodChaos{
    76  						ObjectMeta: metav1.ObjectMeta{
    77  							Namespace: metav1.NamespaceDefault,
    78  							Name:      "foo7",
    79  						},
    80  						Spec: PodChaosSpec{
    81  							Action: ContainerKillAction,
    82  						},
    83  					},
    84  					execute: func(chaos *PodChaos) error {
    85  						return chaos.ValidateCreate()
    86  					},
    87  					expect: "error",
    88  				},
    89  			}
    90  
    91  			for _, tc := range tcs {
    92  				err := tc.execute(&tc.chaos)
    93  				if tc.expect == "error" {
    94  					Expect(err).To(HaveOccurred())
    95  				} else {
    96  					Expect(err).NotTo(HaveOccurred())
    97  				}
    98  			}
    99  		})
   100  	})
   101  })
   102