github.com/ph/moby@v1.13.1/pkg/parsers/kernel/kernel_unix_test.go (about) 1 // +build !windows 2 3 package kernel 4 5 import ( 6 "fmt" 7 "testing" 8 ) 9 10 func assertParseRelease(t *testing.T, release string, b *VersionInfo, result int) { 11 var ( 12 a *VersionInfo 13 ) 14 a, _ = ParseRelease(release) 15 16 if r := CompareKernelVersion(*a, *b); r != result { 17 t.Fatalf("Unexpected kernel version comparison result for (%v,%v). Found %d, expected %d", release, b, r, result) 18 } 19 if a.Flavor != b.Flavor { 20 t.Fatalf("Unexpected parsed kernel flavor. Found %s, expected %s", a.Flavor, b.Flavor) 21 } 22 } 23 24 // TestParseRelease tests the ParseRelease() function 25 func TestParseRelease(t *testing.T) { 26 assertParseRelease(t, "3.8.0", &VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 0) 27 assertParseRelease(t, "3.4.54.longterm-1", &VersionInfo{Kernel: 3, Major: 4, Minor: 54, Flavor: ".longterm-1"}, 0) 28 assertParseRelease(t, "3.4.54.longterm-1", &VersionInfo{Kernel: 3, Major: 4, Minor: 54, Flavor: ".longterm-1"}, 0) 29 assertParseRelease(t, "3.8.0-19-generic", &VersionInfo{Kernel: 3, Major: 8, Minor: 0, Flavor: "-19-generic"}, 0) 30 assertParseRelease(t, "3.12.8tag", &VersionInfo{Kernel: 3, Major: 12, Minor: 8, Flavor: "tag"}, 0) 31 assertParseRelease(t, "3.12-1-amd64", &VersionInfo{Kernel: 3, Major: 12, Minor: 0, Flavor: "-1-amd64"}, 0) 32 assertParseRelease(t, "3.8.0", &VersionInfo{Kernel: 4, Major: 8, Minor: 0}, -1) 33 // Errors 34 invalids := []string{ 35 "3", 36 "a", 37 "a.a", 38 "a.a.a-a", 39 } 40 for _, invalid := range invalids { 41 expectedMessage := fmt.Sprintf("Can't parse kernel version %v", invalid) 42 if _, err := ParseRelease(invalid); err == nil || err.Error() != expectedMessage { 43 44 } 45 } 46 } 47 48 func assertKernelVersion(t *testing.T, a, b VersionInfo, result int) { 49 if r := CompareKernelVersion(a, b); r != result { 50 t.Fatalf("Unexpected kernel version comparison result. Found %d, expected %d", r, result) 51 } 52 } 53 54 // TestCompareKernelVersion tests the CompareKernelVersion() function 55 func TestCompareKernelVersion(t *testing.T) { 56 assertKernelVersion(t, 57 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 58 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 59 0) 60 assertKernelVersion(t, 61 VersionInfo{Kernel: 2, Major: 6, Minor: 0}, 62 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 63 -1) 64 assertKernelVersion(t, 65 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 66 VersionInfo{Kernel: 2, Major: 6, Minor: 0}, 67 1) 68 assertKernelVersion(t, 69 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 70 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 71 0) 72 assertKernelVersion(t, 73 VersionInfo{Kernel: 3, Major: 8, Minor: 5}, 74 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 75 1) 76 assertKernelVersion(t, 77 VersionInfo{Kernel: 3, Major: 0, Minor: 20}, 78 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 79 -1) 80 assertKernelVersion(t, 81 VersionInfo{Kernel: 3, Major: 7, Minor: 20}, 82 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 83 -1) 84 assertKernelVersion(t, 85 VersionInfo{Kernel: 3, Major: 8, Minor: 20}, 86 VersionInfo{Kernel: 3, Major: 7, Minor: 0}, 87 1) 88 assertKernelVersion(t, 89 VersionInfo{Kernel: 3, Major: 8, Minor: 20}, 90 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 91 1) 92 assertKernelVersion(t, 93 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 94 VersionInfo{Kernel: 3, Major: 8, Minor: 20}, 95 -1) 96 }