From 8619535c19ac43fe3a9b39b78dd3b92348d3ac4c Mon Sep 17 00:00:00 2001 From: Guy Arbitman Date: Mon, 9 Dec 2024 14:11:59 +0200 Subject: [PATCH] event monitor: tests: Allow setting cached hostname (#31863) --- pkg/ebpf/uprobes/attacher_test.go | 2 ++ pkg/eventmonitor/consumers/testutil/testutil.go | 4 +++- pkg/network/usm/monitor_tls_test.go | 2 ++ pkg/network/usm/sharedlibraries/watcher_test.go | 5 +++-- pkg/process/monitor/process_monitor_test.go | 2 ++ pkg/security/utils/hostname_testutil.go | 15 +++++++++++++++ 6 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 pkg/security/utils/hostname_testutil.go diff --git a/pkg/ebpf/uprobes/attacher_test.go b/pkg/ebpf/uprobes/attacher_test.go index abad66f8778f2..26428868c1d70 100644 --- a/pkg/ebpf/uprobes/attacher_test.go +++ b/pkg/ebpf/uprobes/attacher_test.go @@ -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" ) @@ -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) } diff --git a/pkg/eventmonitor/consumers/testutil/testutil.go b/pkg/eventmonitor/consumers/testutil/testutil.go index 4ed3b9fb2a1ea..871cf780b0089 100644 --- a/pkg/eventmonitor/consumers/testutil/testutil.go +++ b/pkg/eventmonitor/consumers/testutil/testutil.go @@ -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 @@ -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} diff --git a/pkg/network/usm/monitor_tls_test.go b/pkg/network/usm/monitor_tls_test.go index a4fd5924bd630..322f9be425da4 100644 --- a/pkg/network/usm/monitor_tls_test.go +++ b/pkg/network/usm/monitor_tls_test.go @@ -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" ) @@ -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) diff --git a/pkg/network/usm/sharedlibraries/watcher_test.go b/pkg/network/usm/sharedlibraries/watcher_test.go index ec8eddb5f8edf..ca401e180fc50 100644 --- a/pkg/network/usm/sharedlibraries/watcher_test.go +++ b/pkg/network/usm/sharedlibraries/watcher_test.go @@ -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" @@ -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" ) @@ -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) } } diff --git a/pkg/process/monitor/process_monitor_test.go b/pkg/process/monitor/process_monitor_test.go index b6cc085089e74..0fe9e80bd21cb 100644 --- a/pkg/process/monitor/process_monitor_test.go +++ b/pkg/process/monitor/process_monitor_test.go @@ -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" @@ -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) diff --git a/pkg/security/utils/hostname_testutil.go b/pkg/security/utils/hostname_testutil.go new file mode 100644 index 0000000000000..e8b180e1665b6 --- /dev/null +++ b/pkg/security/utils/hostname_testutil.go @@ -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() +}