Skip to content

Commit

Permalink
add bpf2go test emit-and-load specs+bpf objects
Browse files Browse the repository at this point in the history
This commit extends `api_test.go` to check whether specs and bpf objects
are correctly loaded. It also accounts for the new Variable/VariableSpec
in the test.
In addition, this commit adds a bpf2go test in `output_test.go` to verify
that all the listed bpf objects (i.e., variables, maps and programs) are
emitted. The tests looks for the output program, stored in a buffer, which
should contain the expected declarations.

This commits introduces further checks in the `api_test.go`

Signed-off-by: Simone Magnani <[email protected]>
  • Loading branch information
smagnani96 committed Dec 6, 2024
1 parent 7e17150 commit 0912b3a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cmd/bpf2go/gen/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,25 @@ func TestCustomIdentifier(t *testing.T) {
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.StringContains(buf.String(), "DO_THING"))
}

func TestBpfObjects(t *testing.T) {
var buf bytes.Buffer
args := GenerateArgs{
Package: "foo",
Stem: "bar",
Maps: []string{"map1"},
Variables: []string{"var_1"},
Programs: []string{"prog_foo_1"},
Output: &buf,
}
err := Generate(args)
qt.Assert(t, qt.IsNil(err))

qt.Assert(t, qt.StringContains(buf.String(), "Map1 *ebpf.MapSpec `ebpf:\"map1\"`"))
qt.Assert(t, qt.StringContains(buf.String(), "Var1 *ebpf.VariableSpec `ebpf:\"var_1\"`"))
qt.Assert(t, qt.StringContains(buf.String(), "ProgFoo1 *ebpf.ProgramSpec `ebpf:\"prog_foo_1\"`"))

qt.Assert(t, qt.StringContains(buf.String(), "Map1 *ebpf.Map `ebpf:\"map1\"`"))
qt.Assert(t, qt.StringContains(buf.String(), "Var1 *ebpf.Variable `ebpf:\"var_1\"`"))
qt.Assert(t, qt.StringContains(buf.String(), "ProgFoo1 *ebpf.Program `ebpf:\"prog_foo_1\"`"))
}
16 changes: 16 additions & 0 deletions cmd/bpf2go/test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ func TestLoadingSpec(t *testing.T) {
if spec == nil {
t.Fatal("Got a nil spec")
}

if spec.Programs == nil {
t.Error("Got a nil spec for programs")
}

if spec.Maps == nil {
t.Error("Got a nil spec for maps")
}

if spec.Variables == nil {
t.Error("Got a nil spec for variables")
}
}

func TestLoadingObjects(t *testing.T) {
Expand All @@ -36,6 +48,10 @@ func TestLoadingObjects(t *testing.T) {
if objs.Map1 == nil {
t.Error("Loading returns an object with nil maps")
}

if objs.MyConstant == nil || objs.StructConst == nil {
t.Error("Loading returns an object with nil variables")
}
}

func TestTypes(t *testing.T) {
Expand Down

0 comments on commit 0912b3a

Please sign in to comment.