Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helpers: handle non numeric kernel versions #345

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions helpers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"
"syscall"
"unicode"
)

func checkEnvPath(env string) (string, error) {
Expand Down Expand Up @@ -88,6 +89,7 @@ const (
// 4.18.0-305.7.1.el8_4.centos.x86_64 (centos)
// 4.18.0-305.7.1.el8_4.centos.plus.x86_64 (centos + plus repo)
// 5.13.13-arch1-1 (archlinux)
// 5.4.228+ (ubuntu-gke 5.4)
func CompareKernelRelease(base, given string) (KernelVersionComparison, error) {
b := strings.Split(base, "-") // [base]-xxx
b = strings.Split(b[0], ".") // [major][minor][patch]
Expand All @@ -108,11 +110,11 @@ func CompareKernelRelease(base, given string) (KernelVersionComparison, error) {
}

for n := 0; n <= 2; n++ {
givenValue, err := strconv.Atoi(g[n])
givenValue, err := strconv.Atoi(cleanVersionNumber(g[n]))
if err != nil {
return KernelVersionInvalid, fmt.Errorf("invalid given kernel version value: %s issue with: %s", given, g[n])
}
baseValue, err := strconv.Atoi(b[n])
baseValue, err := strconv.Atoi(cleanVersionNumber(b[n]))
if err != nil {
return KernelVersionInvalid, fmt.Errorf("invalid base kernel version value: %s issue with: %s", base, b[n])
}
Expand All @@ -127,3 +129,12 @@ func CompareKernelRelease(base, given string) (KernelVersionComparison, error) {
}
return KernelVersionEqual, nil
}

func cleanVersionNumber(number string) string {
return strings.Map(func(r rune) rune {
if unicode.IsDigit(r) {
return r
}
return -1
}, number)
}
12 changes: 12 additions & 0 deletions helpers/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ func TestCompareKernelRelease(t *testing.T) {
expectedComparison: KernelVersionOlder,
expectedError: nil,
},
{
testName: "valid with +, older than",
base: "5.4.228+",
given: "4.18",
expectedComparison: KernelVersionOlder,
},
{
testName: "valid with alphabet, older than",
base: "5.6b",
given: "4.18",
expectedComparison: KernelVersionOlder,
},
{
testName: "equal",
base: "5.0",
Expand Down
Loading