...
1
2
3
4
5
6
7
8
9
10
11
12
13
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