Skip to content

Commit

Permalink
test: to check the status of pvc as bound
Browse files Browse the repository at this point in the history
this test creates pvc and pod and verifies the status of pvc to be bound, and deletes them.

Signed-off-by: riya-singhal31 <[email protected]>
  • Loading branch information
riya-singhal31 committed Apr 25, 2022
1 parent 4bb8d1d commit 973cbac
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 11 deletions.
52 changes: 52 additions & 0 deletions e2e/common.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,64 @@
package e2e

import (
"context"
"fmt"

"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
k8sv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
)

//nolint:errcheck
func debug(msg string, args ...interface{}) {
ginkgo.GinkgoWriter.Write([]byte(fmt.Sprintf(msg, args...)))
}

// WaitForPVCBound waits for a pvc with a given name and namespace to reach BOUND phase.
func WaitForPVCBound(pvc *k8sv1.PersistentVolumeClaim, pod *k8sv1.Pod, pvcname string, podname string, volumemode k8sv1.PersistentVolumeMode) error {
pvc = GetSamplePVC(StorageClass, "5Gi", pvcname, volumemode)
pod = GetSamplePod(podname, pvcname)

err := DeployManagerObj.GetCrClient().Create(context.TODO(), pvc)
if err != nil && !errors.IsAlreadyExists(err) {
return err
}

err = DeployManagerObj.GetCrClient().Create(context.TODO(), pod)
if err != nil && !errors.IsAlreadyExists(err) {
return err
}

Eventually(func() bool {
err := DeployManagerObj.GetCrClient().Get(context.TODO(), types.NamespacedName{Name: pvc.Name, Namespace: pvc.Namespace}, pvc)
return err == nil
}, timeout, interval).Should(BeTrue())
debug("PVC %s is created\n", pvc.Name)

Eventually(func() bool {
err = DeployManagerObj.GetCrClient().Get(context.TODO(), types.NamespacedName{Name: pvc.Name, Namespace: pvc.Namespace}, pvc)
return pvc.Status.Phase == k8sv1.ClaimBound
}, timeout, interval).Should(BeTrue())
debug("PVC %s is bound\n", pvc.Name)

Eventually(func() bool {
err = DeployManagerObj.GetCrClient().Get(context.TODO(), types.NamespacedName{Name: pod.Name, Namespace: pod.Namespace}, pod)
return err == nil && pod.Status.Phase == k8sv1.PodRunning
}, timeout, interval).Should(BeTrue())
debug("Pod %s is running\n", pod.Name)

err = DeployManagerObj.GetCrClient().Delete(context.TODO(), pod)
if err != nil {
return err
}
debug("Pod %s is deleted\n", pod.Name)

err = DeployManagerObj.GetCrClient().Delete(context.TODO(), pvc)
if err != nil {
return err
}
debug("PVC %s is deleted\n", pvc.Name)
return nil
}
2 changes: 2 additions & 0 deletions e2e/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var DeployManagerObj *deploymanager.DeployManager
// SuiteFailed indicates whether any test in the current suite has failed
var SuiteFailed = false

const StorageClass = "odf-lvm-vg1"

func init() {
flag.StringVar(&LvmCatalogSourceImage, "lvm-catalog-image", "", "The LVM CatalogSource container image to use in the deployment")
flag.StringVar(&LvmSubscriptionChannel, "lvm-subscription-channel", "", "The subscription channel to revise updates from")
Expand Down
11 changes: 0 additions & 11 deletions e2e/lvm/lvm_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

tests "github.com/red-hat-storage/lvm-operator/e2e"
)

Expand All @@ -23,13 +22,3 @@ var _ = BeforeSuite(func() {
var _ = AfterSuite(func() {
tests.AfterTestSuiteCleanup()
})

// Test to validate all the resources created by LVMO.
var _ = Describe("Validation test", func() {
Context("Validate LVMCluster reconciliation", func() {
It("Should validate LVMCluster reconciliation", func() {
err := tests.ValidateResources()
Expect(err).To(BeNil())
})
})
})
17 changes: 17 additions & 0 deletions e2e/lvm/lvmcluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package lvm_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
tests "github.com/red-hat-storage/lvm-operator/e2e"
)

// Test to validate all the resources created by LVMO.
var _ = Describe("Validation test", func() {
Context("Validate LVMCluster reconciliation", func() {
It("Should validate LVMCluster reconciliation", func() {
err := tests.ValidateResources()
Expect(err).To(BeNil())
})
})
})
32 changes: 32 additions & 0 deletions e2e/lvm/pvc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package lvm_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
tests "github.com/red-hat-storage/lvm-operator/e2e"
k8sv1 "k8s.io/api/core/v1"
)

// Test to verify the PVC status
var _ = Describe("PVC Status check", VerifyPVCStatus)

func VerifyPVCStatus() {

Describe("pvc", func() {
var filepvc *k8sv1.PersistentVolumeClaim
var filepod *k8sv1.Pod
var blockpvc *k8sv1.PersistentVolumeClaim
var blockpod *k8sv1.Pod

Context("create pvc and pod", func() {
It("Verifies the status", func() {
By("PVC Should be bound and Pod should be running")
err := tests.WaitForPVCBound(filepvc, filepod, "lvmfilepvc", "lvmfilepod", k8sv1.PersistentVolumeFilesystem)
Expect(err).To(BeNil())
err = tests.WaitForPVCBound(blockpvc, blockpod, "lvmblockpvc", "lvmblockpod", k8sv1.PersistentVolumeBlock)
Expect(err).To(BeNil())
})
})

})
}
63 changes: 63 additions & 0 deletions e2e/testdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package e2e

import (
k8sv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// GetSamplePVC returns a sample pvc.
func GetSamplePVC(storageClass, quantity, name string, volumemode k8sv1.PersistentVolumeMode) *k8sv1.PersistentVolumeClaim {
pvc := &k8sv1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: TestNamespace,
},
Spec: k8sv1.PersistentVolumeClaimSpec{
StorageClassName: &storageClass,
AccessModes: []k8sv1.PersistentVolumeAccessMode{k8sv1.ReadWriteOnce},
VolumeMode: &volumemode,
Resources: k8sv1.ResourceRequirements{
Requests: k8sv1.ResourceList{
k8sv1.ResourceStorage: resource.MustParse(quantity),
},
},
},
}
return pvc
}

// GetSamplePod returns a sample pod.
func GetSamplePod(name, pvcName string) *k8sv1.Pod {
pod := &k8sv1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: TestNamespace,
},
Spec: k8sv1.PodSpec{
Volumes: []k8sv1.Volume{
{
Name: "storage",
VolumeSource: k8sv1.VolumeSource{
PersistentVolumeClaim: &k8sv1.PersistentVolumeClaimVolumeSource{
ClaimName: pvcName,
},
},
},
},
Containers: []k8sv1.Container{
{
Name: "container",
Image: "public.ecr.aws/docker/library/nginx:latest",
Ports: []k8sv1.ContainerPort{
{
Name: "http-server",
ContainerPort: 80,
},
},
},
},
},
}
return pod
}

0 comments on commit 973cbac

Please sign in to comment.