Refactored code and added unit tests

- Extracted ParseRelease method from GetKernelVersion to make code
  more testable
- Added tests for ParseRelease method
This commit is contained in:
Mohit Soni 2013-08-24 00:24:40 -07:00
Родитель ab882da03b
Коммит f4432d50c3
2 изменённых файлов: 30 добавлений и 4 удалений

Просмотреть файл

@ -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 {

Просмотреть файл

@ -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)
}