From ad879f3767ba6d716e792774b690993794d3ea53 Mon Sep 17 00:00:00 2001 From: Tiger Kaovilai Date: Tue, 20 Feb 2024 01:20:17 -0500 Subject: [PATCH] revert utils_test.go Signed-off-by: Tiger Kaovilai --- pkg/util/kube/utils_test.go | 53 +++---------------------------------- 1 file changed, 4 insertions(+), 49 deletions(-) diff --git a/pkg/util/kube/utils_test.go b/pkg/util/kube/utils_test.go index a423aa60d9..31110dc8c0 100644 --- a/pkg/util/kube/utils_test.go +++ b/pkg/util/kube/utils_test.go @@ -36,8 +36,6 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "sigs.k8s.io/controller-runtime/pkg/client/fake" - kfake "k8s.io/client-go/kubernetes/fake" - "github.com/vmware-tanzu/velero/pkg/builder" velerotest "github.com/vmware-tanzu/velero/pkg/test" "github.com/vmware-tanzu/velero/pkg/uploader" @@ -84,14 +82,6 @@ func TestEnsureNamespaceExistsAndIsReady(t *testing.T) { expectedResult: true, expectedCreatedResult: true, }, - { - name: "namespace not found, namespace from backup contained deletionTimestamp, successfully created", - expectCreate: true, - expectedResult: true, - expectedCreatedResult: true, - nsPhase: corev1.NamespaceTerminating, - nsDeleting: true, - }, { name: "namespace not found initially, create returns already exists error, returned namespace is ready", alreadyExists: true, @@ -125,56 +115,21 @@ func TestEnsureNamespaceExistsAndIsReady(t *testing.T) { timeout := time.Millisecond - var fakeClientObjs []runtime.Object - if test.expectNSFound || test.alreadyExists { - fakeClientObjs = append(fakeClientObjs, namespace) - } - clientSet := kfake.NewSimpleClientset(fakeClientObjs...) - // using deepcopy to avoid modifying the original namespace object for the mock client tests below - firstReady, firstNsCreated, _ := EnsureNamespaceExistsAndIsReady(namespace.DeepCopy(), clientSet.CoreV1().Namespaces(), timeout) - assert.Equal(t, test.expectedResult, firstReady) - assert.Equal(t, test.expectedCreatedResult, firstNsCreated) - // EnsureNamespaceExistsAndIsReady should not timeout when called the second time on the same namespace. - // This second call test ensures that the first call did not create a terminating namespace. - secondReady, secondNsCreated, err := EnsureNamespaceExistsAndIsReady(namespace.DeepCopy(), clientSet.CoreV1().Namespaces(), timeout) - if !test.nsDeleting && !(test.nsPhase == corev1.NamespaceTerminating) { - // assert that namespace is ready after second call - assert.Equal(t, true, secondReady) - // assert that no error such as "namespace is terminating" is returned - assert.Nil(t, err) - } else if test.alreadyExists || test.expectNSFound { - // if namespace is already existing and is in terminating phase, EnsureNamespaceExistsAndIsReady should return timeout error - assert.NotNil(t, err) - assert.Contains(t, err.Error(), "timed out waiting for terminating namespace") - } - // assert that namespace is not created second time - assert.Equal(t, false, secondNsCreated) - - // This section tests that Get Not Found and Create Already Exists errors are handled correctly nsClient := &velerotest.FakeNamespaceClient{} defer nsClient.AssertExpectations(t) + if test.expectNSFound { nsClient.On("Get", "test", metav1.GetOptions{}).Return(namespace, nil) } else { nsClient.On("Get", "test", metav1.GetOptions{}).Return(&corev1.Namespace{}, k8serrors.NewNotFound(schema.GroupResource{Resource: "namespaces"}, "test")) } - var namespaceToCreate *corev1.Namespace - if test.nsDeleting || test.nsPhase == corev1.NamespaceTerminating { - namespaceToCreate = namespace.DeepCopy() - // scrub deletionTimestamp and Phase if any, which could be coming from backup tarball - namespaceToCreate.SetDeletionTimestamp(nil) - namespaceToCreate.Status.Phase = "" - } else { - namespaceToCreate = namespace - } + if test.alreadyExists { - // alreadyExists try to create namespace without deletionTimestamp and Phase - // and return already exists error with the original namespace which can contain deletionTimestamp and Phase - nsClient.On("Create", namespaceToCreate).Return(namespace, k8serrors.NewAlreadyExists(schema.GroupResource{Resource: "namespaces"}, "test")) + nsClient.On("Create", namespace).Return(namespace, k8serrors.NewAlreadyExists(schema.GroupResource{Resource: "namespaces"}, "test")) } if test.expectCreate { - nsClient.On("Create", namespaceToCreate).Return(namespaceToCreate, nil) + nsClient.On("Create", namespace).Return(namespace, nil) } result, nsCreated, _ := EnsureNamespaceExistsAndIsReady(namespace, nsClient, timeout)