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

btf: stop accessing Spec.types from tests #1176

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions btf/btf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,22 +336,24 @@ func TestGuessBTFByteOrder(t *testing.T) {

func TestSpecCopy(t *testing.T) {
spec := parseELFBTF(t, "../testdata/loader-el.elf")
cpy := spec.Copy()

if len(spec.types) < 1 {
t.Fatal("Not enough types")
}
have := typesFromSpec(t, spec)
qt.Assert(t, len(spec.types) > 0, qt.IsTrue)

cpy := spec.Copy()
for i := range cpy.types {
if _, ok := cpy.types[i].(*Void); ok {
want := typesFromSpec(t, cpy)
qt.Assert(t, want, qt.HasLen, len(have))

for i := range want {
if _, ok := have[i].(*Void); ok {
// Since Void is an empty struct, a Type interface value containing
// &Void{} stores (*Void, nil). Since interface equality first compares
// the type and then the concrete value, Void is always equal.
continue
}

if cpy.types[i] == spec.types[i] {
t.Fatalf("Type at index %d is not a copy: %T == %T", i, cpy.types[i], spec.types[i])
if have[i] == want[i] {
t.Fatalf("Type at index %d is not a copy: %T == %T", i, have[i], want[i])
}
}
}
Expand Down
19 changes: 15 additions & 4 deletions btf/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestBuilderAdd(t *testing.T) {
}

func TestRoundtripVMlinux(t *testing.T) {
types := vmlinuxSpec(t).types
types := typesFromSpec(t, vmlinuxSpec(t))

// Randomize the order to force different permutations of walking the type
// graph. Keep Void at index 0.
Expand Down Expand Up @@ -153,8 +153,7 @@ func TestMarshalEnum64(t *testing.T) {
}

func BenchmarkMarshaler(b *testing.B) {
spec := vmlinuxTestdataSpec(b)
types := spec.types[:100]
types := typesFromSpec(b, vmlinuxTestdataSpec(b))[:100]

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -169,7 +168,7 @@ func BenchmarkMarshaler(b *testing.B) {
}

func BenchmarkBuildVmlinux(b *testing.B) {
types := vmlinuxTestdataSpec(b).types
types := typesFromSpec(b, vmlinuxTestdataSpec(b))

b.ReportAllocs()
b.ResetTimer()
Expand Down Expand Up @@ -202,3 +201,15 @@ func specFromTypes(tb testing.TB, types []Type) *Spec {

return spec
}

func typesFromSpec(tb testing.TB, spec *Spec) []Type {
tb.Helper()

var types []Type
iter := spec.Iterate()
for iter.Next() {
types = append(types, iter.Type)
}

return types
}