...

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