testutils

package
v0.22.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2026 License: MIT Imports: 29 Imported by: 0

Documentation

Overview

The netns implementation in this file was taken from cilium/cilium.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckFeatureMatrix added in v0.10.0

func CheckFeatureMatrix[K comparable](t *testing.T, fm internal.FeatureMatrix[K])

func CheckFeatureTest

func CheckFeatureTest(t *testing.T, fn func() error)

func ClangBin added in v0.16.0

func ClangBin(tb testing.TB) string

func Contains added in v0.17.0

func Contains[T, I any](i I) bool

Contains checks if interface value I is of type T. Use with qt.Satisfies:

qt.Assert(t, qt.Satisfies(p, testutils.Contains[*ebpf.Program]))

func CreateCgroup added in v0.7.0

func CreateCgroup(tb testing.TB) *os.File

func DupFD added in v0.18.0

func DupFD(tb testing.TB, fd int) int

func Files added in v0.6.0

func Files(t *testing.T, files []string, fn func(*testing.T, string))

Files calls fn for each given file.

The function errors out if the pattern matches no files.

func GetCgroupIno added in v0.7.0

func GetCgroupIno(t *testing.T, cgroup *os.File) uint64

func Glob added in v0.6.0

func Glob(tb testing.TB, pattern string, excludes ...string) []string

Glob finds files matching a pattern.

The pattern should may include full path. Excludes use the same syntax as pattern, but are only applied to the basename instead of the full path.

func IsDeepCopy added in v0.16.0

func IsDeepCopy[T any](got, want T) qt.Checker

IsDeepCopy checks that got is a deep copy of want.

All primitive values must be equal, but pointers must be distinct. This is different from reflect.DeepEqual which will accept equal pointer values. That is, reflect.DeepEqual(a, a) is true, while IsDeepCopy(a, a) is false.

func IsVersionLessThan added in v0.18.0

func IsVersionLessThan(tb testing.TB, minVersions ...string) bool

Check whether the current runtime version is less than some minimum.

func LockOSThreadToSingleCPU added in v0.15.0

func LockOSThreadToSingleCPU(tb testing.TB)

LockOSThreadToSingleCPU force the current goroutine to run on a single CPU.

func NativeFile added in v0.14.0

func NativeFile(tb testing.TB, path string) string

NativeFile substitutes %s with an abbreviation of the host endianness.

func Rand added in v0.11.0

func Rand(tb testing.TB) *rand.Rand

func RunWithToken added in v0.22.0

func RunWithToken(t *testing.T, name string, delegated Delegated, fn func(t *testing.T))

RunWithToken runs fn as a subtest as an unprivileged, user-namespaced subprocess with only the given BPF permissions delegated to it. The subtest carries the given name.

The subprocess retains CAP_BPF and CAP_DAC_READ_SEARCH to make BPF() syscalls with a token and read files owned by the test user.

testutils.RunWithToken(t, "foo", testutils.Delegated{
	Cmds: []sys.Cmd{sys.BPF_MAP_CREATE},
	Maps: []sys.MapType{sys.BPF_MAP_TYPE_HASH},
}, func(t *testing.T) {
	_, err := newMap(t, hashMapSpec, nil)
	qt.Assert(t, qt.IsNil(err))
})

Only works on Linux 6.9 and later.

func SkipIfNotSupported

func SkipIfNotSupported(tb testing.TB, err error)

func SkipIfNotSupportedOnOS added in v0.17.0

func SkipIfNotSupportedOnOS(tb testing.TB, err error)

func SkipNonNativeEndian added in v0.21.0

func SkipNonNativeEndian(tb testing.TB, bo binary.ByteOrder)

SkipNonNativeEndian skips the test or benchmark if bo doesn't match the host's native endianness.

func SkipOnOldKernel

func SkipOnOldKernel(tb testing.TB, minVersion, feature string)

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 TempBPFFS added in v0.5.0

func TempBPFFS(tb testing.TB) string

TempBPFFS creates a temporary directory on a BPF FS.

The directory is automatically cleaned up at the end of the test run.

func WaitChan added in v0.19.0

func WaitChan[T any](tb testing.TB, ch <-chan T, timeout time.Duration)

WaitChan waits for a value to be sent on a channel, or for a timeout to occur. If the timeout is reached, the test will fail.

func WithCapabilities added in v0.21.0

func WithCapabilities(tb testing.TB, caps []Capability, f func())

WithCapabilities runs `f` with only the given capabilities in the effective set. This allows us to assert that certain operations only require specific capabilities.

The code in `f` and any code called by `f` must NOT call runtime.LockOSThread, as this could leave the current goroutine permanently pinned to an OS thread. It must also not create any goroutines of its own, as that will result in a new OS thread being created that may or may not inherit the new capabilities of its parent, and will later be released into the schedulable pool of threads available for goroutine scheduling.

Warning: on non-linux platforms, this function calls through to `f` without side effects.

Types

type Capability added in v0.21.0

type Capability int
const (
	CAP_DAC_READ_SEARCH Capability = 2
	CAP_NET_ADMIN       Capability = 12
	CAP_SYS_ADMIN       Capability = 21
	CAP_SYS_RESOURCE    Capability = 24
	CAP_PERFMON         Capability = 38
	CAP_BPF             Capability = 39
)

Mirrors of constants from x/sys/unix

type Delegated added in v0.22.0

type Delegated struct {
	Cmds        []sys.Cmd
	Maps        []sys.MapType
	Progs       []sys.ProgType
	AttachTypes []sys.AttachType
}

type NetNS added in v0.21.0

type NetNS struct {
	// contains filtered or unexported fields
}

func NewNetNS added in v0.21.0

func NewNetNS(tb testing.TB) *NetNS

NewNetNS returns a new network namespace.

func (*NetNS) Do added in v0.21.0

func (h *NetNS) Do(f func() error) error

Do runs the provided func in the netns without changing the calling thread's netns.

The code in f and any code called by f must NOT call runtime.LockOSThread, as this could leave the goroutine created by Do permanently pinned to an OS thread.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL