github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/parsers/kernel/kernel_unix_test.go (about) 1 // +build !windows 2 3 package kernel // import "github.com/demonoid81/moby/pkg/parsers/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.10.0-862.2.3.el7.x86_64", &VersionInfo{Kernel: 3, Major: 10, Minor: 0, Flavor: "-862.2.3.el7.x86_64"}, 0) 31 assertParseRelease(t, "3.12.8tag", &VersionInfo{Kernel: 3, Major: 12, Minor: 8, Flavor: "tag"}, 0) 32 assertParseRelease(t, "3.12-1-amd64", &VersionInfo{Kernel: 3, Major: 12, Minor: 0, Flavor: "-1-amd64"}, 0) 33 assertParseRelease(t, "3.8.0", &VersionInfo{Kernel: 4, Major: 8, Minor: 0}, -1) 34 // Errors 35 invalids := []string{ 36 "3", 37 "a", 38 "a.a", 39 "a.a.a-a", 40 } 41 for _, invalid := range invalids { 42 expectedMessage := fmt.Sprintf("Can't parse kernel version %v", invalid) 43 if _, err := ParseRelease(invalid); err == nil || err.Error() != expectedMessage { 44 if err == nil { 45 t.Fatalf("Expected %q, got nil", expectedMessage) 46 } 47 t.Fatalf("Expected %q, got %q", expectedMessage, err.Error()) 48 } 49 } 50 } 51 52 func assertKernelVersion(t *testing.T, a, b VersionInfo, result int) { 53 if r := CompareKernelVersion(a, b); r != result { 54 t.Fatalf("Unexpected kernel version comparison result. Found %d, expected %d", r, result) 55 } 56 } 57 58 // TestCompareKernelVersion tests the CompareKernelVersion() function 59 func TestCompareKernelVersion(t *testing.T) { 60 assertKernelVersion(t, 61 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 62 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 63 0) 64 assertKernelVersion(t, 65 VersionInfo{Kernel: 2, Major: 6, Minor: 0}, 66 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 67 -1) 68 assertKernelVersion(t, 69 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 70 VersionInfo{Kernel: 2, Major: 6, Minor: 0}, 71 1) 72 assertKernelVersion(t, 73 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 74 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 75 0) 76 assertKernelVersion(t, 77 VersionInfo{Kernel: 3, Major: 8, Minor: 5}, 78 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 79 1) 80 assertKernelVersion(t, 81 VersionInfo{Kernel: 3, Major: 0, Minor: 20}, 82 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 83 -1) 84 assertKernelVersion(t, 85 VersionInfo{Kernel: 3, Major: 7, Minor: 20}, 86 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 87 -1) 88 assertKernelVersion(t, 89 VersionInfo{Kernel: 3, Major: 8, Minor: 20}, 90 VersionInfo{Kernel: 3, Major: 7, Minor: 0}, 91 1) 92 assertKernelVersion(t, 93 VersionInfo{Kernel: 3, Major: 8, Minor: 20}, 94 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 95 1) 96 assertKernelVersion(t, 97 VersionInfo{Kernel: 3, Major: 8, Minor: 0}, 98 VersionInfo{Kernel: 3, Major: 8, Minor: 20}, 99 -1) 100 }