Skip to content

Commit

Permalink
fix tests and golangci-lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Oct 22, 2024
1 parent ef09a09 commit d30fc18
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (

"github.com/DataDog/datadog-agent/comp/core"
"github.com/DataDog/datadog-agent/comp/core/status"
"github.com/DataDog/datadog-agent/comp/core/tagger"
"github.com/DataDog/datadog-agent/comp/core/tagger/taggerimpl"
"github.com/DataDog/datadog-agent/comp/forwarder/defaultforwarder"
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform/eventplatformimpl"
"github.com/DataDog/datadog-agent/comp/forwarder/orchestrator/orchestratorimpl"
Expand Down Expand Up @@ -53,12 +55,17 @@ func TestStatusOutPut(t *testing.T) {
}},
}

mockTagger := taggerimpl.SetupFakeTagger(t)

deps := fxutil.Test[dependencies](t, fx.Options(
core.MockBundle(),
compressionimpl.MockModule(),
defaultforwarder.MockModule(),
orchestratorimpl.MockModule(),
eventplatformimpl.MockModule(),
fx.Provide(func() tagger.Component {
return mockTagger
}),
fx.Supply(
Params{
continueOnMissingHostname: true,
Expand Down
3 changes: 3 additions & 0 deletions comp/core/flare/flare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
log "github.com/DataDog/datadog-agent/comp/core/log/def"
logmock "github.com/DataDog/datadog-agent/comp/core/log/mock"
"github.com/DataDog/datadog-agent/comp/core/secrets/secretsimpl"
nooptagger "github.com/DataDog/datadog-agent/comp/core/tagger/noopimpl"
nooptelemetry "github.com/DataDog/datadog-agent/comp/core/telemetry/noopsimpl"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
Expand Down Expand Up @@ -54,6 +55,7 @@ func TestFlareCreation(t *testing.T) {
func() types.FlareCallback { return realProvider },
fx.ResultTags(`group:"flare"`),
)),
nooptagger.Module(),
),
)

Expand All @@ -75,6 +77,7 @@ func TestRunProviders(t *testing.T) {
fx.Provide(func() diagnosesendermanager.Component { return nil }),
fx.Provide(func() Params { return Params{} }),
collector.NoneModule(),
nooptagger.Module(),
fx.Supply(optional.NewNoneOption[workloadmeta.Component]()),
fx.Supply(optional.NewNoneOption[autodiscovery.Component]()),
// provider a nil FlareCallback
Expand Down
20 changes: 10 additions & 10 deletions pkg/aggregator/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
orchestratorforwarder "github.com/DataDog/datadog-agent/comp/forwarder/orchestrator"
"github.com/DataDog/datadog-agent/comp/serializer/compression/compressionimpl"
checkid "github.com/DataDog/datadog-agent/pkg/collector/check/id"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
configmock "github.com/DataDog/datadog-agent/pkg/config/mock"
"github.com/DataDog/datadog-agent/pkg/metrics"
"github.com/DataDog/datadog-agent/pkg/metrics/event"
"github.com/DataDog/datadog-agent/pkg/metrics/servicecheck"
Expand Down Expand Up @@ -581,9 +581,12 @@ func TestTags(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer pkgconfigsetup.Datadog().SetWithoutSource("basic_telemetry_add_container_tags", nil)
pkgconfigsetup.Datadog().SetWithoutSource("basic_telemetry_add_container_tags", tt.tlmContainerTagsEnabled)
mockConfig := configmock.New(t)
mockConfig.SetWithoutSource("basic_telemetry_add_container_tags", tt.tlmContainerTagsEnabled)

taggerComponent := fxutil.Test[tagger.Mock](t, taggerimpl.MockModule())
defer taggerComponent.ResetTagger()

agg := NewBufferedAggregator(nil, nil, taggerComponent, tt.hostname, time.Second)
agg.agentTags = tt.agentTags
agg.globalTags = tt.globalTags
Expand All @@ -593,9 +596,8 @@ func TestTags(t *testing.T) {
}

func TestTimeSamplerFlush(t *testing.T) {
pc := pkgconfigsetup.Datadog().GetInt("dogstatsd_pipeline_count")
pkgconfigsetup.Datadog().SetWithoutSource("dogstatsd_pipeline_count", 1)
defer pkgconfigsetup.Datadog().SetWithoutSource("dogstatsd_pipeline_count", pc)
mockConfig := configmock.New(t)
mockConfig.SetWithoutSource("dogstatsd_pipeline_count", 1)

s := &MockSerializerIterableSerie{}
s.On("AreSeriesEnabled").Return(true)
Expand All @@ -613,10 +615,8 @@ func TestTimeSamplerFlush(t *testing.T) {
func TestAddDJMRecurrentSeries(t *testing.T) {
// this test IS USING globals (recurrentSeries)
// -

djmEnabled := pkgconfigsetup.Datadog().GetBool("djm_config.enabled")
pkgconfigsetup.Datadog().SetWithoutSource("djm_config.enabled", true)
defer pkgconfigsetup.Datadog().SetWithoutSource("djm_config.enabled", djmEnabled)
mockConfig := configmock.New(t)
mockConfig.SetWithoutSource("djm_config.enabled", true)

s := &MockSerializerIterableSerie{}
// NewBufferedAggregator with DJM enable will create a new recurrentSeries
Expand Down
2 changes: 0 additions & 2 deletions pkg/aggregator/mocksender/mocksender.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform/eventplatformimpl"
"github.com/DataDog/datadog-agent/comp/serializer/compression/compressionimpl"

//nolint:revive // TODO(AML) Fix revive linter

nooptagger "github.com/DataDog/datadog-agent/comp/core/tagger/noopimpl"
"github.com/DataDog/datadog-agent/pkg/aggregator"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type TaskHandlers struct {
tagger tagger.Component
}

// NewTaskHandlers returns a new TaskHandlers.
func NewTaskHandlers(tagger tagger.Component) *TaskHandlers {
return &TaskHandlers{
tagger: tagger,
Expand Down
5 changes: 4 additions & 1 deletion pkg/collector/corechecks/containers/docker/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ func TestDockerCustomPart(t *testing.T) {
Enabled: true,
NameExcludeList: []*regexp.Regexp{regexp.MustCompile("agent-excluded")},
},
store: fxutil.Test[workloadmetamock.Mock](t, core.MockBundle(), workloadmetafxmock.MockModule(workloadmeta.NewParams())),
store: fxutil.Test[workloadmetamock.Mock](t, core.MockBundle(), workloadmetafxmock.MockModule(workloadmeta.NewParams())),
tagger: fakeTagger,
}

err := check.runDockerCustom(mockSender, &dockerClient, dockerClient.FakeContainerList)
Expand Down Expand Up @@ -290,6 +291,7 @@ func TestContainersRunning(t *testing.T) {
dockerHostname: "testhostname",
containerFilter: &containers.Filter{},
store: fxutil.Test[workloadmetamock.Mock](t, core.MockBundle(), workloadmetafxmock.MockModule(workloadmeta.NewParams())),
tagger: fakeTagger,
}

err := check.runDockerCustom(mockSender, &dockerClient, dockerClient.FakeContainerList)
Expand Down Expand Up @@ -345,6 +347,7 @@ func TestProcess_CPUSharesMetric(t *testing.T) {
instance: &DockerConfig{},
processor: *processor,
dockerHostname: "testhostname",
tagger: fakeTagger,
}

err := check.runProcessor(mockSender)
Expand Down
9 changes: 9 additions & 0 deletions pkg/collector/corechecks/containers/docker/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ import (
)

func TestReportExitCodes(t *testing.T) {
fakeTagger := taggerimpl.SetupFakeTagger(t)
defer fakeTagger.ResetTagger()

dockerCheck := &DockerCheck{
instance: &DockerConfig{},
tagger: fakeTagger,
}

dockerCheck.setOkExitCodes()
Expand Down Expand Up @@ -88,6 +92,7 @@ func TestReportExitCodes(t *testing.T) {
instance: &DockerConfig{
OkExitCodes: []int{0},
},
tagger: fakeTagger,
}

dockerCheck.setOkExitCodes()
Expand Down Expand Up @@ -154,6 +159,7 @@ func TestAggregateEvents(t *testing.T) {
"unfiltered_action": 1,
},
alertType: event.AlertTypeInfo,
tagger: fakeTagger,
},
},
},
Expand Down Expand Up @@ -193,6 +199,7 @@ func TestAggregateEvents(t *testing.T) {
"other_action": 1,
},
alertType: event.AlertTypeInfo,
tagger: fakeTagger,
},
},
},
Expand Down Expand Up @@ -225,13 +232,15 @@ func TestAggregateEvents(t *testing.T) {
"other_action": 1,
},
alertType: event.AlertTypeInfo,
tagger: fakeTagger,
},
"other_image": {
imageName: "other_image",
countByAction: map[events.Action]int{
"other_action": 1,
},
alertType: event.AlertTypeInfo,
tagger: fakeTagger,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (suite *ProviderTestSuite) SetupTest() {
NameExcludeList: []*regexp.Regexp{regexp.MustCompile("agent-excluded")},
},
podUtils: common.NewPodUtils(fakeTagger),
tagger: fakeTagger,
}
}

Expand Down
13 changes: 10 additions & 3 deletions pkg/collector/corechecks/orchestrator/ecs/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"go.uber.org/atomic"

"github.com/DataDog/agent-payload/v5/process"

nooptagger "github.com/DataDog/datadog-agent/comp/core/tagger/noopimpl"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/aggregator/mocksender"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/ecs"
Expand Down Expand Up @@ -91,7 +93,7 @@ func (s *fakeSender) Flush() []process.MessageBody {
}

func TestNotECS(t *testing.T) {
check, _, sender := prepareTest(false, "notECS")
check, _, sender := prepareTest(t, false, "notECS")
err := check.Run()
require.NoError(t, err)
require.Len(t, sender.messages, 0)
Expand All @@ -107,7 +109,7 @@ func TestECSV4Disabled(t *testing.T) {
}

func testECS(v4 bool, t *testing.T) {
check, store, sender := prepareTest(v4, "ecs")
check, store, sender := prepareTest(t, v4, "ecs")

// add 2 tasks to fake workloadmetaStore
task1Id := "123"
Expand Down Expand Up @@ -151,7 +153,9 @@ func testECS(v4 bool, t *testing.T) {
}

// prepareTest returns a check, a fake workloadmeta store and a fake sender
func prepareTest(v4 bool, env string) (*Check, *fakeWorkloadmetaStore, *fakeSender) {
func prepareTest(t *testing.T, v4 bool, env string) (*Check, *fakeWorkloadmetaStore, *fakeSender) {
t.Helper()

orchConfig := oconfig.NewDefaultOrchestratorConfig()
orchConfig.OrchestrationCollectionEnabled = true

Expand All @@ -162,9 +166,12 @@ func prepareTest(v4 bool, env string) (*Check, *fakeWorkloadmetaStore, *fakeSend

systemInfo, _ := checks.CollectSystemInfo()

tagger := nooptagger.NewTaggerClient()

c := &Check{
sender: sender,
workloadmetaStore: store,
tagger: tagger,
config: orchConfig,
groupID: atomic.NewInt32(0),
systemInfo: systemInfo,
Expand Down
1 change: 1 addition & 0 deletions pkg/collector/corechecks/orchestrator/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func (suite *PodTestSuite) SetupSuite() {
))

fakeTagger := taggerimpl.SetupFakeTagger(suite.T())
suite.tagger = fakeTagger

suite.check = &Check{
sender: sender,
Expand Down
6 changes: 4 additions & 2 deletions pkg/collector/python/test_aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ func testSubmitEventPlatformEvent(t *testing.T) {
sender.AssertEventPlatformEvent(t, []byte("raw-event"), "dbm-sample")
}

func scopeInitCheckContext(senderManager sender.SenderManager, logReceiver optional.Option[integrations.Component], tagger tagger.Component) func() {
initializeCheckContext(senderManager, logReceiver, tagger)
func scopeInitCheckContext(senderManager sender.SenderManager, logReceiver optional.Option[integrations.Component], taggerComp tagger.Component) func() {
// Ensure the check context is released before initializing a new one
releaseCheckContext()
initializeCheckContext(senderManager, logReceiver, taggerComp)
return releaseCheckContext
}
1 change: 1 addition & 0 deletions pkg/diagnose/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ func NewSuitesDeps(
SecretResolver: secretResolver,
WMeta: wmeta,
AC: ac,
Tagger: tagger,
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/serverless/metrics/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func TestRaceFlushVersusAddSample(t *testing.T) {
}
metricAgent := &ServerlessMetricAgent{
SketchesBucketOffset: time.Second * 10,
Tagger: nooptagger.NewTaggerClient(),
}
defer metricAgent.Stop()
metricAgent.Start(10*time.Second, &ValidMetricConfigMocked{}, &MetricDogStatsD{})
Expand Down
16 changes: 8 additions & 8 deletions test/integration/corechecks/docker/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestMain(m *testing.M) {
var lastRunResult int
var retryCount int

store, err := setup()
store, taggerComp, err := setup()
if err != nil {
log.Infof("Test setup failed: %v", err)
tearOffAndExit(1)
Expand All @@ -98,7 +98,7 @@ func TestMain(m *testing.M) {
case <-retryTicker.C:
retryCount++
log.Infof("Starting run %d", retryCount)
lastRunResult = doRun(m, store)
lastRunResult = doRun(m, store, taggerComp)
if lastRunResult == 0 {
tearOffAndExit(0)
}
Expand All @@ -116,12 +116,12 @@ type testDeps struct {
}

// Called before for first test run: compose up
func setup() (workloadmeta.Component, error) {
func setup() (workloadmeta.Component, tagger.Component, error) {
// Setup global conf
pkgconfigsetup.Datadog().SetConfigType("yaml")
err := pkgconfigsetup.Datadog().ReadConfig(strings.NewReader(datadogCfgString))
if err != nil {
return nil, err
return nil, nil, err
}
env.SetFeaturesNoCleanup(env.Docker)

Expand Down Expand Up @@ -151,15 +151,15 @@ func setup() (workloadmeta.Component, error) {
output, err := compose.Start()
if err != nil {
log.Errorf("Compose didn't start properly: %s", string(output))
return nil, err
return nil, nil, err
}
}
return store, nil
return store, deps.TaggerComp, nil
}

// Reset the state and trigger a new run
func doRun(m *testing.M, store workloadmeta.Component) int {
factory := docker.Factory(store)
func doRun(m *testing.M, store workloadmeta.Component, tagger tagger.Component) int {
factory := docker.Factory(store, tagger)
checkFactory, _ := factory.Get()
dockerCheck = checkFactory()

Expand Down

0 comments on commit d30fc18

Please sign in to comment.