diff --git a/test/e2e/basic/pvc-selected-node-changing.go b/test/e2e/basic/pvc-selected-node-changing.go index d26350f2b..71b125f9b 100644 --- a/test/e2e/basic/pvc-selected-node-changing.go +++ b/test/e2e/basic/pvc-selected-node-changing.go @@ -135,7 +135,7 @@ func (p *PVCSelectedNodeChanging) Restore() error { } func (p *PVCSelectedNodeChanging) Verify() error { By(fmt.Sprintf("PVC selected node should be %s", p.newNodeName), func() { - pvcNameList, err := GetPvcByPodName(p.Ctx, p.mappedNS, p.pvcName) + pvcNameList, err := GetPvcByPVCName(p.Ctx, p.mappedNS, p.pvcName) Expect(err).To(Succeed()) Expect(len(pvcNameList)).Should(Equal(1)) pvc, err := GetPVC(p.Ctx, p.Client, p.mappedNS, pvcNameList[0]) diff --git a/test/e2e/basic/storage-class-changing.go b/test/e2e/basic/storage-class-changing.go index 6255f8e65..43b9cbb5a 100644 --- a/test/e2e/basic/storage-class-changing.go +++ b/test/e2e/basic/storage-class-changing.go @@ -24,9 +24,11 @@ type StorageClasssChanging struct { namespace string srcStorageClass string desStorageClass string + pvcName string volume string podName string mappedNS string + deploymentName string } const SCCBaseName string = "scc-" @@ -55,6 +57,7 @@ func (s *StorageClasssChanging) Init() error { s.data = map[string]string{s.srcStorageClass: s.desStorageClass} s.configmaptName = "change-storage-class-config" s.volume = "volume-1" + s.pvcName = fmt.Sprintf("pvc-%s", s.volume) s.podName = "pod-1" s.BackupArgs = []string{ "create", "--namespace", VeleroCfg.VeleroNamespace, "backup", s.BackupName, @@ -68,6 +71,9 @@ func (s *StorageClasssChanging) Init() error { return nil } func (s *StorageClasssChanging) CreateResources() error { + label := map[string]string{ + "app": "test", + } s.Ctx, s.CtxCancel = context.WithTimeout(context.Background(), 10*time.Minute) By(fmt.Sprintf("Create a storage class %s", s.desStorageClass), func() { Expect(InstallStorageClass(s.Ctx, fmt.Sprintf("../testdata/storage-class/%s.yaml", @@ -78,10 +84,20 @@ func (s *StorageClasssChanging) CreateResources() error { fmt.Sprintf("Failed to create namespace %s", s.namespace)) }) - By(fmt.Sprintf("Create pod %s in namespace %s", s.podName, s.namespace), func() { - _, err := CreatePod(s.Client, s.namespace, s.podName, s.srcStorageClass, "", []string{s.volume}, nil, nil) + By(fmt.Sprintf("Create a deployment in namespace %s", s.VeleroCfg.VeleroNamespace), func() { + + pvc, err := CreatePVC(s.Client, s.namespace, s.pvcName, s.srcStorageClass, nil) + Expect(err).To(Succeed()) + vols := CreateVolumes(pvc.Name, []string{s.volume}) + + deployment := NewDeployment(s.CaseBaseName, s.namespace, 1, label, nil).WithVolume(vols).Result() + deployment, err = CreateDeployment(s.Client.ClientGo, s.namespace, deployment) + Expect(err).To(Succeed()) + s.deploymentName = deployment.Name + err = WaitForReadyDeployment(s.Client.ClientGo, s.namespace, s.deploymentName) Expect(err).To(Succeed()) }) + By(fmt.Sprintf("Create ConfigMap %s in namespace %s", s.configmaptName, s.VeleroCfg.VeleroNamespace), func() { _, err := CreateConfigMap(s.Client.ClientGo, s.VeleroCfg.VeleroNamespace, s.configmaptName, s.labels, s.data) Expect(err).To(Succeed(), fmt.Sprintf("failed to create configmap in the namespace %q", s.VeleroCfg.VeleroNamespace)) @@ -91,19 +107,14 @@ func (s *StorageClasssChanging) CreateResources() error { func (s *StorageClasssChanging) Destroy() error { By(fmt.Sprintf("Expect storage class of PV %s to be %s ", s.volume, s.srcStorageClass), func() { - pvName, err := GetPVByPodName(s.Client, s.namespace, s.volume) - Expect(err).To(Succeed(), fmt.Sprintf("Failed to get PV name by pod name %s", s.podName)) + pvName, err := GetPVByPVCName(s.Client, s.namespace, s.pvcName) + Expect(err).To(Succeed(), fmt.Sprintf("Failed to get PV name by PVC name %s", s.pvcName)) pv, err := GetPersistentVolume(s.Ctx, s.Client, s.namespace, pvName) - Expect(err).To(Succeed(), fmt.Sprintf("Failed to get PV by pod name %s", s.podName)) - fmt.Println(pv) + Expect(err).To(Succeed(), fmt.Sprintf("Failed to get PV by name %s", pvName)) Expect(pv.Spec.StorageClassName).To(Equal(s.srcStorageClass), fmt.Sprintf("PV storage %s is not as expected %s", pv.Spec.StorageClassName, s.srcStorageClass)) }) - By(fmt.Sprintf("Start to destroy namespace %s......", s.CaseBaseName), func() { - Expect(CleanupNamespacesWithPoll(s.Ctx, s.Client, s.CaseBaseName)).To(Succeed(), - fmt.Sprintf("Failed to delete namespace %s", s.CaseBaseName)) - }) return nil } @@ -123,12 +134,11 @@ func (s *StorageClasssChanging) Restore() error { } func (s *StorageClasssChanging) Verify() error { By(fmt.Sprintf("Expect storage class of PV %s to be %s ", s.volume, s.desStorageClass), func() { - Expect(WaitForPods(s.Ctx, s.Client, s.mappedNS, []string{s.podName})).To(Succeed(), fmt.Sprintf("Failed to wait pod ready %s", s.podName)) - pvName, err := GetPVByPodName(s.Client, s.mappedNS, s.volume) + Expect(WaitForReadyDeployment(s.Client.ClientGo, s.mappedNS, s.deploymentName)).To(Succeed()) + pvName, err := GetPVByPVCName(s.Client, s.mappedNS, s.pvcName) Expect(err).To(Succeed(), fmt.Sprintf("Failed to get PV name by pod name %s", s.podName)) pv, err := GetPersistentVolume(s.Ctx, s.Client, s.mappedNS, pvName) Expect(err).To(Succeed(), fmt.Sprintf("Failed to get PV by pod name %s", s.podName)) - fmt.Println(pv) Expect(pv.Spec.StorageClassName).To(Equal(s.desStorageClass), fmt.Sprintf("PV storage %s is not as expected %s", pv.Spec.StorageClassName, s.desStorageClass)) }) @@ -137,6 +147,10 @@ func (s *StorageClasssChanging) Verify() error { func (s *StorageClasssChanging) Clean() error { if !s.VeleroCfg.Debug { + By(fmt.Sprintf("Start to destroy namespace %s......", s.CaseBaseName), func() { + Expect(CleanupNamespacesWithPoll(s.Ctx, s.Client, s.CaseBaseName)).To(Succeed(), + fmt.Sprintf("Failed to delete namespace %s", s.CaseBaseName)) + }) DeleteConfigmap(s.Client.ClientGo, s.VeleroCfg.VeleroNamespace, s.configmaptName) DeleteStorageClass(s.Ctx, s.Client, s.desStorageClass) s.TestCase.Clean() diff --git a/test/e2e/bsl-mgmt/deletion.go b/test/e2e/bsl-mgmt/deletion.go index f7576ff4b..602f1b3ab 100644 --- a/test/e2e/bsl-mgmt/deletion.go +++ b/test/e2e/bsl-mgmt/deletion.go @@ -160,12 +160,12 @@ func BslDeletionTest(useVolumeSnapshots bool) { }) By("Get all 2 PVCs of Kibishii and label them seprately ", func() { - pvc, err := GetPvcByPodName(context.Background(), bslDeletionTestNs, podName_1) + pvc, err := GetPvcByPVCName(context.Background(), bslDeletionTestNs, podName_1) Expect(err).To(Succeed()) fmt.Println(pvc) Expect(len(pvc)).To(Equal(1)) pvc1 := pvc[0] - pvc, err = GetPvcByPodName(context.Background(), bslDeletionTestNs, podName_2) + pvc, err = GetPvcByPVCName(context.Background(), bslDeletionTestNs, podName_2) Expect(err).To(Succeed()) fmt.Println(pvc) Expect(len(pvc)).To(Equal(1)) diff --git a/test/util/csi/common.go b/test/util/csi/common.go index 9c2a2d5b5..e96e865b0 100644 --- a/test/util/csi/common.go +++ b/test/util/csi/common.go @@ -129,7 +129,7 @@ func GetCsiSnapshotHandleV1(client TestClient, backupName string) ([]string, err return snapshotHandleList, nil } func GetVolumeSnapshotContentNameByPod(client TestClient, podName, namespace, backupName string) (string, error) { - pvcList, err := GetPvcByPodName(context.Background(), namespace, podName) + pvcList, err := GetPvcByPVCName(context.Background(), namespace, podName) if err != nil { return "", err } diff --git a/test/util/k8s/common.go b/test/util/k8s/common.go index 7efb56add..80e05c6a5 100644 --- a/test/util/k8s/common.go +++ b/test/util/k8s/common.go @@ -81,7 +81,7 @@ func WaitForPods(ctx context.Context, client TestClient, namespace string, pods return nil } -func GetPvcByPodName(ctx context.Context, namespace, podName string) ([]string, error) { +func GetPvcByPVCName(ctx context.Context, namespace, pvcName string) ([]string, error) { // Example: // NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE // kibishii-data-kibishii-deployment-0 Bound pvc-94b9fdf2-c30f-4a7b-87bf-06eadca0d5b6 1Gi RWO kibishii-storage-class 115s @@ -94,7 +94,7 @@ func GetPvcByPodName(ctx context.Context, namespace, podName string) ([]string, cmd = &common.OsCommandLine{ Cmd: "grep", - Args: []string{podName}, + Args: []string{pvcName}, } cmds = append(cmds, cmd) @@ -233,20 +233,20 @@ func GetAPIVersions(client *TestClient, name string) ([]string, error) { return nil, errors.New("Server API groups is empty") } -func GetPVByPodName(client TestClient, namespace, podName string) (string, error) { - pvcList, err := GetPvcByPodName(context.Background(), namespace, podName) +func GetPVByPVCName(client TestClient, namespace, pvcName string) (string, error) { + pvcList, err := GetPvcByPVCName(context.Background(), namespace, pvcName) if err != nil { return "", err } if len(pvcList) != 1 { - return "", errors.New(fmt.Sprintf("Only 1 PVC of pod %s should be found under namespace %s but got %v", podName, namespace, pvcList)) + return "", errors.New(fmt.Sprintf("Only 1 PVC of pod %s should be found under namespace %s but got %v", pvcName, namespace, pvcList)) } pvList, err := GetPvByPvc(context.Background(), namespace, pvcList[0]) if err != nil { return "", err } if len(pvList) != 1 { - return "", errors.New(fmt.Sprintf("Only 1 PV of PVC %s pod %s should be found under namespace %s", pvcList[0], podName, namespace)) + return "", errors.New(fmt.Sprintf("Only 1 PV of PVC %s pod %s should be found under namespace %s", pvcList[0], pvcName, namespace)) } pv_value, err := GetPersistentVolume(context.Background(), client, "", pvList[0]) fmt.Println(pv_value.Annotations["pv.kubernetes.io/provisioned-by"]) @@ -347,3 +347,19 @@ func GetAllService(ctx context.Context) (string, error) { } return stdout, nil } + +func CreateVolumes(pvcName string, volumeNameList []string) (vols []*corev1.Volume) { + vols = []*corev1.Volume{} + for _, volume := range volumeNameList { + vols = append(vols, &corev1.Volume{ + Name: volume, + VolumeSource: corev1.VolumeSource{ + PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ + ClaimName: pvcName, + ReadOnly: false, + }, + }, + }) + } + return +} diff --git a/test/util/k8s/deployment.go b/test/util/k8s/deployment.go index 469919bec..15a5d372f 100644 --- a/test/util/k8s/deployment.go +++ b/test/util/k8s/deployment.go @@ -105,19 +105,19 @@ func NewDeployment(name, ns string, replicas int32, labels map[string]string, co } } -func (d *DeploymentBuilder) WithVolume(vols []*v1.Volume) *DeploymentBuilder { +func (d *DeploymentBuilder) WithVolume(volumes []*v1.Volume) *DeploymentBuilder { vmList := []v1.VolumeMount{} - for i, v := range vols { + for _, v := range volumes { vmList = append(vmList, v1.VolumeMount{ Name: v.Name, MountPath: "/" + v.Name, }) - d.Spec.Template.Spec.Volumes = append(d.Spec.Template.Spec.Volumes, *vols[i]) + d.Spec.Template.Spec.Volumes = append(d.Spec.Template.Spec.Volumes, *v) + } // NOTE here just mount volumes to the first container d.Spec.Template.Spec.Containers[0].VolumeMounts = vmList - return d }