Skip to content

Commit

Permalink
Merge pull request #1896 from p0lyn0mial/simplify_min_kubelet_ver_test
Browse files Browse the repository at this point in the history
NO-JIRA: node/minimim_kubelet_version_test: simplify TestValidateConfigNodeForMinimumKubeletVersion
  • Loading branch information
openshift-merge-bot[bot] authored Nov 26, 2024
2 parents 543f84d + 45c3480 commit 3c59352
Showing 1 changed file with 39 additions and 47 deletions.
86 changes: 39 additions & 47 deletions pkg/apiserver/node/minimum_kubelet_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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")
}
Expand Down

0 comments on commit 3c59352

Please sign in to comment.