Skip to content

Commit

Permalink
event monitor: tests: Allow setting cached hostname (DataDog#31863)
Browse files Browse the repository at this point in the history
  • Loading branch information
guyarb authored Dec 9, 2024
1 parent 92bf0e8 commit 8619535
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pkg/ebpf/uprobes/attacher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/network/usm/utils"
"github.com/DataDog/datadog-agent/pkg/process/monitor"
procmontestutil "github.com/DataDog/datadog-agent/pkg/process/monitor/testutil"
secutils "github.com/DataDog/datadog-agent/pkg/security/utils"
"github.com/DataDog/datadog-agent/pkg/util/kernel"
)

Expand Down Expand Up @@ -799,6 +800,7 @@ func launchProcessMonitor(t *testing.T, useEventStream bool) *monitor.ProcessMon
t.Cleanup(pm.Stop)
require.NoError(t, pm.Initialize(useEventStream))
if useEventStream {
secutils.SetCachedHostname("test-hostname")
eventmonitortestutil.StartEventMonitor(t, procmontestutil.RegisterProcessMonitorEventConsumer)
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/eventmonitor/consumers/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (

"github.com/DataDog/datadog-agent/pkg/eventmonitor"
"github.com/DataDog/datadog-agent/pkg/eventmonitor/consumers"

eventtestutil "github.com/DataDog/datadog-agent/pkg/eventmonitor/testutil"
"github.com/DataDog/datadog-agent/pkg/security/utils"
)

const defaultChanSize = 100
Expand All @@ -25,6 +25,8 @@ const defaultChanSize = 100
// created for testing. This function should be called in tests that require a ProcessConsumer.
func NewTestProcessConsumer(t *testing.T) *consumers.ProcessConsumer {
var pc *consumers.ProcessConsumer
// Set fake hostname to avoid fetching it from the core agent.
utils.SetCachedHostname("test-hostname")
eventtestutil.StartEventMonitor(t, func(t *testing.T, evm *eventmonitor.EventMonitor) {
var err error
eventTypes := []consumers.ProcessConsumerEventTypes{consumers.ExecEventType, consumers.ExitEventType}
Expand Down
2 changes: 2 additions & 0 deletions pkg/network/usm/monitor_tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
usmtestutil "github.com/DataDog/datadog-agent/pkg/network/usm/testutil"
"github.com/DataDog/datadog-agent/pkg/network/usm/utils"
procmontestutil "github.com/DataDog/datadog-agent/pkg/process/monitor/testutil"
secutils "github.com/DataDog/datadog-agent/pkg/security/utils"
globalutils "github.com/DataDog/datadog-agent/pkg/util/testutil"
dockerutils "github.com/DataDog/datadog-agent/pkg/util/testutil/docker"
)
Expand Down Expand Up @@ -869,6 +870,7 @@ func setupUSMTLSMonitor(t *testing.T, cfg *config.Config) *Monitor {
require.NoError(t, err)
require.NoError(t, usmMonitor.Start())
if cfg.EnableUSMEventStream && usmconfig.NeedProcessMonitor(cfg) {
secutils.SetCachedHostname("test-hostname")
eventmonitortestutil.StartEventMonitor(t, procmontestutil.RegisterProcessMonitorEventConsumer)
}
t.Cleanup(usmMonitor.Stop)
Expand Down
5 changes: 3 additions & 2 deletions pkg/network/usm/sharedlibraries/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/DataDog/datadog-agent/pkg/ebpf/ebpftest"
"github.com/DataDog/datadog-agent/pkg/ebpf/prebuilt"
Expand All @@ -33,6 +32,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/network/usm/utils"
"github.com/DataDog/datadog-agent/pkg/process/monitor"
procmontestutil "github.com/DataDog/datadog-agent/pkg/process/monitor/testutil"
secutils "github.com/DataDog/datadog-agent/pkg/security/utils"
"github.com/DataDog/datadog-agent/pkg/util/kernel"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
Expand All @@ -42,6 +42,7 @@ func launchProcessMonitor(t *testing.T, useEventStream bool) {
t.Cleanup(pm.Stop)
require.NoError(t, pm.Initialize(useEventStream))
if useEventStream {
secutils.SetCachedHostname("test-hostname")
eventmonitortestutil.StartEventMonitor(t, procmontestutil.RegisterProcessMonitorEventConsumer)
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/process/monitor/process_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/eventmonitor"
eventmonitortestutil "github.com/DataDog/datadog-agent/pkg/eventmonitor/testutil"
"github.com/DataDog/datadog-agent/pkg/network/protocols/telemetry"
"github.com/DataDog/datadog-agent/pkg/security/utils"
"github.com/DataDog/datadog-agent/pkg/util"
"github.com/DataDog/datadog-agent/pkg/util/kernel"
"github.com/DataDog/datadog-agent/pkg/util/log"
Expand Down Expand Up @@ -55,6 +56,7 @@ func waitForProcessMonitor(t *testing.T, pm *ProcessMonitor) {
func initializePM(t *testing.T, pm *ProcessMonitor, useEventStream bool) {
require.NoError(t, pm.Initialize(useEventStream))
if useEventStream {
utils.SetCachedHostname("test-hostname")
eventmonitortestutil.StartEventMonitor(t, func(t *testing.T, evm *eventmonitor.EventMonitor) {
// Can't use the implementation in procmontestutil due to import cycles
procmonconsumer, err := NewProcessMonitorEventConsumer(evm)
Expand Down
15 changes: 15 additions & 0 deletions pkg/security/utils/hostname_testutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.

//go:build test

package utils

// SetCachedHostname test utility to set the cached hostname, to avoid fetching it from the core agent.
func SetCachedHostname(name string) {
hostnameLock.Lock()
cachedHostname = name
hostnameLock.Unlock()
}

0 comments on commit 8619535

Please sign in to comment.