From 45c3480ef40769a4a4affb85c0518601c71b35eb Mon Sep 17 00:00:00 2001 From: Lukasz Szaszkiewicz Date: Tue, 26 Nov 2024 09:49:07 +0100 Subject: [PATCH] node/minimim_kubelet_version_test: simplify TestValidateConfigNodeForMinimumKubeletVersion --- .../node/minimum_kubelet_version_test.go | 86 +++++++++---------- 1 file changed, 39 insertions(+), 47 deletions(-) diff --git a/pkg/apiserver/node/minimum_kubelet_version_test.go b/pkg/apiserver/node/minimum_kubelet_version_test.go index 09b14fb5bd..42d06f4ad2 100644 --- a/pkg/apiserver/node/minimum_kubelet_version_test.go +++ b/pkg/apiserver/node/minimum_kubelet_version_test.go @@ -13,24 +13,20 @@ import ( func TestValidateConfigNodeForMinimumKubeletVersion(t *testing.T) { testCases := []struct { - name string - version string - shouldReject bool - tooOld bool - nodes []*v1.Node - nodeListErr error - errMsg string + name string + version string + nodes []*v1.Node + nodeListErr error + expectedErr error }{ // no rejections { - name: "should not reject when minimum kubelet version is empty", - version: "", - shouldReject: false, + name: "should not reject when minimum kubelet version is empty", + version: "", }, { - name: "should reject when min kubelet version bogus", - version: "bogus", - shouldReject: true, + name: "should reject when min kubelet version bogus", + version: "bogus", nodes: []*v1.Node{ { Status: v1.NodeStatus{ @@ -40,12 +36,11 @@ func TestValidateConfigNodeForMinimumKubeletVersion(t *testing.T) { }, }, }, - errMsg: "failed to parse submitted version bogus No Major.Minor.Patch elements found", + expectedErr: errors.New("failed to parse submitted version bogus No Major.Minor.Patch elements found"), }, { - name: "should reject when kubelet version is bogus", - version: "1.30.0", - shouldReject: true, + name: "should reject when kubelet version is bogus", + version: "1.30.0", nodes: []*v1.Node{ { ObjectMeta: metav1.ObjectMeta{ @@ -58,12 +53,11 @@ func TestValidateConfigNodeForMinimumKubeletVersion(t *testing.T) { }, }, }, - errMsg: "failed to parse node version bogus: No Major.Minor.Patch elements found", + expectedErr: errors.New("failed to parse node version bogus: No Major.Minor.Patch elements found"), }, { - name: "should reject when kubelet version is too old", - version: "1.30.0", - shouldReject: true, + name: "should reject when kubelet version is too old", + version: "1.30.0", nodes: []*v1.Node{ { ObjectMeta: metav1.ObjectMeta{ @@ -76,12 +70,11 @@ func TestValidateConfigNodeForMinimumKubeletVersion(t *testing.T) { }, }, }, - errMsg: "kubelet version is outdated: kubelet version is 1.29.0, which is lower than minimumKubeletVersion of 1.30.0", + expectedErr: errors.New("kubelet version is outdated: kubelet version is 1.29.0, which is lower than minimumKubeletVersion of 1.30.0"), }, { - name: "should reject when one kubelet version is too old", - version: "1.30.0", - shouldReject: true, + name: "should reject when one kubelet version is too old", + version: "1.30.0", nodes: []*v1.Node{ { ObjectMeta: metav1.ObjectMeta{ @@ -104,12 +97,11 @@ func TestValidateConfigNodeForMinimumKubeletVersion(t *testing.T) { }, }, }, - errMsg: "kubelet version is outdated: kubelet version is 1.29.0, which is lower than minimumKubeletVersion of 1.30.0", + expectedErr: errors.New("kubelet version is outdated: kubelet version is 1.29.0, which is lower than minimumKubeletVersion of 1.30.0"), }, { - name: "should not reject when kubelet version is equal", - version: "1.30.0", - shouldReject: false, + name: "should not reject when kubelet version is equal", + version: "1.30.0", nodes: []*v1.Node{ { ObjectMeta: metav1.ObjectMeta{ @@ -124,9 +116,8 @@ func TestValidateConfigNodeForMinimumKubeletVersion(t *testing.T) { }, }, { - name: "should reject when min version incomplete", - version: "1.30", - shouldReject: true, + name: "should reject when min version incomplete", + version: "1.30", nodes: []*v1.Node{ { ObjectMeta: metav1.ObjectMeta{ @@ -139,12 +130,11 @@ func TestValidateConfigNodeForMinimumKubeletVersion(t *testing.T) { }, }, }, - errMsg: "failed to parse submitted version 1.30 No Major.Minor.Patch elements found", + expectedErr: errors.New("failed to parse submitted version 1.30 No Major.Minor.Patch elements found"), }, { - name: "should reject when kubelet version incomplete", - version: "1.30.0", - shouldReject: true, + name: "should reject when kubelet version incomplete", + version: "1.30.0", nodes: []*v1.Node{ { ObjectMeta: metav1.ObjectMeta{ @@ -157,12 +147,11 @@ func TestValidateConfigNodeForMinimumKubeletVersion(t *testing.T) { }, }, }, - errMsg: "failed to parse node version 1.30: No Major.Minor.Patch elements found", + expectedErr: errors.New("failed to parse node version 1.30: No Major.Minor.Patch elements found"), }, { - name: "should not reject when kubelet version is new enough", - version: "1.30.0", - shouldReject: false, + name: "should not reject when kubelet version is new enough", + version: "1.30.0", nodes: []*v1.Node{ { ObjectMeta: metav1.ObjectMeta{ @@ -179,16 +168,19 @@ func TestValidateConfigNodeForMinimumKubeletVersion(t *testing.T) { } for _, testCase := range testCases { - shouldStr := "should not be" - if testCase.shouldReject { - shouldStr = "should be" - } t.Run(testCase.name, func(t *testing.T) { err := ValidateMinimumKubeletVersion(testCase.nodes, testCase.version) - assert.Equal(t, testCase.shouldReject, err != nil, "minimum kubelet version %q %s rejected", testCase.version, shouldStr) - if testCase.shouldReject { - assert.Contains(t, err.Error(), testCase.errMsg, "error message should contain %q", testCase.errMsg) + if err == nil && testCase.expectedErr != nil { + t.Fatal("expected to get an error") + } + if err != nil && testCase.expectedErr == nil { + t.Fatal(err) // unexpected error + } + if err != nil && testCase.expectedErr != nil { + if err.Error() != testCase.expectedErr.Error() { + t.Fatalf("unexpected error = %v, expected = %v", err, testCase.expectedErr) + } if strings.Contains(err.Error(), ErrKubeletOutdated.Error()) { assert.True(t, errors.Is(err, ErrKubeletOutdated), "error message should be ErrKubeletOutdated") }