Skip to content

Commit

Permalink
xdp,netkit: remove duplicate kernelMinReq in favor of testutils.SkipO…
Browse files Browse the repository at this point in the history
…nOldKernel

There were two implementations of this, so move them to common testutils.

Signed-off-by: Timo Beckers <[email protected]>
  • Loading branch information
ti-mo committed May 14, 2024
1 parent 6acf8ea commit 9dab4c0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 64 deletions.
32 changes: 0 additions & 32 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,10 @@
package driver

import (
"bytes"
"fmt"
"testing"

"github.com/jsimonetti/rtnetlink/v2"
"golang.org/x/sys/unix"
)

func getKernelVersion() (kernel, major, minor int, err error) {
var uname unix.Utsname
if err := unix.Uname(&uname); err != nil {
return 0, 0, 0, err
}

end := bytes.IndexByte(uname.Release[:], 0)
versionStr := uname.Release[:end]

if count, _ := fmt.Sscanf(string(versionStr), "%d.%d.%d", &kernel, &major, &minor); count < 2 {
err = fmt.Errorf("failed to parse kernel version from: %q", string(versionStr))
}
return
}

// kernelMinReq checks if the runtime kernel is sufficient
// for the test
func kernelMinReq(t *testing.T, kernel, major int) {
k, m, _, err := getKernelVersion()
if err != nil {
t.Fatalf("failed to get host kernel version: %v", err)
}
if k < kernel || k == kernel && m < major {
t.Skipf("host kernel (%d.%d) does not meet test's minimum required version: (%d.%d)",
k, m, kernel, major)
}
}

// setupInterface create a interface for testing
func setupInterface(conn *rtnetlink.Conn, name string, index, master uint32, driver rtnetlink.LinkDriver) error {
attrs := &rtnetlink.LinkAttributes{
Expand Down
2 changes: 1 addition & 1 deletion driver/netkit_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func TestNetkit(t *testing.T) {
kernelMinReq(t, 6, 7)
testutils.SkipOnOldKernel(t, "6.7", "netkit support")

conn, err := rtnetlink.Dial(nil)
if err != nil {
Expand Down
41 changes: 41 additions & 0 deletions internal/testutils/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package testutils

import (
"bytes"
"fmt"
"testing"

"golang.org/x/sys/unix"
)

func getKernelVersion(tb testing.TB) (maj, min, patch uint32) {
tb.Helper()

var uname unix.Utsname
if err := unix.Uname(&uname); err != nil {
tb.Fatalf("getting uname: %s", err)
}

end := bytes.IndexByte(uname.Release[:], 0)
versionStr := uname.Release[:end]

if count, _ := fmt.Sscanf(string(versionStr), "%d.%d.%d", &maj, &min, &patch); count < 2 {
tb.Fatalf("failed to parse kernel version from %s", string(versionStr))
}
return
}

// SkipOnOldKernel skips the test if the host's kernel is lower than the given
// x.y target version.
func SkipOnOldKernel(tb testing.TB, target, reason string) {
maj, min, _ := getKernelVersion(tb)

var maj_t, min_t, patch_t uint32
if count, _ := fmt.Sscanf(target, "%d.%d.%d", &maj_t, &min_t, &patch_t); count < 2 {
tb.Fatalf("failed to parse target version from %s", target)
}

if maj < maj_t || maj == maj_t && min < min_t {
tb.Skipf("host kernel (%d.%d) too old (minimum %d.%d): %s", maj, min, maj_t, min_t, reason)
}
}
32 changes: 1 addition & 31 deletions link_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package rtnetlink

import (
"bytes"
"fmt"
"testing"

"github.com/cilium/ebpf"
Expand All @@ -19,34 +17,6 @@ import (
// lo accesses the loopback interface present in every network namespace.
var lo uint32 = 1

func getKernelVersion() (kernel, major, minor int, err error) {
var uname unix.Utsname
if err := unix.Uname(&uname); err != nil {
return 0, 0, 0, err
}

end := bytes.IndexByte(uname.Release[:], 0)
versionStr := uname.Release[:end]

if count, _ := fmt.Sscanf(string(versionStr), "%d.%d.%d", &kernel, &major, &minor); count < 2 {
err = fmt.Errorf("failed to parse kernel version from: %q", string(versionStr))
}
return
}

// kernelMinReq checks if the runtime kernel is sufficient
// for the test
func kernelMinReq(t *testing.T, kernel, major int) {
k, m, _, err := getKernelVersion()
if err != nil {
t.Fatalf("failed to get host kernel version: %v", err)
}
if k < kernel || k == kernel && m < major {
t.Skipf("host kernel (%d.%d) does not meet test's minimum required version: (%d.%d)",
k, m, kernel, major)
}
}

func xdpPrograms(tb testing.TB) (int32, int32) {
tb.Helper()

Expand Down Expand Up @@ -218,7 +188,7 @@ func TestLinkXDPReplace(t *testing.T) {
// https://elixir.bootlin.com/linux/v5.6/source/net/core/dev.c#L8662
// source kernel 5.7:
// https://elixir.bootlin.com/linux/v5.7/source/net/core/dev.c#L8674
kernelMinReq(t, 5, 7)
testutils.SkipOnOldKernel(t, "5.7", "XDP_FLAGS_REPLACE")

if err := rlimit.RemoveMemlock(); err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 9dab4c0

Please sign in to comment.