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 813f482 commit 0cbc81a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
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)
}
})

}
19 changes: 16 additions & 3 deletions pkg/sensors/program/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ func AddGlobalMap(name string) {
globalMaps.maps[key] = true
}

func DeleteGlobMap(name string) {
key := name
if len(name) > 15 {
key = name[:15]
}
globalMaps.mu.Lock()
defer globalMaps.mu.Unlock()
delete(globalMaps.maps, key)
}

// Map holds pointer to Program object as a source of its ebpf object
// file. We assume all the programs sharing the map have same map
// definition, so it's ok to use the first program if there's more.
Expand All @@ -145,9 +155,6 @@ func AddGlobalMap(name string) {
// ...
// p.PinMap["mapX"] = &mapX
func mapBuilder(name string, ty MapType, owner bool, lds ...*Program) *Map {
if ty == MapTypeGlobal {
AddGlobalMap(name)
}
m := &Map{name, "", lds[0], Idle(), nil, MaxEntries{0, false}, MaxEntries{0, false}, ty, owner}
for _, ld := range lds {
ld.PinMap[name] = m
Expand Down Expand Up @@ -217,6 +224,9 @@ func (m *Map) Unload() error {
if m.MapHandle != nil {
if !option.Config.KeepSensorsOnExit {
m.MapHandle.Unpin()
if m.Type == MapTypeGlobal {
DeleteGlobMap(m.Name)
}
}
err := m.MapHandle.Close()
m.MapHandle = nil
Expand Down Expand Up @@ -294,6 +304,9 @@ func (m *Map) LoadOrCreatePinnedMap(pinPath string, mapSpec *ebpf.MapSpec) error

m.MapHandle = mh
m.PinState.RefInc()
if m.Type == MapTypeGlobal {
AddGlobalMap(m.Name)
}
return nil
}

Expand Down

0 comments on commit 0cbc81a

Please sign in to comment.