...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/awschaos_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("awschaos_webhook", func() {
    25  	Context("webhook.Validator of awschaos", func() {
    26  		It("Validate", func() {
    27  
    28  			type TestCase struct {
    29  				name    string
    30  				chaos   AWSChaos
    31  				execute func(chaos *AWSChaos) error
    32  				expect  string
    33  			}
    34  			testDeviceName := "testDeviceName"
    35  			testEbsVolume := "testEbsVolume"
    36  			tcs := []TestCase{
    37  				{
    38  					name: "simple ValidateCreate for DetachVolume",
    39  					chaos: AWSChaos{
    40  						ObjectMeta: metav1.ObjectMeta{
    41  							Namespace: metav1.NamespaceDefault,
    42  							Name:      "foo1",
    43  						},
    44  						Spec: AWSChaosSpec{
    45  							Action: DetachVolume,
    46  						},
    47  					},
    48  					execute: func(chaos *AWSChaos) error {
    49  						_, err := chaos.ValidateCreate()
    50  						return err
    51  					},
    52  					expect: "error",
    53  				},
    54  				{
    55  					name: "unknow action",
    56  					chaos: AWSChaos{
    57  						ObjectMeta: metav1.ObjectMeta{
    58  							Namespace: metav1.NamespaceDefault,
    59  							Name:      "foo6",
    60  						},
    61  					},
    62  					execute: func(chaos *AWSChaos) error {
    63  						_, err := chaos.ValidateCreate()
    64  						return err
    65  					},
    66  					expect: "error",
    67  				},
    68  				{
    69  					name: "validate the DetachVolume without EbsVolume",
    70  					chaos: AWSChaos{
    71  						ObjectMeta: metav1.ObjectMeta{
    72  							Namespace: metav1.NamespaceDefault,
    73  							Name:      "foo7",
    74  						},
    75  						Spec: AWSChaosSpec{
    76  							Action: DetachVolume,
    77  							AWSSelector: AWSSelector{
    78  								DeviceName: &testDeviceName,
    79  							},
    80  						},
    81  					},
    82  					execute: func(chaos *AWSChaos) error {
    83  						_, err := chaos.ValidateCreate()
    84  						return err
    85  					},
    86  					expect: "error",
    87  				},
    88  				{
    89  					name: "validate the DetachVolume without DeviceName",
    90  					chaos: AWSChaos{
    91  						ObjectMeta: metav1.ObjectMeta{
    92  							Namespace: metav1.NamespaceDefault,
    93  							Name:      "foo7",
    94  						},
    95  						Spec: AWSChaosSpec{
    96  							Action: DetachVolume,
    97  							AWSSelector: AWSSelector{
    98  								EbsVolume: &testEbsVolume,
    99  							},
   100  						},
   101  					},
   102  					execute: func(chaos *AWSChaos) error {
   103  						_, err := chaos.ValidateCreate()
   104  						return err
   105  					},
   106  					expect: "error",
   107  				},
   108  			}
   109  
   110  			for _, tc := range tcs {
   111  				err := tc.execute(&tc.chaos)
   112  				if tc.expect == "error" {
   113  					Expect(err).To(HaveOccurred())
   114  				} else {
   115  					Expect(err).NotTo(HaveOccurred())
   116  				}
   117  			}
   118  		})
   119  	})
   120  })
   121