From f4432d50c388c242bba6b697f31b32f1fe21ff65 Mon Sep 17 00:00:00 2001 From: Mohit Soni Date: Sat, 24 Aug 2013 00:24:40 -0700 Subject: [PATCH] Refactored code and added unit tests - Extracted ParseRelease method from GetKernelVersion to make code more testable - Added tests for ParseRelease method --- utils/utils.go | 16 ++++++++++++---- utils/utils_test.go | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index 69d300e282..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,7 +534,17 @@ 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) + 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 { 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) +}