diff --git a/tests/e2e/backup_restore_suite_test.go b/tests/e2e/backup_restore_suite_test.go index a8a92a2caf..a262eeb164 100644 --- a/tests/e2e/backup_restore_suite_test.go +++ b/tests/e2e/backup_restore_suite_test.go @@ -97,6 +97,10 @@ func runApplicationBackupAndRestore(brCase ApplicationBackupRestoreCase, expecte // create DPA backupName, restoreName := prepareBackupAndRestore(brCase.BackupRestoreCase, updateLastInstallTime) + // Ensure that an existing backup repository is deleted + brerr := lib.DeleteBackupRepositories(runTimeClientForSuiteRun, namespace) + gomega.Expect(brerr).ToNot(gomega.HaveOccurred()) + // install app updateLastInstallTime() log.Printf("Installing application for case %s", brCase.Name) diff --git a/tests/e2e/lib/backup.go b/tests/e2e/lib/backup.go index 7885b621be..97d28a1d00 100755 --- a/tests/e2e/lib/backup.go +++ b/tests/e2e/lib/backup.go @@ -145,3 +145,58 @@ func BackupErrorLogs(c *kubernetes.Clientset, ocClient client.Client, namespace bl := BackupLogs(c, ocClient, namespace, name) return errorLogsExcludingIgnored(bl) } + +func GetBackupRepositoryList(c client.Client, namespace string) (*velero.BackupRepositoryList, error) { + // initialize an empty list of BackupRepositories + backupRepositoryList := &velero.BackupRepositoryList{ + Items: []velero.BackupRepository{}, + } + // get the list of BackupRepositories in the given namespace + err := c.List(context.Background(), backupRepositoryList, client.InNamespace(namespace)) + if err != nil { + log.Printf("error getting BackupRepository list: %v", err) + return nil, err + } + return backupRepositoryList, nil +} + +func DeleteBackupRepository(c client.Client, namespace string, name string) error { + backupRepository := &velero.BackupRepository{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: namespace, + Name: name, + }, + } + err := c.Delete(context.Background(), backupRepository) + if err != nil { + return err + } + return nil +} + +// DeleteBackupRepositories deletes all BackupRepositories in the given namespace. +func DeleteBackupRepositories(c client.Client, namespace string) error { + log.Printf("Checking if backuprepository's exist in %s", namespace) + + backupRepos, err := GetBackupRepositoryList(c, namespace) + if err != nil { + return fmt.Errorf("failed to get BackupRepository list: %v", err) + } + if len(backupRepos.Items) == 0 { + log.Printf("No BackupRepositories found in namespace %s", namespace) + return nil + } + + // Get a list of the BackupRepositories and delete all of them. + for _, repo := range backupRepos.Items { + log.Printf("backuprepository name is %s", repo.Name) + err := DeleteBackupRepository(c, namespace, repo.Name) + if err != nil { + log.Printf("failed to delete BackupRepository %s: ", repo.Name) + return err + } + log.Printf("Successfully deleted BackupRepository: %s", repo.Name) + } + + return nil +} diff --git a/tests/e2e/virt_backup_restore_suite_test.go b/tests/e2e/virt_backup_restore_suite_test.go index 62112b37a2..7973e24d3f 100644 --- a/tests/e2e/virt_backup_restore_suite_test.go +++ b/tests/e2e/virt_backup_restore_suite_test.go @@ -200,6 +200,8 @@ var _ = ginkgo.Describe("VM backup and restore tests", ginkgo.Ordered, func() { gomega.Expect(err).To(gomega.BeNil()) err = v.CreateWaitForFirstConsumerStorageClass("test-sc-wffc") gomega.Expect(err).To(gomega.BeNil()) + err = lib.DeleteBackupRepositories(runTimeClientForSuiteRun, namespace) + gomega.Expect(err).To(gomega.BeNil()) }) var _ = ginkgo.AfterAll(func() {