...

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 2020 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package v1alpha1
    15  
    16  import (
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    20  )
    21  
    22  var _ = Describe("timechaos_webhook", func() {
    23  	Context("Defaulter", func() {
    24  		It("set default namespace selector", func() {
    25  			timechaos := &TimeChaos{
    26  				ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault},
    27  			}
    28  			timechaos.Default()
    29  			Expect(timechaos.Spec.Selector.Namespaces[0]).To(Equal(metav1.NamespaceDefault))
    30  			Expect(timechaos.Spec.ClockIds[0]).To(Equal("CLOCK_REALTIME"))
    31  		})
    32  	})
    33  	Context("webhook.Validator of timechaos", func() {
    34  		It("Validate", func() {
    35  
    36  			type TestCase struct {
    37  				name    string
    38  				chaos   TimeChaos
    39  				execute func(chaos *TimeChaos) error
    40  				expect  string
    41  			}
    42  			tcs := []TestCase{
    43  				{
    44  					name: "simple ValidateCreate",
    45  					chaos: TimeChaos{
    46  						ObjectMeta: metav1.ObjectMeta{
    47  							Namespace: metav1.NamespaceDefault,
    48  							Name:      "foo1",
    49  						},
    50  						Spec: TimeChaosSpec{TimeOffset: "1s"},
    51  					},
    52  					execute: func(chaos *TimeChaos) error {
    53  						return chaos.ValidateCreate()
    54  					},
    55  					expect: "",
    56  				},
    57  				{
    58  					name: "simple ValidateUpdate",
    59  					chaos: TimeChaos{
    60  						ObjectMeta: metav1.ObjectMeta{
    61  							Namespace: metav1.NamespaceDefault,
    62  							Name:      "foo2",
    63  						},
    64  						Spec: TimeChaosSpec{TimeOffset: "1s"},
    65  					},
    66  					execute: func(chaos *TimeChaos) error {
    67  						return chaos.ValidateUpdate(chaos)
    68  					},
    69  					expect: "",
    70  				},
    71  				{
    72  					name: "simple ValidateDelete",
    73  					chaos: TimeChaos{
    74  						ObjectMeta: metav1.ObjectMeta{
    75  							Namespace: metav1.NamespaceDefault,
    76  							Name:      "foo3",
    77  						},
    78  						Spec: TimeChaosSpec{TimeOffset: "1s"},
    79  					},
    80  					execute: func(chaos *TimeChaos) error {
    81  						return chaos.ValidateDelete()
    82  					},
    83  					expect: "",
    84  				},
    85  				{
    86  					name: "validate the timeOffset",
    87  					chaos: TimeChaos{
    88  						ObjectMeta: metav1.ObjectMeta{
    89  							Namespace: metav1.NamespaceDefault,
    90  							Name:      "foo6",
    91  						},
    92  						Spec: TimeChaosSpec{
    93  							TimeOffset: "1S",
    94  						},
    95  					},
    96  					execute: func(chaos *TimeChaos) error {
    97  						return chaos.ValidateCreate()
    98  					},
    99  					expect: "error",
   100  				},
   101  			}
   102  
   103  			for _, tc := range tcs {
   104  				err := tc.execute(&tc.chaos)
   105  				if tc.expect == "error" {
   106  					Expect(err).To(HaveOccurred())
   107  				} else {
   108  					Expect(err).NotTo(HaveOccurred())
   109  				}
   110  			}
   111  		})
   112  	})
   113  })
   114