Skip to content

Commit

Permalink
Fix fx test imports
Browse files Browse the repository at this point in the history
  • Loading branch information
gabedos committed Jan 10, 2025
1 parent c4a12d9 commit 22e1e1c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion comp/core/tagger/mock/fake_tagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Dependencies struct {

// Provides is a struct containing the mock
type Provides struct {
Comp tagger.Component
Comp Mock
}

// New instantiates a new fake tagger
Expand Down
10 changes: 9 additions & 1 deletion comp/core/tagger/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ package mock
import (
"testing"

"go.uber.org/fx"

"github.com/DataDog/datadog-agent/comp/core/config"
tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
)

Expand All @@ -23,5 +27,9 @@ func Module() fxutil.Module {

// SetupFakeTagger calls fxutil.Test to create a mock tagger for testing
func SetupFakeTagger(t testing.TB) Mock {
return fxutil.Test[Mock](t, Module())
return fxutil.Test[Mock](t,
config.MockModule(),
fx.Provide(func(mock Mock) tagger.Component { return mock }),
Module(),
)
}
4 changes: 3 additions & 1 deletion comp/otelcol/otlp/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func TestStartPipeline(t *testing.T) {
}

func TestStartPipelineFromConfig(t *testing.T) {
fakeTagger := mock.SetupFakeTagger(t)

pkgconfigsetup.Datadog().SetWithoutSource("hostname", "otlp-testhostname")
defer pkgconfigsetup.Datadog().SetWithoutSource("hostname", "")

Expand All @@ -101,7 +103,7 @@ func TestStartPipelineFromConfig(t *testing.T) {
t.Run(testInstance.path, func(t *testing.T) {
cfg, err := testutil.LoadConfig(t, "./testdata/"+testInstance.path)
require.NoError(t, err)
pcfg, err := FromAgentConfig(cfg)
pcfg, err := FromAgentConfig(cfg, fakeTagger)
require.NoError(t, err)
if testInstance.err == "" {
AssertSucessfulRun(t, pcfg)
Expand Down
17 changes: 13 additions & 4 deletions comp/otelcol/otlp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/DataDog/datadog-agent/comp/core/tagger/mock"
"github.com/DataDog/datadog-agent/comp/otelcol/otlp/configcheck"
"github.com/DataDog/datadog-agent/comp/otelcol/otlp/testutil"
)
Expand Down Expand Up @@ -46,6 +47,8 @@ func TestIsEnabledEnv(t *testing.T) {
}

func TestFromAgentConfigReceiver(t *testing.T) {
fakeTagger := mock.SetupFakeTagger(t)

tests := []struct {
path string
cfg PipelineConfig
Expand Down Expand Up @@ -189,7 +192,7 @@ func TestFromAgentConfigReceiver(t *testing.T) {
t.Run(testInstance.path, func(t *testing.T) {
cfg, err := testutil.LoadConfig(t, "./testdata/"+testInstance.path)
require.NoError(t, err)
pcfg, err := FromAgentConfig(cfg)
pcfg, err := FromAgentConfig(cfg, fakeTagger)
if err != nil || testInstance.err != "" {
assert.Equal(t, testInstance.err, err.Error())
return
Expand All @@ -202,6 +205,8 @@ func TestFromAgentConfigReceiver(t *testing.T) {
}

func TestFromEnvironmentVariables(t *testing.T) {
fakeTagger := mock.SetupFakeTagger(t)

tests := []struct {
name string
env map[string]string
Expand Down Expand Up @@ -458,7 +463,7 @@ func TestFromEnvironmentVariables(t *testing.T) {
}
cfg, err := testutil.LoadConfig(t, "./testdata/empty.yaml")
require.NoError(t, err)
pcfg, err := FromAgentConfig(cfg)
pcfg, err := FromAgentConfig(cfg, fakeTagger)
if err != nil || testInstance.err != "" {
assert.Equal(t, testInstance.err, err.Error())
return
Expand All @@ -471,6 +476,8 @@ func TestFromEnvironmentVariables(t *testing.T) {
}

func TestFromAgentConfigMetrics(t *testing.T) {
fakeTagger := mock.SetupFakeTagger(t)

tests := []struct {
path string
cfg PipelineConfig
Expand Down Expand Up @@ -510,7 +517,7 @@ func TestFromAgentConfigMetrics(t *testing.T) {
t.Run(testInstance.path, func(t *testing.T) {
cfg, err := testutil.LoadConfig(t, "./testdata/"+testInstance.path)
require.NoError(t, err)
pcfg, err := FromAgentConfig(cfg)
pcfg, err := FromAgentConfig(cfg, fakeTagger)
if err != nil || testInstance.err != "" {
assert.Equal(t, testInstance.err, err.Error())
return
Expand All @@ -523,6 +530,8 @@ func TestFromAgentConfigMetrics(t *testing.T) {
}

func TestFromAgentConfigDebug(t *testing.T) {
fakeTagger := mock.SetupFakeTagger(t)

tests := []struct {
path string
cfg PipelineConfig
Expand Down Expand Up @@ -606,7 +615,7 @@ func TestFromAgentConfigDebug(t *testing.T) {
t.Run(testInstance.path, func(t *testing.T) {
cfg, err := testutil.LoadConfig(t, "./testdata/"+testInstance.path)
require.NoError(t, err)
pcfg, err := FromAgentConfig(cfg)
pcfg, err := FromAgentConfig(cfg, fakeTagger)
if err != nil || testInstance.err != "" {
assert.Equal(t, testInstance.err, err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion comp/trace/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2304,7 +2304,7 @@ func buildConfigComponent(t *testing.T, setHostnameInConfig bool, coreConfigOpti
}

taggerComponent := fxutil.Test[taggermock.Mock](t,
fx.Replace(coreConfig),
corecomp.MockModule(),
taggermock.Module(),
)

Expand Down

0 comments on commit 22e1e1c

Please sign in to comment.