2017-09-06 23:36:13 -07:00
/*
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.
*/
2017-09-19 01:10:50 -07:00
package integration
2017-09-06 23:36:13 -07:00
import (
2017-10-12 16:08:08 -07:00
"fmt"
2018-01-04 00:30:06 -08:00
"strconv"
2017-09-06 23:36:13 -07:00
"time"
"github.com/rook/rook/tests/framework/clients"
2017-10-20 13:18:05 -07:00
"github.com/rook/rook/tests/framework/installer"
2017-09-06 23:36:13 -07:00
"github.com/rook/rook/tests/framework/utils"
2017-10-12 16:08:08 -07:00
"github.com/stretchr/testify/assert"
2017-09-06 23:36:13 -07:00
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
2018-01-04 00:30:06 -08:00
"k8s.io/api/apps/v1beta1"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2017-09-06 23:36:13 -07:00
)
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"
2017-09-14 09:40:34 -07:00
podName := "block-test"
2017-09-06 23:36:13 -07:00
2017-09-14 09:40:34 -07:00
defer blockTestDataCleanUp ( helper , k8sh , namespace , poolName , storageClassName , blockName , podName )
2017-09-06 23:36:13 -07:00
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" )
2017-12-29 00:33:36 -08:00
_ , cbErr := installer . BlockResourceOperation ( k8sh , installer . GetBlockPoolStorageClassAndPvcDef ( namespace , poolName , storageClassName , blockName , "ReadWriteOnce" ), "create" )
2017-09-06 23:36:13 -07:00
require . Nil ( s . T (), cbErr )
2017-10-20 13:18:05 -07:00
require . True ( s . T (), retryBlockImageCountCheck ( helper , len ( initBlockImages ), 1 ), "Make sure a new block is created" )
2017-09-06 23:36:13 -07:00
logger . Infof ( "Block Storage created successfully" )
2017-10-13 15:27:06 -07:00
require . True ( s . T (), k8sh . WaitUntilPVCIsBound ( defaultNamespace , blockName ), "Make sure PVC is Bound" )
2017-09-06 23:36:13 -07:00
logger . Infof ( "step 2: Mount block storage" )
2017-12-29 00:33:36 -08:00
_ , mtErr := rbc . BlockMap ( getBlockPodDefintion ( podName , blockName , false ), blockMountPath )
2017-09-06 23:36:13 -07:00
require . Nil ( s . T (), mtErr )
2017-11-02 16:48:57 -07:00
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 ))
2017-09-06 23:36:13 -07:00
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" )
2017-09-14 09:40:34 -07:00
logger . Infof ( "step 5: Mount same block storage on a different pod. Should not be allowed" )
otherPod := "block-test2"
2017-12-29 00:33:36 -08:00
_ , mtErr = rbc . BlockMap ( getBlockPodDefintion ( otherPod , blockName , false ), blockMountPath )
2017-09-14 09:40:34 -07:00
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" )
2017-12-29 00:33:36 -08:00
_ , unmtErr := rbc . BlockUnmap ( getBlockPodDefintion ( otherPod , blockName , false ), blockMountPath )
2017-09-14 09:40:34 -07:00
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" )
2017-12-29 00:33:36 -08:00
_ , unmtErr = rbc . BlockUnmap ( getBlockPodDefintion ( podName , blockName , false ), blockMountPath )
2017-09-06 23:36:13 -07:00
require . Nil ( s . T (), unmtErr )
2017-11-02 16:48:57 -07:00
require . True ( s . T (), k8sh . IsVolumeAttachmentResourceAbsent ( installer . SystemNamespace ( namespace ), crdName ), fmt . Sprintf ( "make sure VolumeAttachment %s is deleted" , crdName ))
2017-09-06 23:36:13 -07:00
require . True ( s . T (), k8sh . IsPodTerminated ( blockPodName , defaultNamespace ), "make sure block-test pod is terminated" )
logger . Infof ( "Block Storage unmounted successfully" )
2017-09-14 09:40:34 -07:00
logger . Infof ( "step 8: Deleting block storage" )
2017-12-29 00:33:36 -08:00
_ , dbErr := installer . BlockResourceOperation ( k8sh , installer . GetBlockPoolStorageClassAndPvcDef ( namespace , poolName , storageClassName , blockName , "ReadWriteOnce" ), "delete" )
2017-09-06 23:36:13 -07:00
require . Nil ( s . T (), dbErr )
2017-10-20 13:18:05 -07:00
require . True ( s . T (), retryBlockImageCountCheck ( helper , len ( initBlockImages ), 0 ), "Make sure a block is deleted" )
2017-09-06 23:36:13 -07:00
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 )
2017-10-12 16:08:08 -07:00
poolName := "rookpool"
2017-09-06 23:36:13 -07:00
//Check initial number of blocks
2017-10-12 16:08:08 -07:00
defer blockTestDataCleanUp ( helper , k8sh , clusterNamespace , poolName , "rook-block" , "test-block" , "block-test" )
2017-09-06 23:36:13 -07:00
bc := helper . GetBlockClient ()
initialBlocks , err := bc . BlockList ()
require . Nil ( s . T (), err )
initBlockCount := len ( initialBlocks )
2017-10-09 15:40:21 -07:00
logger . Infof ( "step : Create Pool,StorageClass and PVC" )
2017-10-20 13:18:05 -07:00
2017-12-29 00:33:36 -08:00
volumeDef := installer . GetBlockPoolStorageClassAndPvcDef ( clusterNamespace , poolName , "rook-block" , "test-block-claim" , "ReadWriteOnce" )
2017-10-20 13:18:05 -07:00
res1 , err := installer . BlockResourceOperation ( k8sh , volumeDef , "create" )
2017-10-12 16:08:08 -07:00
require . Contains ( s . T (), res1 , fmt . Sprintf ( "pool \"%s\" created" , poolName ), "Make sure test pool is created" )
2017-09-06 23:36:13 -07:00
require . Contains ( s . T (), res1 , "storageclass \"rook-block\" created" , "Make sure storageclass is created" )
2017-10-09 15:40:21 -07:00
require . Contains ( s . T (), res1 , "persistentvolumeclaim \"test-block-claim\" created" , "Make sure pvc is created" )
2017-09-06 23:36:13 -07:00
require . NoError ( s . T (), err )
2017-10-13 15:27:06 -07:00
require . True ( s . T (), k8sh . WaitUntilPVCIsBound ( defaultNamespace , "test-block-claim" ))
2017-09-06 23:36:13 -07:00
//Make sure new block is created
b , _ := bc . BlockList ()
2017-10-12 16:08:08 -07:00
assert . Equal ( s . T (), initBlockCount + 1 , len ( b ), "Make sure new block image is created" )
2017-10-20 13:18:05 -07:00
poolExists , err := foundPool ( helper , poolName )
2017-10-17 15:24:10 -07:00
assert . Nil ( s . T (), err )
2017-10-12 16:08:08 -07:00
assert . True ( s . T (), poolExists )
2017-09-06 23:36:13 -07:00
//Delete pvc and storageclass
2017-10-20 13:18:05 -07:00
_ , err = installer . BlockResourceOperation ( k8sh , volumeDef , "delete" )
2017-10-13 15:27:06 -07:00
assert . NoError ( s . T (), err )
assert . True ( s . T (), k8sh . WaitUntilPVCIsDeleted ( defaultNamespace , "test-block-claim" ))
2017-10-20 13:18:05 -07:00
require . True ( s . T (), retryBlockImageCountCheck ( helper , initBlockCount , 0 ), "Make sure a new block is deleted" )
2017-09-06 23:36:13 -07:00
b , _ = bc . BlockList ()
2017-10-13 15:27:06 -07:00
assert . Equal ( s . T (), initBlockCount , len ( b ), "Make sure new block image is deleted" )
2017-09-06 23:36:13 -07:00
2017-10-12 16:08:08 -07:00
checkPoolDeleted ( helper , s , poolName )
}
func checkPoolDeleted ( helper * clients . TestClient , s suite . Suite , name string ) {
i := 0
for i < utils . RetryLoop {
2017-10-20 13:18:05 -07:00
found , err := foundPool ( helper , name )
2017-10-17 15:24:10 -07:00
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 {
2017-10-12 16:08:08 -07:00
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 ))
}
2017-10-20 13:18:05 -07:00
func foundPool ( helper * clients . TestClient , name string ) ( bool , error ) {
2017-10-12 16:08:08 -07:00
p := helper . GetPoolClient ()
pools , err := p . PoolList ()
2017-10-17 15:24:10 -07:00
if err != nil {
return false , err
}
2017-10-12 16:08:08 -07:00
for _ , pool := range pools {
if name == pool . Name {
2017-10-17 15:24:10 -07:00
return true , nil
2017-10-12 16:08:08 -07:00
}
}
2017-10-17 15:24:10 -07:00
return false , nil
2017-09-06 23:36:13 -07:00
}
2017-09-14 09:40:34 -07:00
func blockTestDataCleanUp ( helper * clients . TestClient , k8sh * utils . K8sHelper , namespace , poolname , storageclassname , blockname , podname string ) {
2017-09-06 23:36:13 -07:00
logger . Infof ( "Cleaning up block storage" )
2017-12-29 00:33:36 -08:00
helper . GetBlockClient (). BlockUnmap ( getBlockPodDefintion ( podname , blockname , false ), blockMountPath )
installer . BlockResourceOperation ( k8sh , installer . GetBlockPoolStorageClassAndPvcDef ( namespace , poolname , storageclassname , blockname , "ReadWriteOnce" ), "delete" )
2017-10-12 16:08:08 -07:00
cleanupDynamicBlockStorage ( helper )
2017-09-06 23:36:13 -07:00
}
// 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
2017-10-20 13:18:05 -07:00
func retryBlockImageCountCheck ( helper * clients . TestClient , imageCount , expectedChange int ) bool {
2017-09-06 23:36:13 -07:00
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
2017-10-12 16:08:08 -07:00
func cleanupDynamicBlockStorage ( helper * clients . TestClient ) {
2017-09-06 23:36:13 -07:00
// Delete storage pool, storage class and pvc
blockImagesList , _ := helper . GetBlockClient (). BlockList ()
for _ , blockImage := range blockImagesList {
helper . GetRestAPIClient (). DeleteBlockImage ( blockImage )
}
}
2017-12-29 00:33:36 -08:00
func getBlockPodDefintion ( podname , blockName string , readOnly bool ) string {
2017-09-06 23:36:13 -07:00
return `apiVersion: v1
kind: Pod
metadata:
2017-09-14 09:40:34 -07:00
name: ` + podname + `
2017-09-06 23:36:13 -07:00
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 + `
2017-12-29 00:33:36 -08:00
readOnly: ` + strconv . FormatBool ( readOnly ) + `
2017-09-06 23:36:13 -07:00
restartPolicy: Never`
}
2017-12-29 00:33:36 -08:00
2018-01-04 00:30:06 -08:00
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
2017-12-29 00:33:36 -08:00
}