Skip to content

Commit

Permalink
backup:add unit test for backup controller
Browse files Browse the repository at this point in the history
Signed-off-by: Xieql <xieqianglong@huawei.com>
  • Loading branch information
Xieql committed Oct 20, 2023
1 parent cc5148b commit d01a441
Show file tree
Hide file tree
Showing 5 changed files with 733 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/fleet-manager/backup-testdata/backup/include-ns.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: velero.io/v1
kind: Backup
metadata:
creationTimestamp: null
labels:
fleet.kurator.dev/fleet-name: quickstart
fleet.kurator.dev/plugin: backup
kurator.dev/backup-name: include-ns
name: kurator-member1-backup-default-include-ns
namespace: velero
spec:
csiSnapshotTimeout: 0s
hooks: {}
includedNamespaces:
- kurator-backup
itemOperationTimeout: 0s
metadata: {}
ttl: 720h0m0s
status: {}
20 changes: 20 additions & 0 deletions pkg/fleet-manager/backup-testdata/backup/label-selector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: velero.io/v1
kind: Backup
metadata:
creationTimestamp: null
labels:
fleet.kurator.dev/fleet-name: quickstart
fleet.kurator.dev/plugin: backup
kurator.dev/migrate-name: label-selector
name: kurator-member2-migrate-default-label-selector
namespace: velero
spec:
csiSnapshotTimeout: 0s
hooks: {}
itemOperationTimeout: 0s
labelSelector:
matchLabels:
app: busybox2
metadata: {}
ttl: 240h0m0s
status: {}
21 changes: 21 additions & 0 deletions pkg/fleet-manager/backup-testdata/backup/schedule.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: velero.io/v1
kind: Schedule
metadata:
creationTimestamp: null
labels:
fleet.kurator.dev/fleet-name: quickstart
fleet.kurator.dev/plugin: backup
kurator.dev/backup-name: schedule
name: kurator-member1-backup-default-schedule
namespace: velero
spec:
schedule: 0 0 * * *
template:
csiSnapshotTimeout: 0s
hooks: {}
includedNamespaces:
- kurator-backup
itemOperationTimeout: 0s
metadata: {}
ttl: 720h0m0s
status: {}
242 changes: 242 additions & 0 deletions pkg/fleet-manager/backup_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
/*
Copyright Kurator Authors.
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 fleet

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

backupapi "kurator.dev/kurator/pkg/apis/backups/v1alpha1"
fleetapi "kurator.dev/kurator/pkg/apis/fleet/v1alpha1"
)

const (
testFleetName = "test-fleet"
testNamespace = "default"
testBackupName = "test-backup"
)

// setupTest creates a fake client, a scheme and a BackupManager for testing.
func setupTest() (*BackupManager) {
scheme := runtime.NewScheme()
backupapi.AddToScheme(scheme)

Check failure on line 42 in pkg/fleet-manager/backup_controller_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value is not checked (errcheck)
fleetapi.AddToScheme(scheme)

Check failure on line 43 in pkg/fleet-manager/backup_controller_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value is not checked (errcheck)

client := fake.NewClientBuilder().WithScheme(scheme).Build()

mgr := &BackupManager{Client: client, Scheme: scheme}

return mgr
}

// createTestReconcileRequest creates a test Reconcile request for the given Backup object.
func createTestReconcileRequest(backup *backupapi.Backup) reconcile.Request {
if backup == nil {
return reconcile.Request{}
}
return reconcile.Request{
NamespacedName: types.NamespacedName{
Name: backup.Name,
Namespace: backup.Namespace,
},
}
}

// createTestBackup creates a test Backup for the given Backup name and namespace.
func createTestBackup(name, namespace string) *backupapi.Backup {
return &backupapi.Backup{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: backupapi.BackupSpec{
Destination: backupapi.Destination{
Fleet: testFleetName,
},
},
}
}

func createTestFleet(name, namespace string) *fleetapi.Fleet {
return &fleetapi.Fleet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
}

func TestReconcile(t *testing.T) {
mgr := setupTest()
fleetObj := createTestFleet(testFleetName, testNamespace)
mgr.Client.Create(context.Background(), fleetObj)

Check failure on line 92 in pkg/fleet-manager/backup_controller_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `mgr.Client.Create` is not checked (errcheck)

tests := []struct {
name string
backup *backupapi.Backup
wantResult ctrl.Result
wantErr bool
}{
{
name: "Backup object not found",
backup: nil,
wantResult: ctrl.Result{},
wantErr: false,
},
{
name: "Backup without finalizer",
backup: createTestBackup(testBackupName, testNamespace),
wantResult: ctrl.Result{},
wantErr: false,
},
{
name: "Backup with deletion timestamp",
backup: func() *backupapi.Backup {
b := createTestBackup(testBackupName, testNamespace)
now := metav1.Now()
b.DeletionTimestamp = &now
return b
}(),
wantResult: ctrl.Result{},
wantErr: false,
},
{
name: "Normal backup",
backup: createTestBackup(testBackupName, testNamespace),
wantResult: ctrl.Result{},
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mgr.Client.Create(context.Background(), tt.backup)

Check failure on line 133 in pkg/fleet-manager/backup_controller_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `mgr.Client.Create` is not checked (errcheck)

ctx := context.TODO()
req := createTestReconcileRequest(tt.backup)

gotResult, gotErr := mgr.Reconcile(ctx, req)
assert.Equal(t, tt.wantResult, gotResult)
if tt.wantErr {
assert.NotNil(t, gotErr)
} else {
assert.Nil(t, gotErr)
}
})
}
}
func TestReconcileBackupResources(t *testing.T) {
mgr := setupTest()
fleetObj := createTestFleet(testFleetName, testNamespace)
mgr.Client.Create(context.Background(), fleetObj)

Check failure on line 151 in pkg/fleet-manager/backup_controller_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `mgr.Client.Create` is not checked (errcheck)

tests := []struct {
name string
backup *backupapi.Backup
wantErr bool
}{
{
name: "Test scheduled backup",
backup: func() *backupapi.Backup {
b := createTestBackup(testBackupName, testNamespace)
b.Spec.Schedule = "test-schedule"
return b
}(),
wantErr: false,
},
{
name: "Test one-time backup",
backup: createTestBackup(testBackupName, testNamespace),
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fleetObj := createTestFleet(testFleetName, testNamespace)
mgr.Client.Create(context.Background(), fleetObj)

Check failure on line 177 in pkg/fleet-manager/backup_controller_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `mgr.Client.Create` is not checked (errcheck)

_, gotErr := mgr.reconcileBackupResources(context.TODO(), tt.backup, nil)

if tt.wantErr {
assert.NotNil(t, gotErr)
} else {
assert.Nil(t, gotErr)
}
})
}
}

func TestReconcileDeleteBackup(t *testing.T) {
mgr := setupTest()
fleetObj := createTestFleet(testFleetName, testNamespace)
mgr.Client.Create(context.Background(), fleetObj)

Check failure on line 193 in pkg/fleet-manager/backup_controller_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `mgr.Client.Create` is not checked (errcheck)

tests := []struct {
name string
backup *backupapi.Backup
wantErr bool
wantFinalizer bool
}{
{
name: "Successful deletion",
backup: func() *backupapi.Backup {
b := createTestBackup(testBackupName, testNamespace)
controllerutil.AddFinalizer(b, BackupFinalizer)
return b
}(),
wantErr: false,
wantFinalizer: false,
},
{
name: "Failed deletion due to fetch error",
backup: func() *backupapi.Backup {
b := createTestBackup("non-existent", "non-existent")
controllerutil.AddFinalizer(b, BackupFinalizer)
return b
}(),
wantErr: true,
wantFinalizer: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mgr.Client.Create(context.Background(), tt.backup)

Check failure on line 225 in pkg/fleet-manager/backup_controller_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `mgr.Client.Create` is not checked (errcheck)

_, err := mgr.reconcileDeleteBackup(context.TODO(), tt.backup)

if tt.wantErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}

if tt.wantFinalizer {
assert.Contains(t, tt.backup.Finalizers, BackupFinalizer)
} else {
assert.NotContains(t, tt.backup.Finalizers, BackupFinalizer)
}
})
}
}
Loading

0 comments on commit d01a441

Please sign in to comment.