github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/internal/version_test.go (about)

     1  package internal
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/go-quicktest/qt"
     7  )
     8  
     9  func TestVersion(t *testing.T) {
    10  	a, err := NewVersion("1.2")
    11  	if err != nil {
    12  		t.Fatal(err)
    13  	}
    14  
    15  	b, err := NewVersion("2.2.1")
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  
    20  	if !a.Less(b) {
    21  		t.Error("A should be less than B")
    22  	}
    23  
    24  	if b.Less(a) {
    25  		t.Error("B shouldn't be less than A")
    26  	}
    27  
    28  	v200 := Version{2, 0, 0}
    29  	if !a.Less(v200) {
    30  		t.Error("1.2.1 should not be less than 2.0.0")
    31  	}
    32  
    33  	if v200.Less(a) {
    34  		t.Error("2.0.0 should not be less than 1.2.1")
    35  	}
    36  }
    37  
    38  func TestKernelVersion(t *testing.T) {
    39  	// Kernels 4.4 and 4.9 have a SUBLEVEL of over 255 and clamp it to 255.
    40  	// In our implementation, the other version segments are truncated.
    41  	if v, want := (Version{256, 256, 256}), uint32(255); v.Kernel() != want {
    42  		t.Errorf("256.256.256 should result in a kernel version of %d, got: %d", want, v.Kernel())
    43  	}
    44  
    45  	// Known good version.
    46  	if v, want := (Version{4, 9, 128}), uint32(264576); v.Kernel() != want {
    47  		t.Errorf("4.9.1 should result in a kernel version of %d, got: %d", want, v.Kernel())
    48  	}
    49  }
    50  
    51  func TestCurrentKernelVersion(t *testing.T) {
    52  	_, err := KernelVersion()
    53  	qt.Assert(t, qt.IsNil(err))
    54  }
    55  
    56  func TestVersionFromCode(t *testing.T) {
    57  	var tests = []struct {
    58  		name string
    59  		code uint32
    60  		v    Version
    61  	}{
    62  		{"0.0.0", 0, Version{0, 0, 0}},
    63  		{"1.0.0", 0x10000, Version{1, 0, 0}},
    64  		{"4.4.255", 0x404ff, Version{4, 4, 255}},
    65  		{"255.255.255", 0xffffff, Version{255, 255, 255}},
    66  	}
    67  
    68  	for _, tt := range tests {
    69  		t.Run(tt.name, func(t *testing.T) {
    70  			v := NewVersionFromCode(tt.code)
    71  			if v != tt.v {
    72  				t.Errorf("unexpected version for code '%d'. got: %v, want: %v", tt.code, v, tt.v)
    73  			}
    74  		})
    75  	}
    76  }
    77  
    78  func TestKernelRelease(t *testing.T) {
    79  	r, err := KernelRelease()
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  
    84  	if r == "" {
    85  		t.Fatal("unexpected empty kernel release")
    86  	}
    87  }