Skip to content

Commit

Permalink
pkg/bugtool: add a unit test on FindMaps variants
Browse files Browse the repository at this point in the history
So that this code is triggered via tests and doesn't fail silently if
cilium/ebpf changes its behavior, like suggested in cilium/ebpf#1566.

Signed-off-by: Mahe Tardy <[email protected]>
  • Loading branch information
mtardy committed Oct 10, 2024
1 parent 87007b2 commit cbb1b35
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion pkg/bugtool/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,52 @@
package bugtool

import (
"os"
"testing"

"github.com/cilium/tetragon/pkg/sensors/base"
tus "github.com/cilium/tetragon/pkg/testutils/sensors"
"github.com/stretchr/testify/assert"

// needed to register the probe type execve for the base sensor
_ "github.com/cilium/tetragon/pkg/sensors/exec"
)

func TestFindPinnedMaps(t *testing.T) {
func TestMain(m *testing.M) {
ec := tus.TestSensorsRun(m, "SensorBugtool")
os.Exit(ec)
}

func TestFindMaps(t *testing.T) {
t.Run("NoSuchFile", func(t *testing.T) {
const path = "/sys/fs/bpf/nosuchfile"
_, err := FindPinnedMaps(path)
assert.Error(t, err)
_, err = FindMapsUsedByPinnedProgs(path)
assert.Error(t, err)
})

t.Run("BaseSensorMemlock", func(t *testing.T) {
tus.LoadSensor(t, base.GetInitialSensor())

const path = "/sys/fs/bpf/testSensorBugtool"
pinnedMaps, err := FindPinnedMaps(path)
assert.NoError(t, err)
if assert.NotEmpty(t, pinnedMaps) {
assert.NotZero(t, pinnedMaps[0].Memlock)
}

mapsUsedByProgs, err := FindMapsUsedByPinnedProgs(path)
assert.NoError(t, err)
if assert.NotEmpty(t, mapsUsedByProgs) {
assert.NotZero(t, mapsUsedByProgs[0].Memlock)
}

allMaps, err := FindAllMaps()
assert.NoError(t, err)
if assert.NotEmpty(t, allMaps) {
assert.NotZero(t, allMaps[0].Memlock)
}
})

}

0 comments on commit cbb1b35

Please sign in to comment.