From a600e31c5567a90e560c4895707a20ee8ed6f2c8 Mon Sep 17 00:00:00 2001 From: haoyun Date: Tue, 19 Oct 2021 11:01:01 +0800 Subject: [PATCH] test: add test for PatchNode when error happend Signed-off-by: haoyun --- .../app/util/apiclient/idempotency_test.go | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/cmd/kubeadm/app/util/apiclient/idempotency_test.go b/cmd/kubeadm/app/util/apiclient/idempotency_test.go index 35976333bd9..8dc14a83938 100644 --- a/cmd/kubeadm/app/util/apiclient/idempotency_test.go +++ b/cmd/kubeadm/app/util/apiclient/idempotency_test.go @@ -26,18 +26,20 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/kubernetes/fake" core "k8s.io/client-go/testing" ) const configMapName = "configmap" -func TestPatchNodeNonErrorCases(t *testing.T) { +func TestPatchNode(t *testing.T) { testcases := []struct { name string lookupName string node v1.Node success bool + fakeError error }{ { name: "simple update", @@ -65,6 +67,30 @@ func TestPatchNodeNonErrorCases(t *testing.T) { }, success: false, }, + { + name: "patch node when timeout", + lookupName: "testnode", + node: v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "testnode", + Labels: map[string]string{v1.LabelHostname: ""}, + }, + }, + success: false, + fakeError: apierrors.NewTimeoutError("fake timeout", -1), + }, + { + name: "patch node when conflict", + lookupName: "testnode", + node: v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "testnode", + Labels: map[string]string{v1.LabelHostname: ""}, + }, + }, + success: false, + fakeError: apierrors.NewConflict(schema.GroupResource{}, "fake conflict", nil), + }, } for _, tc := range testcases { @@ -74,6 +100,11 @@ func TestPatchNodeNonErrorCases(t *testing.T) { if err != nil { t.Fatalf("failed to create node to fake client: %v", err) } + if tc.fakeError != nil { + client.PrependReactor("patch", "nodes", func(action core.Action) (handled bool, ret runtime.Object, err error) { + return true, nil, tc.fakeError + }) + } var lastError error conditionFunction := PatchNodeOnce(client, tc.lookupName, func(node *v1.Node) { node.Annotations = map[string]string{