Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for restore controller #2166

Merged
merged 5 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/backup/restore/restore_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,21 @@ func (rm *restoreManager) ensureRestorePVCExist(restore *v1alpha1.Restore) (stri
}

var _ backup.RestoreManager = &restoreManager{}

type FakeRestoreManager struct {
err error
}

func NewFakeRestoreManager() *FakeRestoreManager {
return &FakeRestoreManager{}
}

func (frm *FakeRestoreManager) SetSyncError(err error) {
frm.err = err
}

func (frm *FakeRestoreManager) Sync(_ *v1alpha1.Restore) error {
return frm.err
}

var _ backup.RestoreManager = &FakeRestoreManager{}
2 changes: 1 addition & 1 deletion pkg/controller/backup/backup_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestBackupControlUpdateBackup(t *testing.T) {
},
},
{
name: "backup manager update failed",
name: "backup manager sync failed",
update: func(backup *v1alpha1.Backup) {
backup.Finalizers = append(backup.Finalizers, label.BackupProtectionFinalizer)
},
Expand Down
24 changes: 24 additions & 0 deletions pkg/controller/backup/backup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestBackupControllerUpdateBackup(t *testing.T) {
type testcase struct {
name string
backupHasBeenDeleted bool
backupIsInvalid bool
backupHasBeenCompleted bool
backupHasBeenScheduled bool
expectFn func(*GomegaWithT, *Controller)
Expand All @@ -68,6 +69,15 @@ func TestBackupControllerUpdateBackup(t *testing.T) {
backup.DeletionTimestamp = &metav1.Time{Time: time.Now()}
}

if test.backupIsInvalid {
backup.Status.Conditions = []v1alpha1.BackupCondition{
{
Type: v1alpha1.BackupInvalid,
Status: corev1.ConditionTrue,
},
}
}

if test.backupHasBeenCompleted {
backup.Status.Conditions = []v1alpha1.BackupCondition{
{
Expand Down Expand Up @@ -96,15 +106,27 @@ func TestBackupControllerUpdateBackup(t *testing.T) {
{
name: "backup has been deleted",
backupHasBeenDeleted: true,
backupIsInvalid: false,
backupHasBeenCompleted: false,
backupHasBeenScheduled: false,
expectFn: func(g *GomegaWithT, bkc *Controller) {
g.Expect(bkc.queue.Len()).To(Equal(1))
},
},
{
name: "backup is invalid",
backupHasBeenDeleted: false,
backupIsInvalid: true,
backupHasBeenCompleted: false,
backupHasBeenScheduled: false,
expectFn: func(g *GomegaWithT, bkc *Controller) {
g.Expect(bkc.queue.Len()).To(Equal(0))
},
},
{
name: "backup has been completed",
backupHasBeenDeleted: false,
backupIsInvalid: false,
backupHasBeenCompleted: true,
backupHasBeenScheduled: false,
expectFn: func(g *GomegaWithT, bkc *Controller) {
Expand All @@ -114,6 +136,7 @@ func TestBackupControllerUpdateBackup(t *testing.T) {
{
name: "backup has been scheduled",
backupHasBeenDeleted: false,
backupIsInvalid: false,
backupHasBeenCompleted: false,
backupHasBeenScheduled: true,
expectFn: func(g *GomegaWithT, bkc *Controller) {
Expand All @@ -123,6 +146,7 @@ func TestBackupControllerUpdateBackup(t *testing.T) {
{
name: "backup is newly created",
backupHasBeenDeleted: false,
backupIsInvalid: false,
backupHasBeenCompleted: false,
backupHasBeenScheduled: false,
expectFn: func(g *GomegaWithT, bkc *Controller) {
Expand Down
36 changes: 36 additions & 0 deletions pkg/controller/restore/restore_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package restore
import (
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/backup"
informers "github.com/pingcap/tidb-operator/pkg/client/informers/externalversions/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
"k8s.io/client-go/tools/cache"
)

// ControlInterface implements the control logic for updating Restore
Expand All @@ -39,8 +41,42 @@ type defaultRestoreControl struct {
restoreManager backup.RestoreManager
}

var _ ControlInterface = &defaultRestoreControl{}

// UpdateRestore executes the core logic loop for a Restore.
func (rc *defaultRestoreControl) UpdateRestore(restore *v1alpha1.Restore) error {
restore.SetGroupVersionKind(controller.RestoreControllerKind)
return rc.restoreManager.Sync(restore)
}

// FakeRestoreControl is a fake RestoreControlInterface
type FakeRestoreControl struct {
backupIndexer cache.Indexer
updateRestoreTracker controller.RequestTracker
}

// NewFakeRestoreControl returns a FakeRestoreControl
func NewFakeRestoreControl(restoreInformer informers.RestoreInformer) *FakeRestoreControl {
return &FakeRestoreControl{
restoreInformer.Informer().GetIndexer(),
controller.RequestTracker{},
}
}

// SetUpdateRestoreError sets the error attributes of updateRestoreTracker
func (fbc *FakeRestoreControl) SetUpdateRestoreError(err error, after int) {
fbc.updateRestoreTracker.SetError(err).SetAfter(after)
}

// UpdateRestore adds the backup to RestoreIndexer
func (fbc *FakeRestoreControl) UpdateRestore(backup *v1alpha1.Restore) error {
defer fbc.updateRestoreTracker.Inc()
if fbc.updateRestoreTracker.ErrorReady() {
defer fbc.updateRestoreTracker.Reset()
return fbc.updateRestoreTracker.GetError()
}

return fbc.backupIndexer.Add(backup)
}

var _ ControlInterface = &FakeRestoreControl{}
84 changes: 84 additions & 0 deletions pkg/controller/restore/restore_control_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2020 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package restore

import (
"fmt"
"strings"
"testing"

. "github.com/onsi/gomega"
"github.com/pingcap/tidb-operator/pkg/backup/restore"
"github.com/pingcap/tidb-operator/pkg/client/clientset/versioned/fake"
informers "github.com/pingcap/tidb-operator/pkg/client/informers/externalversions"
"k8s.io/client-go/tools/cache"
)

func TestRestoreControlUpdateRestore(t *testing.T) {
g := NewGomegaWithT(t)

type testcase struct {
name string
syncRestoreManagerErr bool
errExpectFn func(*GomegaWithT, error)
}
testFn := func(test *testcase, t *testing.T) {
t.Log(test.name)

restore := newRestore()
control, restoreIndexer, restoreManager := newFakeRestoreControl()

restoreIndexer.Add(restore)

if test.syncRestoreManagerErr {
restoreManager.SetSyncError(fmt.Errorf("restore manager sync error"))
}

err := control.UpdateRestore(restore)
if test.errExpectFn != nil {
test.errExpectFn(g, err)
}
}
tests := []testcase{
{
name: "restore manager sync failed",
syncRestoreManagerErr: true,
errExpectFn: func(g *GomegaWithT, err error) {
g.Expect(err).To(HaveOccurred())
g.Expect(strings.Contains(err.Error(), "restore manager sync error")).To(Equal(true))
},
},
{
name: "update newly create restore normally",
syncRestoreManagerErr: false,
errExpectFn: func(g *GomegaWithT, err error) {
g.Expect(err).NotTo(HaveOccurred())
},
},
}

for i := range tests {
testFn(&tests[i], t)
}
}

func newFakeRestoreControl() (ControlInterface, cache.Indexer, *restore.FakeRestoreManager) {
cli := &fake.Clientset{}

restoreInformer := informers.NewSharedInformerFactory(cli, 0).Pingcap().V1alpha1().Restores()
restoreManager := restore.NewFakeRestoreManager()
control := NewDefaultRestoreControl(restoreManager)

return control, restoreInformer.Informer().GetIndexer(), restoreManager
}
Loading