| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelaws_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"net/http" |
| 9 |
"net/http/httptest" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/aws/aws-sdk-go-v2/aws" |
| 13 |
"github.com/aws/aws-sdk-go-v2/service/route53" |
| 14 |
"github.com/aws/aws-sdk-go-v2/service/route53/types" |
| 15 |
smithyauth "github.com/aws/smithy-go/auth" |
| 16 |
smithyhttp "github.com/aws/smithy-go/transport/http" |
| 17 |
"github.com/stretchr/testify/assert" |
| 18 |
"github.com/stretchr/testify/require" |
| 19 |
"go.opentelemetry.io/otel/attribute" |
| 20 |
"go.opentelemetry.io/otel/codes" |
| 21 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 22 |
"go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 23 |
"go.opentelemetry.io/otel/trace" |
| 24 |
|
| 25 |
"go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws" |
| 26 |
) |
| 27 |
|
| 28 |
type route53AuthResolver struct{} |
| 29 |
|
| 30 |
func (*route53AuthResolver) ResolveAuthSchemes(context.Context, *route53.AuthResolverParameters) ([]*smithyauth.Option, error) { |
| 31 |
return []*smithyauth.Option{ |
| 32 |
{SchemeID: smithyauth.SchemeIDAnonymous}, |
| 33 |
}, nil |
| 34 |
} |
| 35 |
|
| 36 |
func TestAppendMiddlewares(t *testing.T) { |
| 37 |
cases := map[string]struct { |
| 38 |
responseStatus int |
| 39 |
responseBody []byte |
| 40 |
expectedRegion string |
| 41 |
expectedError codes.Code |
| 42 |
expectedRequestID string |
| 43 |
expectedStatusCode int |
| 44 |
}{ |
| 45 |
"invalidChangeBatchError": { |
| 46 |
responseStatus: http.StatusInternalServerError, |
| 47 |
responseBody: []byte(`<?xml version="1.0" encoding="UTF-8"?> |
| 48 |
<InvalidChangeBatch xmlns="https://route53.amazonaws.com/doc/2013-04-01/"> |
| 49 |
<Messages> |
| 50 |
<Message>Tried to create resource record set duplicate.example.com. type A, but it already exists</Message> |
| 51 |
</Messages> |
| 52 |
<RequestId>b25f48e8-84fd-11e6-80d9-574e0c4664cb</RequestId> |
| 53 |
</InvalidChangeBatch>`), |
| 54 |
expectedRegion: "us-east-1", |
| 55 |
expectedError: codes.Error, |
| 56 |
expectedRequestID: "b25f48e8-84fd-11e6-80d9-574e0c4664cb", |
| 57 |
expectedStatusCode: http.StatusInternalServerError, |
| 58 |
}, |
| 59 |
|
| 60 |
"standardRestXMLError": { |
| 61 |
responseStatus: http.StatusNotFound, |
| 62 |
responseBody: []byte(`<?xml version="1.0"?> |
| 63 |
<ErrorResponse xmlns="http://route53.amazonaws.com/doc/2016-09-07/"> |
| 64 |
<Error> |
| 65 |
<Type>Sender</Type> |
| 66 |
<Code>MalformedXML</Code> |
| 67 |
<Message>1 validation error detected: Value null at 'route53#ChangeSet' failed to satisfy constraint: Member must not be null</Message> |
| 68 |
</Error> |
| 69 |
<RequestId>1234567890A</RequestId> |
| 70 |
</ErrorResponse> |
| 71 |
`), |
| 72 |
expectedRegion: "us-west-1", |
| 73 |
expectedError: codes.Error, |
| 74 |
expectedRequestID: "1234567890A", |
| 75 |
expectedStatusCode: http.StatusNotFound, |
| 76 |
}, |
| 77 |
|
| 78 |
"Success response": { |
| 79 |
responseStatus: http.StatusOK, |
| 80 |
responseBody: []byte(`<?xml version="1.0" encoding="UTF-8"?> |
| 81 |
<ChangeResourceRecordSetsResponse> |
| 82 |
<ChangeInfo> |
| 83 |
<Comment>mockComment</Comment> |
| 84 |
<Id>mockID</Id> |
| 85 |
</ChangeInfo> |
| 86 |
</ChangeResourceRecordSetsResponse>`), |
| 87 |
expectedRegion: "us-west-2", |
| 88 |
expectedStatusCode: http.StatusOK, |
| 89 |
}, |
| 90 |
} |
| 91 |
|
| 92 |
for name, c := range cases { |
| 93 |
srv := httptest.NewServer(http.HandlerFunc( |
| 94 |
func(w http.ResponseWriter, _ *http.Request) { |
| 95 |
w.WriteHeader(c.responseStatus) |
| 96 |
_, err := w.Write(c.responseBody) |
| 97 |
if err != nil { |
| 98 |
t.Fatal(err) |
| 99 |
} |
| 100 |
})) |
| 101 |
|
| 102 |
t.Run(name, func(t *testing.T) { |
| 103 |
sr := tracetest.NewSpanRecorder() |
| 104 |
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) |
| 105 |
|
| 106 |
svc := route53.New(route53.Options{ |
| 107 |
Region: c.expectedRegion, |
| 108 |
BaseEndpoint: &srv.URL, |
| 109 |
AuthSchemeResolver: &route53AuthResolver{}, |
| 110 |
AuthSchemes: []smithyhttp.AuthScheme{ |
| 111 |
smithyhttp.NewAnonymousScheme(), |
| 112 |
}, |
| 113 |
Retryer: aws.NopRetryer{}, |
| 114 |
}) |
| 115 |
|
| 116 |
_, err := svc.ChangeResourceRecordSets(t.Context(), &route53.ChangeResourceRecordSetsInput{ |
| 117 |
ChangeBatch: &types.ChangeBatch{ |
| 118 |
Changes: []types.Change{}, |
| 119 |
Comment: aws.String("mock"), |
| 120 |
}, |
| 121 |
HostedZoneId: aws.String("zone"), |
| 122 |
}, func(options *route53.Options) { |
| 123 |
otelaws.AppendMiddlewares( |
| 124 |
&options.APIOptions, otelaws.WithTracerProvider(provider)) |
| 125 |
}) |
| 126 |
if c.expectedError == codes.Unset { |
| 127 |
assert.NoError(t, err) |
| 128 |
} else { |
| 129 |
assert.Error(t, err) |
| 130 |
} |
| 131 |
|
| 132 |
spans := sr.Ended() |
| 133 |
require.Len(t, spans, 1) |
| 134 |
span := spans[0] |
| 135 |
|
| 136 |
assert.Equal(t, "Route 53.ChangeResourceRecordSets", span.Name()) |
| 137 |
assert.Equal(t, trace.SpanKindClient, span.SpanKind()) |
| 138 |
assert.Equal(t, c.expectedError, span.Status().Code) |
| 139 |
attrs := span.Attributes() |
| 140 |
assert.Contains(t, attrs, attribute.Int("http.response.status_code", c.expectedStatusCode)) |
| 141 |
if c.expectedRequestID != "" { |
| 142 |
assert.Contains(t, attrs, attribute.String("aws.request_id", c.expectedRequestID)) |
| 143 |
} |
| 144 |
assert.Contains(t, attrs, attribute.String("rpc.system.name", "aws-api")) |
| 145 |
assert.Contains(t, attrs, attribute.String("aws.region", c.expectedRegion)) |
| 146 |
assert.Contains(t, attrs, attribute.String("rpc.method", "Route 53/ChangeResourceRecordSets")) |
| 147 |
}) |
| 148 |
|
| 149 |
srv.Close() |
| 150 |
} |
| 151 |
} |