Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

windows: testutils changes #1666

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ jobs:
./internal/linux
./internal/sysenc
./internal/testutils
./internal/testutils/testmain
./internal/tracefs
./internal/unix

Expand Down
4 changes: 2 additions & 2 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
"github.com/cilium/ebpf/internal/testutils/fdtrace"
"github.com/cilium/ebpf/internal/testutils/testmain"
)

func TestMain(m *testing.M) {
fdtrace.TestMain(m)
testmain.Run(m)
}

func TestCollectionSpecNotModified(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions features/prog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
"github.com/cilium/ebpf/internal/testutils/fdtrace"
"github.com/cilium/ebpf/internal/testutils/testmain"
)

func TestMain(m *testing.M) {
fdtrace.TestMain(m)
testmain.Run(m)
}

func TestHaveProgramType(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion internal/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"runtime"
"strings"
"sync"

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

// ErrNotSupported indicates that a feature is not supported.
Expand Down Expand Up @@ -93,7 +95,7 @@ func NewFeatureTest(name string, fn FeatureTestFn, versions ...string) func() er
break
}

if OnLinux && !strings.ContainsRune(version, ':') {
if platform.IsLinux && !strings.ContainsRune(version, ':') {
// Allow version numbers without a GOOS prefix on Linux.
ft.Version = version
break
Expand Down
7 changes: 4 additions & 3 deletions internal/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (

"github.com/go-quicktest/qt"

"github.com/cilium/ebpf/internal/testutils/fdtrace"
"github.com/cilium/ebpf/internal/platform"
"github.com/cilium/ebpf/internal/testutils/testmain"
)

func TestMain(m *testing.M) {
fdtrace.TestMain(m)
testmain.Run(m)
}

func TestFeatureTest(t *testing.T) {
Expand Down Expand Up @@ -83,7 +84,7 @@ func TestFeatureTestNotSupportedOnOS(t *testing.T) {
qt.Assert(t, qt.IsNotNil(NewFeatureTest("foo", fn)()))
qt.Assert(t, qt.ErrorIs(NewFeatureTest("foo", fn, "froz:1.0.0")(), ErrNotSupportedOnOS))
qt.Assert(t, qt.ErrorIs(NewFeatureTest("foo", fn, runtime.GOOS+":1.0")(), sentinel))
if OnLinux {
if platform.IsLinux {
qt.Assert(t, qt.ErrorIs(NewFeatureTest("foo", fn, "1.0")(), sentinel))
}
}
7 changes: 0 additions & 7 deletions internal/goos.go

This file was deleted.

5 changes: 3 additions & 2 deletions internal/kallsyms/kallsyms.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

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

var errAmbiguousKsym = errors.New("multiple kernel symbols with the same name")
Expand Down Expand Up @@ -49,7 +50,7 @@ func Module(name string) (string, error) {
// Any symbols missing in the kernel are ignored. Returns an error if multiple
// symbols with a given name were found.
func AssignModules(symbols map[string]string) error {
if !internal.OnLinux {
if !platform.IsLinux {
return fmt.Errorf("read /proc/kallsyms: %w", internal.ErrNotSupportedOnOS)
}

Expand Down Expand Up @@ -168,7 +169,7 @@ func Address(symbol string) (uint64, error) {
// Any symbols missing in the kernel are ignored. Returns an error if multiple
// addresses were found for a symbol.
func AssignAddresses(symbols map[string]uint64) error {
if !internal.OnLinux {
if !platform.IsLinux {
return fmt.Errorf("read /proc/kallsyms: %w", internal.ErrNotSupportedOnOS)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/kallsyms/kallsyms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/go-quicktest/qt"

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

Expand Down Expand Up @@ -176,7 +176,7 @@ func BenchmarkAssignAddresses(b *testing.B) {
func mustOpenProcKallsyms(tb testing.TB) *os.File {
tb.Helper()

if !internal.OnLinux {
if !platform.IsLinux {
tb.Skip("/proc/kallsyms is a Linux concept")
}

Expand Down
3 changes: 2 additions & 1 deletion internal/linux/auxv.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"

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

Expand Down Expand Up @@ -46,7 +47,7 @@ func (r *auxvRuntimeReader) ReadAuxvPair() (uint64, uint64, error) {
}

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

Expand Down
8 changes: 8 additions & 0 deletions internal/platform/goos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package platform

import "runtime"

const (
IsLinux = runtime.GOOS == "linux"
IsWindows = runtime.GOOS == "windows"
)
8 changes: 4 additions & 4 deletions internal/sys/fd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
"strings"

"github.com/cilium/ebpf/internal/testutils/fdtrace"
"github.com/cilium/ebpf/internal/testutils/testmain"
"github.com/cilium/ebpf/internal/unix"
)

Expand All @@ -20,7 +20,7 @@ type FD struct {
}

func newFD(value int) *FD {
fdtrace.TraceFD(value, 1)
testmain.TraceFD(value, 1)

fd := &FD{value}
runtime.SetFinalizer(fd, (*FD).finalize)
Expand All @@ -34,7 +34,7 @@ func (fd *FD) finalize() {
return
}

fdtrace.LeakFD(fd.raw)
testmain.LeakFD(fd.raw)

_ = fd.Close()
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func (fd *FD) Close() error {
// lifecycle.
func (fd *FD) Disown() int {
value := fd.raw
fdtrace.ForgetFD(value)
testmain.ForgetFD(value)
fd.raw = -1

runtime.SetFinalizer(fd, nil)
Expand Down
4 changes: 2 additions & 2 deletions internal/sys/syscall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"testing"

"github.com/cilium/ebpf/internal/testutils/fdtrace"
"github.com/cilium/ebpf/internal/testutils/testmain"
"github.com/cilium/ebpf/internal/unix"

"github.com/go-quicktest/qt"
Expand Down Expand Up @@ -62,5 +62,5 @@ func TestSyscallError(t *testing.T) {
}

func TestMain(m *testing.M) {
fdtrace.TestMain(m)
testmain.Run(m)
}
31 changes: 0 additions & 31 deletions internal/testutils/fdtrace/main.go

This file was deleted.

52 changes: 34 additions & 18 deletions internal/testutils/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package testutils
import (
"errors"
"os"
"runtime"
"strings"
"testing"

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

const (
Expand All @@ -23,6 +24,10 @@ func checkFeatureTestError(t *testing.T, err error) {
return
}

if errors.Is(err, internal.ErrNotSupportedOnOS) {
t.Skip(err)
}

var ufe *internal.UnsupportedFeatureError
if errors.As(err, &ufe) {
checkVersion(t, ufe)
Expand Down Expand Up @@ -82,56 +87,67 @@ func checkVersion(tb testing.TB, ufe *internal.UnsupportedFeatureError) {
return
}

if !isKernelLessThan(tb, ufe.MinimumVersion) {
if !isPlatformVersionLessThan(tb, ufe.MinimumVersion, platformVersion(tb)) {
tb.Fatalf("Feature '%s' isn't supported even though kernel is newer than %s",
ufe.Name, ufe.MinimumVersion)
}
}

// Skip a test based on the Linux version we are running on.
//
// Warning: this function does not have an effect on platforms other than Linux.
func SkipOnOldKernel(tb testing.TB, minVersion, feature string) {
tb.Helper()

if platform.IsWindows {
tb.Logf("Ignoring version constraint %s for %s on %s", minVersion, feature, runtime.GOOS)
return
}

if IsKernelLessThan(tb, minVersion) {
tb.Skipf("Test requires at least kernel %s (due to missing %s)", minVersion, feature)
}
}

// Check whether the running Linux version is smaller than a specific version.
//
// Warning: this function always returns false on platforms other than Linux.
func IsKernelLessThan(tb testing.TB, minVersion string) bool {
tb.Helper()

if platform.IsWindows {
tb.Logf("Ignoring version constraint %s on %s", minVersion, runtime.GOOS)
return false
}

minv, err := internal.NewVersion(minVersion)
if err != nil {
tb.Fatalf("Invalid version %s: %s", minVersion, err)
}

return isKernelLessThan(tb, minv)
return isPlatformVersionLessThan(tb, minv, platformVersion(tb))
}

func isKernelLessThan(tb testing.TB, minv internal.Version) bool {
func isPlatformVersionLessThan(tb testing.TB, minv, runv internal.Version) bool {
tb.Helper()

if max := os.Getenv("CI_MAX_KERNEL_VERSION"); max != "" {
key := "CI_MAX_KERNEL_VERSION"
if platform.IsWindows {
key = "CI_MAX_EFW_VERSION"
}

if max := os.Getenv(key); max != "" {
maxv, err := internal.NewVersion(max)
if err != nil {
tb.Fatalf("Invalid version %q in CI_MAX_KERNEL_VERSION: %s", max, err)
tb.Fatalf("Invalid version %q in %s: %s", max, key, err)
}

if maxv.Less(minv) {
tb.Fatalf("Test for %s will never execute on CI since %s is the most recent kernel", minv, maxv)
tb.Fatalf("Test for %s will never execute on CI since %s is the most recent runtime", minv, maxv)
}
}

return kernelVersion(tb).Less(minv)
}

func kernelVersion(tb testing.TB) internal.Version {
tb.Helper()

v, err := linux.KernelVersion()
if err != nil {
tb.Fatal(err)
}
return v
return runv.Less(minv)
}

// ignoreVersionCheck checks whether to omit the version check for a test.
Expand Down
20 changes: 20 additions & 0 deletions internal/testutils/feature_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build !windows

package testutils

import (
"testing"

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

func platformVersion(tb testing.TB) internal.Version {
tb.Helper()

v, err := linux.KernelVersion()
if err != nil {
tb.Fatal(err)
}
return v
}
6 changes: 6 additions & 0 deletions internal/testutils/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package testutils

import (
"testing"

"github.com/go-quicktest/qt"
)

func TestIgnoreKernelVersionCheckWhenEnvVarIsSet(t *testing.T) {
Expand Down Expand Up @@ -52,3 +54,7 @@ func TestIgnoreKernelVersionCheckWhenEnvVarIsSet(t *testing.T) {
})
}
}

func TestPlatformVersion(t *testing.T) {
qt.Assert(t, qt.IsFalse(platformVersion(t).Unspecified()))
}
Loading