...

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