Skip to content

Commit e64398a

Browse files
committed
internal: remove ClangEndian
Replace uses of ClangEndian with a NativeFile test utility. Signed-off-by: Lorenz Bauer <[email protected]>
1 parent 65e1d0c commit e64398a

10 files changed

+34
-24
lines changed

btf/core_reloc_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package btf_test
22

33
import (
4-
"fmt"
54
"io"
65
"os"
76
"strings"
@@ -80,7 +79,7 @@ func TestCORERelocationRead(t *testing.T) {
8079
return
8180
}
8281

83-
targetFile := fmt.Sprintf("testdata/relocs_read_tgt-%s.elf", internal.ClangEndian)
82+
targetFile := testutils.NativeFile(t, "testdata/relocs_read_tgt-%s.elf")
8483
targetSpec, err := btf.LoadSpec(targetFile)
8584
if err != nil {
8685
t.Fatal(err)

cmd/bpf2go/output_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package main
22

33
import (
4-
"fmt"
54
"testing"
65

76
"github.com/go-quicktest/qt"
87
"github.com/google/go-cmp/cmp"
98

109
"github.com/cilium/ebpf"
1110
"github.com/cilium/ebpf/btf"
12-
"github.com/cilium/ebpf/internal"
11+
"github.com/cilium/ebpf/internal/testutils"
1312
)
1413

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

7170
func TestCollectFromSpec(t *testing.T) {
72-
spec, err := ebpf.LoadCollectionSpec(fmt.Sprintf("testdata/minimal-%s.elf", internal.ClangEndian))
71+
spec, err := ebpf.LoadCollectionSpec(testutils.NativeFile(t, "testdata/minimal-%s.elf"))
7372
if err != nil {
7473
t.Fatal(err)
7574
}

collection_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func TestCollectionSpecCopy(t *testing.T) {
9898
}
9999

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

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

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

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

elf_reader_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func BenchmarkELFLoader(b *testing.B) {
228228
}
229229

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

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

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

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

787-
file := fmt.Sprintf("testdata/invalid-kfunc-%s.elf", internal.ClangEndian)
787+
file := testutils.NativeFile(t, "testdata/invalid-kfunc-%s.elf")
788788
coll, err := LoadCollection(file)
789789
if err == nil {
790790
coll.Close()

info_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ func TestHaveProgramInfoMapIDs(t *testing.T) {
371371
func TestProgInfoExtBTF(t *testing.T) {
372372
testutils.SkipOnOldKernel(t, "5.0", "Program BTF (func/line_info)")
373373

374-
spec, err := LoadCollectionSpec(fmt.Sprintf("testdata/loader-%s.elf", internal.ClangEndian))
374+
spec, err := LoadCollectionSpec(testutils.NativeFile(t, "testdata/loader-%s.elf"))
375375
if err != nil {
376376
t.Fatal(err)
377377
}

internal/endian_be.go

-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,3 @@ import "encoding/binary"
77
// NativeEndian is set to either binary.BigEndian or binary.LittleEndian,
88
// depending on the host's endianness.
99
var NativeEndian = binary.BigEndian
10-
11-
// ClangEndian is set to either "el" or "eb" depending on the host's endianness.
12-
const ClangEndian = "eb"

internal/endian_le.go

-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,3 @@ import "encoding/binary"
77
// NativeEndian is set to either binary.BigEndian or binary.LittleEndian,
88
// depending on the host's endianness.
99
var NativeEndian = binary.LittleEndian
10-
11-
// ClangEndian is set to either "el" or "eb" depending on the host's endianness.
12-
const ClangEndian = "el"

internal/testutils/glob.go

+19
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package testutils
22

33
import (
4+
"fmt"
45
"path/filepath"
6+
"strings"
57
"testing"
8+
9+
"golang.org/x/sys/cpu"
610
)
711

812
// Files calls fn for each given file.
@@ -56,3 +60,18 @@ nextFile:
5660

5761
return filtered
5862
}
63+
64+
// NativeFile substitutes %s with an abbreviation of the host endianness.
65+
func NativeFile(tb testing.TB, path string) string {
66+
tb.Helper()
67+
68+
if !strings.Contains(path, "%s") {
69+
tb.Fatalf("File %q doesn't contain %%s", path)
70+
}
71+
72+
if cpu.IsBigEndian {
73+
return fmt.Sprintf(path, "eb")
74+
}
75+
76+
return fmt.Sprintf(path, "el")
77+
}

link/tracing_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package link
22

33
import (
4-
"fmt"
54
"testing"
65

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

54-
spec, err := ebpf.LoadCollectionSpec(fmt.Sprintf("../testdata/fentry_fexit-%s.elf", internal.ClangEndian))
53+
spec, err := ebpf.LoadCollectionSpec(testutils.NativeFile(t, "../testdata/fentry_fexit-%s.elf"))
5554
if err != nil {
5655
t.Fatal("Can't parse ELF:", err)
5756
}

prog_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ func TestProgramInstructions(t *testing.T) {
954954

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

960960
b.ReportAllocs()

0 commit comments

Comments
 (0)