Skip to content

Commit

Permalink
tetragon: Add tests for proper cleanup after sensor unload
Browse files Browse the repository at this point in the history
Adding tests for proper cleanup after sensor unload.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
  • Loading branch information
olsajiri committed Oct 2, 2024
1 parent 0a53920 commit 0391ee3
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions pkg/sensors/test/sensors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package test

import (
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -500,3 +501,101 @@ func TestMaxEntriesInnerMulti(t *testing.T) {
// TODO, we need to check BTF for inner map max entries
t.Skip()
}

func TestCleanup(t *testing.T) {
p1 := program.Builder(
"bpf_map_test_p1.o",
"wake_up_new_task",
"kprobe/wake_up_new_task",
"p1",
"kprobe",
)
p2 := program.Builder(
"bpf_map_test_p2.o",
"wake_up_new_task",
"kprobe/wake_up_new_task",
"p2",
"kprobe",
)

var err error

verifyRemoved := func(files ...string) {
for _, f := range files {
_, err := os.Stat(filepath.Join(bpf.MapPrefixPath(), f))
assert.Error(t, err)
}
}

t.Run("single_ok", func(t *testing.T) {
m1 := program.MapBuilder("m1", p1)
m2 := program.MapBuilder("m2", p2)

s1 := &sensors.Sensor{
Name: "sensor1",
Progs: []*program.Program{p1},
Maps: []*program.Map{m1, m2},
Policy: "policy",
}

err = s1.Load(bpf.MapPrefixPath())
assert.NoError(t, err)

s1.Unload()

verifyRemoved("m1", "m2", "policy")
})

t.Run("multi_ok", func(t *testing.T) {
m1 := program.MapBuilder("m1", p1)
m2 := program.MapBuilder("m2", p2)

s1 := &sensors.Sensor{
Name: "sensor1",
Progs: []*program.Program{p1},
Maps: []*program.Map{m1, m2},
Policy: "policy",
}

s2 := &sensors.Sensor{
Name: "sensor2",
Progs: []*program.Program{p2},
Maps: []*program.Map{m1, m2},
Policy: "policy",
}

err = s1.Load(bpf.MapPrefixPath())
assert.NoError(t, err)
err = s2.Load(bpf.MapPrefixPath())
assert.NoError(t, err)

s1.Unload()

verifyRemoved("policy/sensor1")

s2.Unload()

verifyRemoved("m1", "m2", "policy")
})

t.Run("map_fail", func(t *testing.T) {
m1 := program.MapBuilder("m1", p1, p2)
// map with wrong name
m2 := program.MapBuilder("non-existing", p1, p2)

s1 := &sensors.Sensor{
Name: "sensor1",
Progs: []*program.Program{p1},
Maps: []*program.Map{m1, m2},
Policy: "policy",
}

err = s1.Load(bpf.MapPrefixPath())
assert.Error(t, err)
if err == nil {
defer s1.Unload()
}

verifyRemoved("m1", "m2", "policy")
})
}

0 comments on commit 0391ee3

Please sign in to comment.