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

bug_fix: updating the parseKernelVersion() function #30699

Merged
merged 1 commit into from
Mar 5, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 21 additions & 11 deletions pkg/version/version_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,33 @@ import (

func parseKernelVersion(ver string) (semver.Version, error) {
verStrs := strings.Split(ver, ".")
switch {
case len(verStrs) < 2:

// We are assuming the kernel version will be one of the following:
// 4.9.17-040917-generic or 4.9-040917-generic or 4-generic
// So as observed, the kernel value is N.N.N-m or N.N-m or N-m
// This implies the len(verStrs) should be between 1 and 3

if len(verStrs) < 1 || len(verStrs) > 3 {
return semver.Version{}, fmt.Errorf("unable to get kernel version from %q", ver)
case len(verStrs) < 3:
verStrs = append(verStrs, "0")
}
// We are assuming the kernel version will be something as:
// 4.9.17-040917-generic

// If verStrs is []string{ "4", "9", "17-040917-generic" }
// then we need to retrieve patch number.
patch := regexp.MustCompilePOSIX(`^[0-9]+`).FindString(verStrs[2])
// Given the observations, we use regular expression to extract
// the patch number from the last element of the verStrs array and
// append "0" to the verStrs array in case the until its length is
// 3 as in all cases we want to return from this function :
// Major.Minor.PatchNumber

patch := regexp.MustCompilePOSIX(`^[0-9]+`).FindString(verStrs[len(verStrs)-1])
if patch == "" {
verStrs[2] = "0"
verStrs[len(verStrs)-1] = "0"
} else {
verStrs[2] = patch
verStrs[len(verStrs)-1] = patch
}

for len(verStrs) < 3 {
verStrs = append(verStrs, "0")
}

return versioncheck.Version(strings.Join(verStrs[:3], "."))
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/version/version_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func (vs *VersionSuite) TestParseKernelVersion(c *C) {
{"4.14.0-rc7+", mustHaveVersion("4.14.0")},
{"4.9.17-040917-generic", mustHaveVersion("4.9.17")},
{"4.9.generic", mustHaveVersion("4.9.0")},
{"6.5.0-15-generic", mustHaveVersion("6.5.0")},
{"6.7-amd64", mustHaveVersion("6.7.0")},
{"6.5-15-generic", mustHaveVersion("6.5.0")},
{"6.5.2-rc8+", mustHaveVersion("6.5.2")},
{"6-generic", mustHaveVersion("6.0.0")},
MeherRushi marked this conversation as resolved.
Show resolved Hide resolved
}
for _, tt := range flagtests {
s, err := parseKernelVersion(tt.in)
Expand Down