@@ -17,13 +17,16 @@ package aws
17
17
import (
18
18
"context"
19
19
"errors"
20
+ "net/http"
20
21
"testing"
21
22
"time"
22
23
24
+ "github.com/aws/aws-sdk-go/aws/awserr"
23
25
"github.com/aws/aws-sdk-go/aws/ec2metadata"
24
26
"github.com/stretchr/testify/assert"
25
27
"github.com/stretchr/testify/require"
26
28
29
+ "go.opentelemetry.io/otel/label"
27
30
"go.opentelemetry.io/otel/sdk/resource"
28
31
"go.opentelemetry.io/otel/semconv"
29
32
)
@@ -35,9 +38,39 @@ func TestAWS_Detect(t *testing.T) {
35
38
36
39
type want struct {
37
40
Error string
41
+ Partial bool
38
42
Resource * resource.Resource
39
43
}
40
44
45
+ usWestInst := func () (ec2metadata.EC2InstanceIdentityDocument , error ) {
46
+ // Example from https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html
47
+ doc := ec2metadata.EC2InstanceIdentityDocument {
48
+ MarketplaceProductCodes : []string {"1abc2defghijklm3nopqrs4tu" },
49
+ AvailabilityZone : "us-west-2b" ,
50
+ PrivateIP : "10.158.112.84" ,
51
+ Version : "2017-09-30" ,
52
+ Region : "us-west-2" ,
53
+ InstanceID : "i-1234567890abcdef0" ,
54
+ InstanceType : "t2.micro" ,
55
+ AccountID : "123456789012" ,
56
+ PendingTime : time .Date (2016 , time .November , 19 , 16 , 32 , 11 , 0 , time .UTC ),
57
+ ImageID : "ami-5fb8c835" ,
58
+ Architecture : "x86_64" ,
59
+ }
60
+
61
+ return doc , nil
62
+ }
63
+
64
+ usWestIDLabels := []label.KeyValue {
65
+ semconv .CloudProviderAWS ,
66
+ semconv .CloudRegionKey .String ("us-west-2" ),
67
+ semconv .CloudZoneKey .String ("us-west-2b" ),
68
+ semconv .CloudAccountIDKey .String ("123456789012" ),
69
+ semconv .HostIDKey .String ("i-1234567890abcdef0" ),
70
+ semconv .HostImageIDKey .String ("ami-5fb8c835" ),
71
+ semconv .HostTypeKey .String ("t2.micro" ),
72
+ }
73
+
41
74
testTable := map [string ]struct {
42
75
Fields fields
43
76
Want want
@@ -53,32 +86,63 @@ func TestAWS_Detect(t *testing.T) {
53
86
},
54
87
Want : want {Error : "id not available" },
55
88
},
56
- "Instance ID Available " : {
89
+ "Hostname Not Found " : {
57
90
Fields : fields {
58
- Client : & clientMock {available : true , idDoc : func () (ec2metadata.EC2InstanceIdentityDocument , error ) {
59
- // Example from https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html
60
- doc := ec2metadata.EC2InstanceIdentityDocument {
61
- MarketplaceProductCodes : []string {"1abc2defghijklm3nopqrs4tu" },
62
- AvailabilityZone : "us-west-2b" ,
63
- PrivateIP : "10.158.112.84" ,
64
- Version : "2017-09-30" ,
65
- Region : "us-west-2" ,
66
- InstanceID : "i-1234567890abcdef0" ,
67
- InstanceType : "t2.micro" ,
68
- AccountID : "123456789012" ,
69
- PendingTime : time .Date (2016 , time .November , 19 , 16 , 32 , 11 , 0 , time .UTC ),
70
- ImageID : "ami-5fb8c835" ,
71
- Architecture : "x86_64" ,
72
- }
73
-
74
- return doc , nil
75
- }},
91
+ Client : & clientMock {available : true , idDoc : usWestInst , metadata : map [string ]meta {}},
92
+ },
93
+ Want : want {Resource : resource .New (usWestIDLabels ... )},
94
+ },
95
+ "Hostname Response Error" : {
96
+ Fields : fields {
97
+ Client : & clientMock {
98
+ available : true ,
99
+ idDoc : usWestInst ,
100
+ metadata : map [string ]meta {
101
+ "hostname" : {err : awserr .NewRequestFailure (awserr .New ("EC2MetadataError" , "failed to make EC2Metadata request" , errors .New ("response error" )), http .StatusInternalServerError , "test-request" )},
102
+ },
103
+ },
104
+ },
105
+ Want : want {
106
+ Error : `partial resource: ["hostname": 500 EC2MetadataError]` ,
107
+ Partial : true ,
108
+ Resource : resource .New (usWestIDLabels ... ),
109
+ },
110
+ },
111
+ "Hostname General Error" : {
112
+ Fields : fields {
113
+ Client : & clientMock {
114
+ available : true ,
115
+ idDoc : usWestInst ,
116
+ metadata : map [string ]meta {
117
+ "hostname" : {err : errors .New ("unknown error" )},
118
+ },
119
+ },
120
+ },
121
+ Want : want {
122
+ Error : `partial resource: ["hostname": unknown error]` ,
123
+ Partial : true ,
124
+ Resource : resource .New (usWestIDLabels ... ),
125
+ },
126
+ },
127
+ "All Available" : {
128
+ Fields : fields {
129
+ Client : & clientMock {
130
+ available : true ,
131
+ idDoc : usWestInst ,
132
+ metadata : map [string ]meta {
133
+ "hostname" : {value : "ip-12-34-56-78.us-west-2.compute.internal" },
134
+ },
135
+ },
76
136
},
77
137
Want : want {Resource : resource .New (
78
138
semconv .CloudProviderAWS ,
79
139
semconv .CloudRegionKey .String ("us-west-2" ),
140
+ semconv .CloudZoneKey .String ("us-west-2b" ),
80
141
semconv .CloudAccountIDKey .String ("123456789012" ),
81
142
semconv .HostIDKey .String ("i-1234567890abcdef0" ),
143
+ semconv .HostImageIDKey .String ("ami-5fb8c835" ),
144
+ semconv .HostNameKey .String ("ip-12-34-56-78.us-west-2.compute.internal" ),
145
+ semconv .HostTypeKey .String ("t2.micro" ),
82
146
)},
83
147
},
84
148
}
@@ -93,20 +157,28 @@ func TestAWS_Detect(t *testing.T) {
93
157
94
158
r , err := aws .Detect (context .Background ())
95
159
160
+ assert .Equal (t , tt .Want .Resource , r , "Resource" )
161
+
96
162
if tt .Want .Error != "" {
97
163
require .EqualError (t , err , tt .Want .Error , "Error" )
164
+ assert .Equal (t , tt .Want .Partial , errors .Is (err , resource .ErrPartialResource ), "Partial Resource" )
98
165
return
99
166
}
100
167
101
168
require .NoError (t , err , "Error" )
102
- assert .Equal (t , tt .Want .Resource , r , "Resource" )
103
169
})
104
170
}
105
171
}
106
172
107
173
type clientMock struct {
108
174
available bool
109
175
idDoc func () (ec2metadata.EC2InstanceIdentityDocument , error )
176
+ metadata map [string ]meta
177
+ }
178
+
179
+ type meta struct {
180
+ err error
181
+ value string
110
182
}
111
183
112
184
func (c * clientMock ) Available () bool {
@@ -116,3 +188,12 @@ func (c *clientMock) Available() bool {
116
188
func (c * clientMock ) GetInstanceIdentityDocument () (ec2metadata.EC2InstanceIdentityDocument , error ) {
117
189
return c .idDoc ()
118
190
}
191
+
192
+ func (c * clientMock ) GetMetadata (p string ) (string , error ) {
193
+ v , ok := c .metadata [p ]
194
+ if ! ok {
195
+ return "" , awserr .NewRequestFailure (awserr .New ("EC2MetadataError" , "failed to make EC2Metadata request" , errors .New ("response error" )), http .StatusNotFound , "test-request" )
196
+ }
197
+
198
+ return v .value , v .err
199
+ }
0 commit comments