-
Notifications
You must be signed in to change notification settings - Fork 77
/
testsuites.go
379 lines (331 loc) · 14.1 KB
/
testsuites.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package testsuites
import (
"context"
"fmt"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/kubernetes/test/e2e/framework"
e2elog "k8s.io/kubernetes/test/e2e/framework/log"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
e2epv "k8s.io/kubernetes/test/e2e/framework/pv"
imageutils "k8s.io/kubernetes/test/utils/image"
awscloud "sigs.k8s.io/aws-fsx-csi-driver/pkg/cloud"
)
const (
// Some pods can take much longer to get ready due to volume attach/detach latency.
slowPodStartTimeout = 15 * time.Minute
// Description that will printed during tests
failedConditionDescription = "Error status code"
)
type TestStorageClass struct {
client clientset.Interface
storageClass *storagev1.StorageClass
namespace *v1.Namespace
}
func NewTestStorageClass(c clientset.Interface, ns *v1.Namespace, sc *storagev1.StorageClass) *TestStorageClass {
return &TestStorageClass{
client: c,
storageClass: sc,
namespace: ns,
}
}
func (t *TestStorageClass) Create() storagev1.StorageClass {
var err error
By("creating a StorageClass " + t.storageClass.Name)
t.storageClass, err = t.client.StorageV1().StorageClasses().Create(context.TODO(), t.storageClass, metav1.CreateOptions{})
framework.ExpectNoError(err)
return *t.storageClass
}
func (t *TestStorageClass) Cleanup() {
e2elog.Logf("deleting StorageClass %s", t.storageClass.Name)
err := t.client.StorageV1().StorageClasses().Delete(context.TODO(), t.storageClass.Name, metav1.DeleteOptions{})
framework.ExpectNoError(err)
}
type TestPersistentVolumeClaim struct {
client clientset.Interface
claimSize string
volumeMode v1.PersistentVolumeMode
storageClass *storagev1.StorageClass
namespace *v1.Namespace
persistentVolume *v1.PersistentVolume
persistentVolumeClaim *v1.PersistentVolumeClaim
requestedPersistentVolumeClaim *v1.PersistentVolumeClaim
dataSource *v1.TypedLocalObjectReference
}
func NewTestPersistentVolumeClaim(c clientset.Interface, ns *v1.Namespace, claimSize string, sc *storagev1.StorageClass) *TestPersistentVolumeClaim {
return &TestPersistentVolumeClaim{
client: c,
claimSize: claimSize,
volumeMode: v1.PersistentVolumeFilesystem,
namespace: ns,
storageClass: sc,
}
}
func (t *TestPersistentVolumeClaim) Create() {
var err error
By("creating a PVC")
storageClassName := ""
if t.storageClass != nil {
storageClassName = t.storageClass.Name
}
t.requestedPersistentVolumeClaim = generatePVC(t.namespace.Name, storageClassName, t.claimSize, t.volumeMode, t.dataSource)
t.persistentVolumeClaim, err = t.client.CoreV1().PersistentVolumeClaims(t.namespace.Name).Create(context.TODO(), t.requestedPersistentVolumeClaim, metav1.CreateOptions{})
framework.ExpectNoError(err)
}
func (t *TestPersistentVolumeClaim) ValidateProvisionedPersistentVolume() {
var err error
// Get the bound PersistentVolume
By("validating provisioned PV")
t.persistentVolume, err = t.client.CoreV1().PersistentVolumes().Get(context.TODO(), t.persistentVolumeClaim.Spec.VolumeName, metav1.GetOptions{})
framework.ExpectNoError(err)
// Check sizes
expectedCapacity := t.requestedPersistentVolumeClaim.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
claimCapacity := t.persistentVolumeClaim.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
Expect(claimCapacity.Value()).To(Equal(expectedCapacity.Value()), "claimCapacity is not equal to requestedCapacity")
pvCapacity := t.persistentVolume.Spec.Capacity[v1.ResourceName(v1.ResourceStorage)]
Expect(pvCapacity.Value()).To(Equal(expectedCapacity.Value()), "pvCapacity is not equal to requestedCapacity")
// Check PV properties
By("checking the PV")
expectedAccessModes := t.requestedPersistentVolumeClaim.Spec.AccessModes
Expect(t.persistentVolume.Spec.AccessModes).To(Equal(expectedAccessModes))
Expect(t.persistentVolume.Spec.ClaimRef.Name).To(Equal(t.persistentVolumeClaim.ObjectMeta.Name))
Expect(t.persistentVolume.Spec.ClaimRef.Namespace).To(Equal(t.persistentVolumeClaim.ObjectMeta.Namespace))
// If storageClass is nil, PV was pre-provisioned with these values already set
if t.storageClass != nil {
Expect(t.persistentVolume.Spec.PersistentVolumeReclaimPolicy).To(Equal(*t.storageClass.ReclaimPolicy))
Expect(t.persistentVolume.Spec.MountOptions).To(Equal(t.storageClass.MountOptions))
if *t.storageClass.VolumeBindingMode == storagev1.VolumeBindingWaitForFirstConsumer {
Expect(t.persistentVolume.Spec.NodeAffinity.Required.NodeSelectorTerms[0].MatchExpressions[0].Values).
To(HaveLen(1))
}
if len(t.storageClass.AllowedTopologies) > 0 {
Expect(t.persistentVolume.Spec.NodeAffinity.Required.NodeSelectorTerms[0].MatchExpressions[0].Key).
To(Equal(t.storageClass.AllowedTopologies[0].MatchLabelExpressions[0].Key))
for _, v := range t.persistentVolume.Spec.NodeAffinity.Required.NodeSelectorTerms[0].MatchExpressions[0].Values {
Expect(t.storageClass.AllowedTopologies[0].MatchLabelExpressions[0].Values).To(ContainElement(v))
}
}
}
}
func (t *TestPersistentVolumeClaim) WaitForBound() v1.PersistentVolumeClaim {
var err error
By(fmt.Sprintf("waiting for PVC to be in phase %q", v1.ClaimBound))
err = e2epv.WaitForPersistentVolumeClaimPhase(v1.ClaimBound, t.client, t.namespace.Name, t.persistentVolumeClaim.Name, framework.Poll, 10*time.Minute)
framework.ExpectNoError(err)
By("checking the PVC")
// Get new copy of the claim
t.persistentVolumeClaim, err = t.client.CoreV1().PersistentVolumeClaims(t.namespace.Name).Get(context.TODO(), t.persistentVolumeClaim.Name, metav1.GetOptions{})
framework.ExpectNoError(err)
return *t.persistentVolumeClaim
}
func generatePVC(namespace, storageClassName, claimSize string, volumeMode v1.PersistentVolumeMode, dataSource *v1.TypedLocalObjectReference) *v1.PersistentVolumeClaim {
return &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "pvc-",
Namespace: namespace,
},
Spec: v1.PersistentVolumeClaimSpec{
StorageClassName: &storageClassName,
AccessModes: []v1.PersistentVolumeAccessMode{
v1.ReadWriteOnce,
},
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): resource.MustParse(claimSize),
},
},
VolumeMode: &volumeMode,
DataSource: dataSource,
},
}
}
func (t *TestPersistentVolumeClaim) Cleanup() {
e2elog.Logf("deleting PVC %q/%q", t.namespace.Name, t.persistentVolumeClaim.Name)
err := e2epv.DeletePersistentVolumeClaim(t.client, t.persistentVolumeClaim.Name, t.namespace.Name)
framework.ExpectNoError(err)
// Wait for the PV to get deleted if reclaim policy is Delete. (If it's
// Retain, there's no use waiting because the PV won't be auto-deleted and
// it's expected for the caller to do it.) Technically, the first few delete
// attempts may fail, as the volume is still attached to a node because
// kubelet is slowly cleaning up the previous pod, however it should succeed
// in a couple of minutes.
if t.persistentVolume.Spec.PersistentVolumeReclaimPolicy == v1.PersistentVolumeReclaimDelete {
By(fmt.Sprintf("waiting for claim's PV %q to be deleted", t.persistentVolume.Name))
err := e2epv.WaitForPersistentVolumeDeleted(t.client, t.persistentVolume.Name, 5*time.Second, 10*time.Minute)
framework.ExpectNoError(err)
}
// Wait for the PVC to be deleted
err = waitForPersistentVolumeClaimDeleted(t.client, t.namespace.Name, t.persistentVolumeClaim.Name, 5*time.Second, 5*time.Minute)
framework.ExpectNoError(err)
}
func (t *TestPersistentVolumeClaim) ReclaimPolicy() v1.PersistentVolumeReclaimPolicy {
return t.persistentVolume.Spec.PersistentVolumeReclaimPolicy
}
func (t *TestPersistentVolumeClaim) WaitForPersistentVolumePhase(phase v1.PersistentVolumePhase) {
err := e2epv.WaitForPersistentVolumePhase(phase, t.client, t.persistentVolume.Name, 5*time.Second, 10*time.Minute)
framework.ExpectNoError(err)
}
func (t *TestPersistentVolumeClaim) DeleteBoundPersistentVolume() {
By(fmt.Sprintf("deleting PV %q", t.persistentVolume.Name))
err := e2epv.DeletePersistentVolume(t.client, t.persistentVolume.Name)
framework.ExpectNoError(err)
By(fmt.Sprintf("waiting for claim's PV %q to be deleted", t.persistentVolume.Name))
err = e2epv.WaitForPersistentVolumeDeleted(t.client, t.persistentVolume.Name, 5*time.Second, 10*time.Minute)
framework.ExpectNoError(err)
}
func (t *TestPersistentVolumeClaim) DeleteBackingVolume(cloud awscloud.Cloud) {
volumeID := t.persistentVolume.Spec.CSI.VolumeHandle
By(fmt.Sprintf("deleting FSx filesystem %q", volumeID))
err := cloud.DeleteFileSystem(context.Background(), volumeID)
if err != nil {
Fail(fmt.Sprintf("could not delete volume %q: %v", volumeID, err))
}
}
// waitForPersistentVolumeClaimDeleted waits for a PersistentVolumeClaim to be removed from the system until timeout occurs, whichever comes first.
func waitForPersistentVolumeClaimDeleted(c clientset.Interface, ns string, pvcName string, Poll, timeout time.Duration) error {
framework.Logf("Waiting up to %v for PersistentVolumeClaim %s to be removed", timeout, pvcName)
for start := time.Now(); time.Since(start) < timeout; time.Sleep(Poll) {
_, err := c.CoreV1().PersistentVolumeClaims(ns).Get(context.TODO(), pvcName, metav1.GetOptions{})
if err != nil {
if apierrs.IsNotFound(err) {
framework.Logf("Claim %q in namespace %q doesn't exist in the system", pvcName, ns)
return nil
}
framework.Logf("Failed to get claim %q in namespace %q, retrying in %v. Error: %v", pvcName, ns, Poll, err)
}
}
return fmt.Errorf("PersistentVolumeClaim %s is not removed from the system within %v", pvcName, timeout)
}
type TestPod struct {
client clientset.Interface
pod *v1.Pod
namespace *v1.Namespace
}
func NewTestPod(c clientset.Interface, ns *v1.Namespace, command string) *TestPod {
return &TestPod{
client: c,
namespace: ns,
pod: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "fsx-volume-tester-",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "volume-tester",
Image: imageutils.GetE2EImage(imageutils.BusyBox),
Command: []string{"/bin/sh"},
Args: []string{"-c", command},
VolumeMounts: make([]v1.VolumeMount, 0),
},
},
RestartPolicy: v1.RestartPolicyNever,
Volumes: make([]v1.Volume, 0),
},
},
}
}
func (t *TestPod) Create() {
var err error
t.pod, err = t.client.CoreV1().Pods(t.namespace.Name).Create(context.TODO(), t.pod, metav1.CreateOptions{})
framework.ExpectNoError(err)
}
func (t *TestPod) WaitForSuccess() {
err := e2epod.WaitForPodSuccessInNamespaceSlow(t.client, t.pod.Name, t.namespace.Name)
framework.ExpectNoError(err)
}
func (t *TestPod) WaitForRunning() {
err := e2epod.WaitForPodRunningInNamespace(t.client, t.pod)
framework.ExpectNoError(err)
}
// Ideally this would be in "k8s.io/kubernetes/test/e2e/framework"
// Similar to e2epv.WaitForPodSuccessInNamespaceSlow
var podFailedCondition = func(pod *v1.Pod) (bool, error) {
switch pod.Status.Phase {
case v1.PodFailed:
By("Saw pod failure")
return true, nil
case v1.PodSucceeded:
return true, fmt.Errorf("pod %q successed with reason: %q, message: %q", pod.Name, pod.Status.Reason, pod.Status.Message)
default:
return false, nil
}
}
func (t *TestPod) WaitForFailure() {
err := e2epod.WaitForPodCondition(t.client, t.namespace.Name, t.pod.Name, failedConditionDescription, slowPodStartTimeout, podFailedCondition)
framework.ExpectNoError(err)
}
func (t *TestPod) SetupVolume(pvc *v1.PersistentVolumeClaim, name, mountPath string, readOnly bool) {
volumeMount := v1.VolumeMount{
Name: name,
MountPath: mountPath,
ReadOnly: readOnly,
}
t.pod.Spec.Containers[0].VolumeMounts = append(t.pod.Spec.Containers[0].VolumeMounts, volumeMount)
volume := v1.Volume{
Name: name,
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: pvc.Name,
},
},
}
t.pod.Spec.Volumes = append(t.pod.Spec.Volumes, volume)
}
func (t *TestPod) SetupRawBlockVolume(pvc *v1.PersistentVolumeClaim, name, devicePath string) {
volumeDevice := v1.VolumeDevice{
Name: name,
DevicePath: devicePath,
}
t.pod.Spec.Containers[0].VolumeDevices = append(t.pod.Spec.Containers[0].VolumeDevices, volumeDevice)
volume := v1.Volume{
Name: name,
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: pvc.Name,
},
},
}
t.pod.Spec.Volumes = append(t.pod.Spec.Volumes, volume)
}
func (t *TestPod) SetNodeSelector(nodeSelector map[string]string) {
t.pod.Spec.NodeSelector = nodeSelector
}
func (t *TestPod) Cleanup() {
cleanupPodOrFail(t.client, t.pod.Name, t.namespace.Name)
}
func (t *TestPod) Logs() ([]byte, error) {
return podLogs(t.client, t.pod.Name, t.namespace.Name)
}
func cleanupPodOrFail(client clientset.Interface, name, namespace string) {
e2elog.Logf("deleting Pod %q/%q", namespace, name)
body, err := podLogs(client, name, namespace)
if err != nil {
e2elog.Logf("Error getting logs for pod %s: %v", name, err)
} else {
e2elog.Logf("Pod %s has the following logs: %s", name, body)
}
e2epod.DeletePodOrFail(client, namespace, name)
}
func podLogs(client clientset.Interface, name, namespace string) ([]byte, error) {
return client.CoreV1().Pods(namespace).GetLogs(name, &v1.PodLogOptions{}).Do(context.TODO()).Raw()
}