Skip to content

Commit

Permalink
fixed tests for PES enabled/disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
CordeliaLopez committed Dec 18, 2023
1 parent c4699b9 commit 65473bc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
9 changes: 9 additions & 0 deletions pkg/network/events/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ func UnregisterHandler(handler ProcessEventHandler) {
m.UnregisterHandler(handler)
}

// VerifyEventMonitorLoad returns true if init succeeded (used for testing)
func VerifyEventMonitorLoadSucceeded() bool {
m, ok := theMonitor.Load().(*eventMonitor)
if !ok || m == nil {
return false
}
return true
}

type eventHandlerWrapper struct{}

func (h *eventHandlerWrapper) HandleEvent(ev any) {
Expand Down
11 changes: 4 additions & 7 deletions pkg/network/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ func newTracer(cfg *config.Config) (_ *Tracer, reterr error) {
tr.reverseDNS = newReverseDNS(cfg)
tr.usmMonitor = newUSMMonitor(cfg, tr.ebpfTracer, tr.bpfTelemetry)

tr.verifyPESIsSupported(currKernelVersion)
if tr.cfg.EnableProcessEventMonitoring {
if cfg.EnableProcessEventMonitoring && canRunProcessEventStream(currKernelVersion) {
if err = events.Init(); err != nil {
return nil, fmt.Errorf("could not initialize event monitoring: %w", err)
}
Expand Down Expand Up @@ -839,14 +838,12 @@ func (t *Tracer) DebugDumpProcessCache(ctx context.Context) (interface{}, error)
return nil, nil
}

func (t *Tracer) verifyPESIsSupported(kernelVersion *ebpfkernel.Version) {
if !t.cfg.EnableProcessEventMonitoring {
return
}
func canRunProcessEventStream(kernelVersion *ebpfkernel.Version) bool {
if kernelVersion.IsRH7Kernel() || (!kernelVersion.IsRH8Kernel() && kernelVersion.Code < ebpfkernel.Kernel4_15) {
log.Warn("disabling process event monitoring as it is not supported for this kernel version")
t.cfg.EnableProcessEventMonitoring = false
return false
}
return true
}

func newUSMMonitor(c *config.Config, tracer connection.Tracer, bpfTelemetry *nettelemetry.EBPFTelemetry) *usm.Monitor {
Expand Down
17 changes: 9 additions & 8 deletions pkg/network/tracer/tracer_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/network/config"
"github.com/DataDog/datadog-agent/pkg/network/config/sysctl"
"github.com/DataDog/datadog-agent/pkg/network/ebpf/probes"
"github.com/DataDog/datadog-agent/pkg/network/events"
netlinktestutil "github.com/DataDog/datadog-agent/pkg/network/netlink/testutil"
"github.com/DataDog/datadog-agent/pkg/network/testutil"
"github.com/DataDog/datadog-agent/pkg/network/tracer/connection"
Expand Down Expand Up @@ -2163,13 +2164,13 @@ func (s *TracerSuite) TestPESisDisabledForUnsupportedKernels() {
kv, err := ebpfkernel.NewKernelVersion()
kv4150 := kernel.VersionCode(4, 15, 0)
require.NoError(t, err)
if kv.Code >= kv4150 || kv.IsRH8Kernel() {
t.Skip("This test should only be run on kernels < 4.15.0 and RH7")
}
// fail if tracer cannot start
tr := setupTracer(t, cfg)
require.False(t, tr.cfg.EnableProcessEventMonitoring)
if kv.Code >= kv4150 || kv.IsRH8Kernel() {
t.Skip("These checks should only be run on kernels < 4.15.0 and RH7")
}
require.Nil(t, tr.processCache)
require.False(t, events.VerifyEventMonitorLoadSucceeded())
}

func (s *TracerSuite) TestPESisEnabledForSupportedKernels() {
Expand All @@ -2179,11 +2180,11 @@ func (s *TracerSuite) TestPESisEnabledForSupportedKernels() {
kv, err := ebpfkernel.NewKernelVersion()
kv4150 := kernel.VersionCode(4, 15, 0)
require.NoError(t, err)
if kv.Code < kv4150 || kv.IsRH7Kernel() {
t.Skip("This test should only be run on kernels >= 4.15.0 and RH8")
}
// fail if tracer cannot start
tr := setupTracer(t, cfg)
require.True(t, tr.cfg.EnableProcessEventMonitoring)
if kv.Code < kv4150 || kv.IsRH7Kernel() {
t.Skip("These checks should only be run on kernels >= 4.15.0 and RH8")
}
require.NotNil(t, tr.processCache)
require.True(t, events.VerifyEventMonitorLoadSucceeded())
}

0 comments on commit 65473bc

Please sign in to comment.