1
// SPDX-License-Identifier: Apache-2.0
2
// Copyright Authors of Tetragon
16
"github.com/cilium/tetragon/pkg/bpf"
17
"github.com/cilium/tetragon/pkg/btf"
18
"github.com/cilium/tetragon/pkg/logger"
19
"github.com/cilium/tetragon/pkg/option"
20
"github.com/cilium/tetragon/pkg/sensors/program"
21
"github.com/sirupsen/logrus"
26
// Conf is configuration for testing sensors.
27
// It is intialized in TestSensorsRun() so all sensors test should call this
28
// function in their TestMain
32
CmdWaitTime time.Duration
33
DisableTetragonLogs bool
38
var ConfigDefaults = Config{
39
TetragonLib: filepath.Join(TetragonBpfPath(), "objs"),
40
SelfBinary: filepath.Base(os.Args[0]),
41
// NB: for sensor tests, CmdWaitTime is initialized by TestSensorsRun to 5min
42
CmdWaitTime: 60000 * time.Millisecond,
43
DisableTetragonLogs: false,
50
panic("please call TestSensorsRun() to initialize GetTestSensorsConf")
55
// TetragonBpfPath retrieves bpf code path
56
func TetragonBpfPath() string {
57
_, testFname, _, _ := runtime.Caller(0)
58
return filepath.Join(filepath.Dir(testFname), "..", "..", "..", "bpf")
61
func TestSensorsRun(m *testing.M, sensorName string) int {
65
// instruct loader to keep the loaded collection for TestLoad* tests
66
program.KeepCollection = true
68
// some tests require the name of the current binary.
69
config.SelfBinary = filepath.Base(os.Args[0])
71
flag.StringVar(&config.TetragonLib,
72
"bpf-lib", ConfigDefaults.TetragonLib,
73
"tetragon lib directory (location of btf file and bpf objs). Will be overridden by an TETRAGON_LIB env variable.")
74
flag.DurationVar(&config.CmdWaitTime,
77
"duration to wait for tetragon to gather logs from commands")
79
&config.DisableTetragonLogs,
80
"disable-tetragon-logs",
81
ConfigDefaults.DisableTetragonLogs,
82
"do not output teragon log")
87
"enable debug log output")
92
"enable trace log output. Implies debug. Note that due to a naming conflict this must be passed after -args")
96
if err := logger.SetupLogging(option.Config.LogOpts, true); err != nil {
101
logger.DefaultLogger.SetLevel(logrus.TraceLevel)
104
// use a sensor-specific name for the bpffs directory for the maps.
105
// Also, we currently seem to fail to remove the /sys/fs/bpf/<testMapDir>
106
// Do so here, until we figure out a way to do it properly. Also, issue
108
testMapDir := fmt.Sprintf("test%s", sensorName)
110
bpf.CheckOrMountFS("")
111
bpf.CheckOrMountDebugFS()
112
bpf.ConfigureResourceLimits()
114
if config.TetragonLib != "" {
115
option.Config.HubbleLib = config.TetragonLib
118
bpf.SetMapPrefix(testMapDir)
120
log := logger.GetLogger()
121
path := bpf.MapPrefixPath()
122
_, err := os.Stat(path)
123
if os.IsNotExist(err) {
127
if entries, err := os.ReadDir(path); err == nil {
128
for _, entry := range entries {
129
log.Printf("`%s` still exists after test", entry.Name())
133
log.Printf("map dir `%s` still exists after test. Removing it.", path)
136
if err := btf.InitCachedBTF(config.TetragonLib, ""); err != nil {
137
fmt.Printf("InitCachedBTF failed: %v", err)