Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tagger/mock] Refactor to use fake impl directly and avoid unwanted dependencies #31297

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions comp/api/api/apiimpl/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ import (
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/autodiscoveryimpl"
"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/hostname/hostnameimpl"
log "github.com/DataDog/datadog-agent/comp/core/log/def"
logmock "github.com/DataDog/datadog-agent/comp/core/log/mock"
remoteagentregistry "github.com/DataDog/datadog-agent/comp/core/remoteagentregistry/def"
"github.com/DataDog/datadog-agent/comp/core/secrets/secretsimpl"
tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def"
taggermock "github.com/DataDog/datadog-agent/comp/core/tagger/mock"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
"github.com/DataDog/datadog-agent/comp/core/telemetry/telemetryimpl"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
workloadmetafxmock "github.com/DataDog/datadog-agent/comp/core/workloadmeta/fx-mock"
"github.com/DataDog/datadog-agent/comp/dogstatsd/pidmap/pidmapimpl"
replaymock "github.com/DataDog/datadog-agent/comp/dogstatsd/replay/fx-mock"
dogstatsdServer "github.com/DataDog/datadog-agent/comp/dogstatsd/server"
Expand Down Expand Up @@ -92,6 +97,10 @@ func getTestAPIServer(t *testing.T, params config.MockParams) testdeps {
}
}),
fx.Provide(func() remoteagentregistry.Component { return nil }),
telemetryimpl.MockModule(),
config.MockModule(),
workloadmetafxmock.MockModule(workloadmeta.NewParams()),
fx.Provide(func(t testing.TB) log.Component { return logmock.New(t) }),
)
}

Expand Down
9 changes: 9 additions & 0 deletions comp/api/api/apiimpl/internal/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ import (
"github.com/DataDog/datadog-agent/comp/collector/collector"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/autodiscoveryimpl"
"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/flare/flareimpl"
"github.com/DataDog/datadog-agent/comp/core/hostname/hostnameinterface"
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/telemetry/telemetryimpl"
workloadmetafx "github.com/DataDog/datadog-agent/comp/core/workloadmeta/fx"

"github.com/DataDog/datadog-agent/comp/core/secrets"
"github.com/DataDog/datadog-agent/comp/core/secrets/secretsimpl"
Expand Down Expand Up @@ -108,6 +113,10 @@ func getComponentDeps(t *testing.T) handlerdeps {
autodiscoveryimpl.MockModule(),
),
settingsimpl.MockModule(),
config.MockModule(),
fx.Provide(func(t testing.TB) log.Component { return logmock.New(t) }),
workloadmetafx.Module(workloadmeta.NewParams()),
telemetryimpl.MockModule(),
)
}

Expand Down
3 changes: 2 additions & 1 deletion comp/core/tagger/impl/tagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
log "github.com/DataDog/datadog-agent/comp/core/log/def"
taggercommon "github.com/DataDog/datadog-agent/comp/core/tagger/common"
tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def"
taggermock "github.com/DataDog/datadog-agent/comp/core/tagger/mock"
"github.com/DataDog/datadog-agent/comp/core/tagger/telemetry"
"github.com/DataDog/datadog-agent/comp/core/tagger/types"
"github.com/DataDog/datadog-agent/comp/core/tagger/utils"
Expand Down Expand Up @@ -142,7 +143,7 @@ func NewTaggerClient(params tagger.Params, cfg config.Component, wmeta workloadm
var err error
telemetryStore := telemetry.NewStore(telemetryComp)
if params.UseFakeTagger {
defaultTagger = newFakeTagger()
defaultTagger = taggermock.New().Comp
} else {
defaultTagger, err = newLocalTagger(cfg, wmeta, telemetryStore)
}
Expand Down
11 changes: 6 additions & 5 deletions comp/core/tagger/impl/tagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
log "github.com/DataDog/datadog-agent/comp/core/log/def"
logmock "github.com/DataDog/datadog-agent/comp/core/log/mock"
tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def"
"github.com/DataDog/datadog-agent/comp/core/tagger/mock"
"github.com/DataDog/datadog-agent/comp/core/tagger/types"
noopTelemetry "github.com/DataDog/datadog-agent/comp/core/telemetry/noopsimpl"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
Expand Down Expand Up @@ -67,7 +68,7 @@ func TestEnrichTags(t *testing.T) {

tagger, err := NewTaggerClient(params, c, wmeta, logComponent, noopTelemetry.GetCompatComponent())
assert.NoError(t, err)
fakeTagger := tagger.defaultTagger.(*FakeTagger)
fakeTagger := tagger.defaultTagger.(*mock.FakeTagger)

containerName, initContainerName, containerID, initContainerID, podUID := "container-name", "init-container-name", "container-id", "init-container-id", "pod-uid"

Expand Down Expand Up @@ -191,7 +192,7 @@ func TestEnrichTagsOrchestrator(t *testing.T) {
tagger, err := NewTaggerClient(params, c, wmeta, logComponent, noopTelemetry.GetCompatComponent())
assert.NoError(t, err)

fakeTagger := tagger.defaultTagger.(*FakeTagger)
fakeTagger := tagger.defaultTagger.(*mock.FakeTagger)

fakeTagger.SetTags(types.NewEntityID(types.ContainerID, "bar"), "fooSource", []string{"container-low"}, []string{"container-orch"}, nil, nil)
tb := tagset.NewHashingTagsAccumulator()
Expand All @@ -215,7 +216,7 @@ func TestEnrichTagsOptOut(t *testing.T) {

tagger, err := NewTaggerClient(params, c, wmeta, logComponent, noopTelemetry.GetCompatComponent())
assert.NoError(t, err)
fakeTagger := tagger.defaultTagger.(*FakeTagger)
fakeTagger := tagger.defaultTagger.(*mock.FakeTagger)

fakeTagger.SetTags(types.NewEntityID(types.EntityIDPrefix("foo"), "bar"), "fooSource", []string{"container-low"}, []string{"container-orch"}, nil, nil)
tb := tagset.NewHashingTagsAccumulator()
Expand Down Expand Up @@ -307,7 +308,7 @@ func TestAgentTags(t *testing.T) {

tagger, err := NewTaggerClient(params, c, wmeta, logComponent, noopTelemetry.GetCompatComponent())
assert.NoError(t, err)
fakeTagger := tagger.defaultTagger.(*FakeTagger)
fakeTagger := tagger.defaultTagger.(*mock.FakeTagger)

agentContainerID, podUID := "agentContainerID", "podUID"
mockMetricsProvider := collectormock.NewMetricsProvider()
Expand Down Expand Up @@ -346,7 +347,7 @@ func TestGlobalTags(t *testing.T) {

tagger, err := NewTaggerClient(params, c, wmeta, logComponent, noopTelemetry.GetCompatComponent())
assert.NoError(t, err)
fakeTagger := tagger.defaultTagger.(*FakeTagger)
fakeTagger := tagger.defaultTagger.(*mock.FakeTagger)
fakeTagger.SetTags(types.NewEntityID(types.ContainerID, "bar"), "fooSource", []string{"container-low"}, []string{"container-orch"}, []string{"container-high"}, nil)
fakeTagger.SetGlobalTags([]string{"global-low"}, []string{"global-orch"}, []string{"global-high"}, nil)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,54 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

package taggerimpl
package mock

import (
"context"
"net/http"
"strconv"

api "github.com/DataDog/datadog-agent/comp/api/api/def"
taggercommon "github.com/DataDog/datadog-agent/comp/core/tagger/common"
tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def"
"github.com/DataDog/datadog-agent/comp/core/tagger/tagstore"
"github.com/DataDog/datadog-agent/comp/core/tagger/telemetry"
"github.com/DataDog/datadog-agent/comp/core/tagger/types"

taggertypes "github.com/DataDog/datadog-agent/pkg/tagger/types"
"github.com/DataDog/datadog-agent/pkg/tagset"
)

// Mock implements mock-specific methods for the tagger component.
type Mock interface {
tagger.Component

// SetTags allows to set tags in the mock fake tagger
SetTags(entityID types.EntityID, source string, low, orch, high, std []string)

// SetGlobalTags allows to set tags in store for the global entity
SetGlobalTags(low, orch, high, std []string)
}

// FakeTagger is a fake implementation of the tagger interface
type FakeTagger struct {
errors map[string]error
store *tagstore.TagStore
}

func newFakeTagger() *FakeTagger {
return &FakeTagger{
errors: make(map[string]error),
store: tagstore.NewTagStore(nil),
// Provides is a struct containing the mock and the endpoint
type Provides struct {
Comp Mock
Endpoint api.AgentEndpointProvider
}

// New instantiates a new fake tagger
func New() Provides {
return Provides{
Comp: &FakeTagger{
errors: make(map[string]error),
store: tagstore.NewTagStore(nil),
},
Endpoint: api.NewAgentEndpointProvider(mockHandleRequest, "/tagger-list", "GET"),
}
}

Expand Down Expand Up @@ -170,3 +192,8 @@ func (f *FakeTagger) ChecksCardinality() types.TagCardinality {
func (f *FakeTagger) DogstatsdCardinality() types.TagCardinality {
return types.LowCardinality
}

// mockHandleRequest is a simple mocked http.Handler function to test the route is registered correctly on the api component
func mockHandleRequest(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("OK"))
}
123 changes: 1 addition & 122 deletions comp/core/tagger/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,145 +4,24 @@
// Copyright 2016-present Datadog, Inc.

//go:build test
// +build test

// Package mock contains the implementation of the mock for the tagger component.
package mock

import (
"net/http"
"testing"

"go.uber.org/fx"

"github.com/stretchr/testify/assert"

api "github.com/DataDog/datadog-agent/comp/api/api/def"
"github.com/DataDog/datadog-agent/comp/core/config"
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/sysprobeconfig/sysprobeconfigimpl"
tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def"
taggerimpl "github.com/DataDog/datadog-agent/comp/core/tagger/impl"
"github.com/DataDog/datadog-agent/comp/core/tagger/types"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
noopTelemetry "github.com/DataDog/datadog-agent/comp/core/telemetry/noopsimpl"
"github.com/DataDog/datadog-agent/comp/core/telemetry/telemetryimpl"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
workloadmetafx "github.com/DataDog/datadog-agent/comp/core/workloadmeta/fx"
configmock "github.com/DataDog/datadog-agent/pkg/config/mock"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
)

// Mock implements mock-specific methods for the tagger component.
type Mock interface {
tagger.Component

// SetTags allows to set tags in the mock fake tagger
SetTags(entityID types.EntityID, source string, low, orch, high, std []string)

// SetGlobalTags allows to set tags in store for the global entity
SetGlobalTags(low, orch, high, std []string)
}

// mockTaggerClient is a mock of the tagger Component
type mockTaggerClient struct {
*taggerimpl.TaggerWrapper
}

// mockHandleRequest is a simple mocked http.Handler function to test the route is registered correctly on the api component
func (m *mockTaggerClient) mockHandleRequest(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("OK"))
}

// New returns a Mock
func New(t testing.TB) Mock {
c := configmock.New(t)
params := tagger.Params{
UseFakeTagger: true,
}
logComponent := logmock.New(t)
wmeta := fxutil.Test[workloadmeta.Component](t,
fx.Provide(func() log.Component { return logComponent }),
fx.Provide(func() config.Component { return c }),
workloadmetafx.Module(workloadmeta.NewParams()),
)

tagger, err := taggerimpl.NewTaggerClient(params, c, wmeta, logComponent, noopTelemetry.GetCompatComponent())

assert.NoError(t, err)

return &mockTaggerClient{
tagger,
}
}

// Provides is a struct containing the mock and the endpoint
type Provides struct {
fx.Out

Comp Mock
Endpoint api.AgentEndpointProvider
}

type dependencies struct {
fx.In

Config config.Component
Log log.Component
WMeta workloadmeta.Component
Telemetry telemetry.Component
}

// NewMock returns a Provides
func NewMock(deps dependencies) (Provides, error) {
params := tagger.Params{
UseFakeTagger: true,
}

tagger, err := taggerimpl.NewTaggerClient(params, deps.Config, deps.WMeta, deps.Log, deps.Telemetry)
if err != nil {
return Provides{}, err
}

c := &mockTaggerClient{
tagger,
}
return Provides{
Comp: c,
Endpoint: api.NewAgentEndpointProvider(c.mockHandleRequest, "/tagger-list", "GET"),
}, nil
}

// Module is a module containing the mock, useful for testing
func Module() fxutil.Module {
return fxutil.Component(
fx.Provide(NewMock),
fx.Supply(config.Params{}),
fx.Supply(log.Params{}),
fx.Provide(func(t testing.TB) log.Component { return logmock.New(t) }),
config.MockModule(),
sysprobeconfigimpl.MockModule(),
workloadmetafx.Module(workloadmeta.NewParams()),
telemetryimpl.MockModule(),
fxutil.ProvideComponentConstructor(New),
)
}

// SetupFakeTagger calls fxutil.Test to create a mock tagger for testing
func SetupFakeTagger(t testing.TB) Mock {
return fxutil.Test[Mock](t, Module())
}

// SetTags calls faketagger SetTags which sets the tags for an entity
func (m *mockTaggerClient) SetTags(entity types.EntityID, source string, low, orch, high, std []string) {
if v, ok := m.TaggerWrapper.GetDefaultTagger().(*taggerimpl.FakeTagger); ok {
v.SetTags(entity, source, low, orch, high, std)
}
}

// SetGlobalTags calls faketagger SetGlobalTags which sets the tags for the global entity
func (m *mockTaggerClient) SetGlobalTags(low, orch, high, std []string) {
if v, ok := m.TaggerWrapper.GetDefaultTagger().(*taggerimpl.FakeTagger); ok {
v.SetGlobalTags(low, orch, high, std)
}
}
12 changes: 12 additions & 0 deletions comp/process/agent/agentimpl/agent_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"go.uber.org/fx"

configComp "github.com/DataDog/datadog-agent/comp/core/config"
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/sysprobeconfig/sysprobeconfigimpl"
taggerMock "github.com/DataDog/datadog-agent/comp/core/tagger/mock"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
"github.com/DataDog/datadog-agent/comp/core/telemetry/telemetryimpl"
Expand Down Expand Up @@ -129,6 +132,9 @@ func TestProcessAgentComponentOnLinux(t *testing.T) {
submitterimpl.MockModule(),
taggerMock.Module(),
statsd.MockModule(),
fx.Provide(func(t testing.TB) log.Component { return logmock.New(t) }),
configComp.MockModule(),
sysprobeconfigimpl.MockModule(),
GustavoCaso marked this conversation as resolved.
Show resolved Hide resolved
Module(),

fx.Replace(configComp.MockParams{Overrides: map[string]interface{}{
Expand Down Expand Up @@ -197,6 +203,9 @@ func TestStatusProvider(t *testing.T) {
"process_config.run_in_core_agent.enabled": true,
}}),
processcheckimpl.MockModule(),
fx.Provide(func(t testing.TB) log.Component { return logmock.New(t) }),
configComp.MockModule(),
sysprobeconfigimpl.MockModule(),
fx.Provide(func() func(c *checkMocks.Check) {
return func(c *checkMocks.Check) {
c.On("Init", mock.Anything, mock.Anything, mock.AnythingOfType("bool")).Return(nil).Maybe()
Expand Down Expand Up @@ -242,6 +251,9 @@ func TestTelemetryCoreAgent(t *testing.T) {
"telemetry.enabled": true,
}}),
processcheckimpl.MockModule(),
fx.Provide(func(t testing.TB) log.Component { return logmock.New(t) }),
configComp.MockModule(),
sysprobeconfigimpl.MockModule(),
fx.Provide(func() func(c *checkMocks.Check) {
return func(c *checkMocks.Check) {
c.On("Init", mock.Anything, mock.Anything, mock.AnythingOfType("bool")).Return(nil).Maybe()
Expand Down
Loading
Loading