1
2
3
4
5
6
7
8
9
10
11
12
13
14 package fixture
15
16 import (
17 "sort"
18
19 appsv1 "k8s.io/api/apps/v1"
20 corev1 "k8s.io/api/core/v1"
21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22 "k8s.io/apimachinery/pkg/util/intstr"
23 "k8s.io/utils/pointer"
24
25 "github.com/chaos-mesh/chaos-mesh/test/e2e/config"
26 )
27
28
29 func NewCommonNginxPod(name, namespace string) *corev1.Pod {
30 return &corev1.Pod{
31 ObjectMeta: metav1.ObjectMeta{
32 Name: name,
33 Namespace: namespace,
34 Labels: map[string]string{
35 "app": "nginx",
36 },
37 },
38 Spec: corev1.PodSpec{
39 Containers: []corev1.Container{
40 {
41 Image: "nginx:latest",
42 ImagePullPolicy: corev1.PullIfNotPresent,
43 Name: "nginx",
44 },
45 },
46 },
47 }
48 }
49
50
51 func NewCommonNginxDeployment(name, namespace string, replicas int32) *appsv1.Deployment {
52 return &appsv1.Deployment{
53 ObjectMeta: metav1.ObjectMeta{
54 Name: name,
55 Namespace: namespace,
56 Labels: map[string]string{
57 "app": "nginx",
58 },
59 },
60 Spec: appsv1.DeploymentSpec{
61 Replicas: &replicas,
62 Selector: &metav1.LabelSelector{
63 MatchLabels: map[string]string{
64 "app": "nginx",
65 },
66 },
67 Template: corev1.PodTemplateSpec{
68 ObjectMeta: metav1.ObjectMeta{
69 Labels: map[string]string{
70 "app": "nginx",
71 },
72 },
73 Spec: corev1.PodSpec{
74 Containers: []corev1.Container{
75 {
76 Image: "nginx:latest",
77 ImagePullPolicy: corev1.PullIfNotPresent,
78 Name: "nginx",
79 },
80 },
81 },
82 },
83 },
84 }
85 }
86
87
88 func NewTimerDeployment(name, namespace string) *appsv1.Deployment {
89 return &appsv1.Deployment{
90 ObjectMeta: metav1.ObjectMeta{
91 Name: name,
92 Namespace: namespace,
93 Labels: map[string]string{
94 "app": name,
95 },
96 },
97 Spec: appsv1.DeploymentSpec{
98 Replicas: pointer.Int32Ptr(1),
99 Selector: &metav1.LabelSelector{
100 MatchLabels: map[string]string{
101 "app": name,
102 },
103 },
104 Template: corev1.PodTemplateSpec{
105 ObjectMeta: metav1.ObjectMeta{
106 Labels: map[string]string{
107 "app": name,
108 },
109 },
110 Spec: corev1.PodSpec{
111 Containers: []corev1.Container{
112 {
113 Image: config.TestConfig.E2EImage,
114 ImagePullPolicy: corev1.PullIfNotPresent,
115 Name: name,
116 Command: []string{"/bin/test"},
117 },
118 },
119 },
120 },
121 },
122 }
123 }
124
125
126 func NewNetworkTestDeployment(name, namespace string, extraLabels map[string]string) *appsv1.Deployment {
127 labels := map[string]string{
128 "app": name,
129 }
130 for key, val := range extraLabels {
131 labels[key] = val
132 }
133 return &appsv1.Deployment{
134 ObjectMeta: metav1.ObjectMeta{
135 Name: name,
136 Namespace: namespace,
137 Labels: labels,
138 },
139 Spec: appsv1.DeploymentSpec{
140 Replicas: pointer.Int32Ptr(1),
141 Selector: &metav1.LabelSelector{
142 MatchLabels: labels,
143 },
144 Template: corev1.PodTemplateSpec{
145 ObjectMeta: metav1.ObjectMeta{
146 Labels: labels,
147 },
148 Spec: corev1.PodSpec{
149 Containers: []corev1.Container{
150 {
151 Image: config.TestConfig.E2EImage,
152 ImagePullPolicy: corev1.PullIfNotPresent,
153 Name: "network",
154 Command: []string{"/bin/test"},
155 },
156 },
157 },
158 },
159 },
160 }
161 }
162
163
164 func NewIOTestDeployment(name, namespace string) *appsv1.Deployment {
165 return &appsv1.Deployment{
166 ObjectMeta: metav1.ObjectMeta{
167 Name: name,
168 Namespace: namespace,
169 Labels: map[string]string{
170 "app": "io",
171 },
172 },
173 Spec: appsv1.DeploymentSpec{
174 Replicas: pointer.Int32Ptr(1),
175 Selector: &metav1.LabelSelector{
176 MatchLabels: map[string]string{
177 "app": "io",
178 },
179 },
180 Template: corev1.PodTemplateSpec{
181 ObjectMeta: metav1.ObjectMeta{
182 Labels: map[string]string{
183 "app": "io",
184 },
185 Annotations: map[string]string{
186 "admission-webhook.chaos-mesh.org/request": "chaosfs-io",
187 },
188 },
189 Spec: corev1.PodSpec{
190 Containers: []corev1.Container{
191 {
192 Image: config.TestConfig.E2EImage,
193 ImagePullPolicy: corev1.PullIfNotPresent,
194 Name: "io",
195 Command: []string{"/bin/test"},
196 VolumeMounts: []corev1.VolumeMount{
197 {
198 Name: "datadir",
199 MountPath: "/var/run/data",
200 },
201 },
202 },
203 },
204 Volumes: []corev1.Volume{
205 {
206 Name: "datadir",
207 VolumeSource: corev1.VolumeSource{
208 EmptyDir: &corev1.EmptyDirVolumeSource{},
209 },
210 },
211 },
212 },
213 },
214 },
215 }
216 }
217
218
219 func NewE2EService(name, namespace string) *corev1.Service {
220 return &corev1.Service{
221 ObjectMeta: metav1.ObjectMeta{
222 Namespace: namespace,
223 Name: name,
224 },
225 Spec: corev1.ServiceSpec{
226 Type: corev1.ServiceTypeNodePort,
227 Selector: map[string]string{
228 "app": name,
229 },
230 Ports: []corev1.ServicePort{
231 {
232 Name: "http",
233 Port: 8080,
234 TargetPort: intstr.IntOrString{IntVal: 8080},
235 },
236
237 {
238 Name: "nc-port",
239 Port: 1070,
240 TargetPort: intstr.IntOrString{IntVal: 8000},
241 },
242
243 {
244 Name: "chaosfs",
245 Port: 65534,
246 TargetPort: intstr.IntOrString{IntVal: 65534},
247 },
248 },
249 },
250 }
251 }
252
253
254 func HaveSameUIDs(pods1 []corev1.Pod, pods2 []corev1.Pod) bool {
255 count := len(pods1)
256 if count != len(pods2) {
257 return false
258 }
259 ids1, ids2 := make([]string, count), make([]string, count)
260 for i := 0; i < count; i++ {
261 ids1[i], ids2[i] = string(pods1[i].UID), string(pods2[i].UID)
262 }
263 sort.Strings(ids1)
264 sort.Strings(ids2)
265 for i := 0; i < count; i++ {
266 if ids1[i] != ids2[i] {
267 return false
268 }
269 }
270 return true
271 }
272