2015-10-31 14:22:31 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2014-07-28 20:23:38 -04:00
|
|
|
package kernel
|
|
|
|
|
|
|
|
import (
|
2015-06-29 11:01:20 -04:00
|
|
|
"fmt"
|
2014-07-28 20:23:38 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
func assertParseRelease(t *testing.T, release string, b *VersionInfo, result int) {
|
2014-07-28 20:23:38 -04:00
|
|
|
var (
|
2015-07-25 04:35:07 -04:00
|
|
|
a *VersionInfo
|
2014-07-28 20:23:38 -04:00
|
|
|
)
|
|
|
|
a, _ = ParseRelease(release)
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
if r := CompareKernelVersion(*a, *b); r != result {
|
2015-06-29 11:01:20 -04:00
|
|
|
t.Fatalf("Unexpected kernel version comparison result for (%v,%v). Found %d, expected %d", release, b, r, result)
|
2014-07-28 20:23:38 -04:00
|
|
|
}
|
|
|
|
if a.Flavor != b.Flavor {
|
|
|
|
t.Fatalf("Unexpected parsed kernel flavor. Found %s, expected %s", a.Flavor, b.Flavor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-31 14:22:31 -04:00
|
|
|
// TestParseRelease tests the ParseRelease() function
|
2014-07-28 20:23:38 -04:00
|
|
|
func TestParseRelease(t *testing.T) {
|
2015-07-25 04:35:07 -04:00
|
|
|
assertParseRelease(t, "3.8.0", &VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 0)
|
|
|
|
assertParseRelease(t, "3.4.54.longterm-1", &VersionInfo{Kernel: 3, Major: 4, Minor: 54, Flavor: ".longterm-1"}, 0)
|
|
|
|
assertParseRelease(t, "3.4.54.longterm-1", &VersionInfo{Kernel: 3, Major: 4, Minor: 54, Flavor: ".longterm-1"}, 0)
|
|
|
|
assertParseRelease(t, "3.8.0-19-generic", &VersionInfo{Kernel: 3, Major: 8, Minor: 0, Flavor: "-19-generic"}, 0)
|
|
|
|
assertParseRelease(t, "3.12.8tag", &VersionInfo{Kernel: 3, Major: 12, Minor: 8, Flavor: "tag"}, 0)
|
|
|
|
assertParseRelease(t, "3.12-1-amd64", &VersionInfo{Kernel: 3, Major: 12, Minor: 0, Flavor: "-1-amd64"}, 0)
|
|
|
|
assertParseRelease(t, "3.8.0", &VersionInfo{Kernel: 4, Major: 8, Minor: 0}, -1)
|
2015-06-29 11:01:20 -04:00
|
|
|
// Errors
|
|
|
|
invalids := []string{
|
|
|
|
"3",
|
|
|
|
"a",
|
|
|
|
"a.a",
|
|
|
|
"a.a.a-a",
|
|
|
|
}
|
|
|
|
for _, invalid := range invalids {
|
|
|
|
expectedMessage := fmt.Sprintf("Can't parse kernel version %v", invalid)
|
|
|
|
if _, err := ParseRelease(invalid); err == nil || err.Error() != expectedMessage {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2014-07-28 20:23:38 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
func assertKernelVersion(t *testing.T, a, b VersionInfo, result int) {
|
2014-07-28 20:23:38 -04:00
|
|
|
if r := CompareKernelVersion(a, b); r != result {
|
|
|
|
t.Fatalf("Unexpected kernel version comparison result. Found %d, expected %d", r, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-31 14:22:31 -04:00
|
|
|
// TestCompareKernelVersion tests the CompareKernelVersion() function
|
2014-07-28 20:23:38 -04:00
|
|
|
func TestCompareKernelVersion(t *testing.T) {
|
|
|
|
assertKernelVersion(t,
|
2015-07-25 04:35:07 -04:00
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
2014-07-28 20:23:38 -04:00
|
|
|
0)
|
|
|
|
assertKernelVersion(t,
|
2015-07-25 04:35:07 -04:00
|
|
|
VersionInfo{Kernel: 2, Major: 6, Minor: 0},
|
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
2014-07-28 20:23:38 -04:00
|
|
|
-1)
|
|
|
|
assertKernelVersion(t,
|
2015-07-25 04:35:07 -04:00
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
|
|
|
VersionInfo{Kernel: 2, Major: 6, Minor: 0},
|
2014-07-28 20:23:38 -04:00
|
|
|
1)
|
|
|
|
assertKernelVersion(t,
|
2015-07-25 04:35:07 -04:00
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
2014-07-28 20:23:38 -04:00
|
|
|
0)
|
|
|
|
assertKernelVersion(t,
|
2015-07-25 04:35:07 -04:00
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 5},
|
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
2014-07-28 20:23:38 -04:00
|
|
|
1)
|
|
|
|
assertKernelVersion(t,
|
2015-07-25 04:35:07 -04:00
|
|
|
VersionInfo{Kernel: 3, Major: 0, Minor: 20},
|
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
2014-07-28 20:23:38 -04:00
|
|
|
-1)
|
2015-06-29 11:01:20 -04:00
|
|
|
assertKernelVersion(t,
|
2015-07-25 04:35:07 -04:00
|
|
|
VersionInfo{Kernel: 3, Major: 7, Minor: 20},
|
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
2015-06-29 11:01:20 -04:00
|
|
|
-1)
|
|
|
|
assertKernelVersion(t,
|
2015-07-25 04:35:07 -04:00
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 20},
|
|
|
|
VersionInfo{Kernel: 3, Major: 7, Minor: 0},
|
2015-06-29 11:01:20 -04:00
|
|
|
1)
|
|
|
|
assertKernelVersion(t,
|
2015-07-25 04:35:07 -04:00
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 20},
|
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
2015-06-29 11:01:20 -04:00
|
|
|
1)
|
|
|
|
assertKernelVersion(t,
|
2015-07-25 04:35:07 -04:00
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 0},
|
|
|
|
VersionInfo{Kernel: 3, Major: 8, Minor: 20},
|
2015-06-29 11:01:20 -04:00
|
|
|
-1)
|
2014-07-28 20:23:38 -04:00
|
|
|
}
|