pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/DataDog/datadog-agent/commit/2d4c8db19ba1ef2c81169e50f982e7651eb7f4ee

.css" /> test(discovery): run discovery tests against both Go module and sd-ag… · DataDog/datadog-agent@2d4c8db · GitHub
Skip to content

Commit 2d4c8db

Browse files
authored
test(discovery): run discovery tests against both Go module and sd-agent (#47622)
### What does this PR do? Refactors discovery module tests to run against both the Go discovery module and the Rust sd-agent binary. Tests are converted from standalone test functions into a `discoveryTestSuite` (using testify's suite package), which is parameterized with a setup function for each implementation. This ensures both the Go and Rust discovery modules are exercised by the same test cases. Key changes: - Rename `setupDiscoveryModule` to `setupGoDiscoveryModule` and add `setupRustDiscoveryModule` which starts the sd-agent binary and communicates via Unix socket - Introduce `discoveryTestSuite` struct and `TestDiscovery` entry point that runs the suite for both Go and Rust - Convert all `TestServices*` functions to suite methods so they run against both implementations - Thread `*testDiscoveryModule` (including its HTTP client) through `getServices` and `makeRequest` instead of passing a bare URL - Remove the standalone `TestRustBinary` smoke test (now redundant since real tests run against sd-agent) - Keep `TestIgnoreComm` as a standalone test since ignore-comms is Go-module-only ### Motivation The Rust sd-agent is intended to be a drop-in replacement for the Go discovery module. Running the same test suite against both implementations ensures behavioral parity and catches regressions in either. ### Describe how you validated your changes Ran the test suite locally with both Go and Rust discovery modules. ### Additional Notes The `TestIgnoreComm` test remains standalone because the ignore-comms feature is currently only implemented in the Go discovery module. Co-authored-by: vincent.whitchurch <vincent.whitchurch@datadoghq.com>
1 parent a739c72 commit 2d4c8db

File tree

3 files changed

+136
-80
lines changed

3 files changed

+136
-80
lines changed

pkg/discovery/module/comm_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ const (
3232
// TestIgnoreComm checks that the 'sshd' command is ignored and the 'node' command is not
3333
func TestIgnoreComm(t *testing.T) {
3434
serverDir := buildFakeServer(t)
35-
discovery := setupDiscoveryModule(t)
35+
// The ignore comms feature is currently only supported for the Go discovery module.
36+
discovery := setupGoDiscoveryModule(t)
3637

3738
ctx, cancel := context.WithCancel(context.Background())
3839
t.Cleanup(func() { cancel() })
@@ -53,7 +54,7 @@ func TestIgnoreComm(t *testing.T) {
5354

5455
seen := make(map[int]model.Service)
5556
require.EventuallyWithT(t, func(collect *assert.CollectT) {
56-
resp := getServices(collect, discovery.url)
57+
resp := getServices(collect, discovery)
5758
for _, s := range resp.Services {
5859
seen[s.PID] = s
5960
}

pkg/discovery/module/impl_linux_test.go

Lines changed: 82 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ import (
2323
"strings"
2424
"syscall"
2525
"testing"
26+
"time"
2627

2728
gorillamux "github.com/gorilla/mux"
2829
"github.com/prometheus/procfs"
2930
"github.com/shirou/gopsutil/v4/process"
3031
"github.com/stretchr/testify/require"
32+
"github.com/stretchr/testify/suite"
3133
"golang.org/x/sys/unix"
3234

3335
"github.com/DataDog/datadog-agent/pkg/discovery/core"
@@ -36,6 +38,7 @@ import (
3638
"github.com/DataDog/datadog-agent/pkg/network"
3739
"github.com/DataDog/datadog-agent/pkg/network/protocols/http/testutil"
3840
usmtestutil "github.com/DataDog/datadog-agent/pkg/network/usm/testutil"
41+
spclient "github.com/DataDog/datadog-agent/pkg/system-probe/api/client"
3942
"github.com/DataDog/datadog-agent/pkg/system-probe/api/module"
4043
"github.com/DataDog/datadog-agent/pkg/system-probe/config"
4144
"github.com/DataDog/datadog-agent/pkg/util/kernel"
@@ -52,11 +55,13 @@ func findService(pid int, services []model.Service) *model.Service {
5255
}
5356

5457
type testDiscoveryModule struct {
55-
url string
58+
url string
59+
client *http.Client
5660
}
5761

58-
func setupDiscoveryModule(t *testing.T) *testDiscoveryModule {
62+
func setupGoDiscoveryModule(t *testing.T) *testDiscoveryModule {
5963
t.Helper()
64+
6065
mux := gorillamux.NewRouter()
6166

6267
mod, err := NewDiscoveryModule(nil, module.FactoryDependencies{})
@@ -70,13 +75,85 @@ func setupDiscoveryModule(t *testing.T) *testDiscoveryModule {
7075
t.Cleanup(srv.Close)
7176

7277
return &testDiscoveryModule{
73-
url: srv.URL,
78+
url: srv.URL,
79+
client: http.DefaultClient,
80+
}
81+
}
82+
83+
func setupRustDiscoveryModule(t *testing.T) *testDiscoveryModule {
84+
t.Helper()
85+
86+
// Skip on CentOS 7 due to Rust binary not being statically linked
87+
platform, err := kernel.Platform()
88+
require.NoError(t, err)
89+
platformVersion, err := kernel.PlatformVersion()
90+
require.NoError(t, err)
91+
92+
if platform == "centos" && strings.HasPrefix(platformVersion, "7") {
93+
t.Skip("Skipping Rust binary test on CentOS 7 due to glibc compatibility issues with non-static binary")
94+
}
95+
96+
curDir, err := testutil.CurDir()
97+
require.NoError(t, err)
98+
binaryPath := filepath.Join(curDir, "rust", "embedded", "bin", "system-probe-lite")
99+
require.FileExists(t, binaryPath, "system-probe-lite binary should be built")
100+
101+
socketDir := t.TempDir()
102+
socketPath := filepath.Join(socketDir, "sysprobe.sock")
103+
104+
ctx, cancel := context.WithCancel(context.Background())
105+
cmd := exec.CommandContext(ctx, binaryPath, "--", "/bin/true", "-c", "/dev/null")
106+
cmd.Env = append(os.Environ(),
107+
"DD_DISCOVERY_ENABLED=true",
108+
"DD_DISCOVERY_USE_SYSTEM_PROBE_LITE=true",
109+
"DD_SYSTEM_PROBE_CONFIG_SYSPROBE_SOCKET="+socketPath,
110+
)
111+
cmd.Stdout = os.Stdout
112+
cmd.Stderr = os.Stderr
113+
require.NoError(t, cmd.Start())
114+
t.Cleanup(func() {
115+
cancel()
116+
_ = cmd.Wait()
117+
})
118+
119+
require.Eventually(t, func() bool {
120+
_, err := os.Stat(socketPath)
121+
return err == nil
122+
}, 10*time.Second, 50*time.Millisecond, "system-probe-lite socket did not appear")
123+
124+
return &testDiscoveryModule{
125+
url: "http://sysprobe",
126+
client: &http.Client{
127+
Timeout: 10 * time.Second,
128+
Transport: &http.Transport{
129+
DialContext: spclient.DialContextFunc(socketPath),
130+
},
131+
},
74132
}
75133
}
76134

135+
type discoveryTestSuite struct {
136+
suite.Suite
137+
setupModule func(t *testing.T) *testDiscoveryModule
138+
discovery *testDiscoveryModule
139+
}
140+
141+
func (s *discoveryTestSuite) SetupTest() {
142+
s.discovery = s.setupModule(s.T())
143+
}
144+
145+
func TestDiscovery(t *testing.T) {
146+
t.Run("go", func(t *testing.T) {
147+
suite.Run(t, &discoveryTestSuite{setupModule: setupGoDiscoveryModule})
148+
})
149+
t.Run("rust", func(t *testing.T) {
150+
suite.Run(t, &discoveryTestSuite{setupModule: setupRustDiscoveryModule})
151+
})
152+
}
153+
77154
// makeRequest wraps the request to the discovery module, setting the JSON body if provided,
78155
// and returning the response as the given type.
79-
func makeRequest[T any](t require.TestingT, url string, params *core.Params) *T {
156+
func makeRequest[T any](t require.TestingT, client *http.Client, url string, params *core.Params) *T {
80157
var body *bytes.Buffer
81158
if params != nil {
82159
jsonData, err := params.ToJSON()
@@ -94,7 +171,7 @@ func makeRequest[T any](t require.TestingT, url string, params *core.Params) *T
94171
}
95172
require.NoError(t, err, "failed to create request")
96173

97-
resp, err := http.DefaultClient.Do(req)
174+
resp, err := client.Do(req)
98175
require.NoError(t, err, "failed to send request")
99176
defer resp.Body.Close()
100177

@@ -418,38 +495,3 @@ ffffb7360000-ffffb74ec000 r-xp 00000000 00:22 13920 /opt/ot
418495
})
419496
}
420497
}
421-
422-
func TestRustBinary(t *testing.T) {
423-
// Skip on CentOS 7 due to Rust binary not being statically linked
424-
platform, err := kernel.Platform()
425-
require.NoError(t, err)
426-
platformVersion, err := kernel.PlatformVersion()
427-
require.NoError(t, err)
428-
429-
if platform == "centos" && strings.HasPrefix(platformVersion, "7") {
430-
t.Skip("Skipping Rust binary test on CentOS 7 due to glibc compatibility issues with non-static binary")
431-
}
432-
433-
curDir, err := testutil.CurDir()
434-
require.NoError(t, err)
435-
436-
binaryPath := filepath.Join(curDir, "rust", "embedded", "bin", "system-probe-lite")
437-
438-
require.FileExists(t, binaryPath, "Rust binary should be built")
439-
440-
truePath := "/bin/true"
441-
if _, err := os.Stat(truePath); os.IsNotExist(err) {
442-
truePath = "/usr/bin/true"
443-
}
444-
445-
env := os.Environ()
446-
env = append(env, "DD_DISCOVERY_USE_SYSTEM_PROBE_LITE=true")
447-
env = append(env, "DD_DISCOVERY_ENABLED=false")
448-
// Fake system-probe binary with empty configuration file
449-
cmd := exec.Command(binaryPath, "--", truePath, "-c", "/dev/null")
450-
cmd.Env = env
451-
output, err := cmd.CombinedOutput()
452-
require.NoError(t, err, "Rust binary should execute successfully")
453-
require.Contains(t, string(output), "Discovery is disabled")
454-
require.Equal(t, 0, cmd.ProcessState.ExitCode(), "Binary should exit with code 0", string(output))
455-
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy