Files
velero/test/e2e/basic/pvc-selected-node-changing.go
danfengl 82fc557bd1 Add checkpoint of VSC for data movement migration test
1. In data movement scenario, volumesnapshotcontent by Velero backup will be deleted instead of retained in CSI scenaito, so add
a checkpoint for data movement scenario to verify no volumesnapshotcontent left after Velero backup;

2. Fix global context varaible issue, context varaible is not effective due to it's initialized right after the very beginning of
all tests instead of beginning of each test, so if someone script a new E2E test and did not overwrite it in the test body, then it
will fail the test if it was triggerd one hour later;

3. Due to CSI plugin is deprecated, it breaked down migration tests, because v1.13 still needs to install CSI plugin for the test.

Signed-off-by: danfengl <danfengl@vmware.com>
2024-04-28 09:47:08 +00:00

160 lines
5.5 KiB
Go

package basic
import (
"context"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
. "github.com/vmware-tanzu/velero/test"
. "github.com/vmware-tanzu/velero/test/e2e/test"
. "github.com/vmware-tanzu/velero/test/util/k8s"
. "github.com/vmware-tanzu/velero/test/util/velero"
)
type PVCSelectedNodeChanging struct {
TestCase
labels map[string]string
data map[string]string
configmaptName string
namespace string
oldNodeName string
newNodeName string
volume string
podName string
mappedNS string
pvcName string
ann string
}
var PVCSelectedNodeChangingTest func() = TestFunc(&PVCSelectedNodeChanging{})
func (p *PVCSelectedNodeChanging) Init() error {
p.TestCase.Init()
p.CaseBaseName = "psnc-" + p.UUIDgen
p.namespace = p.CaseBaseName
p.mappedNS = p.namespace + "-mapped"
p.TestMsg = &TestMSG{
Desc: "Changing PVC node selector",
FailedMSG: "Failed to changing PVC node selector",
Text: "Change node selectors of persistent volume claims during restores",
}
p.BackupName = "backup-" + p.CaseBaseName
p.RestoreName = "restore-" + p.CaseBaseName
p.labels = map[string]string{"velero.io/plugin-config": "",
"velero.io/change-pvc-node-selector": "RestoreItemAction"}
p.configmaptName = "change-pvc-node-selector-config"
p.volume = "volume-1"
p.podName = "pod-1"
p.pvcName = "pvc-1"
p.ann = "volume.kubernetes.io/selected-node"
p.BackupArgs = []string{
"create", "--namespace", p.VeleroCfg.VeleroNamespace, "backup", p.BackupName,
"--include-namespaces", p.namespace,
"--snapshot-volumes=false", "--wait",
}
p.RestoreArgs = []string{
"create", "--namespace", p.VeleroCfg.VeleroNamespace, "restore", p.RestoreName,
"--from-backup", p.BackupName, "--namespace-mappings", fmt.Sprintf("%s:%s", p.namespace, p.mappedNS), "--wait",
}
return nil
}
func (p *PVCSelectedNodeChanging) CreateResources() error {
By(fmt.Sprintf("Create namespace %s", p.namespace), func() {
Expect(CreateNamespace(p.Ctx, p.Client, p.namespace)).To(Succeed(),
fmt.Sprintf("Failed to create namespace %s", p.namespace))
})
By(fmt.Sprintf("Create a storage class %s.", StorageClassName), func() {
Expect(InstallStorageClass(context.Background(), fmt.Sprintf("../testdata/storage-class/%s.yaml", p.VeleroCfg.CloudProvider))).To(Succeed())
})
By(fmt.Sprintf("Create a storage class %s.", StorageClassName), func() {
Expect(InstallTestStorageClasses(fmt.Sprintf("../testdata/storage-class/%s.yaml", p.VeleroCfg.CloudProvider))).To(Succeed(), "Failed to install storage class")
})
By(fmt.Sprintf("Create pod %s in namespace %s", p.podName, p.namespace), func() {
nodeNameList, err := GetWorkerNodes(p.Ctx)
Expect(err).To(Succeed())
for _, nodeName := range nodeNameList {
p.oldNodeName = nodeName
fmt.Printf("Create PVC on node %s\n", p.oldNodeName)
pvcAnn := map[string]string{p.ann: nodeName}
_, err := CreatePod(p.Client, p.namespace, p.podName, StorageClassName, p.pvcName, []string{p.volume}, pvcAnn, nil)
Expect(err).To(Succeed())
err = WaitForPods(p.Ctx, p.Client, p.namespace, []string{p.podName})
Expect(err).To(Succeed())
break
}
})
By("Prepare ConfigMap data", func() {
nodeNameList, err := GetWorkerNodes(p.Ctx)
Expect(err).To(Succeed())
Expect(len(nodeNameList)).To(BeNumerically(">=", 2))
for _, nodeName := range nodeNameList {
if nodeName != p.oldNodeName {
p.newNodeName = nodeName
break
}
}
p.data = map[string]string{p.oldNodeName: p.newNodeName}
})
By(fmt.Sprintf("Create ConfigMap %s in namespace %s", p.configmaptName, p.VeleroCfg.VeleroNamespace), func() {
cm, err := CreateConfigMap(p.Client.ClientGo, p.VeleroCfg.VeleroNamespace, p.configmaptName, p.labels, p.data)
Expect(err).To(Succeed(), fmt.Sprintf("failed to create configmap in the namespace %q", p.VeleroCfg.VeleroNamespace))
fmt.Printf("Configmap: %v", cm)
})
return nil
}
func (p *PVCSelectedNodeChanging) Destroy() error {
By(fmt.Sprintf("Start to destroy namespace %s......", p.CaseBaseName), func() {
Expect(CleanupNamespacesWithPoll(p.Ctx, p.Client, p.CaseBaseName)).To(Succeed(),
fmt.Sprintf("Failed to delete namespace %s", p.CaseBaseName))
})
return nil
}
func (p *PVCSelectedNodeChanging) Restore() error {
By(fmt.Sprintf("Start to restore %s .....", p.RestoreName), func() {
Expect(VeleroRestoreExec(p.Ctx, p.VeleroCfg.VeleroCLI,
p.VeleroCfg.VeleroNamespace, p.RestoreName,
p.RestoreArgs, velerov1api.RestorePhaseCompleted)).To(
Succeed(),
func() string {
RunDebug(context.Background(), p.VeleroCfg.VeleroCLI,
p.VeleroCfg.VeleroNamespace, "", p.RestoreName)
return "Fail to restore workload"
})
err := WaitForPods(p.Ctx, p.Client, p.mappedNS, []string{p.podName})
Expect(err).To(Succeed())
})
return nil
}
func (p *PVCSelectedNodeChanging) Verify() error {
By(fmt.Sprintf("PVC selected node should be %s", p.newNodeName), func() {
pvcNameList, err := GetPvcByPVCName(p.Ctx, p.mappedNS, p.pvcName)
Expect(err).To(Succeed())
Expect(pvcNameList).Should(HaveLen(1))
pvc, err := GetPVC(p.Ctx, p.Client, p.mappedNS, pvcNameList[0])
Expect(err).To(Succeed())
Expect(pvc.Annotations[p.ann]).To(Equal(p.newNodeName))
})
return nil
}
func (p *PVCSelectedNodeChanging) Clean() error {
if !p.VeleroCfg.Debug {
p.TestCase.Clean()
By(fmt.Sprintf("Clean namespace with prefix %s after test", p.mappedNS), func() {
CleanupNamespaces(p.Ctx, p.Client, p.mappedNS)
})
}
return nil
}