Skip to content

Commit

Permalink
revert utils_test.go
Browse files Browse the repository at this point in the history
Signed-off-by: Tiger Kaovilai <[email protected]>
  • Loading branch information
kaovilai committed Feb 20, 2024
1 parent 0b36bb5 commit 98ee21a
Showing 1 changed file with 5 additions and 50 deletions.
55 changes: 5 additions & 50 deletions pkg/util/kube/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -526,4 +481,4 @@ func TestSinglePathMatch(t *testing.T) {
_, err := SinglePathMatch("./*/subpath", fakeFS, logrus.StandardLogger())
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "expected one matching path")
}
}

0 comments on commit 98ee21a

Please sign in to comment.