-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_block_test.go
334 lines (295 loc) · 12.1 KB
/
base_block_test.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
/*
Copyright 2016 The Rook Authors. All rights reserved.
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 integration
import (
"fmt"
"strconv"
"time"
"github.com/rook/rook/tests/framework/clients"
"github.com/rook/rook/tests/framework/installer"
"github.com/rook/rook/tests/framework/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"k8s.io/api/apps/v1beta1"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
blockMountPath = "/tmp/rook1"
blockPodName = "block-test"
)
// Smoke Test for Block Storage - Test check the following operations on Block Storage in order
//Create,Mount,Write,Read,Unmount and Delete.
func runBlockE2ETest(helper *clients.TestClient, k8sh *utils.K8sHelper, s suite.Suite, namespace string) {
poolName := "replicapool"
storageClassName := "rook-block"
blockName := "block-pv-claim"
podName := "block-test"
defer blockTestDataCleanUp(helper, k8sh, namespace, poolName, storageClassName, blockName, podName)
logger.Infof("Block Storage End to End Integration Test - create, mount, write to, read from, and unmount")
logger.Infof("Running on Rook Cluster %s", namespace)
rbc := helper.GetBlockClient()
logger.Infof("Step 0 : Get Initial List Block")
initBlockImages, _ := rbc.BlockList()
logger.Infof("step 1: Create block storage")
_, cbErr := installer.BlockResourceOperation(k8sh, installer.GetBlockPoolStorageClassAndPvcDef(namespace, poolName, storageClassName, blockName, "ReadWriteOnce"), "create")
require.Nil(s.T(), cbErr)
require.True(s.T(), retryBlockImageCountCheck(helper, len(initBlockImages), 1), "Make sure a new block is created")
logger.Infof("Block Storage created successfully")
require.True(s.T(), k8sh.WaitUntilPVCIsBound(defaultNamespace, blockName), "Make sure PVC is Bound")
logger.Infof("step 2: Mount block storage")
_, mtErr := rbc.BlockMap(getBlockPodDefintion(podName, blockName, false), blockMountPath)
require.Nil(s.T(), mtErr)
crdName, err := k8sh.GetVolumeAttachmentResourceName(defaultNamespace, blockName)
require.Nil(s.T(), err)
require.True(s.T(), k8sh.IsVolumeAttachmentResourcePresent(installer.SystemNamespace(namespace), crdName), fmt.Sprintf("make sure VolumeAttachment %s is created", crdName))
require.True(s.T(), k8sh.IsPodRunning(blockPodName, defaultNamespace), "make sure block-test pod is in running state")
logger.Infof("Block Storage Mounted successfully")
logger.Infof("step 3: Write to block storage")
_, wtErr := rbc.BlockWrite(blockPodName, blockMountPath, "Smoke Test Data form Block storage", "bsFile1", "")
require.Nil(s.T(), wtErr)
logger.Infof("Write to Block storage successfully")
logger.Infof("step 4: Read from block storage")
read, rErr := rbc.BlockRead(blockPodName, blockMountPath, "bsFile1", "")
require.Nil(s.T(), rErr)
require.Contains(s.T(), read, "Smoke Test Data form Block storage", "make sure content of the files is unchanged")
logger.Infof("Read from Block storage successfully")
logger.Infof("step 5: Mount same block storage on a different pod. Should not be allowed")
otherPod := "block-test2"
_, mtErr = rbc.BlockMap(getBlockPodDefintion(otherPod, blockName, false), blockMountPath)
require.Nil(s.T(), mtErr)
require.True(s.T(), k8sh.IsPodInError(otherPod, defaultNamespace, "FailedMount", "Volume is already attached by pod"), "make sure block-test2 pod errors out while mounting the volume")
logger.Infof("Block Storage successfully fenced")
logger.Infof("step 6: Delete fenced pod")
_, unmtErr := rbc.BlockUnmap(getBlockPodDefintion(otherPod, blockName, false), blockMountPath)
require.Nil(s.T(), unmtErr)
require.True(s.T(), k8sh.IsPodTerminated(otherPod, defaultNamespace), "make sure block-test2 pod is terminated")
logger.Infof("Fenced pod deleted successfully")
logger.Infof("step 7: Unmount block storage")
_, unmtErr = rbc.BlockUnmap(getBlockPodDefintion(podName, blockName, false), blockMountPath)
require.Nil(s.T(), unmtErr)
require.True(s.T(), k8sh.IsVolumeAttachmentResourceAbsent(installer.SystemNamespace(namespace), crdName), fmt.Sprintf("make sure VolumeAttachment %s is deleted", crdName))
require.True(s.T(), k8sh.IsPodTerminated(blockPodName, defaultNamespace), "make sure block-test pod is terminated")
logger.Infof("Block Storage unmounted successfully")
logger.Infof("step 8: Deleting block storage")
_, dbErr := installer.BlockResourceOperation(k8sh, installer.GetBlockPoolStorageClassAndPvcDef(namespace, poolName, storageClassName, blockName, "ReadWriteOnce"), "delete")
require.Nil(s.T(), dbErr)
require.True(s.T(), retryBlockImageCountCheck(helper, len(initBlockImages), 0), "Make sure a block is deleted")
logger.Infof("Block Storage deleted successfully")
}
func runBlockE2ETestLite(helper *clients.TestClient, k8sh *utils.K8sHelper, s suite.Suite, clusterNamespace string) {
logger.Infof("Block Storage End to End Integration Test - create storageclass,pool and pvc")
logger.Infof("Running on Rook Cluster %s", clusterNamespace)
poolName := "rookpool"
//Check initial number of blocks
defer blockTestDataCleanUp(helper, k8sh, clusterNamespace, poolName, "rook-block", "test-block", "block-test")
bc := helper.GetBlockClient()
initialBlocks, err := bc.BlockList()
require.Nil(s.T(), err)
initBlockCount := len(initialBlocks)
logger.Infof("step : Create Pool,StorageClass and PVC")
volumeDef := installer.GetBlockPoolStorageClassAndPvcDef(clusterNamespace, poolName, "rook-block", "test-block-claim", "ReadWriteOnce")
res1, err := installer.BlockResourceOperation(k8sh, volumeDef, "create")
require.Contains(s.T(), res1, fmt.Sprintf("pool \"%s\" created", poolName), "Make sure test pool is created")
require.Contains(s.T(), res1, "storageclass \"rook-block\" created", "Make sure storageclass is created")
require.Contains(s.T(), res1, "persistentvolumeclaim \"test-block-claim\" created", "Make sure pvc is created")
require.NoError(s.T(), err)
require.True(s.T(), k8sh.WaitUntilPVCIsBound(defaultNamespace, "test-block-claim"))
//Make sure new block is created
b, _ := bc.BlockList()
assert.Equal(s.T(), initBlockCount+1, len(b), "Make sure new block image is created")
poolExists, err := foundPool(helper, poolName)
assert.Nil(s.T(), err)
assert.True(s.T(), poolExists)
//Delete pvc and storageclass
_, err = installer.BlockResourceOperation(k8sh, volumeDef, "delete")
assert.NoError(s.T(), err)
assert.True(s.T(), k8sh.WaitUntilPVCIsDeleted(defaultNamespace, "test-block-claim"))
require.True(s.T(), retryBlockImageCountCheck(helper, initBlockCount, 0), "Make sure a new block is deleted")
b, _ = bc.BlockList()
assert.Equal(s.T(), initBlockCount, len(b), "Make sure new block image is deleted")
checkPoolDeleted(helper, s, poolName)
}
func checkPoolDeleted(helper *clients.TestClient, s suite.Suite, name string) {
i := 0
for i < utils.RetryLoop {
found, err := foundPool(helper, name)
if err != nil {
// try again on failure since the pool may have been in an unexpected state while deleting
logger.Warningf("error getting pools. %+v", err)
} else if !found {
logger.Infof("pool %s is deleted", name)
return
}
i++
logger.Infof("pool %s still exists", name)
time.Sleep(time.Second * utils.RetryInterval)
}
assert.Fail(s.T(), fmt.Sprintf("pool %s was not deleted", name))
}
func foundPool(helper *clients.TestClient, name string) (bool, error) {
p := helper.GetPoolClient()
pools, err := p.PoolList()
if err != nil {
return false, err
}
for _, pool := range pools {
if name == pool.Name {
return true, nil
}
}
return false, nil
}
func blockTestDataCleanUp(helper *clients.TestClient, k8sh *utils.K8sHelper, namespace, poolname, storageclassname, blockname, podname string) {
logger.Infof("Cleaning up block storage")
helper.GetBlockClient().BlockUnmap(getBlockPodDefintion(podname, blockname, false), blockMountPath)
installer.BlockResourceOperation(k8sh, installer.GetBlockPoolStorageClassAndPvcDef(namespace, poolname, storageclassname, blockname, "ReadWriteOnce"), "delete")
cleanupDynamicBlockStorage(helper)
}
// periodically checking if block image count has changed to expected value
// When creating pvc in k8s platform, it may take some time for the block Image to be bounded
func retryBlockImageCountCheck(helper *clients.TestClient, imageCount, expectedChange int) bool {
inc := 0
for inc < utils.RetryLoop {
logger.Infof("Getting list of blocks (expecting %d)", (imageCount + expectedChange))
blockImages, _ := helper.GetBlockClient().BlockList()
if imageCount+expectedChange == len(blockImages) {
return true
}
time.Sleep(time.Second * utils.RetryInterval)
inc++
}
return false
}
//CleanUpDymanicBlockStorage is helper method to clean up bock storage created by tests
func cleanupDynamicBlockStorage(helper *clients.TestClient) {
// Delete storage pool, storage class and pvc
blockImagesList, _ := helper.GetBlockClient().BlockList()
for _, blockImage := range blockImagesList {
helper.GetRestAPIClient().DeleteBlockImage(blockImage)
}
}
func getBlockPodDefintion(podname, blockName string, readOnly bool) string {
return `apiVersion: v1
kind: Pod
metadata:
name: ` + podname + `
spec:
containers:
- image: busybox
name: block-test1
command:
- sleep
- "3600"
imagePullPolicy: IfNotPresent
volumeMounts:
- name: block-persistent-storage
mountPath: /tmp/rook1
volumes:
- name: block-persistent-storage
persistentVolumeClaim:
claimName: ` + blockName + `
readOnly: ` + strconv.FormatBool(readOnly) + `
restartPolicy: Never`
}
func getBlockStatefulSetAndServiceDefinition(namespace, statefulsetName, podname, StorageClassName string) (*v1.Service, *v1beta1.StatefulSet) {
service := &v1.Service{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Service",
},
ObjectMeta: metav1.ObjectMeta{
Name: statefulsetName,
Namespace: namespace,
Labels: map[string]string{
"app": statefulsetName,
},
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{
{
Name: statefulsetName,
Port: 80,
},
},
ClusterIP: "None",
Selector: map[string]string{
"app": statefulsetName,
},
},
}
var replica int32 = 1
statefulSet := &v1beta1.StatefulSet{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1beta1",
Kind: "StatefulSet",
},
ObjectMeta: metav1.ObjectMeta{
Name: podname,
Namespace: namespace,
},
Spec: v1beta1.StatefulSetSpec{
ServiceName: statefulsetName,
Replicas: &replica,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": statefulsetName,
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: statefulsetName,
Image: "busybox",
Command: []string{"sleep", "3600"},
Ports: []v1.ContainerPort{
{
ContainerPort: 80,
Name: podname,
},
},
VolumeMounts: []v1.VolumeMount{
{
Name: "rookpvc",
MountPath: "/tmp/rook",
},
},
},
},
},
},
VolumeClaimTemplates: []v1.PersistentVolumeClaim{
{
ObjectMeta: metav1.ObjectMeta{
Name: "rookpvc",
Annotations: map[string]string{
"volume.beta.kubernetes.io/storage-class": StorageClassName,
},
},
Spec: v1.PersistentVolumeClaimSpec{
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceStorage: *resource.NewQuantity(1.0, resource.BinarySI),
},
},
},
},
},
},
}
return service, statefulSet
}