@@ -4,9 +4,9 @@ package process
44
55import (
66 "errors"
7- "testing"
8-
97 "github.com/newrelic/infrastructure-agent/pkg/metrics"
8+ "testing"
9+ "time"
1010
1111 "github.com/newrelic/infrastructure-agent/internal/agent/mocks"
1212 "github.com/newrelic/infrastructure-agent/pkg/config"
@@ -18,7 +18,7 @@ import (
1818func TestProcessSampler_Sample (t * testing.T ) {
1919 ctx := new (mocks.AgentContext )
2020 cfg := & config.Config {RunMode : config .ModeRoot }
21- ctx .On ("Config" ).Times (3 ).Return (cfg )
21+ ctx .On ("Config" ).Times (4 ).Return (cfg )
2222
2323 harvester := & HarvesterMock {}
2424 sampler := NewProcessSampler (ctx ).(* processSampler )
@@ -61,7 +61,7 @@ func TestProcessSampler_Sample(t *testing.T) {
6161func TestProcessSampler_Sample_ErrorOnProcessShouldNotStop (t * testing.T ) {
6262 ctx := new (mocks.AgentContext )
6363 cfg := & config.Config {RunMode : config .ModeRoot }
64- ctx .On ("Config" ).Times (3 ).Return (cfg )
64+ ctx .On ("Config" ).Times (4 ).Return (cfg )
6565
6666 harvester := & HarvesterMock {}
6767 sampler := NewProcessSampler (ctx ).(* processSampler )
@@ -108,7 +108,7 @@ func TestProcessSampler_Sample_ErrorOnProcessShouldNotStop(t *testing.T) {
108108func TestProcessSampler_Sample_DockerDecorator (t * testing.T ) {
109109 ctx := new (mocks.AgentContext )
110110 cfg := & config.Config {RunMode : config .ModeRoot }
111- ctx .On ("Config" ).Times (3 ).Return (cfg )
111+ ctx .On ("Config" ).Times (4 ).Return (cfg )
112112
113113 harvester := & HarvesterMock {}
114114 sampler := NewProcessSampler (ctx ).(* processSampler )
@@ -153,3 +153,63 @@ func TestProcessSampler_Sample_DockerDecorator(t *testing.T) {
153153
154154 mock .AssertExpectationsForObjects (t , ctx , harvester )
155155}
156+
157+ //nolint:paralleltest
158+ func TestProcessSampler_Sample_DisabledDockerDecorator (t * testing.T ) {
159+ ctx := new (mocks.AgentContext )
160+ cfg := config .NewConfig ()
161+ cfg .ProcessContainerDecoration = false
162+ ctx .On ("Config" ).Times (4 ).Return (cfg )
163+
164+ // The container sampler getter should not be called
165+ containerSamplerGetter = func (cacheTTL time.Duration , dockerAPIVersion , dockerContainerdNamespace string ) []metrics.ContainerSampler {
166+ t .Errorf ("containerSamplerGetter should not be called" )
167+
168+ return nil
169+ }
170+
171+ defer func () {
172+ containerSamplerGetter = metrics .GetContainerSamplers
173+ }()
174+
175+ var expected []metrics.ContainerSampler
176+ sampler := NewProcessSampler (ctx ).(* processSampler ) //nolint:forcetypeassert
177+ assert .Equal (t , expected , sampler .containerSamplers )
178+ }
179+
180+ //nolint:paralleltest
181+ func TestProcessSampler_Sample_DockerDecoratorEnabledByDefault (t * testing.T ) {
182+ ctx := new (mocks.AgentContext )
183+ cfg := config .NewConfig ()
184+ ctx .On ("Config" ).Times (4 ).Return (cfg )
185+
186+ containerSamplerGetter = func (cacheTTL time.Duration , dockerAPIVersion , dockerContainerdNamespace string ) []metrics.ContainerSampler {
187+ return []metrics.ContainerSampler {& fakeContainerSampler {}}
188+ }
189+
190+ defer func () {
191+ containerSamplerGetter = metrics .GetContainerSamplers
192+ }()
193+
194+ expected := []metrics.ContainerSampler {& fakeContainerSampler {}}
195+ sampler := NewProcessSampler (ctx ).(* processSampler ) //nolint:forcetypeassert
196+ assert .Equal (t , expected , sampler .containerSamplers )
197+ }
198+
199+ //nolint:paralleltest
200+ func TestProcessSampler_Sample_DockerDecoratorEnabledWithNoConfig (t * testing.T ) {
201+ ctx := new (mocks.AgentContext )
202+ ctx .On ("Config" ).Times (2 ).Return (nil )
203+
204+ containerSamplerGetter = func (cacheTTL time.Duration , dockerAPIVersion , dockerContainerdNamespace string ) []metrics.ContainerSampler {
205+ return []metrics.ContainerSampler {& fakeContainerSampler {}}
206+ }
207+
208+ defer func () {
209+ containerSamplerGetter = metrics .GetContainerSamplers
210+ }()
211+
212+ expected := []metrics.ContainerSampler {& fakeContainerSampler {}}
213+ sampler := NewProcessSampler (ctx ).(* processSampler ) //nolint:forcetypeassert
214+ assert .Equal (t , expected , sampler .containerSamplers )
215+ }
0 commit comments