-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix invalid prerelease characters when parsing kernel version (#2449)
Check kernel version splitted array length and intercept the first 3 elements for parsing kernel version. Signed-off-by: Zhengdong Wu <zhengdong.wu@transwarp.io>
- Loading branch information
Wu zhengdong
committed
Jul 27, 2021
1 parent
9adf9c9
commit 6e34ef4
Showing
2 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2021 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package runtime | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/blang/semver" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseKernelVersion(t *testing.T) { | ||
testcases := []struct { | ||
// input | ||
kernelString string | ||
// expectations | ||
expectedKernelVersion semver.Version | ||
expectedError error | ||
}{ | ||
{ | ||
kernelString: "4.10", | ||
expectedKernelVersion: semver.MustParse("4.10.0"), | ||
expectedError: nil, | ||
}, | ||
{ | ||
kernelString: "4.10.3", | ||
expectedKernelVersion: semver.MustParse("4.10.3"), | ||
expectedError: nil, | ||
}, | ||
{ | ||
kernelString: "4.12.17-040917-generic", | ||
expectedKernelVersion: semver.MustParse("4.12.17-040917-generic"), | ||
expectedError: nil, | ||
}, | ||
{ | ||
kernelString: "4.13.18-300.el7.x86_64", | ||
expectedKernelVersion: semver.MustParse("4.13.18-300"), | ||
expectedError: nil, | ||
}, | ||
{ | ||
kernelString: "5", | ||
expectedKernelVersion: semver.Version{}, | ||
expectedError: fmt.Errorf("unable to get kernel version from \"5\""), | ||
}, | ||
} | ||
for _, tc := range testcases { | ||
parsedKernelVersion, err := parseKernelVersionStr(tc.kernelString) | ||
if tc.expectedError != nil { | ||
assert.Equal(t, tc.expectedError, err) | ||
} else { | ||
require.Nil(t, err) | ||
assert.Equal(t, tc.expectedKernelVersion, parsedKernelVersion) | ||
} | ||
|
||
} | ||
} |