forked from kubernetes-sigs/aws-ebs-csi-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_provsioning.go
192 lines (173 loc) · 5.59 KB
/
pre_provsioning.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
/*
Copyright 2018 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 e2e
import (
"context"
"fmt"
"math/rand"
"os"
"strings"
awscloud "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/testsuites"
. "github.com/onsi/ginkgo"
v1 "k8s.io/api/core/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/kubernetes/test/e2e/framework"
ebscsidriver "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver"
)
const (
defaultDiskSize = 4
defaultVoluemType = awscloud.VolumeTypeGP2
awsAvailabilityZonesEnv = "AWS_AVAILABILITY_ZONES"
dummyVolumeName = "pre-provisioned"
)
var (
defaultDiskSizeBytes int64 = defaultDiskSize * 1024 * 1024 * 1024
)
// Requires env AWS_AVAILABILITY_ZONES a comma separated list of AZs to be set
var _ = Describe("[ebs-csi-e2e] [single-az] Pre-Provisioned", func() {
f := framework.NewDefaultFramework("ebs")
var (
cs clientset.Interface
ns *v1.Namespace
ebsDriver driver.PreProvisionedVolumeTestDriver
cloud awscloud.Cloud
volumeID string
diskSize string
// Set to true if the volume should be deleted automatically after test
skipManuallyDeletingVolume bool
)
BeforeEach(func() {
cs = f.ClientSet
ns = f.Namespace
ebsDriver = driver.InitEbsCSIDriver()
// setup EBS volume
if os.Getenv(awsAvailabilityZonesEnv) == "" {
Skip(fmt.Sprintf("env %q not set", awsAvailabilityZonesEnv))
}
availabilityZones := strings.Split(os.Getenv(awsAvailabilityZonesEnv), ",")
availabilityZone := availabilityZones[rand.Intn(len(availabilityZones))]
region := availabilityZone[0 : len(availabilityZone)-1]
diskOptions := &awscloud.DiskOptions{
CapacityBytes: defaultDiskSizeBytes,
VolumeType: defaultVoluemType,
AvailabilityZone: availabilityZone,
Tags: map[string]string{awscloud.VolumeNameTagKey: dummyVolumeName},
}
var err error
cloud, err = awscloud.NewCloud(region)
if err != nil {
Fail(fmt.Sprintf("could not get NewCloud: %v", err))
}
disk, err := cloud.CreateDisk(context.Background(), "", diskOptions)
if err != nil {
Fail(fmt.Sprintf("could not provision a volume: %v", err))
}
volumeID = disk.VolumeID
diskSize = fmt.Sprintf("%dGi", defaultDiskSize)
By(fmt.Sprintf("Successfully provisioned EBS volume: %q\n", volumeID))
})
AfterEach(func() {
if !skipManuallyDeletingVolume {
err := cloud.WaitForAttachmentState(context.Background(), volumeID, "detached")
if err != nil {
Fail(fmt.Sprintf("could not detach volume %q: %v", volumeID, err))
}
ok, err := cloud.DeleteDisk(context.Background(), volumeID)
if err != nil || !ok {
Fail(fmt.Sprintf("could not delete volume %q: %v", volumeID, err))
}
}
})
It("[env] should write and read to a pre-provisioned volume", func() {
pods := []testsuites.PodDetails{
{
Cmd: "echo 'hello world' > /mnt/test-1/data && grep 'hello world' /mnt/test-1/data",
Volumes: []testsuites.VolumeDetails{
{
VolumeID: volumeID,
FSType: ebscsidriver.FSTypeExt4,
ClaimSize: diskSize,
VolumeMount: testsuites.VolumeMountDetails{
NameGenerate: "test-volume-",
MountPathGenerate: "/mnt/test-",
},
},
},
},
}
test := testsuites.PreProvisionedVolumeTest{
CSIDriver: ebsDriver,
Pods: pods,
}
test.Run(cs, ns)
})
It("[env] should use a pre-provisioned volume and mount it as readOnly in a pod", func() {
pods := []testsuites.PodDetails{
{
Cmd: "echo 'hello world' > /mnt/test-1/data && grep 'hello world' /mnt/test-1/data",
Volumes: []testsuites.VolumeDetails{
{
VolumeID: volumeID,
FSType: ebscsidriver.FSTypeExt4,
ClaimSize: diskSize,
VolumeMount: testsuites.VolumeMountDetails{
NameGenerate: "test-volume-",
MountPathGenerate: "/mnt/test-",
ReadOnly: true,
},
},
},
},
}
test := testsuites.PreProvisionedReadOnlyVolumeTest{
CSIDriver: ebsDriver,
Pods: pods,
}
test.Run(cs, ns)
})
It(fmt.Sprintf("[env] should use a pre-provisioned volume and retain PV with reclaimPolicy %q", v1.PersistentVolumeReclaimRetain), func() {
reclaimPolicy := v1.PersistentVolumeReclaimRetain
volumes := []testsuites.VolumeDetails{
{
VolumeID: volumeID,
FSType: ebscsidriver.FSTypeExt4,
ClaimSize: diskSize,
ReclaimPolicy: &reclaimPolicy,
},
}
test := testsuites.PreProvisionedReclaimPolicyTest{
CSIDriver: ebsDriver,
Volumes: volumes,
}
test.Run(cs, ns)
})
It(fmt.Sprintf("[env] should use a pre-provisioned volume and delete PV with reclaimPolicy %q", v1.PersistentVolumeReclaimDelete), func() {
reclaimPolicy := v1.PersistentVolumeReclaimDelete
skipManuallyDeletingVolume = true
volumes := []testsuites.VolumeDetails{
{
VolumeID: volumeID,
FSType: ebscsidriver.FSTypeExt4,
ClaimSize: diskSize,
ReclaimPolicy: &reclaimPolicy,
},
}
test := testsuites.PreProvisionedReclaimPolicyTest{
CSIDriver: ebsDriver,
Volumes: volumes,
}
test.Run(cs, ns)
})
})