Skip to content

Commit

Permalink
test: add testcase for backup
Browse files Browse the repository at this point in the history
  • Loading branch information
fengluodb committed Oct 21, 2023
1 parent ead34d1 commit 56f75ee
Showing 1 changed file with 124 additions and 1 deletion.
125 changes: 124 additions & 1 deletion controllers/dataprotection/backup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"time"

vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -194,11 +196,58 @@ var _ = Describe("Backup Controller test", func() {
})
backupKey := client.ObjectKeyFromObject(backup)

By("check backup failed and its expiration is set")
Eventually(testapps.CheckObj(&testCtx, backupKey, func(g Gomega, fetched *dpv1alpha1.Backup) {
g.Expect(fetched.Status.Phase).To(Equal(dpv1alpha1.BackupPhaseFailed))
g.Expect(fetched.Status.Expiration).Should(BeNil())
})).Should(Succeed())
})
})

Context("creates a backup with retentionPeriod", func() {
It("create an valid backup", func() {
By("creating a backup from backupPolicy " + testdp.BackupPolicyName)
backup := testdp.NewFakeBackup(&testCtx, func(backup *dpv1alpha1.Backup) {
backup.Spec.RetentionPeriod = "1h"
})
backupKey := client.ObjectKeyFromObject(backup)

getJobKey := func() client.ObjectKey {
return client.ObjectKey{
Name: dpbackup.GenerateBackupJobName(backup, dpbackup.BackupDataJobNamePrefix),
Namespace: backup.Namespace,
}
}

By("check backup expiration is set by start time when backup is running")
Eventually(testapps.CheckObj(&testCtx, backupKey, func(g Gomega, fetched *dpv1alpha1.Backup) {
g.Expect(fetched.Status.Phase).Should(Equal(dpv1alpha1.BackupPhaseRunning))
g.Expect(fetched.Status.Expiration.Second()).Should(Equal(fetched.Status.StartTimestamp.Add(time.Hour).Second()))
})).Should(Succeed())

testdp.PatchK8sJobStatus(&testCtx, getJobKey(), batchv1.JobComplete)

By("check backup expiration is update by completion time when backup is completed")
Eventually(testapps.CheckObj(&testCtx, backupKey, func(g Gomega, fetched *dpv1alpha1.Backup) {
g.Expect(fetched.Status.Phase).To(Equal(dpv1alpha1.BackupPhaseCompleted))
g.Expect(fetched.Status.CompletionTimestamp).ShouldNot(BeNil())
g.Expect(fetched.Status.Expiration.Second()).Should(Equal(fetched.Status.CompletionTimestamp.Add(time.Hour).Second()))
})).Should(Succeed())
})

It("create an invalid backup", func() {
By("creating a backup using a not found backupPolicy")
backup := testdp.NewFakeBackup(&testCtx, func(backup *dpv1alpha1.Backup) {
backup.Spec.BackupPolicyName = "not-found"
backup.Spec.RetentionPeriod = "1h"
})
backupKey := client.ObjectKeyFromObject(backup)

By("check backup failed and its expiration is set")
Eventually(testapps.CheckObj(&testCtx, backupKey, func(g Gomega, fetched *dpv1alpha1.Backup) {
g.Expect(fetched.Status.Phase).To(Equal(dpv1alpha1.BackupPhaseFailed))
g.Expect(fetched.Status.Expiration).ShouldNot(BeNil())
}))
})).Should(Succeed())
})
})

Expand Down Expand Up @@ -361,6 +410,10 @@ var _ = Describe("Backup Controller test", func() {
})

When("with exceptional settings", func() {
var (
backupPolicy *dpv1alpha1.BackupPolicy
)

Context("creates a backup with non-existent backup policy", func() {
var backupKey types.NamespacedName
BeforeEach(func() {
Expand All @@ -375,6 +428,76 @@ var _ = Describe("Backup Controller test", func() {
})).Should(Succeed())
})
})

Context("creates a backup using non-existent backup method", func() {
BeforeEach(func() {
By("creating a backupPolicy without backup method")
backupPolicy = testdp.NewFakeBackupPolicy(&testCtx, nil)
})

It("should fail because of no-existent backup method", func() {
backup := testdp.NewFakeBackup(&testCtx, func(backup *dpv1alpha1.Backup) {
backup.Spec.BackupPolicyName = backupPolicy.Name
backup.Spec.BackupMethod = "non-existent"
})
backupKey := client.ObjectKeyFromObject(backup)

By("check backup status failed")
Eventually(testapps.CheckObj(&testCtx, backupKey, func(g Gomega, fetched *dpv1alpha1.Backup) {
g.Expect(fetched.Status.Phase).To(Equal(dpv1alpha1.BackupPhaseFailed))
})).Should(Succeed())
})
})

Context("creates a backup with invalid backup method", func() {
BeforeEach(func() {
backupPolicy = testdp.NewFakeBackupPolicy(&testCtx, func(backupPolicy *dpv1alpha1.BackupPolicy) {
backupPolicy.Spec.BackupMethods = append(backupPolicy.Spec.BackupMethods, dpv1alpha1.BackupMethod{
Name: "invalid",
ActionSetName: "",
})
})
})

It("should fail because of invalid backup method", func() {
backup := testdp.NewFakeBackup(&testCtx, func(backup *dpv1alpha1.Backup) {
backup.Spec.BackupPolicyName = backupPolicy.Name
backup.Spec.BackupMethod = "invalid"
})
backupKey := client.ObjectKeyFromObject(backup)

By("check backup status failed")
Eventually(testapps.CheckObj(&testCtx, backupKey, func(g Gomega, fetched *dpv1alpha1.Backup) {
g.Expect(fetched.Status.Phase).To(Equal(dpv1alpha1.BackupPhaseFailed))
})).Should(Succeed())
})

It("should fail because of no-existing actionSet", func() {
backup := testdp.NewFakeBackup(&testCtx, nil)
backupKey := client.ObjectKeyFromObject(backup)

By("check backup status failed")
Eventually(testapps.CheckObj(&testCtx, backupKey, func(g Gomega, fetched *dpv1alpha1.Backup) {
g.Expect(fetched.Status.Phase).To(Equal(dpv1alpha1.BackupPhaseFailed))
})).Should(Succeed())
})

It("should fail because actionSet's backup type isn't Full", func() {
actionSet := testdp.NewFakeActionSet(&testCtx)
actionSetKey := client.ObjectKeyFromObject(actionSet)
Eventually(testapps.GetAndChangeObj(&testCtx, actionSetKey, func(fetched *dpv1alpha1.ActionSet) {
fetched.Spec.BackupType = dpv1alpha1.BackupTypeIncremental
}))

backup := testdp.NewFakeBackup(&testCtx, nil)
backupKey := client.ObjectKeyFromObject(backup)

By("check backup status failed")
Eventually(testapps.CheckObj(&testCtx, backupKey, func(g Gomega, fetched *dpv1alpha1.Backup) {
g.Expect(fetched.Status.Phase).To(Equal(dpv1alpha1.BackupPhaseFailed))
})).Should(Succeed())
})
})
})

When("with backup repo", func() {
Expand Down

0 comments on commit 56f75ee

Please sign in to comment.