Skip to content

Commit

Permalink
helpers: handle non numeric kernel versions (#345)
Browse files Browse the repository at this point in the history
Some kernels report their version with non numeric characters such as +.
Handle these cases in kernel version comparisons.
  • Loading branch information
NDStrahilevitz authored Aug 1, 2023
1 parent d217980 commit b4eee59
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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

0 comments on commit b4eee59

Please sign in to comment.