From 64567f8dec3a4d8abb67c254672d53dfd16029f7 Mon Sep 17 00:00:00 2001 From: Lorenz Bauer Date: Tue, 27 Jun 2023 15:11:05 +0100 Subject: [PATCH] btf: don't access Spec.types directly from tests Spec.types is going away, so refactor tests to use an iterator to read all types instead. Signed-off-by: Lorenz Bauer --- btf/btf_test.go | 18 ++++++++++-------- btf/marshal_test.go | 19 +++++++++++++++---- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/btf/btf_test.go b/btf/btf_test.go index 5fb32d809..4976fe481 100644 --- a/btf/btf_test.go +++ b/btf/btf_test.go @@ -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]) } } } diff --git a/btf/marshal_test.go b/btf/marshal_test.go index 42dd88e2c..baa26a154 100644 --- a/btf/marshal_test.go +++ b/btf/marshal_test.go @@ -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. @@ -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() @@ -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() @@ -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 +}