...

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 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("stresschaos_webhook", func() {
    25  	Context("Defaulter", func() {
    26  		It("set default namespace selector", func() {
    27  			stresschaos := &StressChaos{
    28  				ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault},
    29  			}
    30  			stresschaos.Default()
    31  			Expect(stresschaos.Spec.Selector.Namespaces[0]).To(Equal(metav1.NamespaceDefault))
    32  		})
    33  	})
    34  	Context("webhook.Validator of stresschaos", func() {
    35  		It("Validate StressChaos", func() {
    36  
    37  			type TestCase struct {
    38  				name    string
    39  				chaos   StressChaos
    40  				execute func(chaos *StressChaos) error
    41  				expect  string
    42  			}
    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: "missing stressors",
    99  					chaos: StressChaos{
   100  						ObjectMeta: metav1.ObjectMeta{
   101  							Namespace: metav1.NamespaceDefault,
   102  							Name:      "foo5",
   103  						},
   104  					},
   105  					execute: func(chaos *StressChaos) error {
   106  						return chaos.ValidateCreate()
   107  					},
   108  					expect: "error",
   109  				},
   110  			}
   111  
   112  			for _, tc := range tcs {
   113  				err := tc.execute(&tc.chaos)
   114  				if tc.expect == "error" {
   115  					Expect(err).To(HaveOccurred())
   116  				} else {
   117  					Expect(err).NotTo(HaveOccurred())
   118  				}
   119  			}
   120  		})
   121  
   122  		//		It("Validate Stressors", func() {
   123  		//type TestCase struct {
   124  		//name     string
   125  		//stressor Validateable
   126  		//errs     int
   127  		//}
   128  		//tcs := []TestCase{
   129  		//{
   130  		//name:     "missing workers",
   131  		//stressor: &Stressor{},
   132  		//errs:     1,
   133  		//},
   134  		//{
   135  		//name: "default MemoryStressor",
   136  		//stressor: &MemoryStressor{
   137  		//Stressor: Stressor{Workers: 1},
   138  		//},
   139  		//errs: 0,
   140  		//},
   141  		//{
   142  		//name: "default CPUStressor",
   143  		//stressor: &CPUStressor{
   144  		//Stressor: Stressor{Workers: 1},
   145  		//},
   146  		//errs: 0,
   147  		//},
   148  		//}
   149  		//parent := field.NewPath("parent")
   150  		//for _, tc := range tcs {
   151  		//Expect(tc.stressor.Validate(parent)).To(HaveLen(tc.errs))
   152  		//}
   153  		//})
   154  
   155  		//It("Parse MemoryStressor fields", func() {
   156  		//vm := MemoryStressor{}
   157  		//incorrectBytes := []string{"-1", "-1%", "101%", "x%", "-1Kb"}
   158  		//for _, b := range incorrectBytes {
   159  		//vm.Size = b
   160  		//Expect(vm.tryParseBytes()).Should(HaveOccurred())
   161  		//}
   162  		//correctBytes := []string{"", "1%", "100KB", "100B"}
   163  		//for _, b := range correctBytes {
   164  		//vm.Size = b
   165  		//Expect(vm.tryParseBytes()).ShouldNot(HaveOccurred())
   166  		//}
   167  		//})
   168  
   169  	})
   170  
   171  })
   172