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("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 return chaos.ValidateCreate()
62 },
63 expect: "",
64 },
65 {
66 name: "simple ValidateUpdate",
67 chaos: StressChaos{
68 ObjectMeta: metav1.ObjectMeta{
69 Namespace: metav1.NamespaceDefault,
70 Name: "foo2",
71 },
72 Spec: StressChaosSpec{
73 Stressors: stressors,
74 },
75 },
76 execute: func(chaos *StressChaos) error {
77 return chaos.ValidateUpdate(chaos)
78 },
79 expect: "",
80 },
81 {
82 name: "simple ValidateDelete",
83 chaos: StressChaos{
84 ObjectMeta: metav1.ObjectMeta{
85 Namespace: metav1.NamespaceDefault,
86 Name: "foo3",
87 },
88 Spec: StressChaosSpec{
89 Stressors: stressors,
90 },
91 },
92 execute: func(chaos *StressChaos) error {
93 return chaos.ValidateDelete()
94 },
95 expect: "",
96 },
97 {
98 name: "missing stressors",
99 chaos: StressChaos{
100 ObjectMeta: metav1.ObjectMeta{
101 Namespace: metav1.NamespaceDefault,
102 Name: "foo5",
103 },
104 },
105 execute: func(chaos *StressChaos) error {
106 return chaos.ValidateCreate()
107 },
108 expect: "error",
109 },
110 }
111
112 for _, tc := range tcs {
113 err := tc.execute(&tc.chaos)
114 if tc.expect == "error" {
115 Expect(err).To(HaveOccurred())
116 } else {
117 Expect(err).NotTo(HaveOccurred())
118 }
119 }
120 })
121
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