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("stresschaos_webhook", func() {
25 Context("Defaulter", func() {
26 It("set default namespace selector", func() {
27 stresschaos := &StressChaos{
28 ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault},
29 }
30 stresschaos.Default()
31 Expect(stresschaos.Spec.Selector.Namespaces[0]).To(Equal(metav1.NamespaceDefault))
32 })
33 })
34 Context("webhook.Validator of stresschaos", func() {
35 It("Validate StressChaos", func() {
36
37 type TestCase struct {
38 name string
39 chaos StressChaos
40 execute func(chaos *StressChaos) error
41 expect string
42 }
43 stressors := &Stressors{
44 MemoryStressor: &MemoryStressor{
45 Stressor: Stressor{Workers: 1},
46 },
47 }
48 tcs := []TestCase{
49 {
50 name: "simple ValidateCreate",
51 chaos: StressChaos{
52 ObjectMeta: metav1.ObjectMeta{
53 Namespace: metav1.NamespaceDefault,
54 Name: "foo1",
55 },
56 Spec: StressChaosSpec{
57 Stressors: stressors,
58 },
59 },
60 execute: func(chaos *StressChaos) error {
61 _, err := chaos.ValidateCreate()
62 return err
63 },
64 expect: "",
65 },
66 {
67 name: "simple ValidateUpdate",
68 chaos: StressChaos{
69 ObjectMeta: metav1.ObjectMeta{
70 Namespace: metav1.NamespaceDefault,
71 Name: "foo2",
72 },
73 Spec: StressChaosSpec{
74 Stressors: stressors,
75 },
76 },
77 execute: func(chaos *StressChaos) error {
78 _, err := chaos.ValidateUpdate(chaos)
79 return err
80 },
81 expect: "",
82 },
83 {
84 name: "simple ValidateDelete",
85 chaos: StressChaos{
86 ObjectMeta: metav1.ObjectMeta{
87 Namespace: metav1.NamespaceDefault,
88 Name: "foo3",
89 },
90 Spec: StressChaosSpec{
91 Stressors: stressors,
92 },
93 },
94 execute: func(chaos *StressChaos) error {
95 _, err := chaos.ValidateDelete()
96 return err
97 },
98 expect: "",
99 },
100 {
101 name: "missing stressors",
102 chaos: StressChaos{
103 ObjectMeta: metav1.ObjectMeta{
104 Namespace: metav1.NamespaceDefault,
105 Name: "foo5",
106 },
107 },
108 execute: func(chaos *StressChaos) error {
109 _, err := chaos.ValidateCreate()
110 return err
111 },
112 expect: "error",
113 },
114 }
115
116 for _, tc := range tcs {
117 err := tc.execute(&tc.chaos)
118 if tc.expect == "error" {
119 Expect(err).To(HaveOccurred())
120 } else {
121 Expect(err).NotTo(HaveOccurred())
122 }
123 }
124 })
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 })
174
175 })
176