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/v2"
20 . "github.com/onsi/gomega"
21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22 )
23
24 var _ = Describe("iochaos_webhook", func() {
25 Context("Defaulter", func() {
26 It("set default namespace selector", func() {
27 iochaos := &IOChaos{
28 ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault},
29 }
30 iochaos.Default()
31 Expect(iochaos.Spec.Selector.Namespaces[0]).To(Equal(metav1.NamespaceDefault))
32 })
33 })
34 Context("webhook.Validator of iochaos", func() {
35 It("Validate", func() {
36
37 type TestCase struct {
38 name string
39 chaos IOChaos
40 execute func(chaos *IOChaos) error
41 expect string
42 }
43 errorDuration := "400S"
44
45 tcs := []TestCase{
46 {
47 name: "simple ValidateCreate",
48 chaos: IOChaos{
49 ObjectMeta: metav1.ObjectMeta{
50 Namespace: metav1.NamespaceDefault,
51 Name: "foo1",
52 },
53 },
54 execute: func(chaos *IOChaos) error {
55 _, err := chaos.ValidateCreate()
56 return err
57 },
58 expect: "",
59 },
60 {
61 name: "simple ValidateUpdate",
62 chaos: IOChaos{
63 ObjectMeta: metav1.ObjectMeta{
64 Namespace: metav1.NamespaceDefault,
65 Name: "foo2",
66 },
67 },
68 execute: func(chaos *IOChaos) error {
69 _, err := chaos.ValidateUpdate(chaos)
70 return err
71 },
72 expect: "",
73 },
74 {
75 name: "simple ValidateDelete",
76 chaos: IOChaos{
77 ObjectMeta: metav1.ObjectMeta{
78 Namespace: metav1.NamespaceDefault,
79 Name: "foo3",
80 },
81 },
82 execute: func(chaos *IOChaos) error {
83 _, err := chaos.ValidateDelete()
84 return err
85 },
86 expect: "",
87 },
88 {
89 name: "parse the duration error",
90 chaos: IOChaos{
91 ObjectMeta: metav1.ObjectMeta{
92 Namespace: metav1.NamespaceDefault,
93 Name: "foo6",
94 },
95 Spec: IOChaosSpec{
96 Duration: &errorDuration,
97 },
98 },
99 execute: func(chaos *IOChaos) error {
100 _, err := chaos.ValidateCreate()
101 return err
102 },
103 expect: "error",
104 },
105 {
106 name: "validate value with FixedPercentMode",
107 chaos: IOChaos{
108 ObjectMeta: metav1.ObjectMeta{
109 Namespace: metav1.NamespaceDefault,
110 Name: "foo7",
111 },
112 Spec: IOChaosSpec{
113 ContainerSelector: ContainerSelector{
114 PodSelector: PodSelector{
115 Value: "0",
116 Mode: FixedMode,
117 },
118 },
119 },
120 },
121 execute: func(chaos *IOChaos) error {
122 _, err := chaos.ValidateCreate()
123 return err
124 },
125 expect: "error",
126 },
127 {
128 name: "validate value with FixedPercentMode, parse value error",
129 chaos: IOChaos{
130 ObjectMeta: metav1.ObjectMeta{
131 Namespace: metav1.NamespaceDefault,
132 Name: "foo8",
133 },
134 Spec: IOChaosSpec{
135 ContainerSelector: ContainerSelector{
136 PodSelector: PodSelector{
137 Value: "num",
138 Mode: FixedMode,
139 },
140 },
141 },
142 },
143 execute: func(chaos *IOChaos) error {
144 _, err := chaos.ValidateCreate()
145 return err
146 },
147 expect: "error",
148 },
149 {
150 name: "validate value with RandomMaxPercentMode",
151 chaos: IOChaos{
152 ObjectMeta: metav1.ObjectMeta{
153 Namespace: metav1.NamespaceDefault,
154 Name: "foo9",
155 },
156 Spec: IOChaosSpec{
157 ContainerSelector: ContainerSelector{
158 PodSelector: PodSelector{
159 Value: "0",
160 Mode: RandomMaxPercentMode,
161 },
162 },
163 },
164 },
165 execute: func(chaos *IOChaos) error {
166 _, err := chaos.ValidateCreate()
167 return err
168 },
169 expect: "error",
170 },
171 {
172 name: "validate value with RandomMaxPercentMode ,parse value error",
173 chaos: IOChaos{
174 ObjectMeta: metav1.ObjectMeta{
175 Namespace: metav1.NamespaceDefault,
176 Name: "foo10",
177 },
178 Spec: IOChaosSpec{
179 ContainerSelector: ContainerSelector{
180 PodSelector: PodSelector{
181 Value: "num",
182 Mode: RandomMaxPercentMode,
183 },
184 },
185 },
186 },
187 execute: func(chaos *IOChaos) error {
188 _, err := chaos.ValidateCreate()
189 return err
190 },
191 expect: "error",
192 },
193 {
194 name: "validate value with FixedPercentMode",
195 chaos: IOChaos{
196 ObjectMeta: metav1.ObjectMeta{
197 Namespace: metav1.NamespaceDefault,
198 Name: "foo11",
199 },
200 Spec: IOChaosSpec{
201 ContainerSelector: ContainerSelector{
202 PodSelector: PodSelector{
203 Value: "101",
204 Mode: FixedPercentMode,
205 },
206 },
207 },
208 },
209 execute: func(chaos *IOChaos) error {
210 _, err := chaos.ValidateCreate()
211 return err
212 },
213 expect: "error",
214 },
215 {
216 name: "validate delay",
217 chaos: IOChaos{
218 ObjectMeta: metav1.ObjectMeta{
219 Namespace: metav1.NamespaceDefault,
220 Name: "foo12",
221 },
222 Spec: IOChaosSpec{
223 Delay: "1S",
224 Action: IoLatency,
225 },
226 },
227 execute: func(chaos *IOChaos) error {
228 _, err := chaos.ValidateCreate()
229 return err
230 },
231 expect: "error",
232 },
233 }
234
235 for _, tc := range tcs {
236 err := tc.execute(&tc.chaos)
237 if tc.expect == "error" {
238 Expect(err).To(HaveOccurred())
239 } else {
240 Expect(err).NotTo(HaveOccurred())
241 }
242 }
243 })
244 })
245 })
246