From 58f0a32b62edc4c92301d60e596a00a2d4c018af Mon Sep 17 00:00:00 2001 From: Steve Leon Date: Thu, 2 Nov 2017 16:48:57 -0700 Subject: [PATCH] Check volumeattachment crd in int-tests Fixes https://github.com/rook/rook/issues/1175 --- tests/framework/utils/k8s_helper.go | 76 ++++++++++++++++++++++++++++ tests/integration/base_block_test.go | 4 ++ 2 files changed, 80 insertions(+) diff --git a/tests/framework/utils/k8s_helper.go b/tests/framework/utils/k8s_helper.go index 4a85d4837..b5d4529b0 100644 --- a/tests/framework/utils/k8s_helper.go +++ b/tests/framework/utils/k8s_helper.go @@ -30,6 +30,8 @@ import ( "strconv" "github.com/coreos/pkg/capnslog" + "github.com/rook/rook/pkg/agent/flexvolume/crd" + "github.com/rook/rook/pkg/operator/k8sutil" "github.com/rook/rook/pkg/util/exec" "github.com/stretchr/testify/require" "k8s.io/api/core/v1" @@ -461,6 +463,80 @@ func (k8sh *K8sHelper) IsCRDPresent(crdName string) bool { return false } +// GetVolumeAttachmentResourceName gets the VolumeAttachment object name from the PVC +func (k8sh *K8sHelper) GetVolumeAttachmentResourceName(namespace, pvcName string) (string, error) { + + getOpts := metav1.GetOptions{} + pvc, err := k8sh.Clientset.PersistentVolumeClaims(namespace).Get(pvcName, getOpts) + if err != nil { + return "", err + } + return pvc.Spec.VolumeName, nil +} + +//IsVolumeAttachmentResourcePresent returns true if VolumeAttachment resource is present +func (k8sh *K8sHelper) IsVolumeAttachmentResourcePresent(namespace, volumeAttachmentName string) bool { + err := k8sh.waitForVolumeAttachment(namespace, volumeAttachmentName, true) + if err != nil { + k8slogger.Error(err.Error()) + return false + } + return true +} + +//IsVolumeAttachmentResourceAbsent returns true if the VolumeAttachment resource is deleted/absent within 90s else returns false +func (k8sh *K8sHelper) IsVolumeAttachmentResourceAbsent(namespace, volumeAttachmentName string) bool { + + err := k8sh.waitForVolumeAttachment(namespace, volumeAttachmentName, false) + if err != nil { + k8slogger.Error(err.Error()) + return false + } + return true +} + +func (k8sh *K8sHelper) waitForVolumeAttachment(namespace, volumeAttachmentName string, exist bool) error { + + action := "exist" + if !exist { + action = "not " + action + } + + inc := 0 + for inc < RetryLoop { + isExist, err := k8sh.isVolumeAttachmentExist(namespace, volumeAttachmentName) + if err != nil { + return fmt.Errorf("Errors encountered while getting VolumeAttachment %s/%s: %v", namespace, volumeAttachmentName, err) + } + if isExist == exist { + return nil + } + + k8slogger.Infof("waiting for VolumeAttachment %s in namespace %s to %s", volumeAttachmentName, namespace, action) + time.Sleep(RetryInterval * time.Second) + inc++ + + } + return fmt.Errorf("timeout for VolumeAttachment %s in namespace %s wait to %s", volumeAttachmentName, namespace, action) +} + +func (k8sh *K8sHelper) isVolumeAttachmentExist(namespace, name string) (bool, error) { + var result crd.VolumeAttachment + uri := fmt.Sprintf("apis/%s/%s/namespaces/%s/%s", k8sutil.CustomResourceGroup, k8sutil.V1Alpha1, namespace, crd.CustomResourceNamePlural) + err := k8sh.Clientset.Core().RESTClient().Get(). + RequestURI(uri). + Name(name). + Do(). + Into(&result) + if err != nil { + if errors.IsNotFound(err) { + return false, nil + } + return false, err + } + return true, nil +} + //GetPodDetails returns details about a pod func (k8sh *K8sHelper) GetPodDetails(podNamePattern string, namespace string) (string, error) { args := []string{"get", "pods", "-l", "app=" + podNamePattern, "-o", "wide", "--no-headers=true", "-o", "name"} diff --git a/tests/integration/base_block_test.go b/tests/integration/base_block_test.go index a48c3a0dd..d187c79a0 100644 --- a/tests/integration/base_block_test.go +++ b/tests/integration/base_block_test.go @@ -59,6 +59,9 @@ func runBlockE2ETest(helper *clients.TestClient, k8sh *utils.K8sHelper, s suite. logger.Infof("step 2: Mount block storage") _, mtErr := rbc.BlockMap(getBlockPodDefintion(podName, blockName), 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") @@ -89,6 +92,7 @@ func runBlockE2ETest(helper *clients.TestClient, k8sh *utils.K8sHelper, s suite. logger.Infof("step 7: Unmount block storage") _, unmtErr = rbc.BlockUnmap(getBlockPodDefintion(podName, blockName), 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")