Skip to content

Commit

Permalink
tests: remove !IsNativeEndian skips
Browse files Browse the repository at this point in the history
We have a lot of tests which first glob a list of files containing
BPF for little and big endian, then parse those files and finally
skip a test if the files endianness doesn't match the native
endianness.

This is pretty wasteful, since we parse BPF for no good reason and
makes the tests more verbose than they have to be. Use NativeFile
instead and remove almost all checks for IsNativeEndian from tests.

The two remaining cases are in the ELF loader tests, where they do
have value: we want to make sure that we can parse things from any
endianness.

Signed-off-by: Lorenz Bauer <[email protected]>
  • Loading branch information
lmb committed Mar 22, 2024
1 parent e64398a commit 9a277ae
Show file tree
Hide file tree
Showing 5 changed files with 548 additions and 640 deletions.
207 changes: 96 additions & 111 deletions btf/core_reloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,126 +13,111 @@ import (
)

func TestCORERelocationLoad(t *testing.T) {
testutils.Files(t, testutils.Glob(t, "testdata/relocs-*.elf"), func(t *testing.T, file string) {
fh, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
defer fh.Close()

spec, err := ebpf.LoadCollectionSpecFromReader(fh)
if err != nil {
t.Fatal(err)
}

if spec.ByteOrder != internal.NativeEndian {
return
}

for _, progSpec := range spec.Programs {
t.Run(progSpec.Name, func(t *testing.T) {
if _, err := fh.Seek(0, io.SeekStart); err != nil {
t.Fatal(err)
}

prog, err := ebpf.NewProgramWithOptions(progSpec, ebpf.ProgramOptions{
KernelTypes: spec.Types,
})
testutils.SkipIfNotSupported(t, err)

if strings.HasPrefix(progSpec.Name, "err_") {
if err == nil {
prog.Close()
t.Fatal("Expected an error")
}
t.Log("Got expected error:", err)
return
}

if err != nil {
t.Fatal("Load program:", err)
}
defer prog.Close()

ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Error when running:", err)
}
file := testutils.NativeFile(t, "testdata/relocs-%s.elf")
fh, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
defer fh.Close()

spec, err := ebpf.LoadCollectionSpecFromReader(fh)
if err != nil {
t.Fatal(err)
}

for _, progSpec := range spec.Programs {
t.Run(progSpec.Name, func(t *testing.T) {
if _, err := fh.Seek(0, io.SeekStart); err != nil {
t.Fatal(err)
}

prog, err := ebpf.NewProgramWithOptions(progSpec, ebpf.ProgramOptions{
KernelTypes: spec.Types,
})
testutils.SkipIfNotSupported(t, err)

if ret != 0 {
t.Error("Assertion failed on line", ret)
if strings.HasPrefix(progSpec.Name, "err_") {
if err == nil {
prog.Close()
t.Fatal("Expected an error")
}
})
}
})
t.Log("Got expected error:", err)
return
}

if err != nil {
t.Fatal("Load program:", err)
}
defer prog.Close()

ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Error when running:", err)
}

if ret != 0 {
t.Error("Assertion failed on line", ret)
}
})
}
}

func TestCORERelocationRead(t *testing.T) {
testutils.Files(t, testutils.Glob(t, "testdata/relocs_read-*.elf"), func(t *testing.T, file string) {
spec, err := ebpf.LoadCollectionSpec(file)
if err != nil {
t.Fatal(err)
}

if spec.ByteOrder != internal.NativeEndian {
return
}

targetFile := testutils.NativeFile(t, "testdata/relocs_read_tgt-%s.elf")
targetSpec, err := btf.LoadSpec(targetFile)
if err != nil {
t.Fatal(err)
}

for _, progSpec := range spec.Programs {
t.Run(progSpec.Name, func(t *testing.T) {
prog, err := ebpf.NewProgramWithOptions(progSpec, ebpf.ProgramOptions{
KernelTypes: targetSpec,
})
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Load program:", err)
}
defer prog.Close()

ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Error when running:", err)
}

if ret != 0 {
t.Error("Assertion failed on line", ret)
}
file := testutils.NativeFile(t, "testdata/relocs_read-%s.elf")
spec, err := ebpf.LoadCollectionSpec(file)
if err != nil {
t.Fatal(err)
}

targetFile := testutils.NativeFile(t, "testdata/relocs_read_tgt-%s.elf")
targetSpec, err := btf.LoadSpec(targetFile)
if err != nil {
t.Fatal(err)
}

for _, progSpec := range spec.Programs {
t.Run(progSpec.Name, func(t *testing.T) {
prog, err := ebpf.NewProgramWithOptions(progSpec, ebpf.ProgramOptions{
KernelTypes: targetSpec,
})
}
})
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Load program:", err)
}
defer prog.Close()

ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Error when running:", err)
}

if ret != 0 {
t.Error("Assertion failed on line", ret)
}
})
}
}

func TestLD64IMMReloc(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.4", "vmlinux BTF in sysfs")

testutils.Files(t, testutils.Glob(t, "testdata/relocs_enum-*.elf"), func(t *testing.T, file string) {
fh, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
defer fh.Close()

spec, err := ebpf.LoadCollectionSpecFromReader(fh)
if err != nil {
t.Fatal(err)
}

if spec.ByteOrder != internal.NativeEndian {
return
}

coll, err := ebpf.NewCollection(spec)
if err != nil {
t.Fatal(err)
}
defer coll.Close()
})
file := testutils.NativeFile(t, "testdata/relocs_enum-%s.elf")
fh, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
defer fh.Close()

spec, err := ebpf.LoadCollectionSpecFromReader(fh)
if err != nil {
t.Fatal(err)
}

coll, err := ebpf.NewCollection(spec)
if err != nil {
t.Fatal(err)
}
defer coll.Close()
}
Loading

0 comments on commit 9a277ae

Please sign in to comment.