Skip to content

Commit

Permalink
tetragon: Add max entries test
Browse files Browse the repository at this point in the history
wip

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
  • Loading branch information
olsajiri committed Jun 26, 2024
1 parent 35c5240 commit 20f86f7
Showing 1 changed file with 125 additions and 0 deletions.
125 changes: 125 additions & 0 deletions pkg/sensors/tracing/kprobe_maxentries_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package tracing

import (
"os"
"testing"

"github.com/cilium/ebpf"
"github.com/cilium/tetragon/pkg/observer/observertesthelper"
"github.com/cilium/tetragon/pkg/sensors"
tus "github.com/cilium/tetragon/pkg/testutils/sensors"
"github.com/stretchr/testify/assert"
)

func runConfig(t *testing.T, config string, fn func(string, int)) {

var err error
err = os.WriteFile(testConfigFile, []byte(config), 0644)
if err != nil {
t.Fatalf("writeFile(%s): err %s", testConfigFile, err)
}
sens, err := observertesthelper.GetDefaultSensorsWithFile(t, testConfigFile,
tus.Conf().TetragonLib)
if err != nil {
t.Fatalf("GetDefaultObserverWithFile error: %s", err)
}

for _, sensor := range sens {
for _, load := range sensor.Progs {
c := load.LC
for _, p := range c.Programs {
for _, id := range p.MapIDs {
m, err := ebpf.NewMapFromID(id)
if err != nil {
t.Fatalf("can't open map id %d: %s\n", id, err)
}
info, err := m.Info()
if err != nil {
t.Fatalf("can't get map info: %s\n", err)
}
fn(info.Name, int(info.MaxEntries))
}
}
}
}

sensi := make([]sensors.SensorIface, 0, len(sens))
for _, s := range sens {
sensi = append(sensi, s)
}
sensors.UnloadSensors(sensi)
}

func TestMaxEntries(t *testing.T) {

t.Run("fdinstall_1", func(t *testing.T) {
config := `
apiVersion: cilium.io/v1alpha1
kind: TracingPolicy
metadata:
name: "sys-read"
spec:
kprobes:
- call: "sys_read"
syscall: true
return: true
args:
- index: 0
type: "int"
- index: 1
type: "char_buf"
returnCopy: true
- index: 2
type: "size_t"
returnArg:
type: "size_t"
`

runConfig(t, config, func(name string, entries int) {
if name == "fdinstall_map" {
assert.Equal(t, 1, entries)
}
})
})

t.Run("fdinstall_32000", func(t *testing.T) {
config := `
apiVersion: cilium.io/v1alpha1
kind: TracingPolicy
metadata:
name: "syswritefollowfdpsswd"
spec:
kprobes:
- call: "fd_install"
syscall: false
args:
- index: 0
type: int
- index: 1
type: "file"
selectors:
- matchArgs:
- index: 1
operator: "Equal"
values:
- "/tmp/test"
matchBinaries:
- operator: "In"
values:
- "/usr/bin/vim"
matchActions:
- action: FollowFD
argFd: 0
argName: 1
`

runConfig(t, config, func(name string, entries int) {
if name == "fdinstall_map" {
assert.Equal(t, 32000, entries)
}
})
})
}

0 comments on commit 20f86f7

Please sign in to comment.