Skip to content

Commit

Permalink
Wait for volume status in e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
bertinatto committed Oct 3, 2018
1 parent ab93ac7 commit 4917df0
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import (
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/bertinatto/ebs-csi-driver/pkg/cloud"
csi "github.com/container-storage-interface/spec/lib/go/csi/v0"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -48,10 +49,11 @@ var (

var _ = Describe("EBS CSI Driver", func() {

It("Should create->attach->stage->mount volume and check if it is writable, then unmount->unstage->detach->delete and check disk is deleted", func() {
It("Should create, attach, stage and mount volume, check if it's writable, unmount, unstage, detach, delete, and check if it's deleted", func() {

r1 := rand.New(rand.NewSource(time.Now().UnixNano()))
req := &csi.CreateVolumeRequest{
Name: "volume-name-e2e-test",
Name: fmt.Sprintf("volume-name-e2e-test-%d", r1.Uint64()),
CapacityRange: stdCapRange,
VolumeCapabilities: stdVolCap,
Parameters: nil,
Expand All @@ -62,37 +64,20 @@ var _ = Describe("EBS CSI Driver", func() {

volume := resp.GetVolume()
Expect(volume).NotTo(BeNil(), "Expected valid volume, got nil")

// Verifying that volume was created ans is valid
descParams := &ec2.DescribeVolumesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:" + cloud.VolumeNameTagKey),
Values: []*string{aws.String(req.GetName())},
},
},
}
waitForVolumes(descParams, 1 /* number of expected volumes */)
waitForVolumeState(volume.Id, "available")

// Delete volume
defer func() {
_, err = csiClient.ctrl.DeleteVolume(context.Background(), &csi.DeleteVolumeRequest{VolumeId: volume.Id})
Expect(err).To(BeNil(), "Could not delete volume")
waitForVolumes(descParams, 0 /* number of expected volumes */)
waitForVolumes(volume.Id, 0 /* number of expected volumes */)

// Deleting volume twice
_, err = csiClient.ctrl.DeleteVolume(context.Background(), &csi.DeleteVolumeRequest{VolumeId: volume.Id})
Expect(err).To(BeNil(), "Error when trying to delete volume twice")

// Trying to delete non-existent volume
nonexistentVolume := "vol-0f13f3ff21126cabf"
if nonexistentVolume != volume.Id {
_, err = csiClient.ctrl.DeleteVolume(context.Background(), &csi.DeleteVolumeRequest{VolumeId: nonexistentVolume})
Expect(err).To(BeNil(), "Error deleting non-existing volume: %v", err)
}
}()

// TODO: attach, stage, publish, unpublish, unstage, detach
// Attach, stage, publish, unpublish, unstage, detach
nodeID := ebs.GetMetadata().GetInstanceID()
testAttachWriteReadDetach(volume.Id, req.GetName(), nodeID, false)

Expand Down Expand Up @@ -155,15 +140,17 @@ func testAttachWriteReadDetach(volumeID, volName, nodeID string, readOnly bool)
VolumeCapability: stdVolCap[0],
})
Expect(err).To(BeNil(), "NodePublishVolume failed with error")
//err = testutils.ForceChmod(instance, filepath.Join("/tmp/", volName), "777")
//Expect(err).To(BeNil(), "Chmod failed with error")
testFileContents := "test"

if !readOnly {
// Write a file
testFileContents := []byte("sample content")
testFile := filepath.Join(publishDir, "testfile")
//err = testutils.WriteFile(instance, testFile, testFileContents)
err := ioutil.WriteFile(testFile, []byte(testFileContents), 0644)
err := ioutil.WriteFile(testFile, testFileContents, 0644)
Expect(err).To(BeNil(), "Failed to write file")
// Read the file and check if content is correct
data, err := ioutil.ReadFile(testFile)
Expect(err).To(BeNil(), "Failed to read file")
Expect(data).To(Equal(testFileContents), "File content is incorrect")
}

// Unmount Disk
Expand All @@ -177,28 +164,40 @@ func testAttachWriteReadDetach(volumeID, volName, nodeID string, readOnly bool)

}

func waitForVolumes(params *ec2.DescribeVolumesInput, nVolumes int) {
func waitForVolumeState(volumeID, state string) {
backoff := wait.Backoff{
Duration: 60 * time.Second,
Factor: 1.2,
Steps: 21,
Duration: 1 * time.Second,
Factor: 1.8,
Steps: 13,
}
verifyVolumeFunc := func() (bool, error) {
params := &ec2.DescribeVolumesInput{
VolumeIds: []*string{aws.String(volumeID)},
}
volumes, err := describeVolumes(params)
if err != nil {
return false, err
}
if len(volumes) != nVolumes {
if len(volumes) != 1 {
return false, fmt.Errorf("expected 1 volume, got %d", len(volumes))
}
if aws.StringValue(volumes[0].State) != state {
return false, nil
}
// We need to check the atachment state when the volume is "in-use",
// as it might still be "attaching" rather than "attached".
if state == "in-use" {
if aws.StringValue(volumes[0].Attachments[0].State) != "attached" {
return false, nil
}
}
return true, nil

}
waitErr := wait.ExponentialBackoff(backoff, verifyVolumeFunc)
Expect(waitErr).To(BeNil(), "Timeout error when looking for volume: %v", waitErr)
Expect(waitErr).To(BeNil(), "Timeout error waiting for volume state %q: %v", waitErr, state)
}

func waitForVolumeState(volumeID, state string) {
func waitForVolumes(volumeID string, nVolumes int) {
backoff := wait.Backoff{
Duration: 1 * time.Second,
Factor: 1.8,
Expand All @@ -208,23 +207,24 @@ func waitForVolumeState(volumeID, state string) {
params := &ec2.DescribeVolumesInput{
VolumeIds: []*string{aws.String(volumeID)},
}
// FIXME: for some reason this always returns the same state
volumes, err := describeVolumes(params)
if err != nil {
if nVolumes == 0 {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "InvalidVolume.NotFound" {
return true, nil
}
}
}
return false, err
}
if len(volumes) < 1 {
return false, fmt.Errorf("expected 1 valid volume, got nothing")
}
for _, attachment := range volumes[0].Attachments {
if aws.StringValue(attachment.State) == state {
return true, nil
}
if len(volumes) != nVolumes {
return false, nil
}
return false, nil
return true, nil
}
waitErr := wait.ExponentialBackoff(backoff, verifyVolumeFunc)
Expect(waitErr).To(BeNil(), "Timeout error waiting for volume state %q: %v", waitErr, state)
Expect(waitErr).To(BeNil(), "Timeout error when looking for volume %q: %v", volumeID, waitErr)
}

func describeVolumes(params *ec2.DescribeVolumesInput) ([]*ec2.Volume, error) {
Expand Down

0 comments on commit 4917df0

Please sign in to comment.