-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: to check the status of pvc as bound
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
1 parent
4bb8d1d
commit 6ab87b2
Showing
6 changed files
with
226 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
}) | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |