...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/stresschaos_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  	"k8s.io/apimachinery/pkg/util/validation/field"
    21  )
    22  
    23  var _ = Describe("stresschaos_webhook", func() {
    24  	Context("Defaulter", func() {
    25  		It("set default namespace selector", func() {
    26  			stresschaos := &StressChaos{
    27  				ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault},
    28  			}
    29  			stresschaos.Default()
    30  			Expect(stresschaos.Spec.Selector.Namespaces[0]).To(Equal(metav1.NamespaceDefault))
    31  		})
    32  	})
    33  	Context("ChaosValidator of stresschaos", func() {
    34  		It("Validate StressChaos", func() {
    35  
    36  			type TestCase struct {
    37  				name    string
    38  				chaos   StressChaos
    39  				execute func(chaos *StressChaos) error
    40  				expect  string
    41  			}
    42  			duration := "400s"
    43  			stressors := &Stressors{
    44  				MemoryStressor: &MemoryStressor{
    45  					Stressor: Stressor{Workers: 1},
    46  				},
    47  			}
    48  			tcs := []TestCase{
    49  				{
    50  					name: "simple ValidateCreate",
    51  					chaos: StressChaos{
    52  						ObjectMeta: metav1.ObjectMeta{
    53  							Namespace: metav1.NamespaceDefault,
    54  							Name:      "foo1",
    55  						},
    56  						Spec: StressChaosSpec{
    57  							Stressors: stressors,
    58  						},
    59  					},
    60  					execute: func(chaos *StressChaos) error {
    61  						return chaos.ValidateCreate()
    62  					},
    63  					expect: "",
    64  				},
    65  				{
    66  					name: "simple ValidateUpdate",
    67  					chaos: StressChaos{
    68  						ObjectMeta: metav1.ObjectMeta{
    69  							Namespace: metav1.NamespaceDefault,
    70  							Name:      "foo2",
    71  						},
    72  						Spec: StressChaosSpec{
    73  							Stressors: stressors,
    74  						},
    75  					},
    76  					execute: func(chaos *StressChaos) error {
    77  						return chaos.ValidateUpdate(chaos)
    78  					},
    79  					expect: "",
    80  				},
    81  				{
    82  					name: "simple ValidateDelete",
    83  					chaos: StressChaos{
    84  						ObjectMeta: metav1.ObjectMeta{
    85  							Namespace: metav1.NamespaceDefault,
    86  							Name:      "foo3",
    87  						},
    88  						Spec: StressChaosSpec{
    89  							Stressors: stressors,
    90  						},
    91  					},
    92  					execute: func(chaos *StressChaos) error {
    93  						return chaos.ValidateDelete()
    94  					},
    95  					expect: "",
    96  				},
    97  				{
    98  					name: "only define the Scheduler",
    99  					chaos: StressChaos{
   100  						ObjectMeta: metav1.ObjectMeta{
   101  							Namespace: metav1.NamespaceDefault,
   102  							Name:      "foo4",
   103  						},
   104  						Spec: StressChaosSpec{
   105  							Stressors: stressors,
   106  							Scheduler: &SchedulerSpec{
   107  								Cron: "@every 10m",
   108  							},
   109  						},
   110  					},
   111  					execute: func(chaos *StressChaos) error {
   112  						return chaos.ValidateCreate()
   113  					},
   114  					expect: "error",
   115  				},
   116  				{
   117  					name: "only define the Duration",
   118  					chaos: StressChaos{
   119  						ObjectMeta: metav1.ObjectMeta{
   120  							Namespace: metav1.NamespaceDefault,
   121  							Name:      "foo5",
   122  						},
   123  						Spec: StressChaosSpec{
   124  							Stressors: stressors,
   125  							Duration:  &duration,
   126  						},
   127  					},
   128  					execute: func(chaos *StressChaos) error {
   129  						return chaos.ValidateCreate()
   130  					},
   131  					expect: "error",
   132  				},
   133  				{
   134  					name: "missing stressors",
   135  					chaos: StressChaos{
   136  						ObjectMeta: metav1.ObjectMeta{
   137  							Namespace: metav1.NamespaceDefault,
   138  							Name:      "foo5",
   139  						},
   140  					},
   141  					execute: func(chaos *StressChaos) error {
   142  						return chaos.ValidateCreate()
   143  					},
   144  					expect: "error",
   145  				},
   146  			}
   147  
   148  			for _, tc := range tcs {
   149  				err := tc.execute(&tc.chaos)
   150  				if tc.expect == "error" {
   151  					Expect(err).To(HaveOccurred())
   152  				} else {
   153  					Expect(err).NotTo(HaveOccurred())
   154  				}
   155  			}
   156  		})
   157  
   158  		It("Validate Stressors", func() {
   159  			type TestCase struct {
   160  				name     string
   161  				stressor Validateable
   162  				errs     int
   163  			}
   164  			tcs := []TestCase{
   165  				{
   166  					name:     "missing workers",
   167  					stressor: &Stressor{},
   168  					errs:     1,
   169  				},
   170  				{
   171  					name: "default MemoryStressor",
   172  					stressor: &MemoryStressor{
   173  						Stressor: Stressor{Workers: 1},
   174  					},
   175  					errs: 0,
   176  				},
   177  				{
   178  					name: "default CPUStressor",
   179  					stressor: &CPUStressor{
   180  						Stressor: Stressor{Workers: 1},
   181  					},
   182  					errs: 0,
   183  				},
   184  			}
   185  			parent := field.NewPath("parent")
   186  			for _, tc := range tcs {
   187  				Expect(tc.stressor.Validate(parent)).To(HaveLen(tc.errs))
   188  			}
   189  		})
   190  
   191  		It("Parse MemoryStressor fields", func() {
   192  			vm := MemoryStressor{}
   193  			incorrectBytes := []string{"-1", "-1%", "101%", "x%", "-1Kb"}
   194  			for _, b := range incorrectBytes {
   195  				vm.Size = b
   196  				Expect(vm.tryParseBytes()).Should(HaveOccurred())
   197  			}
   198  			correctBytes := []string{"", "1%", "100KB", "100B"}
   199  			for _, b := range correctBytes {
   200  				vm.Size = b
   201  				Expect(vm.tryParseBytes()).ShouldNot(HaveOccurred())
   202  			}
   203  		})
   204  
   205  	})
   206  
   207  })
   208