Skip to content

Commit

Permalink
Add schedule backup timing E2E test
Browse files Browse the repository at this point in the history
Signed-off-by: danfengl <[email protected]>
  • Loading branch information
danfengliu committed Sep 18, 2022
1 parent 100d6b4 commit 5873b37
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 10 deletions.
128 changes: 128 additions & 0 deletions test/e2e/backups/schedule.go
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
}
1 change: 1 addition & 0 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ var _ = Describe("[Backups][Deletion][Restic] Velero tests of Restic backup dele
var _ = Describe("[Backups][Deletion][Snapshot] Velero tests of snapshot backup deletion", BackupDeletionWithSnapshots)
var _ = Describe("[Backups][TTL] Local backups and restic repos will be deleted once the corresponding backup storage location is deleted", TTLTest)
var _ = Describe("[Backups][BackupsSync] Backups in object storage are synced to a new Velero and deleted backups in object storage are synced to be deleted in Velero", BackupsSyncTest)
var _ = Describe("[Backups][Schedule] Backup triggered by schedule", ScheduleBackupTest)

var _ = Describe("[PrivilegesMgmt][SSR] Velero test on ssr object when controller namespace mix-ups", SSRTest)

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/util/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func GetListBy2Pipes(ctx context.Context, cmdline1, cmdline2, cmdline3 OsCommand
_ = c2.Wait()
_ = c3.Wait()

fmt.Println(&b2)
//fmt.Println(&b2)
scanner := bufio.NewScanner(&b2)
var ret []string
for scanner.Scan() {
Expand Down
60 changes: 51 additions & 9 deletions test/e2e/util/velero/velero_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,43 @@ func GetBackupsFromBsl(ctx context.Context, veleroCLI, bslName string) ([]string
return common.GetListBy2Pipes(ctx, *CmdLine1, *CmdLine2, *CmdLine3)
}

func GetScheduledBackupsCreationTime(ctx context.Context, veleroCLI, bslName, scheduleName string) ([]string, error) {
var creationTimes []string
backups, err := GetBackupsCreationTime(ctx, veleroCLI, bslName)
if err != nil {
return nil, err
}
for _, b := range backups {
if strings.Contains(b, scheduleName) {
creationTimes = append(creationTimes, b)
}
}
return creationTimes, nil
}
func GetBackupsCreationTime(ctx context.Context, veleroCLI, bslName string) ([]string, error) {
args1 := []string{"get", "backups"}
createdTime := "$1,\",\" $5,$6,$7,$8"
if strings.TrimSpace(bslName) != "" {
args1 = append(args1, "-l", "velero.io/storage-location="+bslName)
}
CmdLine1 := &common.OsCommandLine{
Cmd: veleroCLI,
Args: args1,
}

CmdLine2 := &common.OsCommandLine{
Cmd: "awk",
Args: []string{"{print " + createdTime + "}"},
}

CmdLine3 := &common.OsCommandLine{
Cmd: "tail",
Args: []string{"-n", "+2"},
}

return common.GetListBy2Pipes(ctx, *CmdLine1, *CmdLine2, *CmdLine3)
}

func GetAllBackups(ctx context.Context, veleroCLI string) ([]string, error) {
return GetBackupsFromBsl(ctx, veleroCLI, "")
}
Expand Down Expand Up @@ -976,23 +1013,15 @@ func GetSnapshotCheckPoint(client TestClient, VeleroCfg VerleroConfig, expectCou
}

func GetBackupTTL(ctx context.Context, veleroNamespace, backupName string) (string, error) {

checkSnapshotCmd := exec.CommandContext(ctx, "kubectl",
"get", "backup", "-n", veleroNamespace, backupName, "-o=jsonpath='{.spec.ttl}'")
fmt.Printf("checkSnapshotCmd cmd =%v\n", checkSnapshotCmd)
stdout, stderr, err := veleroexec.RunCommand(checkSnapshotCmd)
if err != nil {
fmt.Print(stdout)
fmt.Print(stderr)
return "", errors.Wrap(err, "failed to verify")
return "", errors.Wrap(err, fmt.Sprintf("failed to run command %s", checkSnapshotCmd))
}
// lines := strings.Split(stdout, "\n")
// complete := true
// for _, curLine := range lines {
// fmt.Println(curLine)

// }
// return complete, nil
return stdout, err
}

Expand Down Expand Up @@ -1026,3 +1055,16 @@ func DeleteBackups(ctx context.Context, client TestClient) error {
}
return nil
}

func GetSchedule(ctx context.Context, veleroNamespace, scheduleName string) (string, error) {
checkSnapshotCmd := exec.CommandContext(ctx, "kubectl",
"get", "schedule", "-n", veleroNamespace, scheduleName, "-o=jsonpath='{.metadata.creationTimestamp}'")
fmt.Printf("Cmd =%v\n", checkSnapshotCmd)
stdout, stderr, err := veleroexec.RunCommand(checkSnapshotCmd)
if err != nil {
fmt.Print(stdout)
fmt.Print(stderr)
return "", errors.Wrap(err, fmt.Sprintf("failed to run command %s", checkSnapshotCmd))
}
return stdout, err
}

0 comments on commit 5873b37

Please sign in to comment.