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 22, 2022
1 parent 4bb8d1d commit 6ab87b2
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 11 deletions.
72 changes: 72 additions & 0 deletions e2e/common.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,84 @@
package e2e

import (
"context"
"fmt"
"time"

"github.com/onsi/ginkgo/v2"
k8sv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilwait "k8s.io/apimachinery/pkg/util/wait"
)

//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, namespace string, pod *k8sv1.Pod) error {
pvc, err := DeployManagerObj.GetK8sClient().CoreV1().PersistentVolumeClaims(namespace).Create(context.TODO(), pvc, metav1.CreateOptions{})
if err != nil && !errors.IsAlreadyExists(err) {
return err
}

_, err = DeployManagerObj.GetK8sClient().CoreV1().Pods(namespace).Create(context.TODO(), pod, metav1.CreateOptions{})
if err != nil && !errors.IsAlreadyExists(err) {
return err
}

lastReason := ""
timeout := 100 * time.Second
interval := 1 * time.Second

// Wait for namespace to terminate
err = utilwait.PollImmediate(interval, timeout, func() (done bool, err error) {
pvc, err = DeployManagerObj.GetK8sClient().CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(context.TODO(), pvc.Name, metav1.GetOptions{})
if err != nil {
lastReason = fmt.Sprintf("error talking to k8s apiserver: %v", err)
return false, nil
}
if pvc.Status.Phase != k8sv1.ClaimBound {
lastReason = fmt.Sprintf("waiting on pvc %s/%s to reach bound state, currently %s", pvc.Namespace, pvc.Name, pvc.Status.Phase)
return false, nil
}

return true, nil
})

if err != nil {
return fmt.Errorf("%v: %s", err, lastReason)
}

return nil
}

// WaitForPodRunning waits for a pod with a given name and namespace to reach running phase.
func WaitForPodRunning(namespace string, pod *k8sv1.Pod) error {
lastReason := ""
timeout := 100 * time.Second
interval := 1 * time.Second

// Wait for namespace to terminate
err := utilwait.PollImmediate(interval, timeout, func() (done bool, err error) {
pod, err = DeployManagerObj.GetK8sClient().CoreV1().Pods(pod.Namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{})
if err != nil {
lastReason = fmt.Sprintf("error talking to k8s apiserver: %v", err)
return false, nil
}
if pod.Status.Phase != k8sv1.PodRunning {
lastReason = fmt.Sprintf("waiting on pod %s/%s to reach running state, currently %s", pod.Namespace, pod.Name, pod.Status.Phase)
return false, nil
}

return true, nil
})

if err != nil {
return fmt.Errorf("%v: %s", err, lastReason)
}

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())
})
})
})
72 changes: 72 additions & 0 deletions e2e/lvm/pvc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package lvm_test

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
tests "github.com/red-hat-storage/lvm-operator/e2e"
k8sv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/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
var namespace string

BeforeEach(func() {
namespace = tests.TestNamespace
filepvc = tests.GetSamplePVC(tests.StorageClass, "5Gi", "lvmfilepvc", k8sv1.PersistentVolumeFilesystem)
filepod = tests.GetSamplePod("lvmfilepod", "lvmfilepvc")
blockpvc = tests.GetSamplePVC(tests.StorageClass, "5Gi", "lvmblockpvc", k8sv1.PersistentVolumeBlock)
blockpod = tests.GetSamplePod("lvmblockpod", "lvmblockpvc")
})

AfterEach(func() {
err := tests.DeployManagerObj.GetK8sClient().CoreV1().Pods(namespace).Delete(context.TODO(), filepod.Name, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
Expect(err).To(BeNil())
}
err = tests.DeployManagerObj.GetK8sClient().CoreV1().PersistentVolumeClaims(namespace).Delete(context.TODO(), filepvc.Name, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
Expect(err).To(BeNil())
}

err = tests.DeployManagerObj.GetK8sClient().CoreV1().Pods(namespace).Delete(context.TODO(), blockpod.Name, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
Expect(err).To(BeNil())
}
err = tests.DeployManagerObj.GetK8sClient().CoreV1().PersistentVolumeClaims(namespace).Delete(context.TODO(), blockpvc.Name, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
Expect(err).To(BeNil())
}

})

Context("create pvc and pod", func() {
It("PVC should be bound and Pod should be running", func() {
By("PVC Should be bound")
err := tests.WaitForPVCBound(filepvc, namespace, filepod)
Expect(err).To(BeNil())
err = tests.WaitForPVCBound(blockpvc, namespace, blockpod)
Expect(err).To(BeNil())

By("Pod should be running")
err = tests.WaitForPodRunning(namespace, filepod)
Expect(err).To(BeNil())
err = tests.WaitForPodRunning(namespace, blockpod)
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 6ab87b2

Please sign in to comment.