Skip to content

Commit

Permalink
wip tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Goldstein <andy.goldstein@gmail.com>
  • Loading branch information
ncdc committed Mar 16, 2018
1 parent fa3953b commit ea74e5d
Show file tree
Hide file tree
Showing 7 changed files with 548 additions and 305 deletions.
3 changes: 3 additions & 0 deletions pkg/backup/delete_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// NewDeleteBackupRequest creates a DeleteBackupRequest for the backup identified by name.
func NewDeleteBackupRequest(name string) *v1.DeleteBackupRequest {
return &v1.DeleteBackupRequest{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -37,6 +38,8 @@ func NewDeleteBackupRequest(name string) *v1.DeleteBackupRequest {
}
}

// NewDeleteBackupRequestListOptions creates a ListOptions with a label selector configured to
// find DeleteBackupRequests for the backup identified by name.
func NewDeleteBackupRequestListOptions(name string) metav1.ListOptions {
return metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", v1.BackupNameLabel, name),
Expand Down
18 changes: 12 additions & 6 deletions pkg/cmd/cli/backup/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/heptio/ark/pkg/client"
"github.com/heptio/ark/pkg/cmd"
Expand Down Expand Up @@ -67,18 +68,23 @@ func (o *DeleteOptions) Validate(c *cobra.Command, args []string, f client.Facto
return errors.New("you must specify only one argument, the backup's name")
}

return nil
}

func (o *DeleteOptions) Complete(f client.Factory, args []string) error {
o.Name = args[0]

var err error
o.client, err = f.Client()
if err != nil {
return err
}

_, err = o.client.ArkV1().Backups(f.Namespace()).Get(args[0], metav1.GetOptions{})
if err != nil {
return err
}

return nil
}

func (o *DeleteOptions) Complete(f client.Factory, args []string) error {
o.Name = args[0]

o.namespace = f.Namespace()

return nil
Expand Down
16 changes: 3 additions & 13 deletions pkg/controller/backup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ func TestProcessBackup(t *testing.T) {
backup.Status.Phase = v1.BackupPhaseInProgress
backup.Status.Expiration.Time = expiration
backup.Status.Version = 1
backup.Finalizers = []string{v1.GCFinalizer}
backupper.On("Backup", backup, mock.Anything, mock.Anything, mock.Anything).Return(nil)

cloudBackups.On("UploadBackup", "bucket", backup.Name, mock.Anything, mock.Anything, mock.Anything).Return(nil)
Expand Down Expand Up @@ -226,7 +225,6 @@ func TestProcessBackup(t *testing.T) {
res.Status.Version = 1
res.Status.Expiration.Time = expiration
res.Status.Phase = v1.BackupPhase(phase)
res.Finalizers = []string{v1.GCFinalizer}

return true, res, nil
})
Expand All @@ -249,15 +247,15 @@ func TestProcessBackup(t *testing.T) {
actions := client.Actions()
require.Equal(t, 2, len(actions))

// validate Patch call 1 (setting finalizer, version, expiration, and phase)
// validate Patch call 1 (setting version, expiration, and phase)
patchAction, ok := actions[0].(core.PatchAction)
require.True(t, ok, "action is not a PatchAction")

patch := make(map[string]interface{})
require.NoError(t, json.Unmarshal(patchAction.GetPatch(), &patch), "cannot unmarshal patch")

// should have metadata and status
assert.Equal(t, 2, len(patch), "patch has wrong number of keys")
// should have status
assert.Equal(t, 1, len(patch), "patch has wrong number of keys")

expectedStatusKeys := 2
if test.backup.Spec.TTL.Duration > 0 {
Expand All @@ -271,14 +269,6 @@ func TestProcessBackup(t *testing.T) {
res, _ := collections.GetMap(patch, "status")
assert.Equal(t, expectedStatusKeys, len(res), "patch's status has the wrong number of keys")

finalizers, err := collections.GetSlice(patch, "metadata.finalizers")
require.NoError(t, err, "patch does not contain metadata.finalizers")
assert.Equal(t, 1, len(finalizers))
assert.Equal(t, v1.GCFinalizer, finalizers[0])

res, _ = collections.GetMap(patch, "metadata")
assert.Equal(t, 1, len(res), "patch's metadata has the wrong number of keys")

// validate Patch call 2 (setting phase)
patchAction, ok = actions[1].(core.PatchAction)
require.True(t, ok, "action is not a PatchAction")
Expand Down
5 changes: 4 additions & 1 deletion pkg/controller/backup_deletion_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type backupDeletionController struct {
bucket string
restoreLister listers.RestoreLister
restoreClient arkv1client.RestoresGetter

processRequestFunc func(*v1.DeleteBackupRequest) error
}

// NewBackupDeletionController creates a new backup deletion controller.
Expand Down Expand Up @@ -75,6 +77,7 @@ func NewBackupDeletionController(

c.syncHandler = c.processQueueItem
c.cacheSyncWaiters = append(c.cacheSyncWaiters, deleteBackupRequestInformer.Informer().HasSynced, restoreInformer.Informer().HasSynced)
c.processRequestFunc = c.processRequest

deleteBackupRequestInformer.Informer().AddEventHandler(
cache.ResourceEventHandlerFuncs{
Expand Down Expand Up @@ -110,7 +113,7 @@ func (c *backupDeletionController) processQueueItem(key string) error {
default:
// Don't mutate the shared cache
reqCopy := req.DeepCopy()
return c.processRequest(reqCopy)
return c.processRequestFunc(reqCopy)
}

return nil
Expand Down
Loading

0 comments on commit ea74e5d

Please sign in to comment.