Skip to content

Commit

Permalink
Refactor E2E: Extract resize-related helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSirenko committed Nov 24, 2023
1 parent 246706f commit 74108d8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,9 @@ limitations under the License.
package testsuites

import (
"context"
"fmt"
"time"

"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/util"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/test/e2e/framework"

. "github.com/onsi/ginkgo/v2"
v1 "k8s.io/api/core/v1"
clientset "k8s.io/client-go/kubernetes"
)

Expand All @@ -45,9 +36,10 @@ func (t *DynamicallyProvisionedResizeVolumeTest) Run(client clientset.Interface,
tpvc, _ := volume.SetupDynamicPersistentVolumeClaim(client, namespace, t.CSIDriver)
defer tpvc.Cleanup()

ResizeTestPvc(client, namespace, tpvc, 1)
By("resizing the volume")
ResizeTestPvc(client, namespace, tpvc, DefaultSizeIncreaseGi)

By("Validate volume can be attached")
By("validate volume can be attached")
tpod := NewTestPod(client, namespace, t.Pod.Cmd)

tpod.SetupVolume(tpvc.persistentVolumeClaim, volume.VolumeMount.NameGenerate+"1", volume.VolumeMount.MountPathGenerate+"1", volume.VolumeMount.ReadOnly)
Expand All @@ -58,44 +50,4 @@ func (t *DynamicallyProvisionedResizeVolumeTest) Run(client clientset.Interface,
tpod.WaitForSuccess()

defer tpod.Cleanup()

}

// WaitForPvToResize waiting for pvc size to be resized to desired size
func WaitForPvToResize(c clientset.Interface, ns *v1.Namespace, pvName string, desiredSize resource.Quantity, timeout time.Duration, interval time.Duration) error {
By(fmt.Sprintf("Waiting up to %v for pv in namespace %q to be complete", timeout, ns.Name))
for start := time.Now(); time.Since(start) < timeout; time.Sleep(interval) {
newPv, _ := c.CoreV1().PersistentVolumes().Get(context.TODO(), pvName, metav1.GetOptions{})
newPvSize := newPv.Spec.Capacity["storage"]
if desiredSize.Equal(newPvSize) {
By(fmt.Sprintf("Pv size is updated to %v", newPvSize.String()))
return nil
}
}
return fmt.Errorf("Gave up after waiting %v for pv %q to complete resizing", timeout, pvName)
}

// ResizeTestPvc increases size of given persistent volume claim by `sizeIncreaseGi` Gigabytes
func ResizeTestPvc(client clientset.Interface, namespace *v1.Namespace, testPvc *TestPersistentVolumeClaim, sizeIncreaseGi int64) (updatedPvc *v1.PersistentVolumeClaim, updatedSize resource.Quantity) {
By(fmt.Sprintf("getting pvc name: %v", testPvc.persistentVolumeClaim.Name))
pvc, _ := client.CoreV1().PersistentVolumeClaims(namespace.Name).Get(context.TODO(), testPvc.persistentVolumeClaim.Name, metav1.GetOptions{})

originalSize := pvc.Spec.Resources.Requests["storage"]
delta := resource.Quantity{}
delta.Set(util.GiBToBytes(sizeIncreaseGi))
originalSize.Add(delta)
pvc.Spec.Resources.Requests["storage"] = originalSize

By("resizing the pvc")
updatedPvc, err := client.CoreV1().PersistentVolumeClaims(namespace.Name).Update(context.TODO(), pvc, metav1.UpdateOptions{})
if err != nil {
framework.ExpectNoError(err, fmt.Sprintf("fail to resize pvc(%s): %v", pvc.Name, err))
}
updatedSize = updatedPvc.Spec.Resources.Requests["storage"]

By("checking the resizing PV result")
err = WaitForPvToResize(client, namespace, updatedPvc.Spec.VolumeName, updatedSize, 1*time.Minute, 5*time.Second)
framework.ExpectNoError(err)

return
}
61 changes: 60 additions & 1 deletion tests/e2e/testsuites/e2e_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,78 @@ limitations under the License.

package testsuites

import "fmt"
import (
"context"
"fmt"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/util"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/kubernetes/test/e2e/framework"
"time"
)

const (
DefaultVolumeName = "test-volume-1"
DefaultMountPath = "/mnt/default-mount"

DefaultE2EIopsIoVolumes = "100"

DefaultSizeIncreaseGi = 1

DefaultResizeTimout = 1 * time.Minute
DefaultK8sApiPollingInterval = 5 * time.Second
)

func PodCmdWriteToVolume(volumeMountPath string) string {
return fmt.Sprintf("echo 'hello world' >> %s/data && grep 'hello world' %s/data && sync", volumeMountPath, volumeMountPath)
}

// IncreasePvcObjectStorage increases `storage` of a K8s PVC object by `sizeIncreaseGi`
func IncreasePvcObjectStorage(pvc *v1.PersistentVolumeClaim, sizeIncreaseGi int64) resource.Quantity {
pvcSize := pvc.Spec.Resources.Requests["storage"]
delta := resource.Quantity{}
delta.Set(util.GiBToBytes(sizeIncreaseGi))
pvcSize.Add(delta)
pvc.Spec.Resources.Requests["storage"] = pvcSize
return pvcSize
}

// WaitForPvToResize waiting for pvc size to be resized to desired size
func WaitForPvToResize(c clientset.Interface, ns *v1.Namespace, pvName string, desiredSize resource.Quantity, timeout time.Duration, interval time.Duration) error {
framework.Logf("waiting up to %v for pv resize in namespace %q to be complete", timeout, ns.Name)
for start := time.Now(); time.Since(start) < timeout; time.Sleep(interval) {
newPv, _ := c.CoreV1().PersistentVolumes().Get(context.TODO(), pvName, metav1.GetOptions{})
newPvSize := newPv.Spec.Capacity["storage"]
if desiredSize.Equal(newPvSize) {
framework.Logf("pv size is updated to %v", newPvSize.String())
return nil
}
}
return fmt.Errorf("gave up after waiting %v for pv %q to complete resizing", timeout, pvName)
}

// ResizeTestPvc increases size of given `TestPersistentVolumeClaim` by `sizeIncreaseGi` Gigabytes
func ResizeTestPvc(client clientset.Interface, namespace *v1.Namespace, testPvc *TestPersistentVolumeClaim, sizeIncreaseGi int64) (updatedSize resource.Quantity) {
framework.Logf("getting pvc name: %v", testPvc.persistentVolumeClaim.Name)
pvc, _ := client.CoreV1().PersistentVolumeClaims(namespace.Name).Get(context.TODO(), testPvc.persistentVolumeClaim.Name, metav1.GetOptions{})

IncreasePvcObjectStorage(pvc, sizeIncreaseGi)

framework.Logf("updating the pvc object")
updatedPvc, err := client.CoreV1().PersistentVolumeClaims(namespace.Name).Update(context.TODO(), pvc, metav1.UpdateOptions{})
if err != nil {
framework.ExpectNoError(err, fmt.Sprintf("fail to resize pvc(%s): %v", pvc.Name, err))
}
updatedSize = updatedPvc.Spec.Resources.Requests["storage"]

framework.Logf("checking the resizing PV result")
err = WaitForPvToResize(client, namespace, updatedPvc.Spec.VolumeName, updatedSize, DefaultResizeTimout, DefaultK8sApiPollingInterval)
framework.ExpectNoError(err)
return updatedSize
}

func CreateVolumeDetails(createVolumeParameters map[string]string, volumeSize string) *VolumeDetails {
allowVolumeExpansion := true

Expand Down
6 changes: 1 addition & 5 deletions tests/e2e/testsuites/format_options_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ type FormatOptionTest struct {
CreateVolumeParameters map[string]string
}

const (
volumeSizeIncreaseAmtGi = 1
)

func (t *FormatOptionTest) Run(client clientset.Interface, namespace *v1.Namespace, ebsDriver driver.PVTestDriver) {
By("setting up pvc with custom format option")
volumeDetails := CreateVolumeDetails(t.CreateVolumeParameters, driver.MinimumSizeForVolumeType(t.CreateVolumeParameters[ebscsidriver.VolumeTypeKey]))
Expand All @@ -44,7 +40,7 @@ func (t *FormatOptionTest) Run(client clientset.Interface, namespace *v1.Namespa
formatOptionMountPod.WaitForSuccess()

By("testing that pvc is able to be resized")
ResizeTestPvc(client, namespace, testPvc, volumeSizeIncreaseAmtGi)
ResizeTestPvc(client, namespace, testPvc, DefaultSizeIncreaseGi)

By("validating resized pvc by deploying new pod")
resizeTestPod := createPodWithVolume(client, namespace, PodCmdWriteToVolume(DefaultMountPath), testPvc, volumeDetails)
Expand Down

0 comments on commit 74108d8

Please sign in to comment.