diff --git a/pkg/sensors/tracing/kprobe_maxentries_test.go b/pkg/sensors/tracing/kprobe_maxentries_test.go new file mode 100644 index 00000000000..cd599e345d0 --- /dev/null +++ b/pkg/sensors/tracing/kprobe_maxentries_test.go @@ -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) + } + }) + }) +}