9
9
context2 "context"
10
10
"encoding/json"
11
11
"fmt"
12
- "github.com/newrelic/infrastructure-agent/internal/agent/cmdchannel/fflag"
13
12
"io/ioutil"
14
13
"net/http"
15
14
"net/http/httptest"
@@ -21,6 +20,8 @@ import (
21
20
"testing"
22
21
"time"
23
22
23
+ "github.com/newrelic/infrastructure-agent/internal/agent/cmdchannel/fflag"
24
+
24
25
"github.com/newrelic/infrastructure-agent/internal/agent/delta"
25
26
agentTypes "github.com/newrelic/infrastructure-agent/internal/agent/types"
26
27
"github.com/newrelic/infrastructure-agent/internal/feature_flags"
@@ -116,17 +117,17 @@ func (self *TestAgentData) SortKey() string {
116
117
117
118
func TestIgnoreInventory (t * testing.T ) {
118
119
ffRetriever := & feature_flags.FeatureFlagRetrieverMock {}
119
- a := newTesting (& config.Config {
120
+ agent := newTesting (& config.Config {
120
121
IgnoredInventoryPathsMap : map [string ]struct {}{
121
122
"test/plugin/yum" : {},
122
123
},
123
124
MaxInventorySize : 1024 ,
124
125
}, ffRetriever )
125
126
defer func () {
126
- _ = os .RemoveAll (a .store .DataDir )
127
+ _ = os .RemoveAll (agent .store .DataDir )
127
128
}()
128
129
129
- assert .NoError (t , a .storePluginOutput (agentTypes.PluginOutput {
130
+ assert .NoError (t , agent .storePluginOutput (agentTypes.PluginOutput {
130
131
Id : ids.PluginID {"test" , "plugin" },
131
132
Entity : entity .NewFromNameWithoutID ("someEntity" ),
132
133
Data : agentTypes.PluginInventoryDataset {
@@ -135,7 +136,7 @@ func TestIgnoreInventory(t *testing.T) {
135
136
},
136
137
}))
137
138
138
- restoredDataBytes , err := ioutil .ReadFile (filepath .Join (a .store .DataDir , "test" , "someEntity" , "plugin.json" ))
139
+ restoredDataBytes , err := ioutil .ReadFile (filepath .Join (agent .store .DataDir , "test" , "someEntity" , "plugin.json" ))
139
140
require .NoError (t , err )
140
141
141
142
var restoredData map [string ]interface {}
@@ -165,61 +166,61 @@ func TestServicePidMap(t *testing.T) {
165
166
166
167
func TestSetAgentKeysDisplayInstance (t * testing.T ) {
167
168
ffRetriever := & feature_flags.FeatureFlagRetrieverMock {}
168
- a := newTesting (nil , ffRetriever )
169
- defer os .RemoveAll (a .store .DataDir )
169
+ agent := newTesting (nil , ffRetriever )
170
+ defer os .RemoveAll (agent .store .DataDir )
170
171
171
172
idMap := host.IDLookup {
172
173
sysinfo .HOST_SOURCE_DISPLAY_NAME : "displayName" ,
173
174
sysinfo .HOST_SOURCE_HOSTNAME : "hostName" ,
174
175
sysinfo .HOST_SOURCE_INSTANCE_ID : "instanceId" ,
175
176
}
176
177
177
- a .setAgentKey (idMap )
178
- assert .Equal (t , idMap [sysinfo .HOST_SOURCE_INSTANCE_ID ], a .Context .EntityKey ())
178
+ agent .setAgentKey (idMap )
179
+ assert .Equal (t , idMap [sysinfo .HOST_SOURCE_INSTANCE_ID ], agent .Context .EntityKey ())
179
180
}
180
181
181
182
// Test that empty strings in the identity map are properly ignored in favor of non-empty ones
182
183
func TestSetAgentKeysInstanceEmptyString (t * testing.T ) {
183
184
ffRetriever := & feature_flags.FeatureFlagRetrieverMock {}
184
- a := newTesting (nil , ffRetriever )
185
- defer os .RemoveAll (a .store .DataDir )
185
+ agent := newTesting (nil , ffRetriever )
186
+ defer os .RemoveAll (agent .store .DataDir )
186
187
187
188
keys := host.IDLookup {
188
189
sysinfo .HOST_SOURCE_DISPLAY_NAME : "displayName" ,
189
190
sysinfo .HOST_SOURCE_HOSTNAME : "hostName" ,
190
191
sysinfo .HOST_SOURCE_INSTANCE_ID : "" ,
191
192
}
192
193
193
- a .setAgentKey (keys )
194
- assert .Equal (t , keys [sysinfo .HOST_SOURCE_DISPLAY_NAME ], a .Context .EntityKey ())
194
+ agent .setAgentKey (keys )
195
+ assert .Equal (t , keys [sysinfo .HOST_SOURCE_DISPLAY_NAME ], agent .Context .EntityKey ())
195
196
}
196
197
197
198
func TestSetAgentKeysDisplayNameMatchesHostName (t * testing.T ) {
198
199
ffRetriever := & feature_flags.FeatureFlagRetrieverMock {}
199
- a := newTesting (nil , ffRetriever )
200
- defer os .RemoveAll (a .store .DataDir )
200
+ agent := newTesting (nil , ffRetriever )
201
+ defer os .RemoveAll (agent .store .DataDir )
201
202
202
203
keyMap := host.IDLookup {
203
204
sysinfo .HOST_SOURCE_DISPLAY_NAME : "hostName" ,
204
205
sysinfo .HOST_SOURCE_HOSTNAME : "hostName" ,
205
206
}
206
207
207
- a .setAgentKey (keyMap )
208
- assert .Equal (t , "hostName" , a .Context .EntityKey ())
208
+ agent .setAgentKey (keyMap )
209
+ assert .Equal (t , "hostName" , agent .Context .EntityKey ())
209
210
}
210
211
211
212
func TestSetAgentKeysNoValues (t * testing.T ) {
212
213
ffRetriever := & feature_flags.FeatureFlagRetrieverMock {}
213
- a := newTesting (nil , ffRetriever )
214
- defer os .RemoveAll (a .store .DataDir )
214
+ agent := newTesting (nil , ffRetriever )
215
+ defer os .RemoveAll (agent .store .DataDir )
215
216
216
- assert .Error (t , a .setAgentKey (host.IDLookup {}))
217
+ assert .Error (t , agent .setAgentKey (host.IDLookup {}))
217
218
}
218
219
219
220
func TestUpdateIDLookupTable (t * testing.T ) {
220
221
ffRetriever := & feature_flags.FeatureFlagRetrieverMock {}
221
- a := newTesting (nil , ffRetriever )
222
- defer os .RemoveAll (a .store .DataDir )
222
+ agent := newTesting (nil , ffRetriever )
223
+ defer os .RemoveAll (agent .store .DataDir )
223
224
224
225
dataset := agentTypes.PluginInventoryDataset {}
225
226
dataset = append (dataset , sysinfo.HostAliases {
@@ -235,8 +236,8 @@ func TestUpdateIDLookupTable(t *testing.T) {
235
236
Source : sysinfo .HOST_SOURCE_HOSTNAME_SHORT ,
236
237
})
237
238
238
- assert .NoError (t , a .updateIDLookupTable (dataset ))
239
- assert .Equal (t , "instanceId" , a .Context .EntityKey ())
239
+ assert .NoError (t , agent .updateIDLookupTable (dataset ))
240
+ assert .Equal (t , "instanceId" , agent .Context .EntityKey ())
240
241
}
241
242
242
243
func TestIDLookup_EntityNameCloudInstance (t * testing.T ) {
@@ -387,24 +388,24 @@ func TestRemoveOutdatedEntities(t *testing.T) {
387
388
func TestReconnectablePlugins (t * testing.T ) {
388
389
// Given an agent
389
390
ffRetriever := & feature_flags.FeatureFlagRetrieverMock {}
390
- a := newTesting (nil , ffRetriever )
391
- defer os .RemoveAll (a .store .DataDir )
391
+ agent := newTesting (nil , ffRetriever )
392
+ defer os .RemoveAll (agent .store .DataDir )
392
393
393
394
wg := sync.WaitGroup {}
394
395
wg .Add (2 )
395
396
// With a set of registered plugins
396
397
nrp := nonReconnectingPlugin {invocations : 0 , wg : & wg }
397
- a .RegisterPlugin (& nrp )
398
- rp := reconnectingPlugin {invocations : 0 , context : a .Context , wg : & wg }
399
- a .RegisterPlugin (& rp )
398
+ agent .RegisterPlugin (& nrp )
399
+ rp := reconnectingPlugin {invocations : 0 , context : agent .Context , wg : & wg }
400
+ agent .RegisterPlugin (& rp )
400
401
401
402
// That successfully started
402
- a .startPlugins ()
403
+ agent .startPlugins ()
403
404
assert .NoError (t , wait (time .Second , & wg ))
404
405
405
406
// When the agent reconnects
406
407
wg .Add (1 )
407
- a .Context .Reconnect ()
408
+ agent .Context .Reconnect ()
408
409
assert .NoError (t , wait (time .Second , & wg ))
409
410
410
411
// The non-reconnecting plugins are not invoked again
@@ -498,17 +499,17 @@ func (killingPlugin) GetExternalPluginName() string { return "" }
498
499
499
500
func TestTerminate (t * testing.T ) {
500
501
ffRetriever := & feature_flags.FeatureFlagRetrieverMock {}
501
- a := newTesting (nil , ffRetriever )
502
+ agent := newTesting (nil , ffRetriever )
502
503
defer func () {
503
- _ = os .RemoveAll (a .store .DataDir )
504
+ _ = os .RemoveAll (agent .store .DataDir )
504
505
}()
505
- a .plugins = []Plugin {
506
+ agent .plugins = []Plugin {
506
507
& killingPlugin {killed : false }, & killingPlugin {killed : false }, & killingPlugin {killed : false },
507
508
}
508
509
509
- a .Terminate ()
510
- assert .Len (t , a .plugins , 3 )
511
- for _ , plugin := range a .plugins {
510
+ agent .Terminate ()
511
+ assert .Len (t , agent .plugins , 3 )
512
+ for _ , plugin := range agent .plugins {
512
513
assert .True (t , plugin .(* killingPlugin ).killed )
513
514
}
514
515
}
@@ -519,21 +520,21 @@ func TestStopByCancelFn_UsedBySignalHandler(t *testing.T) {
519
520
520
521
ffRetriever := & feature_flags.FeatureFlagRetrieverMock {}
521
522
ffRetriever .ShouldGetFeatureFlag (fflag .FlagFullInventoryDeletion , false , false )
522
- a := newTesting (nil , ffRetriever )
523
+ agent := newTesting (nil , ffRetriever )
523
524
524
525
defer func () {
525
- _ = os .RemoveAll (a .store .DataDir )
526
+ _ = os .RemoveAll (agent .store .DataDir )
526
527
}()
527
- a .plugins = []Plugin {
528
+ agent .plugins = []Plugin {
528
529
& killingPlugin {killed : false }, & killingPlugin {killed : false }, & killingPlugin {killed : false },
529
530
}
530
531
531
532
go func () {
532
- assert .NoError (t , a .Run ())
533
+ assert .NoError (t , agent .Run ())
533
534
wg .Done ()
534
535
}()
535
536
536
- a .Context .CancelFn ()
537
+ agent .Context .CancelFn ()
537
538
wg .Wait ()
538
539
}
539
540
@@ -603,17 +604,17 @@ func TestAgent_Run_DontSendInventoryIfFwdOnly(t *testing.T) {
603
604
604
605
ffRetriever := & feature_flags.FeatureFlagRetrieverMock {}
605
606
ffRetriever .ShouldGetFeatureFlag (fflag .FlagFullInventoryDeletion , false , false )
606
- a := newTesting (cfg , ffRetriever )
607
+ agent := newTesting (cfg , ffRetriever )
607
608
// Give time to at least send one request
608
- ctxTimeout , _ := context2 .WithTimeout (a .Context .Ctx , time .Millisecond * 10 )
609
- a .Context .Ctx = ctxTimeout
609
+ ctxTimeout , _ := context2 .WithTimeout (agent .Context .Ctx , time .Millisecond * 10 )
610
+ agent .Context .Ctx = ctxTimeout
610
611
611
612
// Inventory recording calls
612
613
snd := & patchSenderCallRecorder {}
613
- a .inventories = map [string ]* inventoryEntity {"test" : {sender : snd }}
614
+ agent .inventories = map [string ]* inventoryEntity {"test" : {sender : snd }}
614
615
615
616
go func () {
616
- assert .NoError (t , a .Run ())
617
+ assert .NoError (t , agent .Run ())
617
618
wg .Done ()
618
619
}()
619
620
wg .Wait ()
@@ -736,12 +737,12 @@ func (self *testAgentNullableData) SortKey() string {
736
737
737
738
func TestStorePluginOutput (t * testing.T ) {
738
739
ffRetriever := & feature_flags.FeatureFlagRetrieverMock {}
739
- a := newTesting (nil , ffRetriever )
740
- defer os .RemoveAll (a .store .DataDir )
740
+ agent := newTesting (nil , ffRetriever )
741
+ defer os .RemoveAll (agent .store .DataDir )
741
742
aV := "aValue"
742
743
bV := "bValue"
743
744
cV := "cValue"
744
- err := a .storePluginOutput (agentTypes.PluginOutput {
745
+ err := agent .storePluginOutput (agentTypes.PluginOutput {
745
746
Id : ids.PluginID {"test" , "plugin" },
746
747
Entity : entity .NewFromNameWithoutID ("someEntity" ),
747
748
Data : agentTypes.PluginInventoryDataset {
@@ -754,7 +755,7 @@ func TestStorePluginOutput(t *testing.T) {
754
755
755
756
assert .NoError (t , err )
756
757
757
- sourceFile := filepath .Join (a .store .DataDir , "test" , "someEntity" , "plugin.json" )
758
+ sourceFile := filepath .Join (agent .store .DataDir , "test" , "someEntity" , "plugin.json" )
758
759
sourceB , err := ioutil .ReadFile (sourceFile )
759
760
require .NoError (t , err )
760
761
@@ -787,8 +788,8 @@ func (self mockHostinfoData) SortKey() string {
787
788
788
789
func BenchmarkStorePluginOutput (b * testing.B ) {
789
790
ffRetriever := & feature_flags.FeatureFlagRetrieverMock {}
790
- a := newTesting (& config.Config {MaxInventorySize : 1000 * 1000 }, ffRetriever )
791
- defer os .RemoveAll (a .store .DataDir )
791
+ agent := newTesting (& config.Config {MaxInventorySize : 1000 * 1000 }, ffRetriever )
792
+ defer os .RemoveAll (agent .store .DataDir )
792
793
793
794
distroName := "Fedora 29 (Cloud Edition)"
794
795
benchmarks := []struct {
@@ -830,7 +831,7 @@ func BenchmarkStorePluginOutput(b *testing.B) {
830
831
831
832
b .ResetTimer ()
832
833
for i := 0 ; i < b .N ; i ++ {
833
- _ = a .storePluginOutput (output )
834
+ _ = agent .storePluginOutput (output )
834
835
}
835
836
b .StopTimer ()
836
837
})
@@ -907,10 +908,10 @@ func Test_ProcessSampling(t *testing.T) {
907
908
}
908
909
909
910
for _ , tc := range testCases {
910
- a , _ := NewAgent (tc .c , "test" , "userAgent" , tc .ff )
911
+ agent , _ := NewAgent (tc .c , "test" , "userAgent" , tc .ff )
911
912
912
913
t .Run (tc .name , func (t * testing.T ) {
913
- actual := a .Context .shouldIncludeEvent (someSample )
914
+ actual := agent .Context .shouldIncludeEvent (someSample )
914
915
assert .Equal (t , tc .want , actual )
915
916
})
916
917
}
@@ -1104,12 +1105,12 @@ func Test_ProcessSamplingExcludesAllCases(t *testing.T) {
1104
1105
}
1105
1106
1106
1107
ff := test .NewFFRetrieverReturning (false , false )
1107
- a , _ := NewAgent (cnf , "test" , "userAgent" , ff )
1108
+ agent , _ := NewAgent (cnf , "test" , "userAgent" , ff )
1108
1109
1109
1110
t .Run (testCase .name , func (t * testing.T ) {
1110
1111
t .Parallel ()
1111
1112
1112
- assert .Equal (t , testCase .expectInclude , a .Context .IncludeEvent (someSample ))
1113
+ assert .Equal (t , testCase .expectInclude , agent .Context .IncludeEvent (someSample ))
1113
1114
})
1114
1115
}
1115
1116
}
@@ -1244,10 +1245,10 @@ func TestAgent_checkInstanceIDRetry(t *testing.T) {
1244
1245
t .Parallel ()
1245
1246
1246
1247
ffRetriever := & feature_flags.FeatureFlagRetrieverMock {}
1247
- a := newTesting (nil , ffRetriever )
1248
- a .cloudHarvester = testCase .cloudHarvester
1248
+ agent := newTesting (nil , ffRetriever )
1249
+ agent .cloudHarvester = testCase .cloudHarvester
1249
1250
1250
- if err := a .checkInstanceIDRetry (testCase .args .maxRetries , testCase .args .backoffTime ); (err != nil ) != testCase .wantErr {
1251
+ if err := agent .checkInstanceIDRetry (testCase .args .maxRetries , testCase .args .backoffTime ); (err != nil ) != testCase .wantErr {
1251
1252
t .Errorf ("Agent.checkInstanceIDRetry() error = %v, wantErr %v" , err , testCase .wantErr )
1252
1253
}
1253
1254
})
0 commit comments