github.com/minio/madmin-go/v3@v3.0.51/kernel/kernel_test.go (about)

     1  //
     2  // Copyright (c) 2015-2022 MinIO, Inc.
     3  //
     4  // This file is part of MinIO Object Storage stack
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU Affero General Public License as
     8  // published by the Free Software Foundation, either version 3 of the
     9  // License, or (at your option) any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU Affero General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU Affero General Public License
    17  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    18  //
    19  
    20  //go:build linux
    21  // +build linux
    22  
    23  package kernel
    24  
    25  import "testing"
    26  
    27  var testData = []struct {
    28  	success       bool
    29  	releaseString string
    30  	kernelVersion uint32
    31  }{
    32  	{true, "4.1.2-3", 262402},
    33  	{true, "4.8.14-200.fc24.x86_64", 264206},
    34  	{true, "4.1.2-3foo", 262402},
    35  	{true, "4.1.2foo-1", 262402},
    36  	{true, "4.1.2-rkt-v1", 262402},
    37  	{true, "4.1.2rkt-v1", 262402},
    38  	{true, "4.1.2-3 foo", 262402},
    39  	{true, "3.10.0-1062.el7.x86_64", 199168},
    40  	{true, "3.0.0", 196608},
    41  	{true, "2.6.32", 132640},
    42  	{true, "5.13.0-30-generic", 331008},
    43  	{true, "5.10.0-1052-oem", 330240},
    44  	{false, "foo 4.1.2-3", 0},
    45  	{true, "4.1.2", 262402},
    46  	{false, ".4.1.2", 0},
    47  	{false, "4.1.", 0},
    48  	{false, "4.1", 0},
    49  }
    50  
    51  func TestVersionFromRelease(t *testing.T) {
    52  	for _, test := range testData {
    53  		version, err := VersionFromRelease(test.releaseString)
    54  		if err != nil && test.success {
    55  			t.Errorf("expected %q to success: %s", test.releaseString, err)
    56  		} else if err == nil && !test.success {
    57  			t.Errorf("expected %q to fail", test.releaseString)
    58  		}
    59  		if version != test.kernelVersion {
    60  			t.Errorf("expected kernel version %d, got %d", test.kernelVersion, version)
    61  		}
    62  	}
    63  }
    64  
    65  func TestParseDebianVersion(t *testing.T) {
    66  	for _, tc := range []struct {
    67  		success       bool
    68  		releaseString string
    69  		kernelVersion uint32
    70  	}{
    71  		// 4.9.168
    72  		{true, "Linux version 4.9.0-9-amd64 (debian-kernel@lists.debian.org) (gcc version 6.3.0 20170516 (Debian 6.3.0-18+deb9u1) ) #1 SMP Debian 4.9.168-1+deb9u3 (2019-06-16)", 264616},
    73  		// 4.9.88
    74  		{true, "Linux ip-10-0-75-49 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1+deb9u1 (2018-05-07) x86_64 GNU/Linux", 264536},
    75  		// 3.0.4
    76  		{true, "Linux version 3.16.0-9-amd64 (debian-kernel@lists.debian.org) (gcc version 4.9.2 (Debian 4.9.2-10+deb8u2) ) #1 SMP Debian 3.16.68-1 (2019-05-22)", 200772},
    77  		// Invalid
    78  		{false, "Linux version 4.9.125-linuxkit (root@659b6d51c354) (gcc version 6.4.0 (Alpine 6.4.0) ) #1 SMP Fri Sep 7 08:20:28 UTC 2018", 0},
    79  	} {
    80  		var version uint32
    81  		release, err := parseDebianRelease(tc.releaseString)
    82  		if err == nil {
    83  			version, err = VersionFromRelease(release)
    84  		}
    85  		if err != nil && tc.success {
    86  			t.Errorf("expected %q to success: %s", tc.releaseString, err)
    87  		} else if err == nil && !tc.success {
    88  			t.Errorf("expected %q to fail", tc.releaseString)
    89  		}
    90  		if version != tc.kernelVersion {
    91  			t.Errorf("expected kernel version %d, got %d", tc.kernelVersion, version)
    92  		}
    93  	}
    94  }