github.com/damirazo/docker@v1.9.0/pkg/parsers/kernel/kernel_test.go (about)

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