diff --git a/changelogs/unreleased/4437-mqiu b/changelogs/unreleased/4437-mqiu new file mode 100644 index 000000000..75c03a143 --- /dev/null +++ b/changelogs/unreleased/4437-mqiu @@ -0,0 +1 @@ +Limit backup namespaces on test resource filtering cases diff --git a/test/e2e/resource-filtering/base.go b/test/e2e/resource-filtering/base.go index 77d97da19..fcfd1fc37 100644 --- a/test/e2e/resource-filtering/base.go +++ b/test/e2e/resource-filtering/base.go @@ -19,6 +19,7 @@ package filtering import ( "context" "fmt" + "math/rand" "time" "github.com/google/uuid" @@ -41,14 +42,15 @@ type FilteringCase struct { var testInBackup = FilteringCase{IsTestInBackup: true} var testInRestore = FilteringCase{IsTestInBackup: false} -func (f *FilteringCase) Init() { +func (f *FilteringCase) Init() error { + rand.Seed(time.Now().UnixNano()) UUIDgen, _ = uuid.NewRandom() f.replica = int32(2) f.labels = map[string]string{"resourcefiltering": "true"} f.labelSelector = "resourcefiltering" f.Client = TestClientInstance - f.NamespacesTotal = 5 + f.NamespacesTotal = 3 f.BackupArgs = []string{ "create", "--namespace", VeleroCfg.VeleroNamespace, "backup", f.BackupName, "--default-volumes-to-restic", "--wait", @@ -58,6 +60,9 @@ func (f *FilteringCase) Init() { "create", "--namespace", VeleroCfg.VeleroNamespace, "restore", f.RestoreName, "--from-backup", f.BackupName, "--wait", } + + f.NSIncluded = &[]string{} + return nil } func (f *FilteringCase) CreateResources() error { @@ -68,7 +73,15 @@ func (f *FilteringCase) CreateResources() error { if err := CreateNamespace(f.Ctx, f.Client, namespace); err != nil { return errors.Wrapf(err, "Failed to create namespace %s", namespace) } - + serviceAccountName := "default" + // wait until the service account is created before patch the image pull secret + if err := WaitUntilServiceAccountCreated(f.Ctx, f.Client, namespace, serviceAccountName, 10*time.Minute); err != nil { + return errors.Wrapf(err, "failed to wait the service account %q created under the namespace %q", serviceAccountName, namespace) + } + // add the image pull secret to avoid the image pull limit issue of Docker Hub + if err := PatchServiceAccountWithImagePullSecret(f.Ctx, f.Client, namespace, serviceAccountName, VeleroCfg.RegistryCredentialFile); err != nil { + return errors.Wrapf(err, "failed to patch the service account %q under the namespace %q", serviceAccountName, namespace) + } //Create deployment fmt.Printf("Creating deployment in namespaces ...%s\n", namespace) deployment := NewDeployment(f.NSBaseName, namespace, f.replica, f.labels) diff --git a/test/e2e/resource-filtering/exclude_label.go b/test/e2e/resource-filtering/exclude_label.go index 919be15f5..72307fe3f 100644 --- a/test/e2e/resource-filtering/exclude_label.go +++ b/test/e2e/resource-filtering/exclude_label.go @@ -19,10 +19,9 @@ package filtering import ( "context" "fmt" - "math/rand" + "strings" "time" - "github.com/google/uuid" "github.com/pkg/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -42,24 +41,28 @@ type ExcludeFromBackup struct { var ExcludeFromBackupTest func() = TestFunc(&ExcludeFromBackup{testInBackup}) -func (e *ExcludeFromBackup) Init() { - rand.Seed(time.Now().UnixNano()) - UUIDgen, _ = uuid.NewRandom() +func (e *ExcludeFromBackup) Init() error { e.FilteringCase.Init() e.BackupName = "backup-exclude-from-backup-" + UUIDgen.String() - e.RestoreName = "restore-exclude-from-backup-" + UUIDgen.String() + e.RestoreName = "restore-" + UUIDgen.String() e.NSBaseName = "exclude-from-backup-" + UUIDgen.String() e.TestMsg = &TestMSG{ Desc: "Backup with the label velero.io/exclude-from-backup=true are not included test", Text: "Should not backup resources with the label velero.io/exclude-from-backup=true", FailedMSG: "Failed to backup resources with the label velero.io/exclude-from-backup=true", } + for nsNum := 0; nsNum < e.NamespacesTotal; nsNum++ { + createNSName := fmt.Sprintf("%s-%00000d", e.NSBaseName, nsNum) + *e.NSIncluded = append(*e.NSIncluded, createNSName) + } e.labels = map[string]string{ "velero.io/exclude-from-backup": "true", } e.labelSelector = "velero.io/exclude-from-backup" + e.BackupArgs = []string{ "create", "--namespace", VeleroCfg.VeleroNamespace, "backup", e.BackupName, + "--include-namespaces", strings.Join(*e.NSIncluded, ","), "--default-volumes-to-restic", "--wait", } @@ -67,6 +70,7 @@ func (e *ExcludeFromBackup) Init() { "create", "--namespace", VeleroCfg.VeleroNamespace, "restore", e.RestoreName, "--from-backup", e.BackupName, "--wait", } + return nil } func (e *ExcludeFromBackup) CreateResources() error { @@ -83,7 +87,15 @@ func (e *ExcludeFromBackup) CreateResources() error { if err := CreateNamespaceWithLabel(e.Ctx, e.Client, namespace, labels); err != nil { return errors.Wrapf(err, "Failed to create namespace %s", namespace) } - + serviceAccountName := "default" + // wait until the service account is created before patch the image pull secret + if err := WaitUntilServiceAccountCreated(e.Ctx, e.Client, namespace, serviceAccountName, 10*time.Minute); err != nil { + return errors.Wrapf(err, "failed to wait the service account %q created under the namespace %q", serviceAccountName, namespace) + } + // add the image pull secret to avoid the image pull limit issue of Docker Hub + if err := PatchServiceAccountWithImagePullSecret(e.Ctx, e.Client, namespace, serviceAccountName, VeleroCfg.RegistryCredentialFile); err != nil { + return errors.Wrapf(err, "failed to patch the service account %q under the namespace %q", serviceAccountName, namespace) + } //Create deployment fmt.Printf("Creating deployment in namespaces ...%s\n", namespace) diff --git a/test/e2e/resource-filtering/exclude_namespaces.go b/test/e2e/resource-filtering/exclude_namespaces.go index 88ccb446e..b76b15fb1 100644 --- a/test/e2e/resource-filtering/exclude_namespaces.go +++ b/test/e2e/resource-filtering/exclude_namespaces.go @@ -19,11 +19,9 @@ package filtering import ( "context" "fmt" - "math/rand" "strings" "time" - "github.com/google/uuid" "github.com/pkg/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -50,21 +48,21 @@ type ExcludeNamespaces struct { var BackupWithExcludeNamespaces func() = TestFunc(&ExcludeNamespaces{FilteringCase: testInBackup}) var RestoreWithExcludeNamespaces func() = TestFunc(&ExcludeNamespaces{FilteringCase: testInRestore}) -func (e *ExcludeNamespaces) Init() { - rand.Seed(time.Now().UnixNano()) - UUIDgen, _ = uuid.NewRandom() +func (e *ExcludeNamespaces) Init() error { e.FilteringCase.Init() - e.BackupName = "backup-exclude-namespaces-" + UUIDgen.String() - e.RestoreName = "restore-exclude-namespaces-" + UUIDgen.String() e.namespacesExcluded = e.NamespacesTotal / 2 e.NSBaseName = "exclude-namespaces-" + UUIDgen.String() if e.IsTestInBackup { + e.BackupName = "backup-exclude-namespaces-" + UUIDgen.String() + e.RestoreName = "restore-" + UUIDgen.String() e.TestMsg = &TestMSG{ Desc: "Backup resources with exclude namespace test", FailedMSG: "Failed to backup and restore with namespace include", Text: fmt.Sprintf("should not backup %d namespaces of %d", e.namespacesExcluded, e.NamespacesTotal), } } else { + e.BackupName = "backup-" + UUIDgen.String() + e.RestoreName = "restore-exclude-namespaces-" + UUIDgen.String() e.TestMsg = &TestMSG{ Desc: "Restore resources with exclude namespace test", FailedMSG: "Failed to restore with namespace exclude", @@ -76,12 +74,15 @@ func (e *ExcludeNamespaces) Init() { createNSName := fmt.Sprintf("%s-%00000d", e.NSBaseName, nsNum) if nsNum < e.namespacesExcluded { *e.nsExcluded = append(*e.nsExcluded, createNSName) + } else { + *e.NSIncluded = append(*e.NSIncluded, createNSName) } } if e.IsTestInBackup { e.BackupArgs = []string{ "create", "--namespace", VeleroCfg.VeleroNamespace, "backup", e.BackupName, "--exclude-namespaces", strings.Join(*e.nsExcluded, ","), + "--include-namespaces", strings.Join(*e.NSIncluded, ","), "--default-volumes-to-restic", "--wait", } @@ -91,8 +92,10 @@ func (e *ExcludeNamespaces) Init() { } } else { + *e.NSIncluded = append(*e.NSIncluded, *e.nsExcluded...) e.BackupArgs = []string{ "create", "--namespace", VeleroCfg.VeleroNamespace, "backup", e.BackupName, + "--include-namespaces", strings.Join(*e.NSIncluded, ","), "--default-volumes-to-restic", "--wait", } @@ -102,6 +105,7 @@ func (e *ExcludeNamespaces) Init() { "--from-backup", e.BackupName, "--wait", } } + return nil } func (e *ExcludeNamespaces) CreateResources() error { diff --git a/test/e2e/resource-filtering/exclude_resources.go b/test/e2e/resource-filtering/exclude_resources.go index 81fd9a133..1080ee70f 100644 --- a/test/e2e/resource-filtering/exclude_resources.go +++ b/test/e2e/resource-filtering/exclude_resources.go @@ -19,10 +19,8 @@ package filtering import ( "context" "fmt" - "math/rand" - "time" + "strings" - "github.com/google/uuid" "github.com/pkg/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -49,11 +47,13 @@ type ExcludeResources struct { var BackupWithExcludeResources func() = TestFunc(&ExcludeResources{testInBackup}) var RestoreWithExcludeResources func() = TestFunc(&ExcludeResources{testInRestore}) -func (e *ExcludeResources) Init() { - rand.Seed(time.Now().UnixNano()) - UUIDgen, _ = uuid.NewRandom() +func (e *ExcludeResources) Init() error { e.FilteringCase.Init() e.NSBaseName = "exclude-resources-" + UUIDgen.String() + for nsNum := 0; nsNum < e.NamespacesTotal; nsNum++ { + createNSName := fmt.Sprintf("%s-%00000d", e.NSBaseName, nsNum) + *e.NSIncluded = append(*e.NSIncluded, createNSName) + } if e.IsTestInBackup { // testing case backup with exclude-resources option e.TestMsg = &TestMSG{ Desc: "Backup resources with resources included test", @@ -64,6 +64,7 @@ func (e *ExcludeResources) Init() { e.RestoreName = "restore-" + UUIDgen.String() e.BackupArgs = []string{ "create", "--namespace", VeleroCfg.VeleroNamespace, "backup", e.BackupName, + "--include-namespaces", strings.Join(*e.NSIncluded, ","), "--exclude-resources", "secrets", "--default-volumes-to-restic", "--wait", } @@ -73,15 +74,18 @@ func (e *ExcludeResources) Init() { "--from-backup", e.BackupName, "--wait", } } else { // testing case restore with exclude-resources option + e.BackupName = "backup-" + UUIDgen.String() + e.RestoreName = "restore-exclude-resources-" + UUIDgen.String() e.TestMsg = &TestMSG{ Desc: "Restore resources with resources included test", Text: "Should not restore resources which is excluded others should be backup", FailedMSG: "Failed to restore with resource exclude", } - e.BackupName = "backup-" + UUIDgen.String() + e.BackupName = "backup-exclude-resources-" + UUIDgen.String() e.RestoreName = "restore-exclude-resources-" + UUIDgen.String() e.BackupArgs = []string{ "create", "--namespace", VeleroCfg.VeleroNamespace, "backup", e.BackupName, + "--include-namespaces", strings.Join(*e.NSIncluded, ","), "--default-volumes-to-restic", "--wait", } e.RestoreArgs = []string{ @@ -90,6 +94,7 @@ func (e *ExcludeResources) Init() { "--from-backup", e.BackupName, "--wait", } } + return nil } func (e *ExcludeResources) Verify() error { diff --git a/test/e2e/resource-filtering/include_namespaces.go b/test/e2e/resource-filtering/include_namespaces.go index 7476beb0c..1d6fdf49d 100644 --- a/test/e2e/resource-filtering/include_namespaces.go +++ b/test/e2e/resource-filtering/include_namespaces.go @@ -19,11 +19,9 @@ package filtering import ( "context" "fmt" - "math/rand" "strings" "time" - "github.com/google/uuid" "github.com/pkg/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -43,7 +41,7 @@ velero restore create --include-namespaces ,