Skip to content

Commit d37ad9c

Browse files
dylandreimerinkti-mo
authored andcommitted
struct_ops_test: Fix TestCreateStructOpsMapSpecSimple
On newer kernels fields have been added to the value type of the `bpf_testmod_ops` struct. The test was assuming a static size of 448 bytes, but that is no longer valid for all kernels. Instead of hardcoding the size, we now look up the value type in the BTF of the module and use that size to create the map contents. Signed-off-by: Dylan Reimerink <dylan.reimerink@isovalent.com>
1 parent 5e8bcc7 commit d37ad9c

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

struct_ops_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package ebpf
33
import (
44
"testing"
55

6+
"github.com/go-quicktest/qt"
7+
68
"github.com/cilium/ebpf/btf"
79
"github.com/cilium/ebpf/internal/sys"
810
"github.com/cilium/ebpf/internal/testutils"
@@ -11,6 +13,12 @@ import (
1113
func TestCreateStructOpsMapSpecSimple(t *testing.T) {
1214
requireTestmodOps(t)
1315

16+
btfSpec, err := btf.LoadKernelModuleSpec("bpf_testmod")
17+
qt.Assert(t, qt.IsNil(err))
18+
19+
var outerValueType *btf.Struct
20+
qt.Assert(t, qt.IsNil(btfSpec.TypeByName(structOpsValuePrefix+"bpf_testmod_ops", &outerValueType)))
21+
1422
ms := &MapSpec{
1523
Name: "testmod_ops",
1624
Type: StructOpsMap,
@@ -22,15 +30,13 @@ func TestCreateStructOpsMapSpecSimple(t *testing.T) {
2230
Contents: []MapKV{
2331
{
2432
Key: uint32(0),
25-
Value: make([]byte, 448),
33+
Value: make([]byte, outerValueType.Size),
2634
},
2735
},
2836
}
2937

3038
m, err := NewMap(ms)
3139
testutils.SkipIfNotSupported(t, err)
32-
if err != nil {
33-
t.Fatalf("creating struct_ops map failed: %v", err)
34-
}
40+
qt.Assert(t, qt.IsNil(err))
3541
t.Cleanup(func() { _ = m.Close() })
3642
}

0 commit comments

Comments
 (0)