Limit backup namespaces on test resource filtering cases

Signed-off-by: ming qiu <mqiu@mqiu-a01.vmware.com>
This commit is contained in:
ming qiu
2021-12-08 17:04:22 +08:00
parent 769af3f7b8
commit c58fc1445e
9 changed files with 109 additions and 62 deletions

View File

@@ -0,0 +1 @@
Limit backup namespaces on test resource filtering cases

View File

@@ -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)

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 <backup-name> --include-namespaces <namespace1>,<namespace
*/
type IncludeNamespaces struct {
nsIncluded *[]string
allTestNamespaces *[]string
namespacesIncluded int
FilteringCase
}
@@ -51,18 +49,17 @@ type IncludeNamespaces struct {
var BackupWithIncludeNamespaces func() = TestFunc(&IncludeNamespaces{FilteringCase: testInBackup})
var RestoreWithIncludeNamespaces func() = TestFunc(&IncludeNamespaces{FilteringCase: testInRestore})
func (i *IncludeNamespaces) Init() {
rand.Seed(time.Now().UnixNano())
UUIDgen, _ = uuid.NewRandom()
func (i *IncludeNamespaces) Init() error {
i.FilteringCase.Init()
i.namespacesIncluded = i.NamespacesTotal / 2
i.nsIncluded = &[]string{}
i.allTestNamespaces = &[]string{}
i.NSBaseName = "include-namespaces-" + UUIDgen.String()
for nsNum := 0; nsNum < i.NamespacesTotal; nsNum++ {
createNSName := fmt.Sprintf("%s-%00000d", i.NSBaseName, nsNum)
if nsNum < i.namespacesIncluded {
*i.nsIncluded = append(*i.nsIncluded, createNSName)
*i.NSIncluded = append(*i.NSIncluded, createNSName)
}
*i.allTestNamespaces = append(*i.allTestNamespaces, createNSName)
}
if i.IsTestInBackup {
@@ -75,7 +72,7 @@ func (i *IncludeNamespaces) Init() {
}
i.BackupArgs = []string{
"create", "--namespace", VeleroCfg.VeleroNamespace, "backup", i.BackupName,
"--include-namespaces", strings.Join(*i.nsIncluded, ","),
"--include-namespaces", strings.Join(*i.NSIncluded, ","),
"--default-volumes-to-restic", "--wait",
}
@@ -94,15 +91,17 @@ func (i *IncludeNamespaces) Init() {
}
i.BackupArgs = []string{
"create", "--namespace", VeleroCfg.VeleroNamespace, "backup", i.BackupName,
"--include-namespaces", strings.Join(*i.allTestNamespaces, ","),
"--default-volumes-to-restic", "--wait",
}
i.RestoreArgs = []string{
"create", "--namespace", VeleroCfg.VeleroNamespace, "restore", i.RestoreName,
"--include-namespaces", strings.Join(*i.nsIncluded, ","),
"--include-namespaces", strings.Join(*i.NSIncluded, ","),
"--from-backup", i.BackupName, "--wait",
}
}
return nil
}
func (i *IncludeNamespaces) CreateResources() error {
@@ -113,9 +112,6 @@ func (i *IncludeNamespaces) CreateResources() error {
if err := CreateNamespace(i.Ctx, i.Client, createNSName); err != nil {
return errors.Wrapf(err, "Failed to create namespace %s", createNSName)
}
if nsNum <= i.namespacesIncluded {
*i.nsIncluded = append(*i.nsIncluded, createNSName)
}
}
return nil
}

View File

@@ -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"
@@ -47,11 +45,13 @@ type IncludeResources struct {
var BackupWithIncludeResources func() = TestFunc(&IncludeResources{testInBackup})
var RestoreWithIncludeResources func() = TestFunc(&IncludeResources{testInRestore})
func (i *IncludeResources) Init() {
rand.Seed(time.Now().UnixNano())
UUIDgen, _ = uuid.NewRandom()
func (i *IncludeResources) Init() error {
i.FilteringCase.Init()
i.NSBaseName = "include-resources-" + UUIDgen.String()
for nsNum := 0; nsNum < i.NamespacesTotal; nsNum++ {
createNSName := fmt.Sprintf("%s-%00000d", i.NSBaseName, nsNum)
*i.NSIncluded = append(*i.NSIncluded, createNSName)
}
if i.IsTestInBackup { // testing case backup with include-resources option
i.TestMsg = &TestMSG{
Desc: "Backup resources with resources included test",
@@ -80,6 +80,7 @@ func (i *IncludeResources) Init() {
i.RestoreName = "restore-include-resources-" + UUIDgen.String()
i.BackupArgs = []string{
"create", "--namespace", VeleroCfg.VeleroNamespace, "backup", i.BackupName,
"--include-namespaces", strings.Join(*i.NSIncluded, ","),
"--default-volumes-to-restic", "--wait",
}
i.RestoreArgs = []string{
@@ -88,6 +89,7 @@ func (i *IncludeResources) Init() {
"--from-backup", i.BackupName, "--wait",
}
}
return nil
}
func (i *IncludeResources) Verify() error {

View File

@@ -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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -43,13 +42,15 @@ type LabelSelector struct {
var BackupWithLabelSelector func() = TestFunc(&LabelSelector{testInBackup})
func (l *LabelSelector) Init() {
rand.Seed(time.Now().UnixNano())
UUIDgen, _ = uuid.NewRandom()
func (l *LabelSelector) Init() error {
l.FilteringCase.Init()
l.BackupName = "backup-label-selector-" + UUIDgen.String()
l.RestoreName = "restore-label-selector-" + UUIDgen.String()
l.RestoreName = "restore-" + UUIDgen.String()
l.NSBaseName = "backup-label-selector-" + UUIDgen.String()
for nsNum := 0; nsNum < l.NamespacesTotal; nsNum++ {
createNSName := fmt.Sprintf("%s-%00000d", l.NSBaseName, nsNum)
*l.NSIncluded = append(*l.NSIncluded, createNSName)
}
l.TestMsg = &TestMSG{
Desc: "Backup with the label selector test",
Text: "Should backup resources with selected label resource",
@@ -62,6 +63,7 @@ func (l *LabelSelector) Init() {
l.BackupArgs = []string{
"create", "--namespace", VeleroCfg.VeleroNamespace, "backup", l.BackupName,
"--selector", "resourcefiltering=true",
"--include-namespaces", strings.Join(*l.NSIncluded, ","),
"--default-volumes-to-restic", "--wait",
}
@@ -69,6 +71,7 @@ func (l *LabelSelector) Init() {
"create", "--namespace", VeleroCfg.VeleroNamespace, "restore", l.RestoreName,
"--from-backup", l.BackupName, "--wait",
}
return nil
}
func (l *LabelSelector) CreateResources() error {
@@ -86,6 +89,15 @@ func (l *LabelSelector) CreateResources() error {
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(l.Ctx, l.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(l.Ctx, l.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)

View File

@@ -31,8 +31,14 @@ import (
. "github.com/vmware-tanzu/velero/test/e2e/util/velero"
)
type VeleroTest interface {
Init()
/*
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.
And the cases have similar execute function and similar data. it's both fine for you to use it or not which
depends on your test patterns.
*/
type VeleroBackupRestoreTest interface {
Init() error
CreateResources() error
Backup() error
Destroy() error
@@ -58,11 +64,13 @@ type TestCase struct {
TestMsg *TestMSG
Client TestClient
Ctx context.Context
NSIncluded *[]string
}
var TestClientInstance TestClient
var isVeleroInstalled bool = false
func TestFunc(test VeleroTest) func() {
func TestFunc(test VeleroBackupRestoreTest) func() {
return func() {
var err error
TestClientInstance, err = NewTestClient()
@@ -70,16 +78,10 @@ func TestFunc(test VeleroTest) func() {
test.Init()
BeforeEach(func() {
flag.Parse()
if VeleroCfg.InstallVelero {
if VeleroCfg.InstallVelero && !isVeleroInstalled {
Expect(VeleroUninstall(context.Background(), VeleroCfg.VeleroCLI, VeleroCfg.VeleroNamespace)).To((Succeed()))
Expect(VeleroInstall(context.Background(), &VeleroCfg, "", false)).To(Succeed())
}
})
AfterEach(func() {
if VeleroCfg.InstallVelero {
err := VeleroUninstall(context.Background(), VeleroCfg.VeleroCLI, VeleroCfg.VeleroNamespace)
Expect(err).To(Succeed())
isVeleroInstalled = true
}
})
@@ -132,7 +134,7 @@ func (t *TestCase) GetTestMsg() *TestMSG {
return t.TestMsg
}
func RunTestCase(test VeleroTest) error {
func RunTestCase(test VeleroBackupRestoreTest) error {
fmt.Printf("Running test case %s\n", test.GetTestMsg().Desc)
if test == nil {
return errors.New("No case should be tested")