Skip to content

Commit

Permalink
mock patch to fail failure due to connection refused
Browse files Browse the repository at this point in the history
Signed-off-by: Tiger Kaovilai <tkaovila@redhat.com>
  • Loading branch information
kaovilai committed Jun 24, 2024
1 parent 7f54e8a commit 4e0a810
Show file tree
Hide file tree
Showing 4 changed files with 329 additions and 8 deletions.
28 changes: 24 additions & 4 deletions pkg/controller/backup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"reflect"
"sort"
"strings"
"syscall"
"testing"
"time"

Expand All @@ -43,6 +44,7 @@ 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"
Expand All @@ -63,6 +65,7 @@ import (
pluginmocks "github.com/vmware-tanzu/velero/pkg/plugin/mocks"
biav2 "github.com/vmware-tanzu/velero/pkg/plugin/velero/backupitemaction/v2"
velerotest "github.com/vmware-tanzu/velero/pkg/test"
velerotestmocks "github.com/vmware-tanzu/velero/pkg/test/mocks"
"github.com/vmware-tanzu/velero/pkg/util/boolptr"
"github.com/vmware-tanzu/velero/pkg/util/logging"
)
Expand Down Expand Up @@ -153,23 +156,28 @@ func TestProcessBackupNonProcessedItems(t *testing.T) {
func TestProcessBackupInProgressFailOnSecondReconcile(t *testing.T) {
tests := []struct {
name string
key string
tracked bool
reconciledPhase velerov1api.BackupPhase
expectedErr error
mockFailedPatch bool
}{
{
name: "InProgress backup tracked as being in-progress is not processed",
key: "velero/backup-1",
tracked: true,
reconciledPhase: velerov1api.BackupPhaseInProgress,
},
{
name: "InProgress backup untracked as being in-progress is marked as failed",
key: "velero/backup-1",
tracked: false,
reconciledPhase: velerov1api.BackupPhaseFailed,
},
{
name: "InProgress backup untracked is marked as failed, if patch fails, err is returned from reconcile to retry patch again, backup still inprogress",
tracked: false,
reconciledPhase: velerov1api.BackupPhaseInProgress,
mockFailedPatch: true,
expectedErr: syscall.ECONNREFUSED,
},
}

for _, test := range tests {
Expand All @@ -179,8 +187,20 @@ func TestProcessBackupInProgressFailOnSecondReconcile(t *testing.T) {
logger = logging.DefaultLogger(logrus.DebugLevel, formatFlag)
)
backup := defaultBackup().Phase(velerov1api.BackupPhaseInProgress).Result()
var kclient kbclient.Client
fakeKclient := velerotest.NewFakeControllerRuntimeClient(t, backup)
if test.mockFailedPatch {
mockClient := velerotestmocks.NewClient(t)
mockClient.On("Patch", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(syscall.ECONNREFUSED).Once()
mockClient.On("Get", mock.Anything, mock.AnythingOfType("types.NamespacedName"), mock.AnythingOfType("*v1.Backup")).Return(func(ctx context.Context, key client.ObjectKey, obj client.Object) error {
return fakeKclient.Get(ctx, key, obj)
})
kclient = mockClient
} else {
kclient = fakeKclient
}
c := &backupReconciler{
kbClient: velerotest.NewFakeControllerRuntimeClient(t, backup),
kbClient: kclient,
formatFlag: formatFlag,
logger: logger,
backupTracker: NewBackupTracker(),
Expand Down
28 changes: 24 additions & 4 deletions pkg/controller/restore_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"context"
"io"
"syscall"
"testing"
"time"

Expand All @@ -34,6 +35,7 @@ import (
clocktesting "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"

corev1 "k8s.io/api/core/v1"

Expand All @@ -48,6 +50,7 @@ import (
riav2 "github.com/vmware-tanzu/velero/pkg/plugin/velero/restoreitemaction/v2"
pkgrestore "github.com/vmware-tanzu/velero/pkg/restore"
velerotest "github.com/vmware-tanzu/velero/pkg/test"
velerotestmocks "github.com/vmware-tanzu/velero/pkg/test/mocks"
"github.com/vmware-tanzu/velero/pkg/util/logging"
"github.com/vmware-tanzu/velero/pkg/util/results"
"github.com/vmware-tanzu/velero/pkg/volume"
Expand Down Expand Up @@ -926,23 +929,28 @@ func TestMostRecentCompletedBackup(t *testing.T) {
func TestProcessRestoreInProgressFailOnSecondReconcile(t *testing.T) {
tests := []struct {
name string
key string
trackedAsFailed bool
reconciledPhase velerov1api.RestorePhase
expectedErr error
mockFailedPatch bool
}{
{
name: "InProgress restore not tracked as failing is not processed",
key: "velero/restore-1",
trackedAsFailed: false,
reconciledPhase: velerov1api.RestorePhaseInProgress,
},
{
name: "InProgress restore tracked as failing is marked as failed",
key: "velero/restore-1",
trackedAsFailed: true,
reconciledPhase: velerov1api.RestorePhaseFailed,
},
{
name: "InProgress restore tracked as failing is marked as failed, if patch fails, err is returned from reconcile to retry patch again, restore still inprogress",
trackedAsFailed: true,
reconciledPhase: velerov1api.RestorePhaseInProgress,
mockFailedPatch: true,
expectedErr: syscall.ECONNREFUSED,
},
}

for _, test := range tests {
Expand All @@ -960,8 +968,20 @@ func TestProcessRestoreInProgressFailOnSecondReconcile(t *testing.T) {
Phase: velerov1api.RestorePhaseInProgress,
},
}
var kclient kbclient.Client
fakeKclient := velerotest.NewFakeControllerRuntimeClient(t, restore)
if test.mockFailedPatch {
mockClient := velerotestmocks.NewClient(t)
mockClient.On("Patch", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(syscall.ECONNREFUSED).Once()
mockClient.On("Get", mock.Anything, mock.AnythingOfType("types.NamespacedName"), mock.AnythingOfType("*v1.Restore")).Return(func(ctx context.Context, key client.ObjectKey, obj client.Object) error {
return fakeKclient.Get(ctx, key, obj)
})
kclient = mockClient
} else {
kclient = fakeKclient
}
c := &restoreReconciler{
kbClient: velerotest.NewFakeControllerRuntimeClient(t, restore),
kbClient: kclient,
logger: logger,
failingTracker: NewRestoreTracker(),
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/test/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package test
import (
snapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
snapshotv1listers "github.com/kubernetes-csi/external-snapshotter/client/v4/listers/volumesnapshot/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// VolumeSnapshotLister helps list VolumeSnapshots.
Expand All @@ -18,3 +21,17 @@ type VolumeSnapshotLister interface {
VolumeSnapshots(namespace string) snapshotv1listers.VolumeSnapshotNamespaceLister
snapshotv1listers.VolumeSnapshotListerExpansion
}

// Writer knows how to create, delete, and update Kubernetes objects.

//go:generate mockery --name Client
type Client interface {
client.Reader
client.Writer
client.StatusClient

// Scheme returns the scheme this client is using.
Scheme() *runtime.Scheme
// RESTMapper returns the rest this client is using.
RESTMapper() meta.RESTMapper
}
Loading

0 comments on commit 4e0a810

Please sign in to comment.