2017-08-02 17:27:17 +00:00
|
|
|
/*
|
2018-01-02 18:51:49 +00:00
|
|
|
Copyright 2017 the Heptio Ark contributors.
|
2017-08-02 17:27:17 +00:00
|
|
|
|
|
|
|
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 controller
|
|
|
|
|
|
|
|
import (
|
2018-07-10 22:17:53 +00:00
|
|
|
"bytes"
|
2018-09-26 22:18:45 +00:00
|
|
|
"fmt"
|
2017-08-02 17:27:17 +00:00
|
|
|
"io"
|
2018-09-26 22:18:45 +00:00
|
|
|
"sort"
|
2018-07-10 22:17:53 +00:00
|
|
|
"strings"
|
2017-08-02 17:27:17 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-09-25 14:51:28 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-08-20 23:29:54 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-09-26 22:18:45 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-08-20 23:29:54 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-06-20 18:08:07 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-08-02 17:27:17 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/clock"
|
|
|
|
|
|
|
|
"github.com/heptio/ark/pkg/apis/ark/v1"
|
2018-09-26 22:18:45 +00:00
|
|
|
pkgbackup "github.com/heptio/ark/pkg/backup"
|
2017-10-25 16:42:03 +00:00
|
|
|
"github.com/heptio/ark/pkg/generated/clientset/versioned/fake"
|
2017-08-02 17:27:17 +00:00
|
|
|
informers "github.com/heptio/ark/pkg/generated/informers/externalversions"
|
2018-06-06 21:35:06 +00:00
|
|
|
"github.com/heptio/ark/pkg/metrics"
|
2018-08-20 23:29:54 +00:00
|
|
|
"github.com/heptio/ark/pkg/persistence"
|
|
|
|
persistencemocks "github.com/heptio/ark/pkg/persistence/mocks"
|
2018-05-13 13:28:09 +00:00
|
|
|
"github.com/heptio/ark/pkg/plugin"
|
|
|
|
pluginmocks "github.com/heptio/ark/pkg/plugin/mocks"
|
|
|
|
"github.com/heptio/ark/pkg/util/logging"
|
2017-12-11 22:10:52 +00:00
|
|
|
arktest "github.com/heptio/ark/pkg/util/test"
|
2017-08-02 17:27:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type fakeBackupper struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
func (b *fakeBackupper) Backup(logger logrus.FieldLogger, backup *pkgbackup.Request, backupFile io.Writer, actions []pkgbackup.ItemAction, blockStoreGetter pkgbackup.BlockStoreGetter) error {
|
|
|
|
args := b.Called(logger, backup, backupFile, actions, blockStoreGetter)
|
2017-08-02 17:27:17 +00:00
|
|
|
return args.Error(0)
|
|
|
|
}
|
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
func TestProcessBackupNonProcessedItems(t *testing.T) {
|
2017-08-02 17:27:17 +00:00
|
|
|
tests := []struct {
|
2018-09-26 22:18:45 +00:00
|
|
|
name string
|
|
|
|
key string
|
|
|
|
backup *v1.Backup
|
|
|
|
expectedErr string
|
2017-08-02 17:27:17 +00:00
|
|
|
}{
|
|
|
|
{
|
2018-09-26 22:18:45 +00:00
|
|
|
name: "bad key returns error",
|
2017-08-02 17:27:17 +00:00
|
|
|
key: "bad/key/here",
|
2018-09-26 22:18:45 +00:00
|
|
|
expectedErr: "error splitting queue key: unexpected key format: \"bad/key/here\"",
|
2017-08-02 17:27:17 +00:00
|
|
|
},
|
|
|
|
{
|
2018-09-26 22:18:45 +00:00
|
|
|
name: "backup not found in lister returns error",
|
|
|
|
key: "nonexistent/backup",
|
|
|
|
expectedErr: "error getting backup: backup.ark.heptio.com \"backup\" not found",
|
2017-08-02 17:27:17 +00:00
|
|
|
},
|
|
|
|
{
|
2018-09-26 22:18:45 +00:00
|
|
|
name: "FailedValidation backup is not processed",
|
|
|
|
key: "heptio-ark/backup-1",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup-1").WithPhase(v1.BackupPhaseFailedValidation).Backup,
|
2017-08-02 17:27:17 +00:00
|
|
|
},
|
|
|
|
{
|
2018-09-26 22:18:45 +00:00
|
|
|
name: "InProgress backup is not processed",
|
|
|
|
key: "heptio-ark/backup-1",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup-1").WithPhase(v1.BackupPhaseInProgress).Backup,
|
2017-08-02 17:27:17 +00:00
|
|
|
},
|
|
|
|
{
|
2018-09-26 22:18:45 +00:00
|
|
|
name: "Completed backup is not processed",
|
|
|
|
key: "heptio-ark/backup-1",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup-1").WithPhase(v1.BackupPhaseCompleted).Backup,
|
2017-08-02 17:27:17 +00:00
|
|
|
},
|
|
|
|
{
|
2018-09-26 22:18:45 +00:00
|
|
|
name: "Failed backup is not processed",
|
|
|
|
key: "heptio-ark/backup-1",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup-1").WithPhase(v1.BackupPhaseFailed).Backup,
|
2017-08-09 22:52:27 +00:00
|
|
|
},
|
2018-09-26 22:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
sharedInformers = informers.NewSharedInformerFactory(fake.NewSimpleClientset(), 0)
|
|
|
|
logger = logging.DefaultLogger(logrus.DebugLevel)
|
|
|
|
)
|
|
|
|
|
|
|
|
c := &backupController{
|
|
|
|
genericController: newGenericController("backup-test", logger),
|
|
|
|
lister: sharedInformers.Ark().V1().Backups().Lister(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.backup != nil {
|
|
|
|
require.NoError(t, sharedInformers.Ark().V1().Backups().Informer().GetStore().Add(test.backup))
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.processBackup(test.key)
|
|
|
|
if test.expectedErr != "" {
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Equal(t, test.expectedErr, err.Error())
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any backup that would actually proceed to validation will cause a segfault because this
|
|
|
|
// test hasn't set up the necessary controller dependencies for validation/etc. So the lack
|
|
|
|
// of segfaults during test execution here imply that backups are not being processed, which
|
|
|
|
// is what we expect.
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessBackupValidationFailures(t *testing.T) {
|
|
|
|
defaultBackupLocation := arktest.NewTestBackupStorageLocation().WithName("loc-1").BackupStorageLocation
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
backup *v1.Backup
|
|
|
|
backupLocation *v1.BackupStorageLocation
|
|
|
|
expectedErrs []string
|
|
|
|
}{
|
2018-08-16 22:41:59 +00:00
|
|
|
{
|
2018-09-26 22:18:45 +00:00
|
|
|
name: "invalid included/excluded resources fails validation",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup-1").WithIncludedResources("foo").WithExcludedResources("foo").Backup,
|
|
|
|
backupLocation: defaultBackupLocation,
|
|
|
|
expectedErrs: []string{"Invalid included/excluded resource lists: excludes list cannot contain an item in the includes list: foo"},
|
2018-08-16 22:41:59 +00:00
|
|
|
},
|
|
|
|
{
|
2018-09-26 22:18:45 +00:00
|
|
|
name: "invalid included/excluded namespaces fails validation",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup-1").WithIncludedNamespaces("foo").WithExcludedNamespaces("foo").Backup,
|
|
|
|
backupLocation: defaultBackupLocation,
|
|
|
|
expectedErrs: []string{"Invalid included/excluded namespace lists: excludes list cannot contain an item in the includes list: foo"},
|
2018-08-16 22:41:59 +00:00
|
|
|
},
|
|
|
|
{
|
2018-09-26 22:18:45 +00:00
|
|
|
name: "non-existent backup location fails validation",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup-1").WithStorageLocation("nonexistent").Backup,
|
|
|
|
expectedErrs: []string{"Error getting backup storage location: backupstoragelocation.ark.heptio.com \"nonexistent\" not found"},
|
2018-08-16 22:41:59 +00:00
|
|
|
},
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2017-09-14 21:27:31 +00:00
|
|
|
var (
|
2018-09-26 22:18:45 +00:00
|
|
|
clientset = fake.NewSimpleClientset(test.backup)
|
|
|
|
sharedInformers = informers.NewSharedInformerFactory(clientset, 0)
|
2018-05-13 13:28:09 +00:00
|
|
|
logger = logging.DefaultLogger(logrus.DebugLevel)
|
2017-09-14 21:27:31 +00:00
|
|
|
)
|
2018-08-20 23:29:54 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
c := &backupController{
|
|
|
|
genericController: newGenericController("backup-test", logger),
|
|
|
|
client: clientset.ArkV1(),
|
|
|
|
lister: sharedInformers.Ark().V1().Backups().Lister(),
|
|
|
|
backupLocationLister: sharedInformers.Ark().V1().BackupStorageLocations().Lister(),
|
|
|
|
defaultBackupLocation: defaultBackupLocation.Name,
|
|
|
|
}
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
require.NotNil(t, test.backup)
|
|
|
|
require.NoError(t, sharedInformers.Ark().V1().Backups().Informer().GetStore().Add(test.backup))
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
if test.backupLocation != nil {
|
|
|
|
_, err := clientset.ArkV1().BackupStorageLocations(test.backupLocation.Namespace).Create(test.backupLocation)
|
|
|
|
require.NoError(t, err)
|
2018-06-20 18:08:07 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
require.NoError(t, sharedInformers.Ark().V1().BackupStorageLocations().Informer().GetStore().Add(test.backupLocation))
|
2018-05-13 13:28:09 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
require.NoError(t, c.processBackup(fmt.Sprintf("%s/%s", test.backup.Namespace, test.backup.Name)))
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
res, err := clientset.ArkV1().Backups(test.backup.Namespace).Get(test.backup.Name, metav1.GetOptions{})
|
|
|
|
require.NoError(t, err)
|
2018-07-10 22:17:53 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
assert.Equal(t, v1.BackupPhaseFailedValidation, res.Status.Phase)
|
|
|
|
assert.Equal(t, test.expectedErrs, res.Status.ValidationErrors)
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
// Any backup that would actually proceed to processing will cause a segfault because this
|
|
|
|
// test hasn't set up the necessary controller dependencies for running backups. So the lack
|
|
|
|
// of segfaults during test execution here imply that backups are not being processed, which
|
|
|
|
// is what we expect.
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-12-11 22:10:52 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
func TestProcessBackupCompletions(t *testing.T) {
|
|
|
|
defaultBackupLocation := arktest.NewTestBackupStorageLocation().WithName("loc-1").BackupStorageLocation
|
2017-12-11 22:10:52 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
now, err := time.Parse(time.RFC1123Z, time.RFC1123Z)
|
|
|
|
require.NoError(t, err)
|
|
|
|
now = now.Local()
|
2017-12-11 22:10:52 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
backup *v1.Backup
|
|
|
|
backupLocation *v1.BackupStorageLocation
|
|
|
|
expectedResult *v1.Backup
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "backup with no backup location gets the default",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup-1").Backup,
|
|
|
|
backupLocation: defaultBackupLocation,
|
|
|
|
expectedResult: &v1.Backup{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: v1.DefaultNamespace,
|
|
|
|
Name: "backup-1",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"ark.heptio.com/storage-location": "loc-1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: v1.BackupSpec{
|
|
|
|
StorageLocation: defaultBackupLocation.Name,
|
|
|
|
},
|
|
|
|
Status: v1.BackupStatus{
|
|
|
|
Phase: v1.BackupPhaseCompleted,
|
|
|
|
Version: 1,
|
|
|
|
StartTimestamp: metav1.NewTime(now),
|
|
|
|
CompletionTimestamp: metav1.NewTime(now),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "backup with a specific backup location keeps it",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup-1").WithStorageLocation("alt-loc").Backup,
|
|
|
|
backupLocation: arktest.NewTestBackupStorageLocation().WithName("alt-loc").BackupStorageLocation,
|
|
|
|
expectedResult: &v1.Backup{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: v1.DefaultNamespace,
|
|
|
|
Name: "backup-1",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"ark.heptio.com/storage-location": "alt-loc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: v1.BackupSpec{
|
|
|
|
StorageLocation: "alt-loc",
|
|
|
|
},
|
|
|
|
Status: v1.BackupStatus{
|
|
|
|
Phase: v1.BackupPhaseCompleted,
|
|
|
|
Version: 1,
|
|
|
|
StartTimestamp: metav1.NewTime(now),
|
|
|
|
CompletionTimestamp: metav1.NewTime(now),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "backup with a TTL has expiration set",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup-1").WithTTL(10 * time.Minute).Backup,
|
|
|
|
backupLocation: defaultBackupLocation,
|
|
|
|
expectedResult: &v1.Backup{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: v1.DefaultNamespace,
|
|
|
|
Name: "backup-1",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"ark.heptio.com/storage-location": "loc-1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: v1.BackupSpec{
|
|
|
|
TTL: metav1.Duration{Duration: 10 * time.Minute},
|
|
|
|
StorageLocation: defaultBackupLocation.Name,
|
|
|
|
},
|
|
|
|
Status: v1.BackupStatus{
|
|
|
|
Phase: v1.BackupPhaseCompleted,
|
|
|
|
Version: 1,
|
|
|
|
Expiration: metav1.NewTime(now.Add(10 * time.Minute)),
|
|
|
|
StartTimestamp: metav1.NewTime(now),
|
|
|
|
CompletionTimestamp: metav1.NewTime(now),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-12-11 22:10:52 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
clientset = fake.NewSimpleClientset(test.backup)
|
|
|
|
sharedInformers = informers.NewSharedInformerFactory(clientset, 0)
|
|
|
|
logger = logging.DefaultLogger(logrus.DebugLevel)
|
|
|
|
pluginManager = new(pluginmocks.Manager)
|
|
|
|
backupStore = new(persistencemocks.BackupStore)
|
|
|
|
backupper = new(fakeBackupper)
|
|
|
|
)
|
2018-06-20 18:08:07 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
c := &backupController{
|
|
|
|
genericController: newGenericController("backup-test", logger),
|
|
|
|
client: clientset.ArkV1(),
|
|
|
|
lister: sharedInformers.Ark().V1().Backups().Lister(),
|
|
|
|
backupLocationLister: sharedInformers.Ark().V1().BackupStorageLocations().Lister(),
|
|
|
|
defaultBackupLocation: defaultBackupLocation.Name,
|
|
|
|
backupTracker: NewBackupTracker(),
|
|
|
|
metrics: metrics.NewServerMetrics(),
|
|
|
|
clock: clock.NewFakeClock(now),
|
|
|
|
newPluginManager: func(logrus.FieldLogger) plugin.Manager { return pluginManager },
|
|
|
|
newBackupStore: func(*v1.BackupStorageLocation, persistence.ObjectStoreGetter, logrus.FieldLogger) (persistence.BackupStore, error) {
|
|
|
|
return backupStore, nil
|
|
|
|
},
|
|
|
|
backupper: backupper,
|
|
|
|
}
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
pluginManager.On("GetBackupItemActions").Return(nil, nil)
|
|
|
|
pluginManager.On("CleanupClients").Return(nil)
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
backupper.On("Backup", mock.Anything, mock.Anything, mock.Anything, []pkgbackup.ItemAction(nil), pluginManager).Return(nil)
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
// Ensure we have a CompletionTimestamp when uploading.
|
|
|
|
// Failures will display the bytes in buf.
|
|
|
|
completionTimestampIsPresent := func(buf *bytes.Buffer) bool {
|
|
|
|
return strings.Contains(buf.String(), `"completionTimestamp": "2006-01-02T22:04:05Z"`)
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
2018-09-26 22:18:45 +00:00
|
|
|
backupStore.On("PutBackup", test.backup.Name, mock.MatchedBy(completionTimestampIsPresent), mock.Anything, mock.Anything).Return(nil)
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
// add the test's backup to the informer/lister store
|
|
|
|
require.NotNil(t, test.backup)
|
|
|
|
require.NoError(t, sharedInformers.Ark().V1().Backups().Informer().GetStore().Add(test.backup))
|
2017-12-11 22:10:52 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
// add the default backup storage location to the clientset and the informer/lister store
|
|
|
|
_, err := clientset.ArkV1().BackupStorageLocations(defaultBackupLocation.Namespace).Create(defaultBackupLocation)
|
|
|
|
require.NoError(t, err)
|
2017-12-11 22:10:52 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
require.NoError(t, sharedInformers.Ark().V1().BackupStorageLocations().Informer().GetStore().Add(defaultBackupLocation))
|
2017-12-11 22:10:52 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
// add the test's backup storage location to the clientset and the informer/lister store
|
|
|
|
// if it's different than the default
|
|
|
|
if test.backupLocation != nil && test.backupLocation != defaultBackupLocation {
|
|
|
|
_, err := clientset.ArkV1().BackupStorageLocations(test.backupLocation.Namespace).Create(test.backupLocation)
|
|
|
|
require.NoError(t, err)
|
2017-12-11 22:10:52 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
require.NoError(t, sharedInformers.Ark().V1().BackupStorageLocations().Informer().GetStore().Add(test.backupLocation))
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
require.NoError(t, c.processBackup(fmt.Sprintf("%s/%s", test.backup.Namespace, test.backup.Name)))
|
2017-12-11 22:10:52 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
res, err := clientset.ArkV1().Backups(test.backup.Namespace).Get(test.backup.Name, metav1.GetOptions{})
|
|
|
|
require.NoError(t, err)
|
2017-12-11 22:10:52 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
assert.Equal(t, test.expectedResult, res)
|
2017-08-02 17:27:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-09-25 14:51:28 +00:00
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
func TestValidateAndGetSnapshotLocations(t *testing.T) {
|
|
|
|
defaultLocationsAWS := map[string]*v1.VolumeSnapshotLocation{
|
|
|
|
"aws": arktest.NewTestVolumeSnapshotLocation().WithName("aws-us-east-2").VolumeSnapshotLocation,
|
|
|
|
}
|
|
|
|
defaultLocationsFake := map[string]*v1.VolumeSnapshotLocation{
|
|
|
|
"fake-provider": arktest.NewTestVolumeSnapshotLocation().WithName("some-name").VolumeSnapshotLocation,
|
|
|
|
}
|
2018-09-25 14:51:28 +00:00
|
|
|
|
|
|
|
multipleLocationNames := []string{"aws-us-west-1", "aws-us-east-1"}
|
|
|
|
|
|
|
|
multipleLocation1 := arktest.LocationInfo{
|
|
|
|
Name: multipleLocationNames[0],
|
|
|
|
Provider: "aws",
|
|
|
|
Config: map[string]string{"region": "us-west-1"},
|
|
|
|
}
|
|
|
|
multipleLocation2 := arktest.LocationInfo{
|
|
|
|
Name: multipleLocationNames[1],
|
|
|
|
Provider: "aws",
|
|
|
|
Config: map[string]string{"region": "us-west-1"},
|
|
|
|
}
|
|
|
|
|
|
|
|
multipleLocationList := []arktest.LocationInfo{multipleLocation1, multipleLocation2}
|
|
|
|
|
|
|
|
dupLocationNames := []string{"aws-us-west-1", "aws-us-west-1"}
|
|
|
|
dupLocation1 := arktest.LocationInfo{
|
|
|
|
Name: dupLocationNames[0],
|
|
|
|
Provider: "aws",
|
|
|
|
Config: map[string]string{"region": "us-west-1"},
|
|
|
|
}
|
|
|
|
dupLocation2 := arktest.LocationInfo{
|
|
|
|
Name: dupLocationNames[0],
|
|
|
|
Provider: "aws",
|
|
|
|
Config: map[string]string{"region": "us-west-1"},
|
|
|
|
}
|
|
|
|
dupLocationList := []arktest.LocationInfo{dupLocation1, dupLocation2}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
backup *arktest.TestBackup
|
|
|
|
locations []*arktest.TestVolumeSnapshotLocation
|
2018-09-26 22:18:45 +00:00
|
|
|
defaultLocations map[string]*v1.VolumeSnapshotLocation
|
2018-09-25 14:51:28 +00:00
|
|
|
expectedVolumeSnapshotLocationNames []string // adding these in the expected order will allow to test with better msgs in case of a test failure
|
|
|
|
expectedErrors string
|
|
|
|
expectedSuccess bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "location name does not correspond to any existing location",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup1").WithPhase(v1.BackupPhaseNew).WithVolumeSnapshotLocations([]string{"random-name"}),
|
|
|
|
locations: arktest.NewTestVolumeSnapshotLocation().WithName(dupLocationNames[0]).WithProviderConfig(dupLocationList),
|
|
|
|
expectedErrors: "error getting volume snapshot location named random-name: volumesnapshotlocation.ark.heptio.com \"random-name\" not found",
|
|
|
|
expectedSuccess: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "duplicate locationName per provider: should filter out dups",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup1").WithPhase(v1.BackupPhaseNew).WithVolumeSnapshotLocations(dupLocationNames),
|
|
|
|
locations: arktest.NewTestVolumeSnapshotLocation().WithName(dupLocationNames[0]).WithProviderConfig(dupLocationList),
|
|
|
|
expectedVolumeSnapshotLocationNames: []string{dupLocationNames[0]},
|
|
|
|
expectedSuccess: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple location names per provider",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup1").WithPhase(v1.BackupPhaseNew).WithVolumeSnapshotLocations(multipleLocationNames),
|
|
|
|
locations: arktest.NewTestVolumeSnapshotLocation().WithName(multipleLocationNames[0]).WithProviderConfig(multipleLocationList),
|
|
|
|
expectedErrors: "more than one VolumeSnapshotLocation name specified for provider aws: aws-us-east-1; unexpected name was aws-us-west-1",
|
|
|
|
expectedSuccess: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no location name for the provider exists: the provider's default should be added",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup1").WithPhase(v1.BackupPhaseNew),
|
|
|
|
defaultLocations: defaultLocationsAWS,
|
2018-09-26 22:18:45 +00:00
|
|
|
expectedVolumeSnapshotLocationNames: []string{defaultLocationsAWS["aws"].Name},
|
2018-09-25 14:51:28 +00:00
|
|
|
expectedSuccess: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no existing location name and no default location name given",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup1").WithPhase(v1.BackupPhaseNew),
|
|
|
|
expectedSuccess: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple location names for a provider, default location name for another provider",
|
|
|
|
backup: arktest.NewTestBackup().WithName("backup1").WithPhase(v1.BackupPhaseNew).WithVolumeSnapshotLocations(dupLocationNames),
|
|
|
|
locations: arktest.NewTestVolumeSnapshotLocation().WithName(dupLocationNames[0]).WithProviderConfig(dupLocationList),
|
|
|
|
defaultLocations: defaultLocationsFake,
|
2018-09-26 22:18:45 +00:00
|
|
|
expectedVolumeSnapshotLocationNames: []string{dupLocationNames[0], defaultLocationsFake["fake-provider"].Name},
|
2018-09-25 14:51:28 +00:00
|
|
|
expectedSuccess: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
client = fake.NewSimpleClientset()
|
|
|
|
sharedInformers = informers.NewSharedInformerFactory(client, 0)
|
|
|
|
)
|
|
|
|
|
|
|
|
c := &backupController{
|
2018-09-26 22:18:45 +00:00
|
|
|
snapshotLocationLister: sharedInformers.Ark().V1().VolumeSnapshotLocations().Lister(),
|
|
|
|
defaultSnapshotLocations: test.defaultLocations,
|
2018-09-25 14:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set up a Backup object to represent what we expect to be passed to backupper.Backup()
|
|
|
|
backup := test.backup.DeepCopy()
|
|
|
|
backup.Spec.VolumeSnapshotLocations = test.backup.Spec.VolumeSnapshotLocations
|
|
|
|
for _, location := range test.locations {
|
|
|
|
require.NoError(t, sharedInformers.Ark().V1().VolumeSnapshotLocations().Informer().GetStore().Add(location.VolumeSnapshotLocation))
|
|
|
|
}
|
|
|
|
|
2018-09-26 22:18:45 +00:00
|
|
|
providerLocations, errs := c.validateAndGetSnapshotLocations(backup)
|
2018-09-25 14:51:28 +00:00
|
|
|
if test.expectedSuccess {
|
|
|
|
for _, err := range errs {
|
2018-09-26 22:18:45 +00:00
|
|
|
require.NoError(t, errors.New(err), "validateAndGetSnapshotLocations unexpected error: %v", err)
|
2018-09-25 14:51:28 +00:00
|
|
|
}
|
2018-09-26 22:18:45 +00:00
|
|
|
|
|
|
|
var locations []string
|
|
|
|
for _, loc := range providerLocations {
|
|
|
|
locations = append(locations, loc.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(test.expectedVolumeSnapshotLocationNames)
|
|
|
|
sort.Strings(locations)
|
|
|
|
require.Equal(t, test.expectedVolumeSnapshotLocationNames, locations)
|
2018-09-25 14:51:28 +00:00
|
|
|
} else {
|
|
|
|
if len(errs) == 0 {
|
2018-09-26 22:18:45 +00:00
|
|
|
require.Error(t, nil, "validateAndGetSnapshotLocations expected error")
|
2018-09-25 14:51:28 +00:00
|
|
|
}
|
|
|
|
require.Contains(t, errs, test.expectedErrors)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|