...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/iochaos_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("iochaos_webhook", func() {
    23  	Context("Defaulter", func() {
    24  		It("set default namespace selector", func() {
    25  			iochaos := &IOChaos{
    26  				ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault},
    27  			}
    28  			iochaos.Default()
    29  			Expect(iochaos.Spec.Selector.Namespaces[0]).To(Equal(metav1.NamespaceDefault))
    30  		})
    31  	})
    32  	Context("webhook.Validator of iochaos", func() {
    33  		It("Validate", func() {
    34  
    35  			type TestCase struct {
    36  				name    string
    37  				chaos   IOChaos
    38  				execute func(chaos *IOChaos) error
    39  				expect  string
    40  			}
    41  			errorDuration := "400S"
    42  
    43  			tcs := []TestCase{
    44  				{
    45  					name: "simple ValidateCreate",
    46  					chaos: IOChaos{
    47  						ObjectMeta: metav1.ObjectMeta{
    48  							Namespace: metav1.NamespaceDefault,
    49  							Name:      "foo1",
    50  						},
    51  					},
    52  					execute: func(chaos *IOChaos) error {
    53  						return chaos.ValidateCreate()
    54  					},
    55  					expect: "",
    56  				},
    57  				{
    58  					name: "simple ValidateUpdate",
    59  					chaos: IOChaos{
    60  						ObjectMeta: metav1.ObjectMeta{
    61  							Namespace: metav1.NamespaceDefault,
    62  							Name:      "foo2",
    63  						},
    64  					},
    65  					execute: func(chaos *IOChaos) error {
    66  						return chaos.ValidateUpdate(chaos)
    67  					},
    68  					expect: "",
    69  				},
    70  				{
    71  					name: "simple ValidateDelete",
    72  					chaos: IOChaos{
    73  						ObjectMeta: metav1.ObjectMeta{
    74  							Namespace: metav1.NamespaceDefault,
    75  							Name:      "foo3",
    76  						},
    77  					},
    78  					execute: func(chaos *IOChaos) error {
    79  						return chaos.ValidateDelete()
    80  					},
    81  					expect: "",
    82  				},
    83  				{
    84  					name: "parse the duration error",
    85  					chaos: IOChaos{
    86  						ObjectMeta: metav1.ObjectMeta{
    87  							Namespace: metav1.NamespaceDefault,
    88  							Name:      "foo6",
    89  						},
    90  						Spec: IOChaosSpec{
    91  							Duration: &errorDuration,
    92  						},
    93  					},
    94  					execute: func(chaos *IOChaos) error {
    95  						return chaos.ValidateCreate()
    96  					},
    97  					expect: "error",
    98  				},
    99  				{
   100  					name: "validate value with FixedPercentPodMode",
   101  					chaos: IOChaos{
   102  						ObjectMeta: metav1.ObjectMeta{
   103  							Namespace: metav1.NamespaceDefault,
   104  							Name:      "foo7",
   105  						},
   106  						Spec: IOChaosSpec{
   107  							ContainerSelector: ContainerSelector{
   108  								PodSelector: PodSelector{
   109  									Value: "0",
   110  									Mode:  FixedPodMode,
   111  								},
   112  							},
   113  						},
   114  					},
   115  					execute: func(chaos *IOChaos) error {
   116  						return chaos.ValidateCreate()
   117  					},
   118  					expect: "error",
   119  				},
   120  				{
   121  					name: "validate value with FixedPercentPodMode, parse value error",
   122  					chaos: IOChaos{
   123  						ObjectMeta: metav1.ObjectMeta{
   124  							Namespace: metav1.NamespaceDefault,
   125  							Name:      "foo8",
   126  						},
   127  						Spec: IOChaosSpec{
   128  							ContainerSelector: ContainerSelector{
   129  								PodSelector: PodSelector{
   130  									Value: "num",
   131  									Mode:  FixedPodMode,
   132  								},
   133  							},
   134  						},
   135  					},
   136  					execute: func(chaos *IOChaos) error {
   137  						return chaos.ValidateCreate()
   138  					},
   139  					expect: "error",
   140  				},
   141  				{
   142  					name: "validate value with RandomMaxPercentPodMode",
   143  					chaos: IOChaos{
   144  						ObjectMeta: metav1.ObjectMeta{
   145  							Namespace: metav1.NamespaceDefault,
   146  							Name:      "foo9",
   147  						},
   148  						Spec: IOChaosSpec{
   149  							ContainerSelector: ContainerSelector{
   150  								PodSelector: PodSelector{
   151  									Value: "0",
   152  									Mode:  RandomMaxPercentPodMode,
   153  								},
   154  							},
   155  						},
   156  					},
   157  					execute: func(chaos *IOChaos) error {
   158  						return chaos.ValidateCreate()
   159  					},
   160  					expect: "error",
   161  				},
   162  				{
   163  					name: "validate value with RandomMaxPercentPodMode ,parse value error",
   164  					chaos: IOChaos{
   165  						ObjectMeta: metav1.ObjectMeta{
   166  							Namespace: metav1.NamespaceDefault,
   167  							Name:      "foo10",
   168  						},
   169  						Spec: IOChaosSpec{
   170  							ContainerSelector: ContainerSelector{
   171  								PodSelector: PodSelector{
   172  									Value: "num",
   173  									Mode:  RandomMaxPercentPodMode,
   174  								},
   175  							},
   176  						},
   177  					},
   178  					execute: func(chaos *IOChaos) error {
   179  						return chaos.ValidateCreate()
   180  					},
   181  					expect: "error",
   182  				},
   183  				{
   184  					name: "validate value with FixedPercentPodMode",
   185  					chaos: IOChaos{
   186  						ObjectMeta: metav1.ObjectMeta{
   187  							Namespace: metav1.NamespaceDefault,
   188  							Name:      "foo11",
   189  						},
   190  						Spec: IOChaosSpec{
   191  							ContainerSelector: ContainerSelector{
   192  								PodSelector: PodSelector{
   193  									Value: "101",
   194  									Mode:  FixedPercentPodMode,
   195  								},
   196  							},
   197  						},
   198  					},
   199  					execute: func(chaos *IOChaos) error {
   200  						return chaos.ValidateCreate()
   201  					},
   202  					expect: "error",
   203  				},
   204  				{
   205  					name: "validate delay",
   206  					chaos: IOChaos{
   207  						ObjectMeta: metav1.ObjectMeta{
   208  							Namespace: metav1.NamespaceDefault,
   209  							Name:      "foo12",
   210  						},
   211  						Spec: IOChaosSpec{
   212  							Delay:  "1S",
   213  							Action: IoLatency,
   214  						},
   215  					},
   216  					execute: func(chaos *IOChaos) error {
   217  						return chaos.ValidateCreate()
   218  					},
   219  					expect: "error",
   220  				},
   221  			}
   222  
   223  			for _, tc := range tcs {
   224  				err := tc.execute(&tc.chaos)
   225  				if tc.expect == "error" {
   226  					Expect(err).To(HaveOccurred())
   227  				} else {
   228  					Expect(err).NotTo(HaveOccurred())
   229  				}
   230  			}
   231  		})
   232  	})
   233  })
   234