@@ -14,6 +14,7 @@ import (
14
14
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
15
15
"github.com/aws/aws-sdk-go-v2/service/ec2"
16
16
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
17
+ smithyhttp "github.com/aws/smithy-go/transport/http"
17
18
"github.com/stretchr/testify/assert"
18
19
"github.com/stretchr/testify/require"
19
20
"go.opentelemetry.io/collector/pdata/pcommon"
@@ -30,10 +31,9 @@ type mockMetadata struct {
30
31
retIDDoc imds.InstanceIdentityDocument
31
32
retErrIDDoc error
32
33
33
- retHostname string
34
- retErrHostname error
35
-
36
- isAvailable bool
34
+ retHostname string
35
+ retErrHostname error
36
+ retErrUnavailable error
37
37
}
38
38
39
39
var _ ec2provider.Provider = (* mockMetadata )(nil )
@@ -51,10 +51,7 @@ func (e *mockClientBuilderError) buildClient(_ context.Context, _ string, _ *htt
51
51
}
52
52
53
53
func (mm mockMetadata ) InstanceID (_ context.Context ) (string , error ) {
54
- if ! mm .isAvailable {
55
- return "" , errUnavailable
56
- }
57
- return "" , nil
54
+ return "" , mm .retErrUnavailable
58
55
}
59
56
60
57
func (mm mockMetadata ) Get (_ context.Context ) (imds.InstanceIdentityDocument , error ) {
@@ -173,7 +170,6 @@ func TestDetector_Detect(t *testing.T) {
173
170
InstanceType : "c4.xlarge" ,
174
171
},
175
172
retHostname : "example-hostname" ,
176
- isAvailable : true ,
177
173
}},
178
174
args : args {ctx : context .Background ()},
179
175
want : func () pcommon.Resource {
@@ -203,7 +199,6 @@ func TestDetector_Detect(t *testing.T) {
203
199
InstanceType : "c4.xlarge" ,
204
200
},
205
201
retHostname : "example-hostname" ,
206
- isAvailable : true ,
207
202
}},
208
203
tagKeyRegexes : []* regexp.Regexp {regexp .MustCompile ("^tag1$" ), regexp .MustCompile ("^tag2$" )},
209
204
args : args {ctx : context .Background ()},
@@ -237,7 +232,6 @@ func TestDetector_Detect(t *testing.T) {
237
232
InstanceType : "c4.xlarge" ,
238
233
},
239
234
retHostname : "example-hostname" ,
240
- isAvailable : true ,
241
235
}},
242
236
args : args {ctx : context .Background ()},
243
237
want : func () pcommon.Resource {
@@ -259,20 +253,29 @@ func TestDetector_Detect(t *testing.T) {
259
253
{
260
254
name : "endpoint not available" ,
261
255
fields : fields {metadataProvider : & mockMetadata {
262
- retIDDoc : imds.InstanceIdentityDocument {},
263
- retErrIDDoc : errors .New ("should not be called" ),
264
- isAvailable : false ,
256
+ retIDDoc : imds.InstanceIdentityDocument {},
257
+ retErrIDDoc : errors .New ("should not be called" ),
258
+ retErrUnavailable : errUnavailable ,
265
259
}},
266
260
args : args {ctx : context .Background ()},
267
261
want : pcommon .NewResource (),
268
262
wantErr : false ,
269
263
},
264
+ {
265
+ name : "network not ready" ,
266
+ fields : fields {metadataProvider : & mockMetadata {
267
+ retIDDoc : imds.InstanceIdentityDocument {},
268
+ retErrUnavailable : & smithyhttp.RequestSendError {Err : errors .New ("nope" )},
269
+ }},
270
+ args : args {ctx : context .Background ()},
271
+ want : pcommon .NewResource (),
272
+ wantErr : true ,
273
+ },
270
274
{
271
275
name : "get fails" ,
272
276
fields : fields {metadataProvider : & mockMetadata {
273
277
retIDDoc : imds.InstanceIdentityDocument {},
274
278
retErrIDDoc : errors .New ("get failed" ),
275
- isAvailable : true ,
276
279
}},
277
280
args : args {ctx : context .Background ()},
278
281
want : pcommon .NewResource (),
@@ -284,7 +287,6 @@ func TestDetector_Detect(t *testing.T) {
284
287
retIDDoc : imds.InstanceIdentityDocument {},
285
288
retHostname : "" ,
286
289
retErrHostname : errors .New ("hostname failed" ),
287
- isAvailable : true ,
288
290
}},
289
291
args : args {ctx : context .Background ()},
290
292
want : pcommon .NewResource (),
0 commit comments