Skip to content

Commit

Permalink
internal: remove ClangEndian
Browse files Browse the repository at this point in the history
Replace uses of ClangEndian with a NativeFile test utility.

Signed-off-by: Lorenz Bauer <[email protected]>
  • Loading branch information
lmb committed Mar 22, 2024
1 parent 65e1d0c commit e64398a
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 24 deletions.
3 changes: 1 addition & 2 deletions btf/core_reloc_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package btf_test

import (
"fmt"
"io"
"os"
"strings"
Expand Down Expand Up @@ -80,7 +79,7 @@ func TestCORERelocationRead(t *testing.T) {
return
}

targetFile := fmt.Sprintf("testdata/relocs_read_tgt-%s.elf", internal.ClangEndian)
targetFile := testutils.NativeFile(t, "testdata/relocs_read_tgt-%s.elf")
targetSpec, err := btf.LoadSpec(targetFile)
if err != nil {
t.Fatal(err)
Expand Down
5 changes: 2 additions & 3 deletions cmd/bpf2go/output_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package main

import (
"fmt"
"testing"

"github.com/go-quicktest/qt"
"github.com/google/go-cmp/cmp"

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

func TestOrderTypes(t *testing.T) {
Expand Down Expand Up @@ -69,7 +68,7 @@ var typesEqualComparer = cmp.Comparer(func(a, b btf.Type) bool {
})

func TestCollectFromSpec(t *testing.T) {
spec, err := ebpf.LoadCollectionSpec(fmt.Sprintf("testdata/minimal-%s.elf", internal.ClangEndian))
spec, err := ebpf.LoadCollectionSpec(testutils.NativeFile(t, "testdata/minimal-%s.elf"))
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 4 additions & 4 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestCollectionSpecCopy(t *testing.T) {
}

func TestCollectionSpecLoadCopy(t *testing.T) {
file := fmt.Sprintf("testdata/loader-%s.elf", internal.ClangEndian)
file := testutils.NativeFile(t, "testdata/loader-%s.elf")
spec, err := LoadCollectionSpec(file)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -691,7 +691,7 @@ func TestIncompleteLoadAndAssign(t *testing.T) {
}

func BenchmarkNewCollection(b *testing.B) {
file := fmt.Sprintf("testdata/loader-%s.elf", internal.ClangEndian)
file := testutils.NativeFile(b, "testdata/loader-%s.elf")
spec, err := LoadCollectionSpec(file)
if err != nil {
b.Fatal(err)
Expand All @@ -715,7 +715,7 @@ func BenchmarkNewCollection(b *testing.B) {
}

func BenchmarkNewCollectionManyProgs(b *testing.B) {
file := fmt.Sprintf("testdata/manyprogs-%s.elf", internal.ClangEndian)
file := testutils.NativeFile(b, "testdata/manyprogs-%s.elf")
spec, err := LoadCollectionSpec(file)
if err != nil {
b.Fatal(err)
Expand All @@ -734,7 +734,7 @@ func BenchmarkNewCollectionManyProgs(b *testing.B) {
}

func BenchmarkLoadCollectionManyProgs(b *testing.B) {
file, err := os.Open(fmt.Sprintf("testdata/manyprogs-%s.elf", internal.ClangEndian))
file, err := os.Open(testutils.NativeFile(b, "testdata/manyprogs-%s.elf"))
qt.Assert(b, qt.IsNil(err))
defer file.Close()

Expand Down
10 changes: 5 additions & 5 deletions elf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func BenchmarkELFLoader(b *testing.B) {
}

func TestDataSections(t *testing.T) {
file := fmt.Sprintf("testdata/loader-%s.elf", internal.ClangEndian)
file := testutils.NativeFile(t, "testdata/loader-%s.elf")
coll, err := LoadCollectionSpec(file)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -258,7 +258,7 @@ func TestDataSections(t *testing.T) {
}

func TestInlineASMConstant(t *testing.T) {
file := fmt.Sprintf("testdata/loader-%s.elf", internal.ClangEndian)
file := testutils.NativeFile(t, "testdata/loader-%s.elf")
coll, err := LoadCollectionSpec(file)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -289,7 +289,7 @@ func TestInlineASMConstant(t *testing.T) {
func TestFreezeRodata(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.9", "sk_lookup program type")

file := fmt.Sprintf("testdata/constants-%s.elf", internal.ClangEndian)
file := testutils.NativeFile(t, "testdata/constants-%s.elf")
spec, err := LoadCollectionSpec(file)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -470,7 +470,7 @@ func TestLoadInvalidInitializedBTFMap(t *testing.T) {
}

func TestStringSection(t *testing.T) {
file := fmt.Sprintf("testdata/strings-%s.elf", internal.ClangEndian)
file := testutils.NativeFile(t, "testdata/strings-%s.elf")
spec, err := LoadCollectionSpec(file)
if err != nil {
t.Fatalf("load collection spec: %s", err)
Expand Down Expand Up @@ -784,7 +784,7 @@ func TestInvalidKfunc(t *testing.T) {
t.Skip("bpf_testmod not loaded")
}

file := fmt.Sprintf("testdata/invalid-kfunc-%s.elf", internal.ClangEndian)
file := testutils.NativeFile(t, "testdata/invalid-kfunc-%s.elf")
coll, err := LoadCollection(file)
if err == nil {
coll.Close()
Expand Down
2 changes: 1 addition & 1 deletion info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func TestHaveProgramInfoMapIDs(t *testing.T) {
func TestProgInfoExtBTF(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.0", "Program BTF (func/line_info)")

spec, err := LoadCollectionSpec(fmt.Sprintf("testdata/loader-%s.elf", internal.ClangEndian))
spec, err := LoadCollectionSpec(testutils.NativeFile(t, "testdata/loader-%s.elf"))
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 0 additions & 3 deletions internal/endian_be.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ import "encoding/binary"
// NativeEndian is set to either binary.BigEndian or binary.LittleEndian,
// depending on the host's endianness.
var NativeEndian = binary.BigEndian

// ClangEndian is set to either "el" or "eb" depending on the host's endianness.
const ClangEndian = "eb"
3 changes: 0 additions & 3 deletions internal/endian_le.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ import "encoding/binary"
// NativeEndian is set to either binary.BigEndian or binary.LittleEndian,
// depending on the host's endianness.
var NativeEndian = binary.LittleEndian

// ClangEndian is set to either "el" or "eb" depending on the host's endianness.
const ClangEndian = "el"
19 changes: 19 additions & 0 deletions internal/testutils/glob.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package testutils

import (
"fmt"
"path/filepath"
"strings"
"testing"

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

// Files calls fn for each given file.
Expand Down Expand Up @@ -56,3 +60,18 @@ nextFile:

return filtered
}

// NativeFile substitutes %s with an abbreviation of the host endianness.
func NativeFile(tb testing.TB, path string) string {
tb.Helper()

if !strings.Contains(path, "%s") {
tb.Fatalf("File %q doesn't contain %%s", path)
}

if cpu.IsBigEndian {
return fmt.Sprintf(path, "eb")
}

return fmt.Sprintf(path, "el")
}
3 changes: 1 addition & 2 deletions link/tracing_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package link

import (
"fmt"
"testing"

"github.com/cilium/ebpf"
Expand Down Expand Up @@ -51,7 +50,7 @@ func TestFreplace(t *testing.T) {
func TestFentryFexit(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.5", "fentry")

spec, err := ebpf.LoadCollectionSpec(fmt.Sprintf("../testdata/fentry_fexit-%s.elf", internal.ClangEndian))
spec, err := ebpf.LoadCollectionSpec(testutils.NativeFile(t, "../testdata/fentry_fexit-%s.elf"))
if err != nil {
t.Fatal("Can't parse ELF:", err)
}
Expand Down
2 changes: 1 addition & 1 deletion prog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ func TestProgramInstructions(t *testing.T) {

func BenchmarkNewProgram(b *testing.B) {
testutils.SkipOnOldKernel(b, "5.18", "kfunc support")
spec, err := LoadCollectionSpec(fmt.Sprintf("testdata/kfunc-%s.elf", internal.ClangEndian))
spec, err := LoadCollectionSpec(testutils.NativeFile(b, "testdata/kfunc-%s.elf"))
qt.Assert(b, qt.IsNil(err))

b.ReportAllocs()
Expand Down

0 comments on commit e64398a

Please sign in to comment.