...

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/v2"
    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  						_, err := chaos.ValidateCreate()
    62  						return err
    63  					},
    64  					expect: "",
    65  				},
    66  				{
    67  					name: "simple ValidateUpdate",
    68  					chaos: StressChaos{
    69  						ObjectMeta: metav1.ObjectMeta{
    70  							Namespace: metav1.NamespaceDefault,
    71  							Name:      "foo2",
    72  						},
    73  						Spec: StressChaosSpec{
    74  							Stressors: stressors,
    75  						},
    76  					},
    77  					execute: func(chaos *StressChaos) error {
    78  						_, err := chaos.ValidateUpdate(chaos)
    79  						return err
    80  					},
    81  					expect: "",
    82  				},
    83  				{
    84  					name: "simple ValidateDelete",
    85  					chaos: StressChaos{
    86  						ObjectMeta: metav1.ObjectMeta{
    87  							Namespace: metav1.NamespaceDefault,
    88  							Name:      "foo3",
    89  						},
    90  						Spec: StressChaosSpec{
    91  							Stressors: stressors,
    92  						},
    93  					},
    94  					execute: func(chaos *StressChaos) error {
    95  						_, err := chaos.ValidateDelete()
    96  						return err
    97  					},
    98  					expect: "",
    99  				},
   100  				{
   101  					name: "missing stressors",
   102  					chaos: StressChaos{
   103  						ObjectMeta: metav1.ObjectMeta{
   104  							Namespace: metav1.NamespaceDefault,
   105  							Name:      "foo5",
   106  						},
   107  					},
   108  					execute: func(chaos *StressChaos) error {
   109  						_, err := chaos.ValidateCreate()
   110  						return err
   111  					},
   112  					expect: "error",
   113  				},
   114  			}
   115  
   116  			for _, tc := range tcs {
   117  				err := tc.execute(&tc.chaos)
   118  				if tc.expect == "error" {
   119  					Expect(err).To(HaveOccurred())
   120  				} else {
   121  					Expect(err).NotTo(HaveOccurred())
   122  				}
   123  			}
   124  		})
   125  
   126  		//		It("Validate Stressors", func() {
   127  		//type TestCase struct {
   128  		//name     string
   129  		//stressor Validateable
   130  		//errs     int
   131  		//}
   132  		//tcs := []TestCase{
   133  		//{
   134  		//name:     "missing workers",
   135  		//stressor: &Stressor{},
   136  		//errs:     1,
   137  		//},
   138  		//{
   139  		//name: "default MemoryStressor",
   140  		//stressor: &MemoryStressor{
   141  		//Stressor: Stressor{Workers: 1},
   142  		//},
   143  		//errs: 0,
   144  		//},
   145  		//{
   146  		//name: "default CPUStressor",
   147  		//stressor: &CPUStressor{
   148  		//Stressor: Stressor{Workers: 1},
   149  		//},
   150  		//errs: 0,
   151  		//},
   152  		//}
   153  		//parent := field.NewPath("parent")
   154  		//for _, tc := range tcs {
   155  		//Expect(tc.stressor.Validate(parent)).To(HaveLen(tc.errs))
   156  		//}
   157  		//})
   158  
   159  		//It("Parse MemoryStressor fields", func() {
   160  		//vm := MemoryStressor{}
   161  		//incorrectBytes := []string{"-1", "-1%", "101%", "x%", "-1Kb"}
   162  		//for _, b := range incorrectBytes {
   163  		//vm.Size = b
   164  		//Expect(vm.tryParseBytes()).Should(HaveOccurred())
   165  		//}
   166  		//correctBytes := []string{"", "1%", "100KB", "100B"}
   167  		//for _, b := range correctBytes {
   168  		//vm.Size = b
   169  		//Expect(vm.tryParseBytes()).ShouldNot(HaveOccurred())
   170  		//}
   171  		//})
   172  
   173  	})
   174  
   175  })
   176