Skip to content

Commit

Permalink
Add PatchResource unit test for backup status
Browse files Browse the repository at this point in the history
Signed-off-by: Tiger Kaovilai <[email protected]>
  • Loading branch information
kaovilai committed Nov 1, 2023
1 parent 705a3bc commit 886e074
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/builder/backup_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,9 @@ func (b *BackupBuilder) DataMover(name string) *BackupBuilder {
b.object.Spec.DataMover = name
return b
}

// WithStatus sets the Backup's status.
func (b *BackupBuilder) WithStatus(status velerov1api.BackupStatus) *BackupBuilder {
b.object.Status = status
return b
}
67 changes: 67 additions & 0 deletions pkg/controller/backup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
"context"
"fmt"
"io"
"reflect"
"sort"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
snapshotv1api "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
snapshotfake "github.com/kubernetes-csi/external-snapshotter/client/v4/clientset/versioned/fake"
snapshotinformers "github.com/kubernetes-csi/external-snapshotter/client/v4/informers/externalversions"
Expand All @@ -41,8 +43,13 @@ import (
"k8s.io/utils/clock"
testclocks "k8s.io/utils/clock/testing"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
kbclient "sigs.k8s.io/controller-runtime/pkg/client"

kubeutil "github.com/vmware-tanzu/velero/pkg/util/kube"

fakeClient "sigs.k8s.io/controller-runtime/pkg/client/fake"

velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
pkgbackup "github.com/vmware-tanzu/velero/pkg/backup"
"github.com/vmware-tanzu/velero/pkg/builder"
Expand Down Expand Up @@ -1665,3 +1672,63 @@ func Test_getLastSuccessBySchedule(t *testing.T) {
})
}
}

// Unit tests to make sure that the backup's status is updated correctly during reconcile.
// To clear up confusion whether status can be updated with Patch alone without status writer and not kbClient.Status().Patch()
func TestPatchResourceWorksWithStatus(t *testing.T) {
type args struct {
original *velerov1api.Backup
updated *velerov1api.Backup
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "patch backup status",
args: args{
original: defaultBackup().SnapshotMoveData(false).Result(),
updated: defaultBackup().SnapshotMoveData(false).WithStatus(velerov1api.BackupStatus{
CSIVolumeSnapshotsCompleted: 1,
}).Result(),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
scheme := runtime.NewScheme()
error := velerov1api.AddToScheme(scheme)
if error != nil {
t.Errorf("PatchResource() error = %v", error)
}
fakeClient := fakeClient.NewClientBuilder().WithScheme(scheme).WithObjects(tt.args.original).Build()
fromCluster := &velerov1api.Backup{
ObjectMeta: metav1.ObjectMeta{
Name: tt.args.original.Name,
Namespace: tt.args.original.Namespace,
},
}
// check original exists
if err := fakeClient.Get(context.Background(), client.ObjectKeyFromObject(tt.args.updated), fromCluster); err != nil {
t.Errorf("PatchResource() error = %v", err)
}
// ignore resourceVersion
tt.args.updated.ResourceVersion = fromCluster.ResourceVersion
tt.args.original.ResourceVersion = fromCluster.ResourceVersion
if err := kubeutil.PatchResource(tt.args.original, tt.args.updated, fakeClient); (err != nil) != tt.wantErr {
t.Errorf("PatchResource() error = %v, wantErr %v", err, tt.wantErr)
}
// check updated exists
if err := fakeClient.Get(context.Background(), client.ObjectKeyFromObject(tt.args.updated), fromCluster); err != nil {
t.Errorf("PatchResource() error = %v", err)
}

// check fromCluster is equal to updated
if !reflect.DeepEqual(fromCluster, tt.args.updated) {
t.Error(cmp.Diff(fromCluster, tt.args.updated))
}
})

}
}
2 changes: 2 additions & 0 deletions pkg/controller/backup_operations_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ func (c *backupOperationsReconciler) updateBackupAndOperationsJSON(
return nil
}

// check progress of backupItemOperations
// return: inProgressOperations, changes, completedCount, failedCount, errs
func getBackupItemOperationProgress(
backup *velerov1api.Backup,
pluginManager clientmgmt.Manager,
Expand Down

0 comments on commit 886e074

Please sign in to comment.