diff --git a/test/e2e/basic/pvc-selected-node-changing.go b/test/e2e/basic/pvc-selected-node-changing.go index b0f097362..9fa2594ed 100644 --- a/test/e2e/basic/pvc-selected-node-changing.go +++ b/test/e2e/basic/pvc-selected-node-changing.go @@ -75,6 +75,15 @@ func (p *PVCSelectedNodeChanging) StartRun() error { } func (p *PVCSelectedNodeChanging) CreateResources() error { p.Ctx, _ = context.WithTimeout(context.Background(), 60*time.Minute) + + 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", VeleroCfg.CloudProvider))).To(Succeed(), "Failed to install storage class") + }) + By(fmt.Sprintf("Create namespace %s", p.namespace), func() { Expect(CreateNamespace(context.Background(), p.Client, p.namespace)).To(Succeed(), fmt.Sprintf("Failed to create namespace %s", p.namespace)) @@ -87,7 +96,7 @@ func (p *PVCSelectedNodeChanging) CreateResources() error { 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, "default", p.pvcName, []string{p.volume}, pvcAnn, nil) + _, err := CreatePod(p.Client, p.namespace, p.podName, StorageClassName, p.pvcName, []string{p.volume}, pvcAnn, nil) Expect(err).To(Succeed()) err = WaitForPods(context.Background(), p.Client, p.namespace, []string{p.podName}) Expect(err).To(Succeed()) diff --git a/test/e2e/basic/storage-class-changing.go b/test/e2e/basic/storage-class-changing.go index ef1b2fde9..1b6fdb12b 100644 --- a/test/e2e/basic/storage-class-changing.go +++ b/test/e2e/basic/storage-class-changing.go @@ -54,8 +54,8 @@ func (s *StorageClasssChanging) Init() error { Text: "Change the storage class of persistent volumes and persistent" + " volume claims during restores", } - s.srcStorageClass = "default" - s.desStorageClass = StorageClassName + s.srcStorageClass = StorageClassName + s.desStorageClass = StorageClassName2 s.labels = map[string]string{"velero.io/change-storage-class": "RestoreItemAction", "velero.io/plugin-config": ""} s.data = map[string]string{s.srcStorageClass: s.desStorageClass} @@ -79,10 +79,11 @@ func (s *StorageClasssChanging) CreateResources() error { "app": "test", } s.Ctx, _ = 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", - s.VeleroCfg.CloudProvider))).To(Succeed()) + + By(("Installing storage class..."), func() { + Expect(InstallTestStorageClasses(fmt.Sprintf("testdata/storage-class/%s.yaml", s.VeleroCfg.CloudProvider))).To(Succeed(), "Failed to install storage class") }) + By(fmt.Sprintf("Create namespace %s", s.namespace), func() { Expect(CreateNamespace(s.Ctx, s.Client, s.namespace)).To(Succeed(), fmt.Sprintf("Failed to create namespace %s", s.namespace)) diff --git a/test/e2e/test/test.go b/test/e2e/test/test.go index 7a6da70bd..71b93790d 100644 --- a/test/e2e/test/test.go +++ b/test/e2e/test/test.go @@ -34,8 +34,6 @@ import ( . "github.com/vmware-tanzu/velero/test/e2e/util/velero" ) -const StorageClassName = "e2e-storage-class" - /* The VeleroBackupRestoreTest interface is just could be suit for the cases that follow the test flow of create resources, backup, delete test resource, restore and verify. diff --git a/test/e2e/util/common/common.go b/test/e2e/util/common/common.go index 2998d9de6..d787eb052 100644 --- a/test/e2e/util/common/common.go +++ b/test/e2e/util/common/common.go @@ -10,6 +10,9 @@ import ( "os/exec" ) +const StorageClassName = "e2e-storage-class" +const StorageClassName2 = "e2e-storage-class-2" + type OsCommandLine struct { Cmd string Args []string diff --git a/test/e2e/util/k8s/common.go b/test/e2e/util/k8s/common.go index 6c45cb723..80fe46941 100644 --- a/test/e2e/util/k8s/common.go +++ b/test/e2e/util/k8s/common.go @@ -20,6 +20,7 @@ import ( "fmt" "os" "os/exec" + "strings" "time" "github.com/pkg/errors" @@ -33,6 +34,9 @@ import ( "github.com/vmware-tanzu/velero/test/e2e/util/common" ) +const StorageClassName = "e2e-storage-class" +const StorageClassName2 = "e2e-storage-class-2" + // ensureClusterExists returns whether or not a Kubernetes cluster exists for tests to be run on. func EnsureClusterExists(ctx context.Context) error { return exec.CommandContext(ctx, "kubectl", "cluster-info").Run() @@ -363,3 +367,30 @@ func GetAPIVersions(client *TestClient, name string) ([]string, error) { } return nil, errors.New("Server API groups is empty") } + +func InstallTestStorageClasses(path string) error { + ctx, ctxCancel := context.WithTimeout(context.Background(), time.Minute*5) + defer ctxCancel() + err := InstallStorageClass(ctx, path) + if err != nil { + return err + } + content, err := os.ReadFile(path) + if err != nil { + return errors.Wrapf(err, "failed to get %s when install storage class", path) + } + + // replace sc to new value + newContent := strings.ReplaceAll(string(content), fmt.Sprintf("name: %s", StorageClassName), fmt.Sprintf("name: %s", StorageClassName2)) + + tmpFile, err := os.CreateTemp("", "sc-file") + if err != nil { + return errors.Wrapf(err, "failed to create temp file when install storage class") + } + + defer os.Remove(tmpFile.Name()) + if _, err := tmpFile.WriteString(newContent); err != nil { + return errors.Wrapf(err, "failed to write content into temp file %s when install storage class", tmpFile.Name()) + } + return InstallStorageClass(ctx, tmpFile.Name()) +}