Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

tetragon: Several observer related cleanups #1525

Merged
merged 5 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions pkg/observer/observertesthelper/observer_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func getDefaultObserverSensors(tb testing.TB, ctx context.Context, base *sensors
ret = append(ret, cnfSensor)
}

if err := loadObserver(tb, base, cnfSensor); err != nil {
if err := loadSensor(tb, base, cnfSensor); err != nil {
return nil, ret, err
}

Expand Down Expand Up @@ -324,13 +324,57 @@ func GetDefaultObserverWithFile(tb testing.TB, ctx context.Context, file, lib st
return GetDefaultObserverWithWatchers(tb, ctx, b, opts...)
}

func GetDefaultSensorsWithFile(tb testing.TB, ctx context.Context, file, lib string, opts ...TestOption) ([]*sensors.Sensor, error) {
func GetDefaultSensorsWithFile(tb testing.TB, file, lib string, opts ...TestOption) ([]*sensors.Sensor, error) {
opts = append(opts, WithConfig(file))
opts = append(opts, WithLib(lib))

b := base.GetInitialSensor()
_, sens, err := getDefaultObserverSensors(tb, ctx, b, opts...)
return sens, err
option.Config.BpfDir = bpf.MapPrefixPath()
option.Config.MapDir = bpf.MapPrefixPath()

testutils.CaptureLog(tb, logger.GetLogger().(*logrus.Logger))

o := newDefaultTestOptions(opts...)

option.Config.HubbleLib = os.Getenv("TETRAGON_LIB")
if option.Config.HubbleLib == "" {
option.Config.HubbleLib = o.observer.lib
}

procfs := os.Getenv("TETRAGON_PROCFS")
if procfs != "" {
option.Config.ProcFS = procfs
}

if testing.Verbose() {
option.Config.Verbosity = 1
}

var tp tracingpolicy.TracingPolicy
var err error

if o.observer.config != "" {
tp, err = tracingpolicy.PolicyFromYAMLFilename(o.observer.config)
if err != nil {
return nil, fmt.Errorf("failed to parse tracingpolicy: %w", err)
}
}

var sensor *sensors.Sensor

if tp != nil {
sensor, err = sensors.GetMergedSensorFromParserPolicy(tp)
Copy link
Contributor

@kkourt kkourt Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor note: I'd like to remove the GetMergedSensorsFromParserPolicy and use everywhere the same code path: parse the policy, get a list of sensors, and we load the sensors. No need to change it now, but something to avoid in the future.

if err != nil {
return nil, err
}
}

base := base.GetInitialSensor()

if err = loadSensor(tb, base, sensor); err != nil {
return nil, err
}

return []*sensors.Sensor{sensor, base}, nil
}

func loadExporter(tb testing.TB, ctx context.Context, obs *observer.Observer, opts *testExporterOptions, oo *testObserverOptions) error {
Expand Down Expand Up @@ -403,7 +447,7 @@ func loadExporter(tb testing.TB, ctx context.Context, obs *observer.Observer, op
return nil
}

func loadObserver(tb testing.TB, base *sensors.Sensor, sens *sensors.Sensor) error {
func loadSensor(tb testing.TB, base *sensors.Sensor, sens *sensors.Sensor) error {
if err := base.Load(option.Config.BpfDir, option.Config.MapDir); err != nil {
tb.Fatalf("Load base error: %s\n", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sensors/tracing/kprobe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3733,7 +3733,7 @@ spec:
if err != nil {
t.Fatalf("writeFile(%s): err %s", testConfigFile, err)
}
sens, err = observertesthelper.GetDefaultSensorsWithFile(t, context.TODO(), testConfigFile, tus.Conf().TetragonLib, observertesthelper.WithMyPid())
sens, err = observertesthelper.GetDefaultSensorsWithFile(t, testConfigFile, tus.Conf().TetragonLib, observertesthelper.WithMyPid())
if err != nil {
t.Fatalf("GetDefaultObserverWithFile error: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sensors/tracing/tracepoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ spec:
if err != nil {
t.Fatalf("writeFile(%s): err %s", testConfigFile, err)
}
sens, err = observertesthelper.GetDefaultSensorsWithFile(t, context.TODO(), testConfigFile, tus.Conf().TetragonLib)
sens, err = observertesthelper.GetDefaultSensorsWithFile(t, testConfigFile, tus.Conf().TetragonLib)
if err != nil {
t.Fatalf("GetDefaultObserverWithFile error: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sensors/tracing/uprobe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ spec:
if err != nil {
t.Fatalf("writeFile(%s): err %s", testConfigFile, err)
}
sens, err = observertesthelper.GetDefaultSensorsWithFile(t, context.TODO(), testConfigFile, tus.Conf().TetragonLib, observertesthelper.WithMyPid())
sens, err = observertesthelper.GetDefaultSensorsWithFile(t, testConfigFile, tus.Conf().TetragonLib, observertesthelper.WithMyPid())
if err != nil {
t.Fatalf("GetDefaultObserverWithFile error: %s", err)
}
Expand Down