| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package ec2 |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"io" |
| 11 |
"net/http" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http" |
| 17 |
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds" |
| 18 |
"github.com/aws/smithy-go/middleware" |
| 19 |
smithyhttp "github.com/aws/smithy-go/transport/http" |
| 20 |
"github.com/stretchr/testify/assert" |
| 21 |
"github.com/stretchr/testify/mock" |
| 22 |
"go.opentelemetry.io/otel/attribute" |
| 23 |
"go.opentelemetry.io/otel/sdk/resource" |
| 24 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 25 |
) |
| 26 |
|
| 27 |
type mockClient struct { |
| 28 |
mock.Mock |
| 29 |
} |
| 30 |
|
| 31 |
func (m *mockClient) GetInstanceIdentityDocument(ctx context.Context, params *imds.GetInstanceIdentityDocumentInput, optFns ...func(*imds.Options)) (*imds.GetInstanceIdentityDocumentOutput, error) { |
| 32 |
args := m.Called(ctx, params, optFns) |
| 33 |
return args.Get(0).(*imds.GetInstanceIdentityDocumentOutput), args.Error(1) |
| 34 |
} |
| 35 |
|
| 36 |
func (m *mockClient) GetMetadata(ctx context.Context, params *imds.GetMetadataInput, optFns ...func(*imds.Options)) (*imds.GetMetadataOutput, error) { |
| 37 |
args := m.Called(ctx, params, optFns) |
| 38 |
return args.Get(0).(*imds.GetMetadataOutput), args.Error(1) |
| 39 |
} |
| 40 |
|
| 41 |
type testCase struct { |
| 42 |
name string |
| 43 |
metadataOutput *imds.GetMetadataOutput |
| 44 |
metadataErr error |
| 45 |
docOutput *imds.GetInstanceIdentityDocumentOutput |
| 46 |
docErr error |
| 47 |
expectedAttrs []attribute.KeyValue |
| 48 |
expectedErr error |
| 49 |
} |
| 50 |
|
| 51 |
func TestAWSResourceDetection(t *testing.T) { |
| 52 |
doc := validIdentityDocument() |
| 53 |
|
| 54 |
testCases := []testCase{ |
| 55 |
{ |
| 56 |
name: "AllFields", |
| 57 |
docOutput: doc, |
| 58 |
metadataOutput: mockMetadataOutput("ip-12-34-56-78.us-west-2.compute.internal"), |
| 59 |
expectedAttrs: []attribute.KeyValue{ |
| 60 |
semconv.CloudProviderAWS, |
| 61 |
semconv.CloudPlatformAWSEC2, |
| 62 |
semconv.CloudRegion("us-west-2"), |
| 63 |
semconv.CloudAvailabilityZone("us-west-2b"), |
| 64 |
semconv.CloudAccountID("123456789012"), |
| 65 |
semconv.HostID("i-1234567890abcdef0"), |
| 66 |
semconv.HostImageID("ami-5fb8c835"), |
| 67 |
semconv.HostType("t2.micro"), |
| 68 |
semconv.HostName("ip-12-34-56-78.us-west-2.compute.internal"), |
| 69 |
}, |
| 70 |
}, |
| 71 |
{ |
| 72 |
name: "NoHostname", |
| 73 |
docOutput: doc, |
| 74 |
metadataOutput: mockMetadataOutput(""), |
| 75 |
metadataErr: errors.New("mock error"), |
| 76 |
expectedAttrs: []attribute.KeyValue{ |
| 77 |
semconv.CloudProviderAWS, |
| 78 |
semconv.CloudPlatformAWSEC2, |
| 79 |
semconv.CloudRegion("us-west-2"), |
| 80 |
semconv.CloudAvailabilityZone("us-west-2b"), |
| 81 |
semconv.CloudAccountID("123456789012"), |
| 82 |
semconv.HostID("i-1234567890abcdef0"), |
| 83 |
semconv.HostImageID("ami-5fb8c835"), |
| 84 |
semconv.HostType("t2.micro"), |
| 85 |
}, |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: "NonEC2Host", |
| 89 |
docErr: errors.New("error getting InstanceIdentityDocument"), |
| 90 |
docOutput: &imds.GetInstanceIdentityDocumentOutput{}, |
| 91 |
metadataOutput: mockMetadataOutput(""), |
| 92 |
expectedAttrs: nil, // Empty resource |
| 93 |
}, |
| 94 |
} |
| 95 |
|
| 96 |
for _, tc := range testCases { |
| 97 |
t.Run(tc.name, func(t *testing.T) { |
| 98 |
clientMock := new(mockClient) |
| 99 |
|
| 100 |
clientMock.On("GetInstanceIdentityDocument", mock.Anything, mock.Anything, mock.Anything). |
| 101 |
Return(tc.docOutput, tc.docErr) |
| 102 |
clientMock.On("GetMetadata", mock.Anything, mock.Anything, mock.Anything). |
| 103 |
Return(tc.metadataOutput, tc.metadataErr) |
| 104 |
|
| 105 |
detector := &resourceDetector{c: clientMock} |
| 106 |
res, _ := detector.Detect(t.Context()) |
| 107 |
|
| 108 |
if tc.expectedAttrs == nil { |
| 109 |
assert.Equal(t, resource.Empty(), res, "Resource should be empty") |
| 110 |
} else { |
| 111 |
expected := resource.NewWithAttributes(semconv.SchemaURL, tc.expectedAttrs...) |
| 112 |
assert.Equal(t, expected, res, "Resource returned is incorrect") |
| 113 |
} |
| 114 |
}) |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
func TestAWSInvalidClient(t *testing.T) { |
| 119 |
detector := &resourceDetector{c: nil} |
| 120 |
_, err := detector.Detect(t.Context()) |
| 121 |
assert.ErrorIs(t, err, errClient) |
| 122 |
} |
| 123 |
|
| 124 |
func TestRecordErrors(t *testing.T) { |
| 125 |
doc := validIdentityDocument() |
| 126 |
|
| 127 |
testCases := []testCase{ |
| 128 |
{ |
| 129 |
name: "404 returns no error", |
| 130 |
docOutput: doc, |
| 131 |
metadataErr: newAwsResponseError(404), |
| 132 |
}, |
| 133 |
{ |
| 134 |
name: "502 returns error", |
| 135 |
docOutput: doc, |
| 136 |
metadataErr: newAwsResponseError(502), |
| 137 |
expectedErr: resource.ErrPartialResource, |
| 138 |
}, |
| 139 |
} |
| 140 |
|
| 141 |
for _, tc := range testCases { |
| 142 |
t.Run(tc.name, func(t *testing.T) { |
| 143 |
clientMock := new(mockClient) |
| 144 |
|
| 145 |
clientMock.On("GetInstanceIdentityDocument", mock.Anything, mock.Anything, mock.Anything). |
| 146 |
Return(tc.docOutput, tc.docErr) |
| 147 |
clientMock.On("GetMetadata", mock.Anything, mock.Anything, mock.Anything). |
| 148 |
Return(tc.metadataOutput, tc.metadataErr) |
| 149 |
|
| 150 |
detector := &resourceDetector{c: clientMock} |
| 151 |
_, err := detector.Detect(t.Context()) |
| 152 |
assert.ErrorIs(t, err, tc.expectedErr) |
| 153 |
}) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func validIdentityDocument() *imds.GetInstanceIdentityDocumentOutput { |
| 158 |
doc := imds.InstanceIdentityDocument{ |
| 159 |
MarketplaceProductCodes: []string{"1abc2defghijklm3nopqrs4tu"}, |
| 160 |
AvailabilityZone: "us-west-2b", |
| 161 |
PrivateIP: "10.158.112.84", |
| 162 |
Version: "2017-09-30", |
| 163 |
Region: "us-west-2", |
| 164 |
InstanceID: "i-1234567890abcdef0", |
| 165 |
InstanceType: "t2.micro", |
| 166 |
AccountID: "123456789012", |
| 167 |
PendingTime: time.Date(2016, time.November, 19, 16, 32, 11, 0, time.UTC), |
| 168 |
ImageID: "ami-5fb8c835", |
| 169 |
Architecture: "x86_64", |
| 170 |
} |
| 171 |
|
| 172 |
return &imds.GetInstanceIdentityDocumentOutput{ |
| 173 |
InstanceIdentityDocument: doc, |
| 174 |
ResultMetadata: middleware.Metadata{}, |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
func mockMetadataOutput(val string) *imds.GetMetadataOutput { |
| 179 |
return &imds.GetMetadataOutput{ |
| 180 |
Content: io.NopCloser(bytes.NewReader([]byte(val))), |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
func newAwsResponseError(statusCode int) *awshttp.ResponseError { |
| 185 |
err := &smithyhttp.ResponseError{ |
| 186 |
Response: &smithyhttp.Response{ |
| 187 |
Response: &http.Response{ |
| 188 |
StatusCode: statusCode, |
| 189 |
Body: io.NopCloser(strings.NewReader("Bad Request")), |
| 190 |
Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 191 |
}, |
| 192 |
}, |
| 193 |
Err: errors.New("error fetching metadata"), |
| 194 |
} |
| 195 |
|
| 196 |
return &awshttp.ResponseError{ |
| 197 |
ResponseError: err, |
| 198 |
RequestID: "test123", |
| 199 |
} |
| 200 |
} |