-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: danfengl <[email protected]>
- Loading branch information
1 parent
100d6b4
commit 5873b37
Showing
4 changed files
with
181 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package backups | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
. "github.com/vmware-tanzu/velero/test/e2e" | ||
. "github.com/vmware-tanzu/velero/test/e2e/test" | ||
. "github.com/vmware-tanzu/velero/test/e2e/util/k8s" | ||
. "github.com/vmware-tanzu/velero/test/e2e/util/velero" | ||
) | ||
|
||
type ScheduleBackup struct { | ||
TestCase | ||
ScheduleName string | ||
ScheduleArgs []string | ||
Period int | ||
} | ||
|
||
var ScheduleBackupTest func() = TestFunc(&ScheduleBackup{TestCase: TestCase{NSBaseName: "ns", NSIncluded: &[]string{"ns1"}}}) | ||
|
||
func (n *ScheduleBackup) Init() error { | ||
n.Client = TestClientInstance | ||
n.Period = 3 | ||
n.TestMsg = &TestMSG{ | ||
Desc: "Backup resources with include namespace test", | ||
FailedMSG: "Failed to backup with namespace include", | ||
Text: "should backup namespaces", | ||
} | ||
return nil | ||
} | ||
|
||
func (n *ScheduleBackup) StartRun() error { | ||
|
||
n.ScheduleName = n.ScheduleName + "schedule-" + UUIDgen.String() | ||
n.RestoreName = n.RestoreName + "restore-ns-mapping-" + UUIDgen.String() | ||
|
||
n.ScheduleArgs = []string{ | ||
"schedule", "create", "--namespace", VeleroCfg.VeleroNamespace, n.ScheduleName, | ||
"--include-namespaces", strings.Join(*n.NSIncluded, ","), | ||
"--schedule=*/" + fmt.Sprintf("%v", n.Period) + " * * * *", | ||
} | ||
n.RestoreArgs = []string{ | ||
"create", "--namespace", VeleroCfg.VeleroNamespace, "restore", n.RestoreName, | ||
"--from-backup", n.BackupName, | ||
"--wait", | ||
} | ||
return nil | ||
} | ||
func (n *ScheduleBackup) CreateResources() error { | ||
n.Ctx, _ = context.WithTimeout(context.Background(), 60*time.Minute) | ||
for _, ns := range *n.NSIncluded { | ||
By(fmt.Sprintf("Creating namespaces ...%s\n", ns), func() { | ||
Expect(CreateNamespace(n.Ctx, n.Client, ns)).To(Succeed(), fmt.Sprintf("Failed to create namespace %s", ns)) | ||
}) | ||
} | ||
return nil | ||
} | ||
|
||
func (n *ScheduleBackup) Backup() error { | ||
for i := 0; i < n.Period*60/30; i++ { | ||
time.Sleep(30 * time.Second) | ||
now := time.Now().Minute() | ||
triggerNow := now % n.Period | ||
if triggerNow == 0 { | ||
Expect(VeleroCmdExec(n.Ctx, VeleroCfg.VeleroCLI, n.ScheduleArgs)).To(Succeed()) | ||
break | ||
} | ||
} | ||
return nil | ||
} | ||
func (n *ScheduleBackup) Destroy() error { | ||
return nil | ||
} | ||
|
||
func (n *ScheduleBackup) Restore() error { | ||
return nil | ||
} | ||
|
||
func (n *ScheduleBackup) Verify() error { | ||
creationTimestamp, err := GetSchedule(context.Background(), VeleroCfg.VeleroNamespace, n.ScheduleName) | ||
Expect(err).To(Succeed()) | ||
|
||
creationTime, err := time.Parse(time.RFC3339, strings.Replace(creationTimestamp, "'", "", -1)) | ||
Expect(err).To(Succeed()) | ||
fmt.Printf("Schedule %s created at %s\n", n.ScheduleName, creationTime) | ||
|
||
for i := 0; i < n.Period; i++ { | ||
time.Sleep(1 * time.Minute) | ||
now := time.Now() | ||
fmt.Printf("now %d", i) | ||
fmt.Println(now) | ||
if i != n.Period-1 { | ||
backupsInBSL1, err := GetScheduledBackupsCreationTime(context.Background(), VeleroCfg.VeleroCLI, "default", n.ScheduleName) | ||
Expect(err).To(Succeed()) | ||
Expect(len(backupsInBSL1) == 0).To(Equal(true)) | ||
} | ||
} | ||
time.Sleep(1 * time.Minute) | ||
for i := 0; i < 5; i++ { | ||
fmt.Printf("Start to sleep %d minute #%d time...\n", n.Period, i+1) | ||
now := time.Now() | ||
fmt.Printf("now ### %d", i) | ||
fmt.Println(now) | ||
time.Sleep(time.Duration(n.Period) * time.Minute) | ||
bMap := make(map[string]string) | ||
now1 := time.Now() | ||
fmt.Printf("now 1### %d", i) | ||
fmt.Println(now1) | ||
backupsInBSL1, err := GetScheduledBackupsCreationTime(context.Background(), VeleroCfg.VeleroCLI, "default", n.ScheduleName) | ||
Expect(err).To(Succeed()) | ||
Expect(len(backupsInBSL1) == i+2).To(Equal(true)) | ||
for _, bi := range backupsInBSL1 { | ||
bList := strings.Split(bi, ",") | ||
bMap[bList[0]] = bList[1] | ||
_, error := time.Parse("2006-01-02 15:04:05 -0700 MST", bList[1]) | ||
if error != nil { | ||
return error | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters