Skip to content

Commit

Permalink
[USM] refactored docker-compose testutil (#31470)
Browse files Browse the repository at this point in the history
Co-authored-by: Guy Arbitman <[email protected]>
  • Loading branch information
val06 and guyarb authored Nov 27, 2024
1 parent 1c86f19 commit a768373
Show file tree
Hide file tree
Showing 21 changed files with 437 additions and 215 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@
/pkg/util/pdhutil/ @DataDog/windows-agent
/pkg/util/winutil/ @DataDog/windows-agent
/pkg/util/testutil/flake @DataDog/agent-devx-loops
/pkg/util/testutil/docker @DataDog/universal-service-monitoring @DataDog/ebpf-platform
/pkg/util/trie @DataDog/container-integrations
/pkg/languagedetection @DataDog/processes @DataDog/universal-service-monitoring
/pkg/linters/ @DataDog/agent-devx-loops
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ import (
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/servicediscovery/model"
"github.com/DataDog/datadog-agent/pkg/network"
"github.com/DataDog/datadog-agent/pkg/network/protocols/http/testutil"
protocolUtils "github.com/DataDog/datadog-agent/pkg/network/protocols/testutil"
"github.com/DataDog/datadog-agent/pkg/network/protocols/tls/nodejs"
fileopener "github.com/DataDog/datadog-agent/pkg/network/usm/sharedlibraries/testutil"
usmtestutil "github.com/DataDog/datadog-agent/pkg/network/usm/testutil"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
"github.com/DataDog/datadog-agent/pkg/util/kernel"
dockerutils "github.com/DataDog/datadog-agent/pkg/util/testutil/docker"
)

func setupDiscoveryModule(t *testing.T) string {
Expand Down Expand Up @@ -772,10 +772,13 @@ func TestDocker(t *testing.T) {
url := setupDiscoveryModule(t)

dir, _ := testutil.CurDir()
err := protocolUtils.RunDockerServer(t, "foo-server",
dir+"/testdata/docker-compose.yml", []string{},
dockerCfg := dockerutils.NewComposeConfig("foo-server",
dockerutils.DefaultTimeout,
dockerutils.DefaultRetries,
regexp.MustCompile("Serving.*"),
protocolUtils.DefaultTimeout, 3)
dockerutils.EmptyEnv,
filepath.Join(dir, "testdata", "docker-compose.yml"))
err := dockerutils.Run(t, dockerCfg)
require.NoError(t, err)

proc, err := procfs.NewDefaultFS()
Expand Down
29 changes: 0 additions & 29 deletions pkg/gpu/testutil/docker.go

This file was deleted.

6 changes: 3 additions & 3 deletions pkg/gpu/testutil/samplebins.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"github.com/stretchr/testify/require"

"github.com/DataDog/datadog-agent/pkg/network/protocols/http/testutil"
prototestutil "github.com/DataDog/datadog-agent/pkg/network/protocols/testutil"
"github.com/DataDog/datadog-agent/pkg/security/utils"
"github.com/DataDog/datadog-agent/pkg/util/log"
dockerutils "github.com/DataDog/datadog-agent/pkg/util/testutil/docker"
)

// SampleName represents the name of the sample binary.
Expand Down Expand Up @@ -177,12 +177,12 @@ func RunSampleInDockerWithArgs(t *testing.T, name SampleName, image DockerImage,
var err error
// The docker container might take a bit to start, so we retry until we get the PID
require.EventuallyWithT(t, func(c *assert.CollectT) {
dockerPID, err = prototestutil.GetDockerPID(containerName)
dockerPID, err = dockerutils.GetMainPID(containerName)
assert.NoError(c, err)
}, 1*time.Second, 100*time.Millisecond, "failed to get docker PID")

require.EventuallyWithT(t, func(c *assert.CollectT) {
dockerContainerID, err = GetDockerContainerID(containerName)
dockerContainerID, err = dockerutils.GetContainerID(containerName)
assert.NoError(c, err)
}, 1*time.Second, 100*time.Millisecond, "failed to get docker container ID")

Expand Down
10 changes: 9 additions & 1 deletion pkg/network/protocols/amqp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

httpUtils "github.com/DataDog/datadog-agent/pkg/network/protocols/http/testutil"
protocolsUtils "github.com/DataDog/datadog-agent/pkg/network/protocols/testutil"
dockerutils "github.com/DataDog/datadog-agent/pkg/util/testutil/docker"
)

const (
Expand Down Expand Up @@ -44,7 +45,14 @@ func RunServer(t testing.TB, serverAddr, serverPort string, enableTLS bool) erro
startupRegexp := startupRegexpGenerators[enableTLS](t, serverPort)

dir, _ := httpUtils.CurDir()
return protocolsUtils.RunDockerServer(t, "amqp", dir+"/testdata/docker-compose.yml", env, startupRegexp, protocolsUtils.DefaultTimeout, 3)

dockerCfg := dockerutils.NewComposeConfig("amqp",
dockerutils.DefaultTimeout,
dockerutils.DefaultRetries,
startupRegexp,
env,
filepath.Join(dir, "testdata", "docker-compose.yml"))
return dockerutils.Run(t, dockerCfg)
}

// getServerEnv returns the environment to configure the amqp server
Expand Down
11 changes: 8 additions & 3 deletions pkg/network/protocols/kafka/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import (
"path/filepath"
"regexp"
"testing"
"time"

"github.com/DataDog/datadog-agent/pkg/network/protocols/http/testutil"
protocolsUtils "github.com/DataDog/datadog-agent/pkg/network/protocols/testutil"
dockerutils "github.com/DataDog/datadog-agent/pkg/util/testutil/docker"
)

// RunServer runs a kafka server in a docker container
Expand All @@ -41,5 +40,11 @@ func RunServer(t testing.TB, serverAddr, serverPort string) error {
return err
}

return protocolsUtils.RunDockerServer(t, "kafka", dir+"/testdata/docker-compose.yml", env, regexp.MustCompile(`.*started \(kafka.server.KafkaServer\).*`), 1*time.Minute, 3)
dockerCfg := dockerutils.NewComposeConfig("kafka",
dockerutils.DefaultTimeout,
dockerutils.DefaultRetries,
regexp.MustCompile(`.*started \(kafka.server.KafkaServer\).*`),
env,
filepath.Join(dir, "testdata", "docker-compose.yml"))
return dockerutils.Run(t, dockerCfg)
}
11 changes: 9 additions & 2 deletions pkg/network/protocols/mongo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ package mongo

import (
"fmt"
"path/filepath"
"regexp"
"testing"

"github.com/DataDog/datadog-agent/pkg/network/protocols/http/testutil"
protocolsUtils "github.com/DataDog/datadog-agent/pkg/network/protocols/testutil"
dockerutils "github.com/DataDog/datadog-agent/pkg/util/testutil/docker"
)

const (
Expand All @@ -30,5 +31,11 @@ func RunServer(t testing.TB, serverAddress, serverPort string) error {
"MONGO_PASSWORD=" + Pass,
}
dir, _ := testutil.CurDir()
return protocolsUtils.RunDockerServer(t, "mongo", dir+"/testdata/docker-compose.yml", env, regexp.MustCompile(fmt.Sprintf(".*Waiting for connections.*port.*:%s.*", serverPort)), protocolsUtils.DefaultTimeout, 3)
dockerCfg := dockerutils.NewComposeConfig("mongo",
dockerutils.DefaultTimeout,
dockerutils.DefaultRetries,
regexp.MustCompile(fmt.Sprintf(".*Waiting for connections.*port.*:%s.*", serverPort)),
env,
filepath.Join(dir, "testdata", "docker-compose.yml"))
return dockerutils.Run(t, dockerCfg)
}
10 changes: 8 additions & 2 deletions pkg/network/protocols/mysql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/DataDog/datadog-agent/pkg/network/protocols/http/testutil"
protocolsUtils "github.com/DataDog/datadog-agent/pkg/network/protocols/testutil"
dockerutils "github.com/DataDog/datadog-agent/pkg/util/testutil/docker"
)

const (
Expand Down Expand Up @@ -45,5 +45,11 @@ func RunServer(t testing.TB, serverAddr, serverPort string, withTLS bool) error
env = append(env, "MYSQL_TLS_ARGS=--require-secure-transport --ssl-cert=/mysql-test/cert.pem.0 --ssl-key=/mysql-test/server.key")
}

return protocolsUtils.RunDockerServer(t, "MYSQL", dir+"/testdata/docker-compose.yml", env, regexp.MustCompile(fmt.Sprintf(".*ready for connections.*port: %s.*", serverPort)), protocolsUtils.DefaultTimeout, 3)
dockerCfg := dockerutils.NewComposeConfig("MYSQL",
dockerutils.DefaultTimeout,
dockerutils.DefaultRetries,
regexp.MustCompile(fmt.Sprintf(".*ready for connections.*port: %s.*", serverPort)),
env,
filepath.Join(dir, "testdata", "docker-compose.yml"))
return dockerutils.Run(t, dockerCfg)
}
11 changes: 8 additions & 3 deletions pkg/network/protocols/postgres/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/DataDog/datadog-agent/pkg/network/protocols/http/testutil"
protocolsUtils "github.com/DataDog/datadog-agent/pkg/network/protocols/testutil"
dockerutils "github.com/DataDog/datadog-agent/pkg/util/testutil/docker"
)

// RunServer runs a postgres server in a docker container
Expand Down Expand Up @@ -48,8 +48,13 @@ func RunServer(t testing.TB, serverAddr, serverPort string, enableTLS bool) erro
"ENCRYPTION_MODE=" + encryptionMode,
"TESTDIR=" + testDataDir,
}

return protocolsUtils.RunDockerServer(t, "postgres", filepath.Join(testDataDir, "docker-compose.yml"), env, regexp.MustCompile(fmt.Sprintf(".*listening on IPv4 address \"0.0.0.0\", port %s", serverPort)), protocolsUtils.DefaultTimeout, 3)
dockerCfg := dockerutils.NewComposeConfig("postgres",
dockerutils.DefaultTimeout,
dockerutils.DefaultRetries,
regexp.MustCompile(fmt.Sprintf(".*listening on IPv4 address \"0.0.0.0\", port %s", serverPort)),
env,
filepath.Join(testDataDir, "docker-compose.yml"))
return dockerutils.Run(t, dockerCfg)
}

// copyFile copies a file from src to dst
Expand Down
10 changes: 8 additions & 2 deletions pkg/network/protocols/redis/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/DataDog/datadog-agent/pkg/network/protocols/http/testutil"
protocolsUtils "github.com/DataDog/datadog-agent/pkg/network/protocols/testutil"
dockerutils "github.com/DataDog/datadog-agent/pkg/util/testutil/docker"
)

// RunServer runs a Redis server in a docker container
Expand All @@ -42,5 +42,11 @@ func RunServer(t testing.TB, serverAddr, serverPort string, enableTLS bool) erro
env = append(env, args)
}

return protocolsUtils.RunDockerServer(t, "redis", dir+"/testdata/docker-compose.yml", env, regexp.MustCompile(".*Ready to accept connections"), protocolsUtils.DefaultTimeout, 3)
dockerCfg := dockerutils.NewComposeConfig("redis",
dockerutils.DefaultTimeout,
dockerutils.DefaultRetries,
regexp.MustCompile(".*Ready to accept connections"),
env,
filepath.Join(dir, "testdata", "docker-compose.yml"))
return dockerutils.Run(t, dockerCfg)
}
1 change: 1 addition & 0 deletions pkg/network/protocols/testutil/pcaputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

//go:build linux

// Package testutil provides general utilities for protocols UTs.
package testutil

import (
Expand Down
Loading

0 comments on commit a768373

Please sign in to comment.