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("blockchaos_webhook", func() {
25 Context("webhook.Validator of blockchaos", func() {
26 It("Validate", func() {
27
28 type TestCase struct {
29 name string
30 chaos BlockChaos
31 execute func(chaos *BlockChaos) error
32 expect string
33 }
34 errorDuration := "400S"
35
36 tcs := []TestCase{
37 {
38 name: "simple ValidateCreate",
39 chaos: BlockChaos{
40 ObjectMeta: metav1.ObjectMeta{
41 Namespace: metav1.NamespaceDefault,
42 Name: "foo1",
43 },
44 },
45 execute: func(chaos *BlockChaos) error {
46 _, err := chaos.ValidateCreate()
47 return err
48 },
49 expect: "",
50 },
51 {
52 name: "simple ValidateUpdate",
53 chaos: BlockChaos{
54 ObjectMeta: metav1.ObjectMeta{
55 Namespace: metav1.NamespaceDefault,
56 Name: "foo2",
57 },
58 },
59 execute: func(chaos *BlockChaos) error {
60 _, err := chaos.ValidateUpdate(chaos)
61 return err
62 },
63 expect: "",
64 },
65 {
66 name: "simple ValidateDelete",
67 chaos: BlockChaos{
68 ObjectMeta: metav1.ObjectMeta{
69 Namespace: metav1.NamespaceDefault,
70 Name: "foo3",
71 },
72 },
73 execute: func(chaos *BlockChaos) error {
74 _, err := chaos.ValidateDelete()
75 return err
76 },
77 expect: "",
78 },
79 {
80 name: "parse the duration error",
81 chaos: BlockChaos{
82 ObjectMeta: metav1.ObjectMeta{
83 Namespace: metav1.NamespaceDefault,
84 Name: "foo6",
85 },
86 Spec: BlockChaosSpec{
87 Duration: &errorDuration,
88 },
89 },
90 execute: func(chaos *BlockChaos) error {
91 _, err := chaos.ValidateCreate()
92 return err
93 },
94 expect: "error",
95 },
96 {
97 name: "validate value with FixedPercentMode",
98 chaos: BlockChaos{
99 ObjectMeta: metav1.ObjectMeta{
100 Namespace: metav1.NamespaceDefault,
101 Name: "foo7",
102 },
103 Spec: BlockChaosSpec{
104 ContainerNodeVolumePathSelector: ContainerNodeVolumePathSelector{
105 ContainerSelector: ContainerSelector{
106 PodSelector: PodSelector{
107 Value: "0",
108 Mode: FixedMode,
109 },
110 },
111 VolumeName: "",
112 },
113 },
114 },
115 execute: func(chaos *BlockChaos) error {
116 _, err := chaos.ValidateCreate()
117 return err
118 },
119 expect: "error",
120 },
121 {
122 name: "validate value with FixedPercentMode, parse value error",
123 chaos: BlockChaos{
124 ObjectMeta: metav1.ObjectMeta{
125 Namespace: metav1.NamespaceDefault,
126 Name: "foo8",
127 },
128 Spec: BlockChaosSpec{
129 ContainerNodeVolumePathSelector: ContainerNodeVolumePathSelector{
130 ContainerSelector: ContainerSelector{
131 PodSelector: PodSelector{
132 Value: "num",
133 Mode: FixedMode,
134 },
135 },
136 VolumeName: "",
137 },
138 },
139 },
140 execute: func(chaos *BlockChaos) error {
141 _, err := chaos.ValidateCreate()
142 return err
143 },
144 expect: "error",
145 },
146 {
147 name: "validate value with RandomMaxPercentMode",
148 chaos: BlockChaos{
149 ObjectMeta: metav1.ObjectMeta{
150 Namespace: metav1.NamespaceDefault,
151 Name: "foo9",
152 },
153 Spec: BlockChaosSpec{
154 ContainerNodeVolumePathSelector: ContainerNodeVolumePathSelector{
155 ContainerSelector: ContainerSelector{
156 PodSelector: PodSelector{
157 Value: "0",
158 Mode: RandomMaxPercentMode,
159 },
160 },
161 VolumeName: "",
162 },
163 },
164 },
165 execute: func(chaos *BlockChaos) 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: BlockChaos{
174 ObjectMeta: metav1.ObjectMeta{
175 Namespace: metav1.NamespaceDefault,
176 Name: "foo10",
177 },
178 Spec: BlockChaosSpec{
179 ContainerNodeVolumePathSelector: ContainerNodeVolumePathSelector{
180 ContainerSelector: ContainerSelector{
181 PodSelector: PodSelector{
182 Value: "num",
183 Mode: RandomMaxPercentMode,
184 },
185 },
186 },
187 },
188 },
189 execute: func(chaos *BlockChaos) error {
190 _, err := chaos.ValidateCreate()
191 return err
192 },
193 expect: "error",
194 },
195 {
196 name: "validate value with FixedPercentMode",
197 chaos: BlockChaos{
198 ObjectMeta: metav1.ObjectMeta{
199 Namespace: metav1.NamespaceDefault,
200 Name: "foo11",
201 },
202 Spec: BlockChaosSpec{
203 ContainerNodeVolumePathSelector: ContainerNodeVolumePathSelector{
204 ContainerSelector: ContainerSelector{
205 PodSelector: PodSelector{
206 Value: "101",
207 Mode: FixedPercentMode,
208 },
209 },
210 },
211 },
212 },
213 execute: func(chaos *BlockChaos) error {
214 _, err := chaos.ValidateCreate()
215 return err
216 },
217 expect: "error",
218 },
219 {
220 name: "validate delay",
221 chaos: BlockChaos{
222 ObjectMeta: metav1.ObjectMeta{
223 Namespace: metav1.NamespaceDefault,
224 Name: "foo12",
225 },
226 Spec: BlockChaosSpec{
227 Action: BlockDelay,
228 Delay: nil,
229 },
230 },
231 execute: func(chaos *BlockChaos) error {
232 _, err := chaos.ValidateCreate()
233 return err
234 },
235 expect: "error",
236 },
237 {
238 name: "validate delay",
239 chaos: BlockChaos{
240 ObjectMeta: metav1.ObjectMeta{
241 Namespace: metav1.NamespaceDefault,
242 Name: "foo13",
243 },
244 Spec: BlockChaosSpec{
245 Action: BlockDelay,
246 Delay: &BlockDelaySpec{
247 Latency: "1SSS",
248 },
249 },
250 },
251 execute: func(chaos *BlockChaos) error {
252 _, err := chaos.ValidateCreate()
253 return err
254 },
255 expect: "error",
256 },
257 }
258
259 for _, tc := range tcs {
260 err := tc.execute(&tc.chaos)
261 if tc.expect == "error" {
262 Expect(err).To(HaveOccurred())
263 } else {
264 Expect(err).NotTo(HaveOccurred())
265 }
266 }
267 })
268 })
269 })
270