Skip to content

Commit

Permalink
internal/linux: skip some tests on non-Linux platforms
Browse files Browse the repository at this point in the history
Skip most tests in the package, with the exception of the vdso
tests that use testdata. For those we need to make the string
manipulation functions in package unix work on Windows.

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
  • Loading branch information
lmb authored and ti-mo committed Jan 27, 2025
1 parent db50d22 commit b7c2bb8
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 8 deletions.
8 changes: 8 additions & 0 deletions internal/linux/auxv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package linux

import (
"errors"
"fmt"
"io"
"runtime"
_ "unsafe"

"github.com/cilium/ebpf/internal"
)

type auxvPairReader interface {
Expand Down Expand Up @@ -47,6 +51,10 @@ func (r *auxvRuntimeReader) ReadAuxvPair() (uint64, uint64, error) {
}

func newAuxvRuntimeReader() (auxvPairReader, error) {
if runtime.GOOS != "linux" {
return nil, fmt.Errorf("read auxv from runtime: %w", internal.ErrNotSupportedOnOS)
}

data := runtime_getAuxv()

if len(data)%2 != 0 {
Expand Down
1 change: 1 addition & 0 deletions internal/linux/auxv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func newDefaultAuxvFileReader() (auxvPairReader, error) {

func TestAuxvBothSourcesEqual(t *testing.T) {
runtimeBased, err := newAuxvRuntimeReader()
skipIfNotSupportedOnOS(t, err)
if err != nil {
t.Fatal(err)
}
Expand Down
22 changes: 22 additions & 0 deletions internal/linux/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package linux

import (
"errors"
"testing"

"github.com/cilium/ebpf/internal"
)

// skipIfNotSupportedOnOS is a copy of testutils.SkipIfNotSupported to avoid
// a circular dependency.
func skipIfNotSupportedOnOS(tb testing.TB, err error) {
tb.Helper()

if err == internal.ErrNotSupportedOnOS {
tb.Fatal("Unwrapped ErrNotSupportedOnOS")
}

if errors.Is(err, internal.ErrNotSupportedOnOS) {
tb.Skip(err.Error())
}
}
1 change: 1 addition & 0 deletions internal/linux/statfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestFSType(t *testing.T) {
{"/sys/fs/bpf", unix.BPF_FS_MAGIC},
} {
fst, err := FSType(fs.path)
skipIfNotSupportedOnOS(t, err)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(fst, fs.magic))
}
Expand Down
1 change: 1 addition & 0 deletions internal/linux/vdso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestAuxvNoVDSO(t *testing.T) {

func TestVDSOVersion(t *testing.T) {
_, err := vdsoVersion()
skipIfNotSupportedOnOS(t, err)
qt.Assert(t, qt.IsNil(err))
}

Expand Down
2 changes: 2 additions & 0 deletions internal/linux/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (

func TestCurrentKernelVersion(t *testing.T) {
_, err := KernelVersion()
skipIfNotSupportedOnOS(t, err)
qt.Assert(t, qt.IsNil(err))
}

func TestKernelRelease(t *testing.T) {
r, err := KernelRelease()
skipIfNotSupportedOnOS(t, err)
if err != nil {
t.Fatal(err)
}
Expand Down
11 changes: 11 additions & 0 deletions internal/unix/strings_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build !linux && !windows

package unix

func BytePtrFromString(s string) (*byte, error) {
return nil, errNonLinux()
}

func ByteSliceToString(s []byte) string {
return ""
}
19 changes: 19 additions & 0 deletions internal/unix/strings_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package unix

import (
"syscall"

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

func BytePtrFromString(s string) (*byte, error) {
p, err := windows.BytePtrFromString(s)
if err == syscall.EINVAL {
err = EINVAL
}
return p, err
}

func ByteSliceToString(s []byte) string {
return windows.ByteSliceToString(s)
}
8 changes: 0 additions & 8 deletions internal/unix/types_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,6 @@ func Tgkill(tgid int, tid int, sig syscall.Signal) (err error) {
return errNonLinux()
}

func BytePtrFromString(s string) (*byte, error) {
return nil, errNonLinux()
}

func ByteSliceToString(s []byte) string {
return ""
}

func Renameat2(olddirfd int, oldpath string, newdirfd int, newpath string, flags uint) error {
return errNonLinux()
}
Expand Down

0 comments on commit b7c2bb8

Please sign in to comment.