replace TestBackup with pkg/backup.Builder (#1593)

Signed-off-by: Steve Kriss <krisss@vmware.com>
pull/1591/head
Steve Kriss 2019-06-21 15:08:08 -06:00 committed by Adnan Abdulhussein
parent d916ae0a25
commit 19052994ed
10 changed files with 330 additions and 405 deletions

View File

@ -17,6 +17,8 @@ limitations under the License.
package backup
import (
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
velerov1api "github.com/heptio/velero/pkg/apis/velero/v1"
@ -66,6 +68,25 @@ func (b *Builder) Name(name string) *Builder {
return b
}
// Labels sets the Backup's labels.
func (b *Builder) Labels(vals ...string) *Builder {
if b.backup.Labels == nil {
b.backup.Labels = map[string]string{}
}
// if we don't have an even number of values, e.g. a key and a value
// for each pair, add an empty-string value at the end to serve as
// the default value for the last key.
if len(vals)%2 != 0 {
vals = append(vals, "")
}
for i := 0; i < len(vals); i += 2 {
b.backup.Labels[vals[i]] = vals[i+1]
}
return b
}
// IncludedNamespaces sets the Backup's included namespaces.
func (b *Builder) IncludedNamespaces(namespaces ...string) *Builder {
b.backup.Spec.IncludedNamespaces = namespaces
@ -107,3 +128,45 @@ func (b *Builder) SnapshotVolumes(val bool) *Builder {
b.backup.Spec.SnapshotVolumes = &val
return b
}
// Phase sets the Backup's phase.
func (b *Builder) Phase(phase velerov1api.BackupPhase) *Builder {
b.backup.Status.Phase = phase
return b
}
// StorageLocation sets the Backup's storage location.
func (b *Builder) StorageLocation(location string) *Builder {
b.backup.Spec.StorageLocation = location
return b
}
// VolumeSnapshotLocations sets the Backup's volume snapshot locations.
func (b *Builder) VolumeSnapshotLocations(locations ...string) *Builder {
b.backup.Spec.VolumeSnapshotLocations = locations
return b
}
// TTL sets the Backup's TTL.
func (b *Builder) TTL(ttl time.Duration) *Builder {
b.backup.Spec.TTL.Duration = ttl
return b
}
// Expiration sets the Backup's expiration.
func (b *Builder) Expiration(val time.Time) *Builder {
b.backup.Status.Expiration.Time = val
return b
}
// StartTimestamp sets the Backup's start timestamp.
func (b *Builder) StartTimestamp(val time.Time) *Builder {
b.backup.Status.StartTimestamp.Time = val
return b
}
// NoTypeMeta removes the type meta from the Backup.
func (b *Builder) NoTypeMeta() *Builder {
b.backup.TypeMeta = metav1.TypeMeta{}
return b
}

View File

@ -33,7 +33,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/clock"
v1 "github.com/heptio/velero/pkg/apis/velero/v1"
velerov1api "github.com/heptio/velero/pkg/apis/velero/v1"
pkgbackup "github.com/heptio/velero/pkg/backup"
"github.com/heptio/velero/pkg/generated/clientset/versioned/fake"
@ -57,11 +56,15 @@ func (b *fakeBackupper) Backup(logger logrus.FieldLogger, backup *pkgbackup.Requ
return args.Error(0)
}
func defaultBackup() *pkgbackup.Builder {
return pkgbackup.NewNamedBuilder(velerov1api.DefaultNamespace, "backup-1")
}
func TestProcessBackupNonProcessedItems(t *testing.T) {
tests := []struct {
name string
key string
backup *v1.Backup
backup *velerov1api.Backup
}{
{
name: "bad key does not return error",
@ -74,22 +77,22 @@ func TestProcessBackupNonProcessedItems(t *testing.T) {
{
name: "FailedValidation backup is not processed",
key: "velero/backup-1",
backup: velerotest.NewTestBackup().WithName("backup-1").WithPhase(v1.BackupPhaseFailedValidation).Backup,
backup: defaultBackup().Phase(velerov1api.BackupPhaseFailedValidation).Backup(),
},
{
name: "InProgress backup is not processed",
key: "velero/backup-1",
backup: velerotest.NewTestBackup().WithName("backup-1").WithPhase(v1.BackupPhaseInProgress).Backup,
backup: defaultBackup().Phase(velerov1api.BackupPhaseInProgress).Backup(),
},
{
name: "Completed backup is not processed",
key: "velero/backup-1",
backup: velerotest.NewTestBackup().WithName("backup-1").WithPhase(v1.BackupPhaseCompleted).Backup,
backup: defaultBackup().Phase(velerov1api.BackupPhaseCompleted).Backup(),
},
{
name: "Failed backup is not processed",
key: "velero/backup-1",
backup: velerotest.NewTestBackup().WithName("backup-1").WithPhase(v1.BackupPhaseFailed).Backup,
backup: defaultBackup().Phase(velerov1api.BackupPhaseFailed).Backup(),
},
}
@ -125,30 +128,30 @@ func TestProcessBackupValidationFailures(t *testing.T) {
tests := []struct {
name string
backup *v1.Backup
backupLocation *v1.BackupStorageLocation
backup *velerov1api.Backup
backupLocation *velerov1api.BackupStorageLocation
expectedErrs []string
}{
{
name: "invalid included/excluded resources fails validation",
backup: velerotest.NewTestBackup().WithName("backup-1").WithIncludedResources("foo").WithExcludedResources("foo").Backup,
backup: defaultBackup().IncludedResources("foo").ExcludedResources("foo").Backup(),
backupLocation: defaultBackupLocation,
expectedErrs: []string{"Invalid included/excluded resource lists: excludes list cannot contain an item in the includes list: foo"},
},
{
name: "invalid included/excluded namespaces fails validation",
backup: velerotest.NewTestBackup().WithName("backup-1").WithIncludedNamespaces("foo").WithExcludedNamespaces("foo").Backup,
backup: defaultBackup().IncludedNamespaces("foo").ExcludedNamespaces("foo").Backup(),
backupLocation: defaultBackupLocation,
expectedErrs: []string{"Invalid included/excluded namespace lists: excludes list cannot contain an item in the includes list: foo"},
},
{
name: "non-existent backup location fails validation",
backup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("nonexistent").Backup,
backup: defaultBackup().StorageLocation("nonexistent").Backup(),
expectedErrs: []string{"a BackupStorageLocation CRD with the name specified in the backup spec needs to be created before this backup can be executed. Error: backupstoragelocation.velero.io \"nonexistent\" not found"},
},
{
name: "backup for read-only backup location fails validation",
backup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("read-only").Backup,
backup: defaultBackup().StorageLocation("read-only").Backup(),
backupLocation: velerotest.NewTestBackupStorageLocation().WithName("read-only").WithAccessMode(velerov1api.BackupStorageLocationAccessModeReadOnly).BackupStorageLocation,
expectedErrs: []string{"backup can't be created because backup storage location read-only is currently in read-only mode"},
},
@ -187,7 +190,7 @@ func TestProcessBackupValidationFailures(t *testing.T) {
res, err := clientset.VeleroV1().Backups(test.backup.Namespace).Get(test.backup.Name, metav1.GetOptions{})
require.NoError(t, err)
assert.Equal(t, v1.BackupPhaseFailedValidation, res.Status.Phase)
assert.Equal(t, velerov1api.BackupPhaseFailedValidation, res.Status.Phase)
assert.Equal(t, test.expectedErrs, res.Status.ValidationErrors)
// Any backup that would actually proceed to processing will cause a segfault because this
@ -201,19 +204,19 @@ func TestProcessBackupValidationFailures(t *testing.T) {
func TestBackupLocationLabel(t *testing.T) {
tests := []struct {
name string
backup *v1.Backup
backupLocation *v1.BackupStorageLocation
backup *velerov1api.Backup
backupLocation *velerov1api.BackupStorageLocation
expectedBackupLocation string
}{
{
name: "valid backup location name should be used as a label",
backup: velerotest.NewTestBackup().WithName("backup-1").Backup,
backup: defaultBackup().Backup(),
backupLocation: velerotest.NewTestBackupStorageLocation().WithName("loc-1").BackupStorageLocation,
expectedBackupLocation: "loc-1",
},
{
name: "invalid storage location name should be handled while creating label",
backup: velerotest.NewTestBackup().WithName("backup-1").Backup,
backup: defaultBackup().Backup(),
backupLocation: velerotest.NewTestBackupStorageLocation().
WithName("defaultdefaultdefaultdefaultdefaultdefaultdefaultdefaultdefaultdefault").BackupStorageLocation,
expectedBackupLocation: "defaultdefaultdefaultdefaultdefaultdefaultdefaultdefaultd58343f",
@ -246,7 +249,6 @@ func TestBackupLocationLabel(t *testing.T) {
}
func TestDefaultBackupTTL(t *testing.T) {
var (
defaultBackupTTL = metav1.Duration{Duration: 24 * 30 * time.Hour}
)
@ -257,20 +259,20 @@ func TestDefaultBackupTTL(t *testing.T) {
tests := []struct {
name string
backup *v1.Backup
backupLocation *v1.BackupStorageLocation
backup *velerov1api.Backup
backupLocation *velerov1api.BackupStorageLocation
expectedTTL metav1.Duration
expectedExpiration metav1.Time
}{
{
name: "backup with no TTL specified",
backup: velerotest.NewTestBackup().WithName("backup-1").Backup,
backup: defaultBackup().Backup(),
expectedTTL: defaultBackupTTL,
expectedExpiration: metav1.NewTime(now.Add(defaultBackupTTL.Duration)),
},
{
name: "backup with TTL specified",
backup: velerotest.NewTestBackup().WithName("backup-1").WithTTL(1 * time.Hour).Backup,
backup: defaultBackup().TTL(time.Hour).Backup(),
expectedTTL: metav1.Duration{Duration: 1 * time.Hour},
expectedExpiration: metav1.NewTime(now.Add(1 * time.Hour)),
},
@ -309,30 +311,34 @@ func TestProcessBackupCompletions(t *testing.T) {
tests := []struct {
name string
backup *v1.Backup
backupLocation *v1.BackupStorageLocation
expectedResult *v1.Backup
backup *velerov1api.Backup
backupLocation *velerov1api.BackupStorageLocation
expectedResult *velerov1api.Backup
backupExists bool
existenceCheckError error
}{
// Completed
{
name: "backup with no backup location gets the default",
backup: velerotest.NewTestBackup().WithName("backup-1").Backup,
backup: defaultBackup().Backup(),
backupLocation: defaultBackupLocation,
expectedResult: &v1.Backup{
expectedResult: &velerov1api.Backup{
TypeMeta: metav1.TypeMeta{
Kind: "Backup",
APIVersion: "velero.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: v1.DefaultNamespace,
Namespace: velerov1api.DefaultNamespace,
Name: "backup-1",
Labels: map[string]string{
"velero.io/storage-location": "loc-1",
},
},
Spec: v1.BackupSpec{
Spec: velerov1api.BackupSpec{
StorageLocation: defaultBackupLocation.Name,
},
Status: v1.BackupStatus{
Phase: v1.BackupPhaseCompleted,
Status: velerov1api.BackupStatus{
Phase: velerov1api.BackupPhaseCompleted,
Version: 1,
StartTimestamp: metav1.NewTime(now),
CompletionTimestamp: metav1.NewTime(now),
@ -342,21 +348,25 @@ func TestProcessBackupCompletions(t *testing.T) {
},
{
name: "backup with a specific backup location keeps it",
backup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("alt-loc").Backup,
backup: defaultBackup().StorageLocation("alt-loc").Backup(),
backupLocation: velerotest.NewTestBackupStorageLocation().WithName("alt-loc").WithObjectStorage("store-1").BackupStorageLocation,
expectedResult: &v1.Backup{
expectedResult: &velerov1api.Backup{
TypeMeta: metav1.TypeMeta{
Kind: "Backup",
APIVersion: "velero.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: v1.DefaultNamespace,
Namespace: velerov1api.DefaultNamespace,
Name: "backup-1",
Labels: map[string]string{
"velero.io/storage-location": "alt-loc",
},
},
Spec: v1.BackupSpec{
Spec: velerov1api.BackupSpec{
StorageLocation: "alt-loc",
},
Status: v1.BackupStatus{
Phase: v1.BackupPhaseCompleted,
Status: velerov1api.BackupStatus{
Phase: velerov1api.BackupPhaseCompleted,
Version: 1,
StartTimestamp: metav1.NewTime(now),
CompletionTimestamp: metav1.NewTime(now),
@ -366,25 +376,29 @@ func TestProcessBackupCompletions(t *testing.T) {
},
{
name: "backup for a location with ReadWrite access mode gets processed",
backup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("read-write").Backup,
backup: defaultBackup().StorageLocation("read-write").Backup(),
backupLocation: velerotest.NewTestBackupStorageLocation().
WithName("read-write").
WithObjectStorage("store-1").
WithAccessMode(v1.BackupStorageLocationAccessModeReadWrite).
WithAccessMode(velerov1api.BackupStorageLocationAccessModeReadWrite).
BackupStorageLocation,
expectedResult: &v1.Backup{
expectedResult: &velerov1api.Backup{
TypeMeta: metav1.TypeMeta{
Kind: "Backup",
APIVersion: "velero.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: v1.DefaultNamespace,
Namespace: velerov1api.DefaultNamespace,
Name: "backup-1",
Labels: map[string]string{
"velero.io/storage-location": "read-write",
},
},
Spec: v1.BackupSpec{
Spec: velerov1api.BackupSpec{
StorageLocation: "read-write",
},
Status: v1.BackupStatus{
Phase: v1.BackupPhaseCompleted,
Status: velerov1api.BackupStatus{
Phase: velerov1api.BackupPhaseCompleted,
Version: 1,
StartTimestamp: metav1.NewTime(now),
CompletionTimestamp: metav1.NewTime(now),
@ -394,22 +408,26 @@ func TestProcessBackupCompletions(t *testing.T) {
},
{
name: "backup with a TTL has expiration set",
backup: velerotest.NewTestBackup().WithName("backup-1").WithTTL(10 * time.Minute).Backup,
backup: defaultBackup().TTL(10 * time.Minute).Backup(),
backupLocation: defaultBackupLocation,
expectedResult: &v1.Backup{
expectedResult: &velerov1api.Backup{
TypeMeta: metav1.TypeMeta{
Kind: "Backup",
APIVersion: "velero.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: v1.DefaultNamespace,
Namespace: velerov1api.DefaultNamespace,
Name: "backup-1",
Labels: map[string]string{
"velero.io/storage-location": "loc-1",
},
},
Spec: v1.BackupSpec{
Spec: velerov1api.BackupSpec{
TTL: metav1.Duration{Duration: 10 * time.Minute},
StorageLocation: defaultBackupLocation.Name,
},
Status: v1.BackupStatus{
Phase: v1.BackupPhaseCompleted,
Status: velerov1api.BackupStatus{
Phase: velerov1api.BackupPhaseCompleted,
Version: 1,
Expiration: metav1.NewTime(now.Add(10 * time.Minute)),
StartTimestamp: metav1.NewTime(now),
@ -420,21 +438,25 @@ func TestProcessBackupCompletions(t *testing.T) {
{
name: "backup without an existing backup will succeed",
backupExists: false,
backup: velerotest.NewTestBackup().WithName("backup-1").Backup,
backup: defaultBackup().Backup(),
backupLocation: defaultBackupLocation,
expectedResult: &v1.Backup{
expectedResult: &velerov1api.Backup{
TypeMeta: metav1.TypeMeta{
Kind: "Backup",
APIVersion: "velero.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: v1.DefaultNamespace,
Namespace: velerov1api.DefaultNamespace,
Name: "backup-1",
Labels: map[string]string{
"velero.io/storage-location": "loc-1",
},
},
Spec: v1.BackupSpec{
Spec: velerov1api.BackupSpec{
StorageLocation: defaultBackupLocation.Name,
},
Status: v1.BackupStatus{
Phase: v1.BackupPhaseCompleted,
Status: velerov1api.BackupStatus{
Phase: velerov1api.BackupPhaseCompleted,
Version: 1,
StartTimestamp: metav1.NewTime(now),
CompletionTimestamp: metav1.NewTime(now),
@ -447,21 +469,25 @@ func TestProcessBackupCompletions(t *testing.T) {
{
name: "backup with existing backup will fail",
backupExists: true,
backup: velerotest.NewTestBackup().WithName("backup-1").Backup,
backup: defaultBackup().Backup(),
backupLocation: defaultBackupLocation,
expectedResult: &v1.Backup{
expectedResult: &velerov1api.Backup{
TypeMeta: metav1.TypeMeta{
Kind: "Backup",
APIVersion: "velero.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: v1.DefaultNamespace,
Namespace: velerov1api.DefaultNamespace,
Name: "backup-1",
Labels: map[string]string{
"velero.io/storage-location": "loc-1",
},
},
Spec: v1.BackupSpec{
Spec: velerov1api.BackupSpec{
StorageLocation: defaultBackupLocation.Name,
},
Status: v1.BackupStatus{
Phase: v1.BackupPhaseFailed,
Status: velerov1api.BackupStatus{
Phase: velerov1api.BackupPhaseFailed,
Version: 1,
StartTimestamp: metav1.NewTime(now),
CompletionTimestamp: metav1.NewTime(now),
@ -471,22 +497,26 @@ func TestProcessBackupCompletions(t *testing.T) {
},
{
name: "error when checking if backup exists will cause backup to fail",
backup: velerotest.NewTestBackup().WithName("backup-1").Backup,
backup: defaultBackup().Backup(),
existenceCheckError: errors.New("Backup already exists in object storage"),
backupLocation: defaultBackupLocation,
expectedResult: &v1.Backup{
expectedResult: &velerov1api.Backup{
TypeMeta: metav1.TypeMeta{
Kind: "Backup",
APIVersion: "velero.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: v1.DefaultNamespace,
Namespace: velerov1api.DefaultNamespace,
Name: "backup-1",
Labels: map[string]string{
"velero.io/storage-location": "loc-1",
},
},
Spec: v1.BackupSpec{
Spec: velerov1api.BackupSpec{
StorageLocation: defaultBackupLocation.Name,
},
Status: v1.BackupStatus{
Phase: v1.BackupPhaseFailed,
Status: velerov1api.BackupStatus{
Phase: velerov1api.BackupPhaseFailed,
Version: 1,
StartTimestamp: metav1.NewTime(now),
CompletionTimestamp: metav1.NewTime(now),
@ -518,7 +548,7 @@ func TestProcessBackupCompletions(t *testing.T) {
metrics: metrics.NewServerMetrics(),
clock: clock.NewFakeClock(now),
newPluginManager: func(logrus.FieldLogger) clientmgmt.Manager { return pluginManager },
newBackupStore: func(*v1.BackupStorageLocation, persistence.ObjectStoreGetter, logrus.FieldLogger) (persistence.BackupStore, error) {
newBackupStore: func(*velerov1api.BackupStorageLocation, persistence.ObjectStoreGetter, logrus.FieldLogger) (persistence.BackupStore, error) {
return backupStore, nil
},
backupper: backupper,
@ -569,7 +599,7 @@ func TestProcessBackupCompletions(t *testing.T) {
func TestValidateAndGetSnapshotLocations(t *testing.T) {
tests := []struct {
name string
backup *velerotest.TestBackup
backup *velerov1api.Backup
locations []*velerotest.TestVolumeSnapshotLocation
defaultLocations map[string]string
expectedVolumeSnapshotLocationNames []string // adding these in the expected order will allow to test with better msgs in case of a test failure
@ -578,7 +608,7 @@ func TestValidateAndGetSnapshotLocations(t *testing.T) {
}{
{
name: "location name does not correspond to any existing location",
backup: velerotest.NewTestBackup().WithName("backup1").WithPhase(v1.BackupPhaseNew).WithVolumeSnapshotLocations("random-name"),
backup: defaultBackup().Phase(velerov1api.BackupPhaseNew).VolumeSnapshotLocations("random-name").Backup(),
locations: []*velerotest.TestVolumeSnapshotLocation{
velerotest.NewTestVolumeSnapshotLocation().WithProvider("aws").WithName("aws-us-east-1"),
velerotest.NewTestVolumeSnapshotLocation().WithProvider("aws").WithName("aws-us-west-1"),
@ -588,7 +618,7 @@ func TestValidateAndGetSnapshotLocations(t *testing.T) {
},
{
name: "duplicate locationName per provider: should filter out dups",
backup: velerotest.NewTestBackup().WithName("backup1").WithPhase(v1.BackupPhaseNew).WithVolumeSnapshotLocations("aws-us-west-1", "aws-us-west-1"),
backup: defaultBackup().Phase(velerov1api.BackupPhaseNew).VolumeSnapshotLocations("aws-us-west-1", "aws-us-west-1").Backup(),
locations: []*velerotest.TestVolumeSnapshotLocation{
velerotest.NewTestVolumeSnapshotLocation().WithProvider("aws").WithName("aws-us-east-1"),
velerotest.NewTestVolumeSnapshotLocation().WithProvider("aws").WithName("aws-us-west-1"),
@ -598,7 +628,7 @@ func TestValidateAndGetSnapshotLocations(t *testing.T) {
},
{
name: "multiple non-dupe location names per provider should error",
backup: velerotest.NewTestBackup().WithName("backup1").WithVolumeSnapshotLocations("aws-us-east-1", "aws-us-west-1"),
backup: defaultBackup().Phase(velerov1api.BackupPhaseNew).VolumeSnapshotLocations("aws-us-east-1", "aws-us-west-1").Backup(),
locations: []*velerotest.TestVolumeSnapshotLocation{
velerotest.NewTestVolumeSnapshotLocation().WithProvider("aws").WithName("aws-us-east-1"),
velerotest.NewTestVolumeSnapshotLocation().WithProvider("aws").WithName("aws-us-west-1"),
@ -609,7 +639,7 @@ func TestValidateAndGetSnapshotLocations(t *testing.T) {
},
{
name: "no location name for the provider exists, only one VSL for the provider: use it",
backup: velerotest.NewTestBackup().WithName("backup1").WithPhase(v1.BackupPhaseNew),
backup: defaultBackup().Phase(velerov1api.BackupPhaseNew).Backup(),
locations: []*velerotest.TestVolumeSnapshotLocation{
velerotest.NewTestVolumeSnapshotLocation().WithProvider("aws").WithName("aws-us-east-1"),
},
@ -618,7 +648,7 @@ func TestValidateAndGetSnapshotLocations(t *testing.T) {
},
{
name: "no location name for the provider exists, no default, more than one VSL for the provider: error",
backup: velerotest.NewTestBackup().WithName("backup1").WithPhase(v1.BackupPhaseNew),
backup: defaultBackup().Phase(velerov1api.BackupPhaseNew).Backup(),
locations: []*velerotest.TestVolumeSnapshotLocation{
velerotest.NewTestVolumeSnapshotLocation().WithProvider("aws").WithName("aws-us-east-1"),
velerotest.NewTestVolumeSnapshotLocation().WithProvider("aws").WithName("aws-us-west-1"),
@ -627,7 +657,7 @@ func TestValidateAndGetSnapshotLocations(t *testing.T) {
},
{
name: "no location name for the provider exists, more than one VSL for the provider: the provider's default should be added",
backup: velerotest.NewTestBackup().WithName("backup1").WithPhase(v1.BackupPhaseNew),
backup: defaultBackup().Phase(velerov1api.BackupPhaseNew).Backup(),
defaultLocations: map[string]string{"aws": "aws-us-east-1"},
locations: []*velerotest.TestVolumeSnapshotLocation{
velerotest.NewTestVolumeSnapshotLocation().WithName("aws-us-east-1").WithProvider("aws"),
@ -638,12 +668,12 @@ func TestValidateAndGetSnapshotLocations(t *testing.T) {
},
{
name: "no existing location name and no default location name given",
backup: velerotest.NewTestBackup().WithName("backup1").WithPhase(v1.BackupPhaseNew),
backup: defaultBackup().Phase(velerov1api.BackupPhaseNew).Backup(),
expectedSuccess: true,
},
{
name: "multiple location names for a provider, default location name for another provider",
backup: velerotest.NewTestBackup().WithName("backup1").WithVolumeSnapshotLocations("aws-us-west-1", "aws-us-west-1"),
backup: defaultBackup().Phase(velerov1api.BackupPhaseNew).VolumeSnapshotLocations("aws-us-west-1", "aws-us-west-1").Backup(),
defaultLocations: map[string]string{"fake-provider": "some-name"},
locations: []*velerotest.TestVolumeSnapshotLocation{
velerotest.NewTestVolumeSnapshotLocation().WithProvider("aws").WithName("aws-us-west-1"),

View File

@ -266,7 +266,7 @@ func TestBackupDeletionControllerProcessRequest(t *testing.T) {
})
t.Run("patching to InProgress fails", func(t *testing.T) {
backup := velerotest.NewTestBackup().WithName("foo").WithStorageLocation("default").Backup
backup := defaultBackup().Name("foo").StorageLocation("default").Backup()
location := velerotest.NewTestBackupStorageLocation().WithName("default").BackupStorageLocation
td := setupBackupDeletionControllerTest(backup)
@ -298,7 +298,7 @@ func TestBackupDeletionControllerProcessRequest(t *testing.T) {
})
t.Run("patching backup to Deleting fails", func(t *testing.T) {
backup := velerotest.NewTestBackup().WithName("foo").WithStorageLocation("default").Backup
backup := defaultBackup().Name("foo").StorageLocation("default").Backup()
location := velerotest.NewTestBackupStorageLocation().WithName("default").BackupStorageLocation
td := setupBackupDeletionControllerTest(backup)
@ -364,7 +364,7 @@ func TestBackupDeletionControllerProcessRequest(t *testing.T) {
})
t.Run("unable to find backup storage location", func(t *testing.T) {
backup := velerotest.NewTestBackup().WithName("foo").WithStorageLocation("default").Backup
backup := defaultBackup().Name("foo").StorageLocation("default").Backup()
td := setupBackupDeletionControllerTest(backup)
@ -390,7 +390,7 @@ func TestBackupDeletionControllerProcessRequest(t *testing.T) {
})
t.Run("backup storage location is in read-only mode", func(t *testing.T) {
backup := velerotest.NewTestBackup().WithName("foo").WithStorageLocation("default").Backup
backup := defaultBackup().Name("foo").StorageLocation("default").Backup()
location := velerotest.NewTestBackupStorageLocation().WithName("default").WithAccessMode(v1.BackupStorageLocationAccessModeReadOnly).BackupStorageLocation
td := setupBackupDeletionControllerTest(backup)
@ -419,7 +419,7 @@ func TestBackupDeletionControllerProcessRequest(t *testing.T) {
})
t.Run("full delete, no errors", func(t *testing.T) {
backup := velerotest.NewTestBackup().WithName("foo").Backup
backup := defaultBackup().Name("foo").Backup()
backup.UID = "uid"
backup.Spec.StorageLocation = "primary"
@ -565,7 +565,7 @@ func TestBackupDeletionControllerProcessRequest(t *testing.T) {
})
t.Run("full delete, no errors, with backup name greater than 63 chars", func(t *testing.T) {
backup := velerotest.NewTestBackup().WithName("the-really-long-backup-name-that-is-much-more-than-63-characters").Backup
backup := defaultBackup().Name("the-really-long-backup-name-that-is-much-more-than-63-characters").Backup()
backup.UID = "uid"
backup.Spec.StorageLocation = "primary"

View File

@ -126,11 +126,11 @@ func TestBackupSyncControllerRun(t *testing.T) {
locations: defaultLocationsList("ns-1"),
cloudBackups: map[string][]*velerov1api.Backup{
"bucket-1": {
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").Backup,
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").Backup,
defaultBackup().Namespace("ns-1").Name("backup-1").Backup(),
defaultBackup().Namespace("ns-1").Name("backup-2").Backup(),
},
"bucket-2": {
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").Backup,
defaultBackup().Namespace("ns-1").Name("backup-3").Backup(),
},
},
},
@ -140,12 +140,12 @@ func TestBackupSyncControllerRun(t *testing.T) {
locations: defaultLocationsList("velero"),
cloudBackups: map[string][]*velerov1api.Backup{
"bucket-1": {
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").Backup,
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").Backup,
defaultBackup().Namespace("ns-1").Name("backup-1").Backup(),
defaultBackup().Namespace("ns-1").Name("backup-2").Backup(),
},
"bucket-2": {
velerotest.NewTestBackup().WithNamespace("ns-2").WithName("backup-3").Backup,
velerotest.NewTestBackup().WithNamespace("velero").WithName("backup-4").Backup,
defaultBackup().Namespace("ns-2").Name("backup-3").Backup(),
defaultBackup().Namespace("velero").Name("backup-4").Backup(),
},
},
},
@ -155,19 +155,19 @@ func TestBackupSyncControllerRun(t *testing.T) {
locations: defaultLocationsList("ns-1"),
cloudBackups: map[string][]*velerov1api.Backup{
"bucket-1": {
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").Backup,
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").Backup,
defaultBackup().Namespace("ns-1").Name("backup-1").Backup(),
defaultBackup().Namespace("ns-1").Name("backup-2").Backup(),
},
"bucket-2": {
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").Backup,
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-4").Backup,
defaultBackup().Namespace("ns-1").Name("backup-3").Backup(),
defaultBackup().Namespace("ns-1").Name("backup-4").Backup(),
},
},
existingBackups: []*velerov1api.Backup{
// add a label to each existing backup so we can differentiate it from the cloud
// backup during verification
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").WithLabel("i-exist", "true").WithStorageLocation("location-1").Backup,
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").WithLabel("i-exist", "true").WithStorageLocation("location-2").Backup,
defaultBackup().Namespace("ns-1").Name("backup-1").Labels("i-exist", "true").StorageLocation("location-1").Backup(),
defaultBackup().Namespace("ns-1").Name("backup-3").Labels("i-exist", "true").StorageLocation("location-2").Backup(),
},
},
{
@ -176,13 +176,13 @@ func TestBackupSyncControllerRun(t *testing.T) {
locations: defaultLocationsList("ns-1"),
cloudBackups: map[string][]*velerov1api.Backup{
"bucket-1": {
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").Backup,
defaultBackup().Namespace("ns-1").Name("backup-1").Backup(),
},
},
existingBackups: []*velerov1api.Backup{
// add a label to each existing backup so we can differentiate it from the cloud
// backup during verification
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").WithLabel("i-exist", "true").Backup,
defaultBackup().Namespace("ns-1").Name("backup-1").Labels("i-exist", "true").StorageLocation("location-1").Backup(),
},
},
{
@ -191,11 +191,11 @@ func TestBackupSyncControllerRun(t *testing.T) {
locations: defaultLocationsList("ns-1"),
cloudBackups: map[string][]*velerov1api.Backup{
"bucket-1": {
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").WithStorageLocation("foo").WithLabel(velerov1api.StorageLocationLabel, "foo").Backup,
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").Backup,
defaultBackup().Namespace("ns-1").Name("backup-1").StorageLocation("foo").Labels(velerov1api.StorageLocationLabel, "foo").Backup(),
defaultBackup().Namespace("ns-1").Name("backup-2").Backup(),
},
"bucket-2": {
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").WithStorageLocation("bar").WithLabel(velerov1api.StorageLocationLabel, "bar").Backup,
defaultBackup().Namespace("ns-1").Name("backup-3").StorageLocation("bar").Labels(velerov1api.StorageLocationLabel, "bar").Backup(),
},
},
},
@ -206,11 +206,11 @@ func TestBackupSyncControllerRun(t *testing.T) {
longLocationNameEnabled: true,
cloudBackups: map[string][]*velerov1api.Backup{
"bucket-1": {
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").WithStorageLocation("foo").WithLabel(velerov1api.StorageLocationLabel, "foo").Backup,
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").Backup,
defaultBackup().Namespace("ns-1").Name("backup-1").StorageLocation("foo").Labels(velerov1api.StorageLocationLabel, "foo").Backup(),
defaultBackup().Namespace("ns-1").Name("backup-2").Backup(),
},
"bucket-2": {
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").WithStorageLocation("bar").WithLabel(velerov1api.StorageLocationLabel, "bar").Backup,
defaultBackup().Namespace("ns-1").Name("backup-3").StorageLocation("bar").Labels(velerov1api.StorageLocationLabel, "bar").Backup(),
},
},
},
@ -328,7 +328,7 @@ func TestDeleteOrphanedBackups(t *testing.T) {
tests := []struct {
name string
cloudBackups sets.String
k8sBackups []*velerotest.TestBackup
k8sBackups []*velerov1api.Backup
namespace string
expectedDeletes sets.String
}{
@ -336,10 +336,10 @@ func TestDeleteOrphanedBackups(t *testing.T) {
name: "no overlapping backups",
namespace: "ns-1",
cloudBackups: sets.NewString("backup-1", "backup-2", "backup-3"),
k8sBackups: []*velerotest.TestBackup{
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backupA").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseCompleted),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backupB").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseCompleted),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backupC").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseCompleted),
k8sBackups: []*velerov1api.Backup{
defaultBackup().Namespace("ns-1").Name("backupA").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseCompleted).Backup(),
defaultBackup().Namespace("ns-1").Name("backupB").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseCompleted).Backup(),
defaultBackup().Namespace("ns-1").Name("backupC").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseCompleted).Backup(),
},
expectedDeletes: sets.NewString("backupA", "backupB", "backupC"),
},
@ -347,10 +347,10 @@ func TestDeleteOrphanedBackups(t *testing.T) {
name: "some overlapping backups",
namespace: "ns-1",
cloudBackups: sets.NewString("backup-1", "backup-2", "backup-3"),
k8sBackups: []*velerotest.TestBackup{
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseCompleted),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseCompleted),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-C").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseCompleted),
k8sBackups: []*velerov1api.Backup{
defaultBackup().Namespace("ns-1").Name("backup-1").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseCompleted).Backup(),
defaultBackup().Namespace("ns-1").Name("backup-2").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseCompleted).Backup(),
defaultBackup().Namespace("ns-1").Name("backup-C").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseCompleted).Backup(),
},
expectedDeletes: sets.NewString("backup-C"),
},
@ -358,10 +358,10 @@ func TestDeleteOrphanedBackups(t *testing.T) {
name: "all overlapping backups",
namespace: "ns-1",
cloudBackups: sets.NewString("backup-1", "backup-2", "backup-3"),
k8sBackups: []*velerotest.TestBackup{
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseCompleted),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseCompleted),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseCompleted),
k8sBackups: []*velerov1api.Backup{
defaultBackup().Namespace("ns-1").Name("backup-1").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseCompleted).Backup(),
defaultBackup().Namespace("ns-1").Name("backup-2").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseCompleted).Backup(),
defaultBackup().Namespace("ns-1").Name("backup-3").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseCompleted).Backup(),
},
expectedDeletes: sets.NewString(),
},
@ -369,13 +369,13 @@ func TestDeleteOrphanedBackups(t *testing.T) {
name: "no overlapping backups but including backups that are not complete",
namespace: "ns-1",
cloudBackups: sets.NewString("backup-1", "backup-2", "backup-3"),
k8sBackups: []*velerotest.TestBackup{
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backupA").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseCompleted),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("Deleting").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseDeleting),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("Failed").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseFailed),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("FailedValidation").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseFailedValidation),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("InProgress").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseInProgress),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("New").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseNew),
k8sBackups: []*velerov1api.Backup{
defaultBackup().Namespace("ns-1").Name("backupA").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseCompleted).Backup(),
defaultBackup().Namespace("ns-1").Name("Deleting").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseDeleting).Backup(),
defaultBackup().Namespace("ns-1").Name("Failed").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseFailed).Backup(),
defaultBackup().Namespace("ns-1").Name("FailedValidation").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseFailedValidation).Backup(),
defaultBackup().Namespace("ns-1").Name("InProgress").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseInProgress).Backup(),
defaultBackup().Namespace("ns-1").Name("New").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseNew).Backup(),
},
expectedDeletes: sets.NewString("backupA"),
},
@ -383,10 +383,10 @@ func TestDeleteOrphanedBackups(t *testing.T) {
name: "all overlapping backups and all backups that are not complete",
namespace: "ns-1",
cloudBackups: sets.NewString("backup-1", "backup-2", "backup-3"),
k8sBackups: []*velerotest.TestBackup{
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseFailed),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseFailedValidation),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseInProgress),
k8sBackups: []*velerov1api.Backup{
defaultBackup().Namespace("ns-1").Name("backup-1").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseFailed).Backup(),
defaultBackup().Namespace("ns-1").Name("backup-2").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseFailedValidation).Backup(),
defaultBackup().Namespace("ns-1").Name("backup-3").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseInProgress).Backup(),
},
expectedDeletes: sets.NewString(),
},
@ -394,13 +394,14 @@ func TestDeleteOrphanedBackups(t *testing.T) {
name: "no completed backups in other locations are deleted",
namespace: "ns-1",
cloudBackups: sets.NewString("backup-1", "backup-2", "backup-3"),
k8sBackups: []*velerotest.TestBackup{
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseCompleted),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseCompleted),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-C").WithLabel(velerov1api.StorageLocationLabel, "default").WithPhase(velerov1api.BackupPhaseCompleted),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-4").WithLabel(velerov1api.StorageLocationLabel, "alternate").WithPhase(velerov1api.BackupPhaseCompleted),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-5").WithLabel(velerov1api.StorageLocationLabel, "alternate").WithPhase(velerov1api.BackupPhaseCompleted),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-6").WithLabel(velerov1api.StorageLocationLabel, "alternate").WithPhase(velerov1api.BackupPhaseCompleted),
k8sBackups: []*velerov1api.Backup{
defaultBackup().Namespace("ns-1").Name("backup-1").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseCompleted).Backup(),
defaultBackup().Namespace("ns-1").Name("backup-2").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseCompleted).Backup(),
defaultBackup().Namespace("ns-1").Name("backup-C").Labels(velerov1api.StorageLocationLabel, "default").Phase(velerov1api.BackupPhaseCompleted).Backup(),
defaultBackup().Namespace("ns-1").Name("backup-4").Labels(velerov1api.StorageLocationLabel, "alternate").Phase(velerov1api.BackupPhaseCompleted).Backup(),
defaultBackup().Namespace("ns-1").Name("backup-5").Labels(velerov1api.StorageLocationLabel, "alternate").Phase(velerov1api.BackupPhaseCompleted).Backup(),
defaultBackup().Namespace("ns-1").Name("backup-6").Labels(velerov1api.StorageLocationLabel, "alternate").Phase(velerov1api.BackupPhaseCompleted).Backup(),
},
expectedDeletes: sets.NewString("backup-C"),
},
@ -429,10 +430,10 @@ func TestDeleteOrphanedBackups(t *testing.T) {
for _, backup := range test.k8sBackups {
// add test backup to informer
require.NoError(t, sharedInformers.Velero().V1().Backups().Informer().GetStore().Add(backup.Backup), "Error adding backup to informer")
require.NoError(t, sharedInformers.Velero().V1().Backups().Informer().GetStore().Add(backup), "Error adding backup to informer")
// add test backup to client
_, err := client.VeleroV1().Backups(test.namespace).Create(backup.Backup)
_, err := client.VeleroV1().Backups(test.namespace).Create(backup)
require.NoError(t, err, "Error adding backup to clientset")
// if we expect this backup to be deleted, set up the expected DeleteAction
@ -464,7 +465,7 @@ func TestStorageLabelsInDeleteOrphanedBackups(t *testing.T) {
tests := []struct {
name string
cloudBackups sets.String
k8sBackups []*velerotest.TestBackup
k8sBackups []*velerov1api.Backup
namespace string
expectedDeletes sets.String
}{
@ -472,13 +473,13 @@ func TestStorageLabelsInDeleteOrphanedBackups(t *testing.T) {
name: "some overlapping backups",
namespace: "ns-1",
cloudBackups: sets.NewString("backup-1", "backup-2", "backup-3"),
k8sBackups: []*velerotest.TestBackup{
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").
WithLabel(velerov1api.StorageLocationLabel, "the-really-long-location-name-that-is-much-more-than-63-c69e779").WithPhase(velerov1api.BackupPhaseCompleted),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").
WithLabel(velerov1api.StorageLocationLabel, "the-really-long-location-name-that-is-much-more-than-63-c69e779").WithPhase(velerov1api.BackupPhaseCompleted),
velerotest.NewTestBackup().WithNamespace("ns-1").WithName("backup-C").
WithLabel(velerov1api.StorageLocationLabel, "the-really-long-location-name-that-is-much-more-than-63-c69e779").WithPhase(velerov1api.BackupPhaseCompleted),
k8sBackups: []*velerov1api.Backup{
defaultBackup().Namespace("ns-1").Name("backup-1").
Labels(velerov1api.StorageLocationLabel, "the-really-long-location-name-that-is-much-more-than-63-c69e779").Phase(velerov1api.BackupPhaseCompleted).Backup(),
defaultBackup().Namespace("ns-1").Name("backup-2").
Labels(velerov1api.StorageLocationLabel, "the-really-long-location-name-that-is-much-more-than-63-c69e779").Phase(velerov1api.BackupPhaseCompleted).Backup(),
defaultBackup().Namespace("ns-1").Name("backup-C").
Labels(velerov1api.StorageLocationLabel, "the-really-long-location-name-that-is-much-more-than-63-c69e779").Phase(velerov1api.BackupPhaseCompleted).Backup(),
},
expectedDeletes: sets.NewString("backup-C"),
},
@ -507,10 +508,10 @@ func TestStorageLabelsInDeleteOrphanedBackups(t *testing.T) {
for _, backup := range test.k8sBackups {
// add test backup to informer
require.NoError(t, sharedInformers.Velero().V1().Backups().Informer().GetStore().Add(backup.Backup), "Error adding backup to informer")
require.NoError(t, sharedInformers.Velero().V1().Backups().Informer().GetStore().Add(backup), "Error adding backup to informer")
// add test backup to client
_, err := client.VeleroV1().Backups(test.namespace).Create(backup.Backup)
_, err := client.VeleroV1().Backups(test.namespace).Create(backup)
require.NoError(t, err, "Error adding backup to clientset")
// if we expect this backup to be deleted, set up the expected DeleteAction

View File

@ -145,7 +145,7 @@ func TestProcessDownloadRequest(t *testing.T) {
{
name: "backup contents request for nonexistent backup returns an error",
downloadRequest: newDownloadRequest("", v1.DownloadTargetKindBackupContents, "a-backup"),
backup: velerotest.NewTestBackup().WithName("non-matching-backup").WithStorageLocation("a-location").Backup,
backup: defaultBackup().Name("non-matching-backup").StorageLocation("a-location").Backup(),
backupLocation: newBackupLocation("a-location", "a-provider", "a-bucket"),
expectedErr: "backup.velero.io \"a-backup\" not found",
},
@ -153,42 +153,42 @@ func TestProcessDownloadRequest(t *testing.T) {
name: "restore log request for nonexistent restore returns an error",
downloadRequest: newDownloadRequest("", v1.DownloadTargetKindRestoreLog, "a-backup-20170912150214"),
restore: velerotest.NewTestRestore(v1.DefaultNamespace, "non-matching-restore", v1.RestorePhaseCompleted).WithBackup("a-backup").Restore,
backup: velerotest.NewTestBackup().WithName("a-backup").WithStorageLocation("a-location").Backup,
backup: defaultBackup().Name("a-backup").StorageLocation("a-location").Backup(),
backupLocation: newBackupLocation("a-location", "a-provider", "a-bucket"),
expectedErr: "error getting Restore: restore.velero.io \"a-backup-20170912150214\" not found",
},
{
name: "backup contents request for backup with nonexistent location returns an error",
downloadRequest: newDownloadRequest("", v1.DownloadTargetKindBackupContents, "a-backup"),
backup: velerotest.NewTestBackup().WithName("a-backup").WithStorageLocation("a-location").Backup,
backup: defaultBackup().Name("a-backup").StorageLocation("a-location").Backup(),
backupLocation: newBackupLocation("non-matching-location", "a-provider", "a-bucket"),
expectedErr: "backupstoragelocation.velero.io \"a-location\" not found",
},
{
name: "backup contents request with phase '' gets a url",
downloadRequest: newDownloadRequest("", v1.DownloadTargetKindBackupContents, "a-backup"),
backup: velerotest.NewTestBackup().WithName("a-backup").WithStorageLocation("a-location").Backup,
backup: defaultBackup().Name("a-backup").StorageLocation("a-location").Backup(),
backupLocation: newBackupLocation("a-location", "a-provider", "a-bucket"),
expectGetsURL: true,
},
{
name: "backup contents request with phase 'New' gets a url",
downloadRequest: newDownloadRequest(v1.DownloadRequestPhaseNew, v1.DownloadTargetKindBackupContents, "a-backup"),
backup: velerotest.NewTestBackup().WithName("a-backup").WithStorageLocation("a-location").Backup,
backup: defaultBackup().Name("a-backup").StorageLocation("a-location").Backup(),
backupLocation: newBackupLocation("a-location", "a-provider", "a-bucket"),
expectGetsURL: true,
},
{
name: "backup log request with phase '' gets a url",
downloadRequest: newDownloadRequest("", v1.DownloadTargetKindBackupLog, "a-backup"),
backup: velerotest.NewTestBackup().WithName("a-backup").WithStorageLocation("a-location").Backup,
backup: defaultBackup().Name("a-backup").StorageLocation("a-location").Backup(),
backupLocation: newBackupLocation("a-location", "a-provider", "a-bucket"),
expectGetsURL: true,
},
{
name: "backup log request with phase 'New' gets a url",
downloadRequest: newDownloadRequest(v1.DownloadRequestPhaseNew, v1.DownloadTargetKindBackupLog, "a-backup"),
backup: velerotest.NewTestBackup().WithName("a-backup").WithStorageLocation("a-location").Backup,
backup: defaultBackup().Name("a-backup").StorageLocation("a-location").Backup(),
backupLocation: newBackupLocation("a-location", "a-provider", "a-bucket"),
expectGetsURL: true,
},
@ -196,7 +196,7 @@ func TestProcessDownloadRequest(t *testing.T) {
name: "restore log request with phase '' gets a url",
downloadRequest: newDownloadRequest("", v1.DownloadTargetKindRestoreLog, "a-backup-20170912150214"),
restore: velerotest.NewTestRestore(v1.DefaultNamespace, "a-backup-20170912150214", v1.RestorePhaseCompleted).WithBackup("a-backup").Restore,
backup: velerotest.NewTestBackup().WithName("a-backup").WithStorageLocation("a-location").Backup,
backup: defaultBackup().Name("a-backup").StorageLocation("a-location").Backup(),
backupLocation: newBackupLocation("a-location", "a-provider", "a-bucket"),
expectGetsURL: true,
},
@ -204,7 +204,7 @@ func TestProcessDownloadRequest(t *testing.T) {
name: "restore log request with phase 'New' gets a url",
downloadRequest: newDownloadRequest(v1.DownloadRequestPhaseNew, v1.DownloadTargetKindRestoreLog, "a-backup-20170912150214"),
restore: velerotest.NewTestRestore(v1.DefaultNamespace, "a-backup-20170912150214", v1.RestorePhaseCompleted).WithBackup("a-backup").Restore,
backup: velerotest.NewTestBackup().WithName("a-backup").WithStorageLocation("a-location").Backup,
backup: defaultBackup().Name("a-backup").StorageLocation("a-location").Backup(),
backupLocation: newBackupLocation("a-location", "a-provider", "a-bucket"),
expectGetsURL: true,
},
@ -212,7 +212,7 @@ func TestProcessDownloadRequest(t *testing.T) {
name: "restore results request with phase '' gets a url",
downloadRequest: newDownloadRequest("", v1.DownloadTargetKindRestoreResults, "a-backup-20170912150214"),
restore: velerotest.NewTestRestore(v1.DefaultNamespace, "a-backup-20170912150214", v1.RestorePhaseCompleted).WithBackup("a-backup").Restore,
backup: velerotest.NewTestBackup().WithName("a-backup").WithStorageLocation("a-location").Backup,
backup: defaultBackup().Name("a-backup").StorageLocation("a-location").Backup(),
backupLocation: newBackupLocation("a-location", "a-provider", "a-bucket"),
expectGetsURL: true,
},
@ -220,19 +220,19 @@ func TestProcessDownloadRequest(t *testing.T) {
name: "restore results request with phase 'New' gets a url",
downloadRequest: newDownloadRequest(v1.DownloadRequestPhaseNew, v1.DownloadTargetKindRestoreResults, "a-backup-20170912150214"),
restore: velerotest.NewTestRestore(v1.DefaultNamespace, "a-backup-20170912150214", v1.RestorePhaseCompleted).WithBackup("a-backup").Restore,
backup: velerotest.NewTestBackup().WithName("a-backup").WithStorageLocation("a-location").Backup,
backup: defaultBackup().Name("a-backup").StorageLocation("a-location").Backup(),
backupLocation: newBackupLocation("a-location", "a-provider", "a-bucket"),
expectGetsURL: true,
},
{
name: "request with phase 'Processed' is not deleted if not expired",
downloadRequest: newDownloadRequest(v1.DownloadRequestPhaseProcessed, v1.DownloadTargetKindBackupLog, "a-backup-20170912150214"),
backup: velerotest.NewTestBackup().WithName("a-backup").WithStorageLocation("a-location").Backup,
backup: defaultBackup().Name("a-backup").StorageLocation("a-location").Backup(),
},
{
name: "request with phase 'Processed' is deleted if expired",
downloadRequest: newDownloadRequest(v1.DownloadRequestPhaseProcessed, v1.DownloadTargetKindBackupLog, "a-backup-20170912150214"),
backup: velerotest.NewTestBackup().WithName("a-backup").WithStorageLocation("a-location").Backup,
backup: defaultBackup().Name("a-backup").StorageLocation("a-location").Backup(),
expired: true,
},
}

View File

@ -66,7 +66,7 @@ func TestGCControllerEnqueueAllBackups(t *testing.T) {
var expected []string
for i := 0; i < 3; i++ {
backup := velerotest.NewTestBackup().WithName(fmt.Sprintf("backup-%d", i)).Backup
backup := defaultBackup().Name(fmt.Sprintf("backup-%d", i)).Backup()
sharedInformers.Velero().V1().Backups().Informer().GetStore().Add(backup)
expected = append(expected, kube.NamespaceAndName(backup))
}
@ -97,7 +97,7 @@ Loop:
}
func TestGCControllerHasUpdateFunc(t *testing.T) {
backup := velerotest.NewTestBackup().WithName("backup").Backup
backup := defaultBackup().Backup()
expected := kube.NamespaceAndName(backup)
client := fake.NewSimpleClientset(backup)
@ -166,52 +166,32 @@ func TestGCControllerProcessQueueItem(t *testing.T) {
name: "can't find backup - no error",
},
{
name: "unexpired backup is not deleted",
backup: velerotest.NewTestBackup().
WithName("backup-1").
WithExpiration(fakeClock.Now().Add(1 * time.Minute)).
WithStorageLocation("default").
Backup,
name: "unexpired backup is not deleted",
backup: defaultBackup().Expiration(fakeClock.Now().Add(time.Minute)).StorageLocation("default").Backup(),
backupLocation: defaultBackupLocation,
expectDeletion: false,
},
{
name: "expired backup in read-only storage location is not deleted",
backup: velerotest.NewTestBackup().
WithName("backup-1").
WithExpiration(fakeClock.Now().Add(-1 * time.Minute)).
WithStorageLocation("read-only").
Backup,
name: "expired backup in read-only storage location is not deleted",
backup: defaultBackup().Expiration(fakeClock.Now().Add(-time.Minute)).StorageLocation("read-only").Backup(),
backupLocation: velerotest.NewTestBackupStorageLocation().WithName("read-only").WithAccessMode(api.BackupStorageLocationAccessModeReadOnly).BackupStorageLocation,
expectDeletion: false,
},
{
name: "expired backup in read-write storage location is deleted",
backup: velerotest.NewTestBackup().
WithName("backup-1").
WithExpiration(fakeClock.Now().Add(-1 * time.Minute)).
WithStorageLocation("read-write").
Backup,
name: "expired backup in read-write storage location is deleted",
backup: defaultBackup().Expiration(fakeClock.Now().Add(-time.Minute)).StorageLocation("read-write").Backup(),
backupLocation: velerotest.NewTestBackupStorageLocation().WithName("read-write").WithAccessMode(api.BackupStorageLocationAccessModeReadWrite).BackupStorageLocation,
expectDeletion: true,
},
{
name: "expired backup with no pending deletion requests is deleted",
backup: velerotest.NewTestBackup().
WithName("backup-1").
WithExpiration(fakeClock.Now().Add(-1 * time.Second)).
WithStorageLocation("default").
Backup,
name: "expired backup with no pending deletion requests is deleted",
backup: defaultBackup().Expiration(fakeClock.Now().Add(-time.Second)).StorageLocation("default").Backup(),
backupLocation: defaultBackupLocation,
expectDeletion: true,
},
{
name: "expired backup with a pending deletion request is not deleted",
backup: velerotest.NewTestBackup().
WithName("backup-1").
WithExpiration(fakeClock.Now().Add(-1 * time.Second)).
WithStorageLocation("default").
Backup,
name: "expired backup with a pending deletion request is not deleted",
backup: defaultBackup().Expiration(fakeClock.Now().Add(-time.Second)).StorageLocation("default").Backup(),
backupLocation: defaultBackupLocation,
deleteBackupRequests: []*api.DeleteBackupRequest{
{
@ -231,12 +211,8 @@ func TestGCControllerProcessQueueItem(t *testing.T) {
expectDeletion: false,
},
{
name: "expired backup with only processed deletion requests is deleted",
backup: velerotest.NewTestBackup().
WithName("backup-1").
WithExpiration(fakeClock.Now().Add(-1 * time.Second)).
WithStorageLocation("default").
Backup,
name: "expired backup with only processed deletion requests is deleted",
backup: defaultBackup().Expiration(fakeClock.Now().Add(-time.Second)).StorageLocation("default").Backup(),
backupLocation: defaultBackupLocation,
deleteBackupRequests: []*api.DeleteBackupRequest{
{
@ -256,12 +232,8 @@ func TestGCControllerProcessQueueItem(t *testing.T) {
expectDeletion: true,
},
{
name: "create DeleteBackupRequest error returns an error",
backup: velerotest.NewTestBackup().
WithName("backup-1").
WithExpiration(fakeClock.Now().Add(-1 * time.Second)).
WithStorageLocation("default").
Backup,
name: "create DeleteBackupRequest error returns an error",
backup: defaultBackup().Expiration(fakeClock.Now().Add(-time.Second)).StorageLocation("default").Backup(),
backupLocation: defaultBackupLocation,
expectDeletion: true,
createDeleteBackupRequestError: true,

View File

@ -66,16 +66,16 @@ func TestFetchBackupInfo(t *testing.T) {
name: "lister has backup",
backupName: "backup-1",
informerLocations: []*api.BackupStorageLocation{velerotest.NewTestBackupStorageLocation().WithName("default").WithProvider("myCloud").WithObjectStorage("bucket").BackupStorageLocation},
informerBackups: []*api.Backup{velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup},
expectedRes: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup,
informerBackups: []*api.Backup{defaultBackup().StorageLocation("default").Backup()},
expectedRes: defaultBackup().StorageLocation("default").Backup(),
},
{
name: "lister does not have a backup, but backupSvc does",
backupName: "backup-1",
backupStoreBackup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup,
backupStoreBackup: defaultBackup().StorageLocation("default").Backup(),
informerLocations: []*api.BackupStorageLocation{velerotest.NewTestBackupStorageLocation().WithName("default").WithProvider("myCloud").WithObjectStorage("bucket").BackupStorageLocation},
informerBackups: []*api.Backup{velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup},
expectedRes: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup,
informerBackups: []*api.Backup{defaultBackup().StorageLocation("default").Backup()},
expectedRes: defaultBackup().StorageLocation("default").Backup(),
},
{
name: "no backup",
@ -241,7 +241,7 @@ func TestProcessQueueItem(t *testing.T) {
name: "restore with both namespace in both includedNamespaces and excludedNamespaces fails validation",
location: velerotest.NewTestBackupStorageLocation().WithName("default").WithProvider("myCloud").WithObjectStorage("bucket").BackupStorageLocation,
restore: NewRestore("foo", "bar", "backup-1", "another-1", "*", api.RestorePhaseNew).WithExcludedNamespace("another-1").Restore,
backup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup,
backup: defaultBackup().StorageLocation("default").Backup(),
expectedErr: false,
expectedPhase: string(api.RestorePhaseFailedValidation),
expectedValidationErrors: []string{"Invalid included/excluded namespace lists: excludes list cannot contain an item in the includes list: another-1"},
@ -250,7 +250,7 @@ func TestProcessQueueItem(t *testing.T) {
name: "restore with resource in both includedResources and excludedResources fails validation",
location: velerotest.NewTestBackupStorageLocation().WithName("default").WithProvider("myCloud").WithObjectStorage("bucket").BackupStorageLocation,
restore: NewRestore("foo", "bar", "backup-1", "*", "a-resource", api.RestorePhaseNew).WithExcludedResource("a-resource").Restore,
backup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup,
backup: defaultBackup().StorageLocation("default").Backup(),
expectedErr: false,
expectedPhase: string(api.RestorePhaseFailedValidation),
expectedValidationErrors: []string{"Invalid included/excluded resource lists: excludes list cannot contain an item in the includes list: a-resource"},
@ -270,16 +270,10 @@ func TestProcessQueueItem(t *testing.T) {
expectedValidationErrors: []string{"Either a backup or schedule must be specified as a source for the restore, but not both"},
},
{
name: "valid restore with schedule name gets executed",
location: velerotest.NewTestBackupStorageLocation().WithName("default").WithProvider("myCloud").WithObjectStorage("bucket").BackupStorageLocation,
restore: NewRestore("foo", "bar", "", "ns-1", "", api.RestorePhaseNew).WithSchedule("sched-1").Restore,
backup: velerotest.
NewTestBackup().
WithName("backup-1").
WithStorageLocation("default").
WithLabel(velerov1api.ScheduleNameLabel, "sched-1").
WithPhase(api.BackupPhaseCompleted).
Backup,
name: "valid restore with schedule name gets executed",
location: velerotest.NewTestBackupStorageLocation().WithName("default").WithProvider("myCloud").WithObjectStorage("bucket").BackupStorageLocation,
restore: NewRestore("foo", "bar", "", "ns-1", "", api.RestorePhaseNew).WithSchedule("sched-1").Restore,
backup: defaultBackup().StorageLocation("default").Labels(velerov1api.ScheduleNameLabel, "sched-1").Phase(api.BackupPhaseCompleted).Backup(),
expectedErr: false,
expectedPhase: string(api.RestorePhaseInProgress),
expectedRestorerCall: NewRestore("foo", "bar", "backup-1", "ns-1", "", api.RestorePhaseInProgress).WithSchedule("sched-1").Restore,
@ -296,7 +290,7 @@ func TestProcessQueueItem(t *testing.T) {
name: "restorer throwing an error causes the restore to fail",
location: velerotest.NewTestBackupStorageLocation().WithName("default").WithProvider("myCloud").WithObjectStorage("bucket").BackupStorageLocation,
restore: NewRestore("foo", "bar", "backup-1", "ns-1", "", api.RestorePhaseNew).Restore,
backup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup,
backup: defaultBackup().StorageLocation("default").Backup(),
restorerError: errors.New("blarg"),
expectedErr: false,
expectedPhase: string(api.RestorePhaseInProgress),
@ -308,7 +302,7 @@ func TestProcessQueueItem(t *testing.T) {
name: "valid restore gets executed",
location: velerotest.NewTestBackupStorageLocation().WithName("default").WithProvider("myCloud").WithObjectStorage("bucket").BackupStorageLocation,
restore: NewRestore("foo", "bar", "backup-1", "ns-1", "", api.RestorePhaseNew).Restore,
backup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup,
backup: defaultBackup().StorageLocation("default").Backup(),
expectedErr: false,
expectedPhase: string(api.RestorePhaseInProgress),
expectedRestorerCall: NewRestore("foo", "bar", "backup-1", "ns-1", "", api.RestorePhaseInProgress).Restore,
@ -317,7 +311,7 @@ func TestProcessQueueItem(t *testing.T) {
name: "restoration of nodes is not supported",
location: velerotest.NewTestBackupStorageLocation().WithName("default").WithProvider("myCloud").WithObjectStorage("bucket").BackupStorageLocation,
restore: NewRestore("foo", "bar", "backup-1", "ns-1", "nodes", api.RestorePhaseNew).Restore,
backup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup,
backup: defaultBackup().StorageLocation("default").Backup(),
expectedErr: false,
expectedPhase: string(api.RestorePhaseFailedValidation),
expectedValidationErrors: []string{
@ -329,7 +323,7 @@ func TestProcessQueueItem(t *testing.T) {
name: "restoration of events is not supported",
location: velerotest.NewTestBackupStorageLocation().WithName("default").WithProvider("myCloud").WithObjectStorage("bucket").BackupStorageLocation,
restore: NewRestore("foo", "bar", "backup-1", "ns-1", "events", api.RestorePhaseNew).Restore,
backup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup,
backup: defaultBackup().StorageLocation("default").Backup(),
expectedErr: false,
expectedPhase: string(api.RestorePhaseFailedValidation),
expectedValidationErrors: []string{
@ -341,7 +335,7 @@ func TestProcessQueueItem(t *testing.T) {
name: "restoration of events.events.k8s.io is not supported",
location: velerotest.NewTestBackupStorageLocation().WithName("default").WithProvider("myCloud").WithObjectStorage("bucket").BackupStorageLocation,
restore: NewRestore("foo", "bar", "backup-1", "ns-1", "events.events.k8s.io", api.RestorePhaseNew).Restore,
backup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup,
backup: defaultBackup().StorageLocation("default").Backup(),
expectedErr: false,
expectedPhase: string(api.RestorePhaseFailedValidation),
expectedValidationErrors: []string{
@ -353,7 +347,7 @@ func TestProcessQueueItem(t *testing.T) {
name: "restoration of backups.velero.io is not supported",
location: velerotest.NewTestBackupStorageLocation().WithName("default").WithProvider("myCloud").WithObjectStorage("bucket").BackupStorageLocation,
restore: NewRestore("foo", "bar", "backup-1", "ns-1", "backups.velero.io", api.RestorePhaseNew).Restore,
backup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup,
backup: defaultBackup().StorageLocation("default").Backup(),
expectedErr: false,
expectedPhase: string(api.RestorePhaseFailedValidation),
expectedValidationErrors: []string{
@ -365,7 +359,7 @@ func TestProcessQueueItem(t *testing.T) {
name: "restoration of restores.velero.io is not supported",
location: velerotest.NewTestBackupStorageLocation().WithName("default").WithProvider("myCloud").WithObjectStorage("bucket").BackupStorageLocation,
restore: NewRestore("foo", "bar", "backup-1", "ns-1", "restores.velero.io", api.RestorePhaseNew).Restore,
backup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup,
backup: defaultBackup().StorageLocation("default").Backup(),
expectedErr: false,
expectedPhase: string(api.RestorePhaseFailedValidation),
expectedValidationErrors: []string{
@ -380,7 +374,7 @@ func TestProcessQueueItem(t *testing.T) {
expectedPhase: string(api.RestorePhaseInProgress),
expectedFinalPhase: string(api.RestorePhaseFailed),
backupStoreGetBackupContentsErr: errors.New("Couldn't download backup"),
backup: velerotest.NewTestBackup().WithName("backup-1").WithStorageLocation("default").Backup,
backup: defaultBackup().StorageLocation("default").Backup(),
},
}
@ -647,12 +641,11 @@ func TestvalidateAndCompleteWhenScheduleNameSpecified(t *testing.T) {
}
// no backups created from the schedule: fail validation
require.NoError(t, sharedInformers.Velero().V1().Backups().Informer().GetStore().Add(velerotest.
NewTestBackup().
WithName("backup-1").
WithLabel(velerov1api.ScheduleNameLabel, "non-matching-schedule").
WithPhase(api.BackupPhaseCompleted).
Backup,
require.NoError(t, sharedInformers.Velero().V1().Backups().Informer().GetStore().Add(
defaultBackup().
Labels(velerov1api.ScheduleNameLabel, "non-matching-schedule").
Phase(velerov1api.BackupPhaseCompleted).
Backup(),
))
errs := c.validateAndComplete(restore, pluginManager)
@ -660,12 +653,12 @@ func TestvalidateAndCompleteWhenScheduleNameSpecified(t *testing.T) {
assert.Empty(t, restore.Spec.BackupName)
// no completed backups created from the schedule: fail validation
require.NoError(t, sharedInformers.Velero().V1().Backups().Informer().GetStore().Add(velerotest.
NewTestBackup().
WithName("backup-2").
WithLabel(velerov1api.ScheduleNameLabel, "schedule-1").
WithPhase(api.BackupPhaseInProgress).
Backup,
require.NoError(t, sharedInformers.Velero().V1().Backups().Informer().GetStore().Add(
defaultBackup().
Name("backup-2").
Labels(velerov1api.ScheduleNameLabel, "schedule-1").
Phase(velerov1api.BackupPhaseInProgress).
Backup(),
))
errs = c.validateAndComplete(restore, pluginManager)
@ -675,21 +668,21 @@ func TestvalidateAndCompleteWhenScheduleNameSpecified(t *testing.T) {
// multiple completed backups created from the schedule: use most recent
now := time.Now()
require.NoError(t, sharedInformers.Velero().V1().Backups().Informer().GetStore().Add(velerotest.
NewTestBackup().
WithName("foo").
WithLabel(velerov1api.ScheduleNameLabel, "schedule-1").
WithPhase(api.BackupPhaseCompleted).
WithStartTimestamp(now).
Backup,
require.NoError(t, sharedInformers.Velero().V1().Backups().Informer().GetStore().Add(
defaultBackup().
Name("foo").
Labels(velerov1api.ScheduleNameLabel, "schedule-1").
Phase(velerov1api.BackupPhaseCompleted).
StartTimestamp(now).
Backup(),
))
require.NoError(t, sharedInformers.Velero().V1().Backups().Informer().GetStore().Add(velerotest.
NewTestBackup().
WithName("bar").
WithLabel(velerov1api.ScheduleNameLabel, "schedule-1").
WithPhase(api.BackupPhaseCompleted).
WithStartTimestamp(now.Add(time.Second)).
Backup,
require.NoError(t, sharedInformers.Velero().V1().Backups().Informer().GetStore().Add(
defaultBackup().
Name("foo").
Labels(velerov1api.ScheduleNameLabel, "schedule-1").
Phase(velerov1api.BackupPhaseCompleted).
StartTimestamp(now.Add(time.Second)).
Backup(),
))
errs = c.validateAndComplete(restore, pluginManager)

View File

@ -93,7 +93,7 @@ func TestProcessSchedule(t *testing.T) {
fakeClockTime: "2017-01-01 12:00:00",
expectedErr: false,
expectedPhase: string(api.SchedulePhaseEnabled),
expectedBackupCreate: velerotest.NewTestBackup().WithNamespace("ns").WithName("name-20170101120000").WithLabel(velerov1api.ScheduleNameLabel, "name").Backup,
expectedBackupCreate: defaultBackup().Namespace("ns").Name("name-20170101120000").Labels(velerov1api.ScheduleNameLabel, "name").NoTypeMeta().Backup(),
expectedLastBackup: "2017-01-01 12:00:00",
},
{
@ -101,7 +101,7 @@ func TestProcessSchedule(t *testing.T) {
schedule: velerotest.NewTestSchedule("ns", "name").WithPhase(api.SchedulePhaseEnabled).WithCronSchedule("@every 5m").Schedule,
fakeClockTime: "2017-01-01 12:00:00",
expectedErr: false,
expectedBackupCreate: velerotest.NewTestBackup().WithNamespace("ns").WithName("name-20170101120000").WithLabel(velerov1api.ScheduleNameLabel, "name").Backup,
expectedBackupCreate: defaultBackup().Namespace("ns").Name("name-20170101120000").Labels(velerov1api.ScheduleNameLabel, "name").NoTypeMeta().Backup(),
expectedLastBackup: "2017-01-01 12:00:00",
},
{
@ -110,7 +110,7 @@ func TestProcessSchedule(t *testing.T) {
WithCronSchedule("@every 5m").WithLastBackupTime("2000-01-01 00:00:00").Schedule,
fakeClockTime: "2017-01-01 12:00:00",
expectedErr: false,
expectedBackupCreate: velerotest.NewTestBackup().WithNamespace("ns").WithName("name-20170101120000").WithLabel(velerov1api.ScheduleNameLabel, "name").Backup,
expectedBackupCreate: defaultBackup().Namespace("ns").Name("name-20170101120000").Labels(velerov1api.ScheduleNameLabel, "name").NoTypeMeta().Backup(),
expectedLastBackup: "2017-01-01 12:00:00",
},
}

View File

@ -26,6 +26,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
api "github.com/heptio/velero/pkg/apis/velero/v1"
"github.com/heptio/velero/pkg/backup"
cloudprovidermocks "github.com/heptio/velero/pkg/cloudprovider/mocks"
"github.com/heptio/velero/pkg/generated/clientset/versioned/fake"
informers "github.com/heptio/velero/pkg/generated/informers/externalversions"
@ -34,6 +35,10 @@ import (
"github.com/heptio/velero/pkg/volume"
)
func defaultBackup() *backup.Builder {
return backup.NewNamedBuilder(api.DefaultNamespace, "backup-1")
}
func TestExecutePVAction_NoSnapshotRestores(t *testing.T) {
tests := []struct {
name string
@ -61,28 +66,28 @@ func TestExecutePVAction_NoSnapshotRestores(t *testing.T) {
name: "ensure spec.claimRef is deleted",
obj: NewTestUnstructured().WithName("pv-1").WithAnnotations("a", "b").WithSpec("claimRef", "someOtherField").Unstructured,
restore: velerotest.NewDefaultTestRestore().WithRestorePVs(false).Restore,
backup: velerotest.NewTestBackup().WithName("backup1").WithPhase(api.BackupPhaseInProgress).Backup,
backup: defaultBackup().Phase(api.BackupPhaseInProgress).Backup(),
expectedRes: NewTestUnstructured().WithAnnotations("a", "b").WithName("pv-1").WithSpec("someOtherField").Unstructured,
},
{
name: "ensure spec.storageClassName is retained",
obj: NewTestUnstructured().WithName("pv-1").WithAnnotations("a", "b").WithSpec("storageClassName", "someOtherField").Unstructured,
restore: velerotest.NewDefaultTestRestore().WithRestorePVs(false).Restore,
backup: velerotest.NewTestBackup().WithName("backup1").WithPhase(api.BackupPhaseInProgress).Backup,
backup: defaultBackup().Phase(api.BackupPhaseInProgress).Backup(),
expectedRes: NewTestUnstructured().WithAnnotations("a", "b").WithName("pv-1").WithSpec("storageClassName", "someOtherField").Unstructured,
},
{
name: "if backup.spec.snapshotVolumes is false, ignore restore.spec.restorePVs and return early",
obj: NewTestUnstructured().WithName("pv-1").WithAnnotations("a", "b").WithSpec("claimRef", "storageClassName", "someOtherField").Unstructured,
restore: velerotest.NewDefaultTestRestore().WithRestorePVs(true).Restore,
backup: velerotest.NewTestBackup().WithName("backup1").WithPhase(api.BackupPhaseInProgress).WithSnapshotVolumes(false).Backup,
backup: defaultBackup().Phase(api.BackupPhaseInProgress).SnapshotVolumes(false).Backup(),
expectedRes: NewTestUnstructured().WithName("pv-1").WithAnnotations("a", "b").WithSpec("storageClassName", "someOtherField").Unstructured,
},
{
name: "restore.spec.restorePVs=false, return early",
obj: NewTestUnstructured().WithName("pv-1").WithSpec().Unstructured,
restore: velerotest.NewDefaultTestRestore().WithRestorePVs(false).Restore,
backup: velerotest.NewTestBackup().WithName("backup1").WithPhase(api.BackupPhaseInProgress).Backup,
backup: defaultBackup().Phase(api.BackupPhaseInProgress).Backup(),
volumeSnapshots: []*volume.Snapshot{
newSnapshot("pv-1", "loc-1", "gp", "az-1", "snap-1", 1000),
},
@ -96,7 +101,7 @@ func TestExecutePVAction_NoSnapshotRestores(t *testing.T) {
name: "volumeSnapshots is empty: return early",
obj: NewTestUnstructured().WithName("pv-1").WithSpec().Unstructured,
restore: velerotest.NewDefaultTestRestore().WithRestorePVs(true).Restore,
backup: velerotest.NewTestBackup().WithName("backup-1").Backup,
backup: defaultBackup().Backup(),
locations: []*api.VolumeSnapshotLocation{
velerotest.NewTestVolumeSnapshotLocation().WithName("loc-1").VolumeSnapshotLocation,
velerotest.NewTestVolumeSnapshotLocation().WithName("loc-2").VolumeSnapshotLocation,
@ -108,7 +113,7 @@ func TestExecutePVAction_NoSnapshotRestores(t *testing.T) {
name: "volumeSnapshots doesn't have a snapshot for PV: return early",
obj: NewTestUnstructured().WithName("pv-1").WithSpec().Unstructured,
restore: velerotest.NewDefaultTestRestore().WithRestorePVs(true).Restore,
backup: velerotest.NewTestBackup().WithName("backup-1").Backup,
backup: defaultBackup().Backup(),
locations: []*api.VolumeSnapshotLocation{
velerotest.NewTestVolumeSnapshotLocation().WithName("loc-1").VolumeSnapshotLocation,
velerotest.NewTestVolumeSnapshotLocation().WithName("loc-2").VolumeSnapshotLocation,
@ -174,7 +179,7 @@ func TestExecutePVAction_SnapshotRestores(t *testing.T) {
name: "backup with a matching volume.Snapshot for PV executes restore",
obj: NewTestUnstructured().WithName("pv-1").WithSpec().Unstructured,
restore: velerotest.NewDefaultTestRestore().WithRestorePVs(true).Restore,
backup: velerotest.NewTestBackup().WithName("backup-1").Backup,
backup: defaultBackup().Backup(),
locations: []*api.VolumeSnapshotLocation{
velerotest.NewTestVolumeSnapshotLocation().WithName("loc-1").WithProvider("provider-1").VolumeSnapshotLocation,
velerotest.NewTestVolumeSnapshotLocation().WithName("loc-2").WithProvider("provider-2").VolumeSnapshotLocation,

View File

@ -1,139 +0,0 @@
/*
Copyright 2017 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 test
import (
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "github.com/heptio/velero/pkg/apis/velero/v1"
)
type TestBackup struct {
*v1.Backup
}
func NewTestBackup() *TestBackup {
return &TestBackup{
Backup: &v1.Backup{
ObjectMeta: metav1.ObjectMeta{
Namespace: v1.DefaultNamespace,
},
},
}
}
func (b *TestBackup) WithNamespace(namespace string) *TestBackup {
b.Namespace = namespace
return b
}
func (b *TestBackup) WithName(name string) *TestBackup {
b.Name = name
return b
}
func (b *TestBackup) WithLabel(key, value string) *TestBackup {
if b.Labels == nil {
b.Labels = make(map[string]string)
}
b.Labels[key] = value
return b
}
func (b *TestBackup) WithPhase(phase v1.BackupPhase) *TestBackup {
b.Status.Phase = phase
return b
}
func (b *TestBackup) WithIncludedResources(r ...string) *TestBackup {
b.Spec.IncludedResources = r
return b
}
func (b *TestBackup) WithExcludedResources(r ...string) *TestBackup {
b.Spec.ExcludedResources = r
return b
}
func (b *TestBackup) WithIncludedNamespaces(ns ...string) *TestBackup {
b.Spec.IncludedNamespaces = ns
return b
}
func (b *TestBackup) WithExcludedNamespaces(ns ...string) *TestBackup {
b.Spec.ExcludedNamespaces = ns
return b
}
func (b *TestBackup) WithTTL(ttl time.Duration) *TestBackup {
b.Spec.TTL = metav1.Duration{Duration: ttl}
return b
}
func (b *TestBackup) WithExpiration(expiration time.Time) *TestBackup {
b.Status.Expiration = metav1.Time{Time: expiration}
return b
}
func (b *TestBackup) WithVersion(version int) *TestBackup {
b.Status.Version = version
return b
}
func (b *TestBackup) WithSnapshotVolumes(value bool) *TestBackup {
b.Spec.SnapshotVolumes = &value
return b
}
func (b *TestBackup) WithSnapshotVolumesPointer(value *bool) *TestBackup {
b.Spec.SnapshotVolumes = value
return b
}
func (b *TestBackup) WithDeletionTimestamp(time time.Time) *TestBackup {
b.DeletionTimestamp = &metav1.Time{Time: time}
return b
}
func (b *TestBackup) WithResourceVersion(version string) *TestBackup {
b.ResourceVersion = version
return b
}
func (b *TestBackup) WithFinalizers(finalizers ...string) *TestBackup {
b.ObjectMeta.Finalizers = append(b.ObjectMeta.Finalizers, finalizers...)
return b
}
func (b *TestBackup) WithStartTimestamp(startTime time.Time) *TestBackup {
b.Status.StartTimestamp = metav1.Time{Time: startTime}
return b
}
func (b *TestBackup) WithStorageLocation(location string) *TestBackup {
b.Spec.StorageLocation = location
return b
}
func (b *TestBackup) WithVolumeSnapshotLocations(locations ...string) *TestBackup {
b.Spec.VolumeSnapshotLocations = locations
return b
}