2022-05-09 06:51:18 +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 .
* /
* /
package backups
import (
"context"
"flag"
"fmt"
"math/rand"
"strings"
"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"
2022-05-09 06:51:18 +00:00
2023-07-31 02:25:48 +00:00
. "github.com/vmware-tanzu/velero/test/util/kibishii"
. "github.com/vmware-tanzu/velero/test/util/providers"
. "github.com/vmware-tanzu/velero/test/util/velero"
2022-05-09 06:51:18 +00:00
)
type TTL struct {
testNS string
backupName string
restoreName string
ttl time . Duration
}
func ( b * TTL ) Init ( ) {
rand . Seed ( time . Now ( ) . UnixNano ( ) )
UUIDgen , _ = uuid . NewRandom ( )
b . testNS = "backup-ttl-test-" + UUIDgen . String ( )
b . backupName = "backup-ttl-test-" + UUIDgen . String ( )
b . restoreName = "restore-ttl-test-" + UUIDgen . String ( )
2023-05-16 03:33:51 +00:00
b . ttl = 10 * time . Minute
2022-05-09 06:51:18 +00:00
}
func TTLTest ( ) {
2022-09-13 04:42:06 +00:00
var err error
2023-01-30 03:58:41 +00:00
var veleroCfg VeleroConfig
2022-05-09 06:51:18 +00:00
useVolumeSnapshots := true
test := new ( TTL )
2023-01-30 03:58:41 +00:00
veleroCfg = VeleroCfg
client := * veleroCfg . ClientToInstallVelero
2022-09-13 04:42:06 +00:00
2022-05-09 06:51:18 +00:00
BeforeEach ( func ( ) {
flag . Parse ( )
2023-01-30 03:58:41 +00:00
veleroCfg = VeleroCfg
if veleroCfg . InstallVelero {
2022-05-09 06:51:18 +00:00
// Make sure GCFrequency is shorter than backup TTL
2023-01-30 03:58:41 +00:00
veleroCfg . GCFrequency = "4m0s"
veleroCfg . UseVolumeSnapshots = useVolumeSnapshots
2023-06-20 10:24:11 +00:00
Expect ( VeleroInstall ( context . Background ( ) , & veleroCfg , false ) ) . To ( Succeed ( ) )
2022-05-09 06:51:18 +00:00
}
} )
AfterEach ( func ( ) {
2023-01-30 03:58:41 +00:00
veleroCfg . GCFrequency = ""
if ! veleroCfg . Debug {
2022-08-15 07:35:30 +00:00
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
} )
2023-07-27 08:04:07 +00:00
ctx , ctxCancel := context . WithTimeout ( context . Background ( ) , time . Minute * 5 )
defer ctxCancel ( )
2023-01-30 03:58:41 +00:00
if veleroCfg . InstallVelero {
2023-07-27 08:04:07 +00:00
Expect ( VeleroUninstall ( ctx , veleroCfg . VeleroCLI , veleroCfg . VeleroNamespace ) ) . To ( Succeed ( ) )
2022-05-09 06:51:18 +00:00
}
2023-04-16 17:59:21 +00:00
Expect ( DeleteNamespace ( ctx , client , test . testNS , false ) ) . To ( Succeed ( ) , fmt . Sprintf ( "Failed to delete the namespace %s" , test . testNS ) )
2022-05-09 06:51:18 +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 ( ) , 1 * time . Hour )
defer ctxCancel ( )
2022-05-09 06:51:18 +00:00
By ( fmt . Sprintf ( "Prepare workload as target to backup by creating namespace %s namespace" , test . testNS ) , func ( ) {
2023-04-16 17:59:21 +00:00
Expect ( CreateNamespace ( ctx , client , test . testNS ) ) . To ( Succeed ( ) ,
2022-05-09 06:51:18 +00:00
fmt . Sprintf ( "Failed to create %s namespace" , test . testNS ) )
} )
By ( "Deploy sample workload of Kibishii" , func ( ) {
2023-04-16 17:59:21 +00:00
Expect ( KibishiiPrepareBeforeBackup ( ctx , client , veleroCfg . CloudProvider ,
2023-01-30 03:58:41 +00:00
test . testNS , veleroCfg . RegistryCredentialFile , veleroCfg . Features ,
veleroCfg . KibishiiDirectory , useVolumeSnapshots , DefaultKibishiiData ) ) . To ( Succeed ( ) )
2022-05-09 06:51:18 +00:00
} )
var BackupCfg BackupConfig
BackupCfg . BackupName = test . backupName
BackupCfg . Namespace = test . testNS
BackupCfg . BackupLocation = ""
BackupCfg . UseVolumeSnapshots = useVolumeSnapshots
BackupCfg . Selector = ""
BackupCfg . TTL = test . ttl
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 , veleroCfg . VeleroNamespace , BackupCfg ) ) . To ( Succeed ( ) , func ( ) string {
2023-01-30 03:58:41 +00:00
RunDebug ( context . Background ( ) , veleroCfg . VeleroCLI , veleroCfg . VeleroNamespace , test . backupName , "" )
2022-05-09 06:51:18 +00:00
return "Fail to backup workload"
} )
} )
var snapshotCheckPoint SnapshotCheckPoint
if useVolumeSnapshots {
2023-01-30 03:58:41 +00:00
if veleroCfg . CloudProvider == "vsphere" {
2022-05-09 06:51:18 +00:00
// TODO - remove after upload progress monitoring is implemented
By ( "Waiting for vSphere uploads to complete" , func ( ) {
2023-04-16 17:59:21 +00:00
Expect ( WaitForVSphereUploadCompletion ( ctx , time . Hour ,
2023-01-30 03:58:41 +00:00
test . testNS , 2 ) ) . To ( Succeed ( ) )
2022-05-09 06:51:18 +00:00
} )
}
2023-05-12 06:06:22 +00:00
snapshotCheckPoint , err = GetSnapshotCheckPoint ( client , veleroCfg , 2 , test . testNS , test . backupName , KibishiiPVCNameList )
2022-05-09 06:51:18 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) , "Fail to get Azure CSI snapshot checkpoint" )
2023-01-30 03:58:41 +00:00
Expect ( SnapshotsShouldBeCreatedInCloud ( veleroCfg . CloudProvider ,
veleroCfg . CloudCredentialsFile , veleroCfg . BSLBucket , veleroCfg . BSLConfig ,
2022-05-09 06:51:18 +00:00
test . backupName , snapshotCheckPoint ) ) . NotTo ( HaveOccurred ( ) , "Fail to get Azure CSI snapshot checkpoint" )
}
By ( fmt . Sprintf ( "Simulating a disaster by removing namespace %s\n" , BackupCfg . BackupName ) , func ( ) {
2023-04-16 17:59:21 +00:00
Expect ( DeleteNamespace ( ctx , client , BackupCfg . BackupName , true ) ) . To ( Succeed ( ) ,
2022-05-09 06:51:18 +00:00
fmt . Sprintf ( "Failed to delete namespace %s" , BackupCfg . BackupName ) )
} )
2023-01-30 03:58:41 +00:00
if veleroCfg . CloudProvider == "aws" && useVolumeSnapshots {
2022-05-09 06:51:18 +00:00
fmt . Println ( "Waiting 7 minutes to make sure the snapshots are ready..." )
time . Sleep ( 7 * time . Minute )
}
By ( fmt . Sprintf ( "Restore %s" , test . testNS ) , func ( ) {
2023-04-16 17:59:21 +00:00
Expect ( VeleroRestore ( ctx , veleroCfg . VeleroCLI ,
2023-01-30 03:58:41 +00:00
veleroCfg . VeleroNamespace , test . restoreName , test . backupName , "" ) ) . To ( Succeed ( ) , func ( ) string {
2023-04-16 17:59:21 +00:00
RunDebug ( ctx , veleroCfg . VeleroCLI ,
2023-01-30 03:58:41 +00:00
veleroCfg . VeleroNamespace , "" , test . restoreName )
2022-05-09 06:51:18 +00:00
return "Fail to restore workload"
} )
} )
By ( "Associated Restores should be created" , func ( ) {
2023-01-30 03:58:41 +00:00
Expect ( ObjectsShouldBeInBucket ( veleroCfg . CloudProvider ,
veleroCfg . CloudCredentialsFile , veleroCfg . BSLBucket ,
veleroCfg . BSLPrefix , veleroCfg . BSLConfig , test . restoreName ,
2022-05-09 06:51:18 +00:00
RestoreObjectsPrefix ) ) . NotTo ( HaveOccurred ( ) , "Fail to get restore object" )
} )
By ( "Check TTL was set correctly" , func ( ) {
2023-04-16 17:59:21 +00:00
ttl , err := GetBackupTTL ( ctx , veleroCfg . VeleroNamespace , test . backupName )
2022-05-09 06:51:18 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) , "Fail to get Azure CSI snapshot checkpoint" )
t , _ := time . ParseDuration ( strings . ReplaceAll ( ttl , "'" , "" ) )
fmt . Println ( t . Round ( time . Minute ) . String ( ) )
Expect ( t ) . To ( Equal ( test . ttl ) )
} )
By ( fmt . Sprintf ( "Waiting %s minutes for removing backup ralated resources by GC" , test . ttl . String ( ) ) , func ( ) {
time . Sleep ( test . ttl )
} )
By ( "Check if backups are deleted by GC" , func ( ) {
2023-04-16 17:59:21 +00:00
Expect ( WaitBackupDeleted ( ctx , veleroCfg . VeleroCLI , test . backupName , time . Minute * 10 ) ) . To ( Succeed ( ) , fmt . Sprintf ( "Backup %s was not deleted by GC" , test . backupName ) )
2022-05-09 06:51:18 +00:00
} )
By ( "Backup file from cloud object storage should be deleted" , func ( ) {
2023-01-30 03:58:41 +00:00
Expect ( ObjectsShouldNotBeInBucket ( veleroCfg . CloudProvider ,
veleroCfg . CloudCredentialsFile , veleroCfg . BSLBucket ,
veleroCfg . BSLPrefix , veleroCfg . BSLConfig , test . backupName ,
2022-05-09 06:51:18 +00:00
BackupObjectsPrefix , 5 ) ) . NotTo ( HaveOccurred ( ) , "Fail to get Azure CSI snapshot checkpoint" )
} )
By ( "PersistentVolume snapshots should be deleted" , func ( ) {
if useVolumeSnapshots {
2023-01-30 03:58:41 +00:00
Expect ( SnapshotsShouldNotExistInCloud ( veleroCfg . CloudProvider ,
veleroCfg . CloudCredentialsFile , veleroCfg . BSLBucket , veleroCfg . BSLConfig ,
2022-05-09 06:51:18 +00:00
test . backupName , snapshotCheckPoint ) ) . NotTo ( HaveOccurred ( ) , "Fail to get Azure CSI snapshot checkpoint" )
}
} )
By ( "Associated Restores should be deleted" , func ( ) {
2023-01-30 03:58:41 +00:00
Expect ( ObjectsShouldNotBeInBucket ( veleroCfg . CloudProvider ,
veleroCfg . CloudCredentialsFile , veleroCfg . BSLBucket ,
veleroCfg . BSLPrefix , veleroCfg . BSLConfig , test . restoreName ,
2022-05-09 06:51:18 +00:00
RestoreObjectsPrefix , 5 ) ) . NotTo ( HaveOccurred ( ) , "Fail to get restore object" )
} )
} )
}