...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/gcpchaos_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  	"fmt"
    20  
    21  	. "github.com/onsi/ginkgo/v2"
    22  	. "github.com/onsi/gomega"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  )
    25  
    26  var _ = Describe("gcpchaos_webhook", func() {
    27  	Context("ChaosValidator of gcpchaos", func() {
    28  		It("Validate", func() {
    29  
    30  			type TestCase struct {
    31  				name    string
    32  				chaos   GCPChaos
    33  				execute func(chaos *GCPChaos) error
    34  				expect  string
    35  			}
    36  			tcs := []TestCase{
    37  				{
    38  					name: "simple ValidateCreate for DiskLoss",
    39  					chaos: GCPChaos{
    40  						ObjectMeta: metav1.ObjectMeta{
    41  							Namespace: metav1.NamespaceDefault,
    42  							Name:      "foo1",
    43  						},
    44  						Spec: GCPChaosSpec{
    45  							Action: DiskLoss,
    46  						},
    47  					},
    48  					execute: func(chaos *GCPChaos) error {
    49  						_, err := chaos.ValidateCreate()
    50  						return err
    51  					},
    52  					expect: "error",
    53  				},
    54  				{
    55  					name: "unknow action",
    56  					chaos: GCPChaos{
    57  						ObjectMeta: metav1.ObjectMeta{
    58  							Namespace: metav1.NamespaceDefault,
    59  							Name:      "foo6",
    60  						},
    61  					},
    62  					execute: func(chaos *GCPChaos) error {
    63  						_, err := chaos.ValidateCreate()
    64  						return err
    65  					},
    66  					expect: "error",
    67  				},
    68  			}
    69  
    70  			for _, tc := range tcs {
    71  				err := tc.execute(&tc.chaos)
    72  				if tc.expect == "error" {
    73  					if err == nil {
    74  						fmt.Println("Aha")
    75  					}
    76  					Expect(err).To(HaveOccurred())
    77  				} else {
    78  					Expect(err).NotTo(HaveOccurred())
    79  				}
    80  			}
    81  		})
    82  	})
    83  })
    84