diff --git a/utils/utils.go b/utils/utils.go index 6a5beb8e48..441f71a126 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -515,9 +515,7 @@ func FindCgroupMountpoint(cgroupType string) (string, error) { func GetKernelVersion() (*KernelVersionInfo, error) { var ( - flavor string - kernel, major, minor int - err error + err error ) uts, err := uname() @@ -536,8 +534,18 @@ func GetKernelVersion() (*KernelVersionInfo, error) { // Remove the \x00 from the release for Atoi to parse correctly release = release[:bytes.IndexByte(release, 0)] - tmp := strings.SplitN(string(release), "-", 2) - tmp2 := strings.SplitN(tmp[0], ".", 3) + return ParseRelease(string(release)) +} + +func ParseRelease(release string) (*KernelVersionInfo, error) { + var ( + flavor string + kernel, major, minor int + err error + ) + + tmp := strings.SplitN(release, "-", 2) + tmp2 := strings.Split(tmp[0], ".") if len(tmp2) > 0 { kernel, err = strconv.Atoi(tmp2[0]) diff --git a/utils/utils_test.go b/utils/utils_test.go index 1030b2902a..20a5820dd5 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -337,3 +337,21 @@ search dotcloud.net`: true, } } } + +func assertParseRelease(t *testing.T, release string, b *KernelVersionInfo, result int) { + var ( + a *KernelVersionInfo + ) + a, _ = ParseRelease(release) + + if r := CompareKernelVersion(a, b); r != result { + t.Fatalf("Unexpected kernel version comparison result. Found %d, expected %d", r, result) + } +} + +func TestParseRelease(t *testing.T) { + assertParseRelease(t, "3.8.0", &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0}, 0) + assertParseRelease(t, "3.4.54.longterm-1", &KernelVersionInfo{Kernel: 3, Major: 4, Minor: 54}, 0) + assertParseRelease(t, "3.4.54.longterm-1", &KernelVersionInfo{Kernel: 3, Major: 4, Minor: 54, Flavor: "1"}, 0) + assertParseRelease(t, "3.8.0-19-generic", &KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0, Flavor: "19-generic"}, 0) +}