2022-01-18 02:18:49 +00:00
/ *
*
* Copyright the Velero contributors .
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
* /
2023-03-27 13:55:29 +00:00
// Refer to https://github.com/vmware-tanzu/velero/issues/4253
2022-01-18 02:18:49 +00:00
package backups
import (
"context"
"flag"
"fmt"
"math/rand"
"time"
"github.com/google/uuid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
2023-07-31 02:25:48 +00:00
. "github.com/vmware-tanzu/velero/test"
. "github.com/vmware-tanzu/velero/test/util/k8s"
. "github.com/vmware-tanzu/velero/test/util/providers"
. "github.com/vmware-tanzu/velero/test/util/velero"
2022-01-18 02:18:49 +00:00
)
type SyncBackups struct {
testNS string
backupName string
}
func ( b * SyncBackups ) Init ( ) {
rand . Seed ( time . Now ( ) . UnixNano ( ) )
UUIDgen , _ = uuid . NewRandom ( )
b . testNS = "sync-bsl-test-" + UUIDgen . String ( )
b . backupName = "sync-bsl-test-" + UUIDgen . String ( )
}
func BackupsSyncTest ( ) {
test := new ( SyncBackups )
2022-04-10 10:20:16 +00:00
var (
2022-07-05 12:37:14 +00:00
err error
2022-04-10 10:20:16 +00:00
)
2022-01-18 02:18:49 +00:00
BeforeEach ( func ( ) {
flag . Parse ( )
if VeleroCfg . InstallVelero {
2023-01-30 03:58:41 +00:00
veleroCfg := VeleroCfg
veleroCfg . UseVolumeSnapshots = false
2023-06-20 10:24:11 +00:00
Expect ( VeleroInstall ( context . Background ( ) , & VeleroCfg , false ) ) . To ( Succeed ( ) )
2022-01-18 02:18:49 +00:00
}
} )
AfterEach ( func ( ) {
2022-08-15 07:35:30 +00:00
if ! VeleroCfg . Debug {
By ( "Clean backups after test" , func ( ) {
2023-08-14 03:48:34 +00:00
DeleteAllBackups ( context . Background ( ) , * VeleroCfg . ClientToInstallVelero )
2022-08-15 07:35:30 +00:00
} )
if VeleroCfg . InstallVelero {
2023-07-27 08:04:07 +00:00
ctx , ctxCancel := context . WithTimeout ( context . Background ( ) , time . Minute * 5 )
defer ctxCancel ( )
Expect ( VeleroUninstall ( ctx , VeleroCfg . VeleroCLI , VeleroCfg . VeleroNamespace ) ) . To ( Succeed ( ) )
2022-04-06 03:53:20 +00:00
}
2022-01-18 02:18:49 +00:00
}
2022-08-15 07:35:30 +00:00
2022-01-18 02:18:49 +00:00
} )
It ( "Backups in object storage should be synced to a new Velero successfully" , func ( ) {
test . Init ( )
2023-04-26 02:33:21 +00:00
ctx , ctxCancel := context . WithTimeout ( context . Background ( ) , 30 * time . Minute )
defer ctxCancel ( )
2022-01-18 02:18:49 +00:00
By ( fmt . Sprintf ( "Prepare workload as target to backup by creating namespace %s namespace" , test . testNS ) )
2023-04-16 17:59:21 +00:00
Expect ( CreateNamespace ( ctx , * VeleroCfg . ClientToInstallVelero , test . testNS ) ) . To ( Succeed ( ) ,
2022-01-18 02:18:49 +00:00
fmt . Sprintf ( "Failed to create %s namespace" , test . testNS ) )
2022-08-15 07:35:30 +00:00
if ! VeleroCfg . Debug {
defer func ( ) {
2023-04-16 17:59:21 +00:00
Expect ( DeleteNamespace ( ctx , * VeleroCfg . ClientToInstallVelero , test . testNS , false ) ) . To ( Succeed ( ) , fmt . Sprintf ( "Failed to delete the namespace %s" , test . testNS ) )
2022-08-15 07:35:30 +00:00
} ( )
}
2022-01-18 02:18:49 +00:00
2022-05-09 06:51:18 +00:00
var BackupCfg BackupConfig
BackupCfg . BackupName = test . backupName
BackupCfg . Namespace = test . testNS
BackupCfg . BackupLocation = ""
BackupCfg . UseVolumeSnapshots = false
BackupCfg . Selector = ""
2022-01-18 02:18:49 +00:00
By ( fmt . Sprintf ( "Backup the workload in %s namespace" , test . testNS ) , func ( ) {
2023-04-16 17:59:21 +00:00
Expect ( VeleroBackupNamespace ( ctx , VeleroCfg . VeleroCLI ,
2022-09-13 09:12:37 +00:00
VeleroCfg . VeleroNamespace , BackupCfg ) ) . To ( Succeed ( ) , func ( ) string {
2022-01-18 02:18:49 +00:00
RunDebug ( context . Background ( ) , VeleroCfg . VeleroCLI , VeleroCfg . VeleroNamespace , test . backupName , "" )
2022-09-13 09:12:37 +00:00
return "Fail to backup workload"
} )
2022-01-18 02:18:49 +00:00
} )
By ( "Uninstall velero" , func ( ) {
2023-04-16 17:59:21 +00:00
Expect ( VeleroUninstall ( ctx , VeleroCfg . VeleroCLI , VeleroCfg . VeleroNamespace ) ) . To ( Succeed ( ) )
2022-01-18 02:18:49 +00:00
} )
By ( "Install velero" , func ( ) {
2023-01-30 03:58:41 +00:00
veleroCfg := VeleroCfg
veleroCfg . UseVolumeSnapshots = false
2023-06-20 10:24:11 +00:00
Expect ( VeleroInstall ( ctx , & VeleroCfg , false ) ) . To ( Succeed ( ) )
2022-01-18 02:18:49 +00:00
} )
By ( "Check all backups in object storage are synced to Velero" , func ( ) {
2023-04-26 02:33:21 +00:00
Expect ( test . IsBackupsSynced ( ctx , ctxCancel ) ) . To ( Succeed ( ) , fmt . Sprintf ( "Failed to sync backup %s from object storage" , test . backupName ) )
2022-01-18 02:18:49 +00:00
} )
} )
It ( "Deleted backups in object storage are synced to be deleted in Velero" , func ( ) {
test . Init ( )
2023-04-26 02:33:21 +00:00
ctx , ctxCancel := context . WithTimeout ( context . Background ( ) , 30 * time . Minute )
2023-04-16 17:59:21 +00:00
defer ctxCancel ( )
2022-01-18 02:18:49 +00:00
By ( fmt . Sprintf ( "Prepare workload as target to backup by creating namespace in %s namespace" , test . testNS ) , func ( ) {
2023-04-16 17:59:21 +00:00
Expect ( CreateNamespace ( ctx , * VeleroCfg . ClientToInstallVelero , test . testNS ) ) . To ( Succeed ( ) ,
2022-01-18 02:18:49 +00:00
fmt . Sprintf ( "Failed to create %s namespace" , test . testNS ) )
} )
2022-05-09 06:51:18 +00:00
if ! VeleroCfg . Debug {
defer func ( ) {
2023-04-16 17:59:21 +00:00
Expect ( DeleteNamespace ( ctx , * VeleroCfg . ClientToInstallVelero , test . testNS , false ) ) . To ( Succeed ( ) ,
2022-05-09 06:51:18 +00:00
fmt . Sprintf ( "Failed to delete the namespace %s" , test . testNS ) )
} ( )
}
var BackupCfg BackupConfig
BackupCfg . BackupName = test . backupName
BackupCfg . Namespace = test . testNS
BackupCfg . BackupLocation = ""
BackupCfg . UseVolumeSnapshots = false
BackupCfg . Selector = ""
2022-01-18 02:18:49 +00:00
By ( fmt . Sprintf ( "Backup the workload in %s namespace" , test . testNS ) , func ( ) {
2023-04-16 17:59:21 +00:00
Expect ( VeleroBackupNamespace ( ctx , VeleroCfg . VeleroCLI ,
2022-09-13 09:12:37 +00:00
VeleroCfg . VeleroNamespace , BackupCfg ) ) . To ( Succeed ( ) , func ( ) string {
2022-01-18 02:18:49 +00:00
RunDebug ( context . Background ( ) , VeleroCfg . VeleroCLI , VeleroCfg . VeleroNamespace , test . backupName , "" )
2022-09-13 09:12:37 +00:00
return "Fail to backup workload"
} )
2022-01-18 02:18:49 +00:00
} )
By ( fmt . Sprintf ( "Delete %s backup files in object store" , test . backupName ) , func ( ) {
err = DeleteObjectsInBucket ( VeleroCfg . CloudProvider , VeleroCfg . CloudCredentialsFile , VeleroCfg . BSLBucket ,
VeleroCfg . BSLPrefix , VeleroCfg . BSLConfig , test . backupName , BackupObjectsPrefix )
Expect ( err ) . To ( Succeed ( ) , fmt . Sprintf ( "Failed to delete object in bucket %s with err %v" , test . backupName , err ) )
} )
By ( fmt . Sprintf ( "Check %s backup files in object store is deleted" , test . backupName ) , func ( ) {
err = ObjectsShouldNotBeInBucket ( VeleroCfg . CloudProvider , VeleroCfg . CloudCredentialsFile , VeleroCfg . BSLBucket ,
VeleroCfg . BSLPrefix , VeleroCfg . BSLConfig , test . backupName , BackupObjectsPrefix , 1 )
Expect ( err ) . To ( Succeed ( ) , fmt . Sprintf ( "Failed to delete object in bucket %s with err %v" , test . backupName , err ) )
} )
By ( "Check if backups are deleted as a result of sync from BSL" , func ( ) {
2023-04-16 17:59:21 +00:00
Expect ( WaitBackupDeleted ( ctx , VeleroCfg . VeleroCLI , test . backupName , time . Minute * 10 ) ) . To ( Succeed ( ) , fmt . Sprintf ( "Failed to check backup %s deleted" , test . backupName ) )
2022-01-18 02:18:49 +00:00
} )
} )
}
2023-04-26 02:33:21 +00:00
func ( b * SyncBackups ) IsBackupsSynced ( ctx context . Context , ctxCancel context . CancelFunc ) error {
2023-04-16 17:59:21 +00:00
defer ctxCancel ( )
return WaitForBackupToBeCreated ( ctx , VeleroCfg . VeleroCLI , b . backupName , 10 * time . Minute )
2022-01-18 02:18:49 +00:00
}