Skip to content

Commit

Permalink
fixed unittest
Browse files Browse the repository at this point in the history
Signed-off-by: sangitaray2021 <[email protected]>
  • Loading branch information
sangitaray2021 committed Sep 19, 2024
1 parent f85838c commit 2717962
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
1 change: 1 addition & 0 deletions pkg/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func (kr *kubernetesRestorer) RestoreWithResolvers(
backupVolumeInfoMap: req.BackupVolumeInfoMap,
restoreVolumeInfoTracker: req.RestoreVolumeInfoTracker,
hooksWaitExecutor: hooksWaitExecutor,
namespaceDeletionStatusTracker: kr.namespaceDeletionStatusTracker,
}

return restoreCtx.execute()
Expand Down
34 changes: 20 additions & 14 deletions pkg/restore/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
uploadermocks "github.com/vmware-tanzu/velero/pkg/podvolume/mocks"
"github.com/vmware-tanzu/velero/pkg/test"
"github.com/vmware-tanzu/velero/pkg/types"
"github.com/vmware-tanzu/velero/pkg/util/kube"
kubeutil "github.com/vmware-tanzu/velero/pkg/util/kube"
. "github.com/vmware-tanzu/velero/pkg/util/results"
)
Expand Down Expand Up @@ -2292,10 +2293,11 @@ func TestShouldRestore(t *testing.T) {
h := newHarness(t)

ctx := &restoreContext{
log: h.log,
dynamicFactory: client.NewDynamicFactory(h.DynamicClient),
namespaceClient: h.KubeClient.CoreV1().Namespaces(),
resourceTerminatingTimeout: time.Millisecond,
log: h.log,
dynamicFactory: client.NewDynamicFactory(h.DynamicClient),
namespaceClient: h.KubeClient.CoreV1().Namespaces(),
resourceTerminatingTimeout: time.Millisecond,
namespaceDeletionStatusTracker: kube.NewNamespaceDeletionStatusTracker(),
}

for _, resource := range tc.apiResources {
Expand Down Expand Up @@ -3710,9 +3712,10 @@ func newHarness(t *testing.T) *harness {
fileSystem: test.NewFakeFileSystem(),

// unsupported
podVolumeRestorerFactory: nil,
podVolumeTimeout: 0,
kbClient: kbClient,
podVolumeRestorerFactory: nil,
podVolumeTimeout: 0,
kbClient: kbClient,
namespaceDeletionStatusTracker: kube.NewNamespaceDeletionStatusTracker(),
},
log: log,
}
Expand Down Expand Up @@ -3899,9 +3902,10 @@ func TestIsAlreadyExistsError(t *testing.T) {
h := newHarness(t)

ctx := &restoreContext{
log: h.log,
dynamicFactory: client.NewDynamicFactory(h.DynamicClient),
namespaceClient: h.KubeClient.CoreV1().Namespaces(),
log: h.log,
dynamicFactory: client.NewDynamicFactory(h.DynamicClient),
namespaceClient: h.KubeClient.CoreV1().Namespaces(),
namespaceDeletionStatusTracker: kube.NewNamespaceDeletionStatusTracker(),
}

if test.apiResource != nil {
Expand Down Expand Up @@ -4018,7 +4022,8 @@ func TestHasCSIVolumeSnapshot(t *testing.T) {
h := newHarness(t)

ctx := &restoreContext{
log: h.log,
log: h.log,
namespaceDeletionStatusTracker: kube.NewNamespaceDeletionStatusTracker(),
}

if tc.vs != nil {
Expand Down Expand Up @@ -4118,9 +4123,10 @@ func TestHasSnapshotDataUpload(t *testing.T) {
h := newHarness(t)

ctx := &restoreContext{
log: h.log,
kbClient: h.restorer.kbClient,
restore: tc.restore,
log: h.log,
kbClient: h.restorer.kbClient,
restore: tc.restore,
namespaceDeletionStatusTracker: kube.NewNamespaceDeletionStatusTracker(),
}

if tc.duResult != nil {
Expand Down
20 changes: 11 additions & 9 deletions pkg/util/kube/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ func TestNamespaceAndName(t *testing.T) {

func TestEnsureNamespaceExistsAndIsReady(t *testing.T) {
tests := []struct {
name string
expectNSFound bool
nsPhase corev1.NamespacePhase
nsDeleting bool
expectCreate bool
alreadyExists bool
expectedResult bool
expectedCreatedResult bool
name string
expectNSFound bool
nsPhase corev1.NamespacePhase
nsDeleting bool
expectCreate bool
alreadyExists bool
expectedResult bool
expectedCreatedResult bool
namespaceDeletionStatusTracker NamespaceDeletionStatusTracker
}{
{
name: "namespace found, not deleting",
Expand Down Expand Up @@ -97,6 +98,7 @@ func TestEnsureNamespaceExistsAndIsReady(t *testing.T) {
},
}

namespaceDeletionStatusTracker := NewNamespaceDeletionStatusTracker()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
namespace := &corev1.Namespace{
Expand Down Expand Up @@ -132,7 +134,7 @@ func TestEnsureNamespaceExistsAndIsReady(t *testing.T) {
nsClient.On("Create", namespace).Return(namespace, nil)
}

result, nsCreated, _ := EnsureNamespaceExistsAndIsReady(namespace, nsClient, timeout)
result, nsCreated, _ := EnsureNamespaceExistsAndIsReady(namespace, nsClient, timeout, namespaceDeletionStatusTracker)

assert.Equal(t, test.expectedResult, result)
assert.Equal(t, test.expectedCreatedResult, nsCreated)
Expand Down

0 comments on commit 2717962

Please sign in to comment.