1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package v1alpha1
17
18 import (
19 . "github.com/onsi/ginkgo"
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 return chaos.ValidateCreate()
50 },
51 expect: "error",
52 },
53 {
54 name: "unknow action",
55 chaos: AWSChaos{
56 ObjectMeta: metav1.ObjectMeta{
57 Namespace: metav1.NamespaceDefault,
58 Name: "foo6",
59 },
60 },
61 execute: func(chaos *AWSChaos) error {
62 return chaos.ValidateCreate()
63 },
64 expect: "error",
65 },
66 {
67 name: "validate the DetachVolume without EbsVolume",
68 chaos: AWSChaos{
69 ObjectMeta: metav1.ObjectMeta{
70 Namespace: metav1.NamespaceDefault,
71 Name: "foo7",
72 },
73 Spec: AWSChaosSpec{
74 Action: DetachVolume,
75 AWSSelector: AWSSelector{
76 DeviceName: &testDeviceName,
77 },
78 },
79 },
80 execute: func(chaos *AWSChaos) error {
81 return chaos.ValidateCreate()
82 },
83 expect: "error",
84 },
85 {
86 name: "validate the DetachVolume without DeviceName",
87 chaos: AWSChaos{
88 ObjectMeta: metav1.ObjectMeta{
89 Namespace: metav1.NamespaceDefault,
90 Name: "foo7",
91 },
92 Spec: AWSChaosSpec{
93 Action: DetachVolume,
94 AWSSelector: AWSSelector{
95 EbsVolume: &testEbsVolume,
96 },
97 },
98 },
99 execute: func(chaos *AWSChaos) error {
100 return chaos.ValidateCreate()
101 },
102 expect: "error",
103 },
104 }
105
106 for _, tc := range tcs {
107 err := tc.execute(&tc.chaos)
108 if tc.expect == "error" {
109 Expect(err).To(HaveOccurred())
110 } else {
111 Expect(err).NotTo(HaveOccurred())
112 }
113 }
114 })
115 })
116 })
117