From cbb1b352c7b843f143762f9ab1fdfdfdf327631d Mon Sep 17 00:00:00 2001 From: Mahe Tardy Date: Thu, 10 Oct 2024 17:57:21 +0200 Subject: [PATCH] pkg/bugtool: add a unit test on FindMaps variants 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 --- pkg/bugtool/maps_test.go | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/pkg/bugtool/maps_test.go b/pkg/bugtool/maps_test.go index fc61129fba6..7867372eaa7 100644 --- a/pkg/bugtool/maps_test.go +++ b/pkg/bugtool/maps_test.go @@ -4,12 +4,23 @@ 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) @@ -17,4 +28,28 @@ func TestFindPinnedMaps(t *testing.T) { _, 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) + } + }) + }