|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package otelaws |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "net/http" |
| 20 | + "net/http/httptest" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 25 | + "github.com/aws/aws-sdk-go-v2/service/route53" |
| 26 | + "github.com/aws/aws-sdk-go-v2/service/route53/types" |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | + |
| 29 | + "go.opentelemetry.io/otel/oteltest" |
| 30 | + "go.opentelemetry.io/otel/trace" |
| 31 | +) |
| 32 | + |
| 33 | +func TestAppendMiddlewares(t *testing.T) { |
| 34 | + cases := map[string]struct { |
| 35 | + responseStatus int |
| 36 | + responseBody []byte |
| 37 | + expectedRegion string |
| 38 | + expectedError string |
| 39 | + expectedRequestID string |
| 40 | + expectedStatusCode int |
| 41 | + }{ |
| 42 | + "invalidChangeBatchError": { |
| 43 | + responseStatus: 500, |
| 44 | + responseBody: []byte(`<?xml version="1.0" encoding="UTF-8"?> |
| 45 | + <InvalidChangeBatch xmlns="https://route53.amazonaws.com/doc/2013-04-01/"> |
| 46 | + <Messages> |
| 47 | + <Message>Tried to create resource record set duplicate.example.com. type A, but it already exists</Message> |
| 48 | + </Messages> |
| 49 | + <RequestId>b25f48e8-84fd-11e6-80d9-574e0c4664cb</RequestId> |
| 50 | + </InvalidChangeBatch>`), |
| 51 | + expectedRegion: "us-east-1", |
| 52 | + expectedError: "Error", |
| 53 | + expectedRequestID: "b25f48e8-84fd-11e6-80d9-574e0c4664cb", |
| 54 | + expectedStatusCode: 500, |
| 55 | + }, |
| 56 | + |
| 57 | + "standardRestXMLError": { |
| 58 | + responseStatus: 404, |
| 59 | + responseBody: []byte(`<?xml version="1.0"?> |
| 60 | + <ErrorResponse xmlns="http://route53.amazonaws.com/doc/2016-09-07/"> |
| 61 | + <Error> |
| 62 | + <Type>Sender</Type> |
| 63 | + <Code>MalformedXML</Code> |
| 64 | + <Message>1 validation error detected: Value null at 'route53#ChangeSet' failed to satisfy constraint: Member must not be null</Message> |
| 65 | + </Error> |
| 66 | + <RequestId>1234567890A</RequestId> |
| 67 | + </ErrorResponse> |
| 68 | + `), |
| 69 | + expectedRegion: "us-west-1", |
| 70 | + expectedError: "Error", |
| 71 | + expectedRequestID: "1234567890A", |
| 72 | + expectedStatusCode: 404, |
| 73 | + }, |
| 74 | + |
| 75 | + "Success response": { |
| 76 | + responseStatus: 200, |
| 77 | + responseBody: []byte(`<?xml version="1.0" encoding="UTF-8"?> |
| 78 | + <ChangeResourceRecordSetsResponse> |
| 79 | + <ChangeInfo> |
| 80 | + <Comment>mockComment</Comment> |
| 81 | + <Id>mockID</Id> |
| 82 | + </ChangeInfo> |
| 83 | + </ChangeResourceRecordSetsResponse>`), |
| 84 | + expectedRegion: "us-west-2", |
| 85 | + expectedStatusCode: 200, |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + for name, c := range cases { |
| 90 | + server := httptest.NewServer(http.HandlerFunc( |
| 91 | + func(w http.ResponseWriter, r *http.Request) { |
| 92 | + w.WriteHeader(c.responseStatus) |
| 93 | + _, err := w.Write(c.responseBody) |
| 94 | + if err != nil { |
| 95 | + t.Fatal(err) |
| 96 | + } |
| 97 | + })) |
| 98 | + defer server.Close() |
| 99 | + |
| 100 | + t.Run(name, func(t *testing.T) { |
| 101 | + sr := new(oteltest.SpanRecorder) |
| 102 | + provider := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr)) |
| 103 | + |
| 104 | + svc := route53.NewFromConfig(aws.Config{ |
| 105 | + Region: c.expectedRegion, |
| 106 | + EndpointResolver: aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) { |
| 107 | + return aws.Endpoint{ |
| 108 | + URL: server.URL, |
| 109 | + SigningName: "route53", |
| 110 | + }, nil |
| 111 | + }), |
| 112 | + Retryer: func() aws.Retryer { |
| 113 | + return aws.NopRetryer{} |
| 114 | + }, |
| 115 | + }) |
| 116 | + _, err := svc.ChangeResourceRecordSets(context.Background(), &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 | + AppendMiddlewares( |
| 124 | + &options.APIOptions, WithTracerProvider(provider)) |
| 125 | + }) |
| 126 | + |
| 127 | + spans := sr.Completed() |
| 128 | + assert.Len(t, spans, 1) |
| 129 | + span := spans[0] |
| 130 | + |
| 131 | + if e, a := "Route 53", span.Name(); !strings.EqualFold(e, a) { |
| 132 | + t.Errorf("expected span name to be %s, got %s", e, a) |
| 133 | + } |
| 134 | + |
| 135 | + if e, a := trace.SpanKindClient, span.SpanKind(); e != a { |
| 136 | + t.Errorf("expected span kind to be %v, got %v", e, a) |
| 137 | + } |
| 138 | + |
| 139 | + if e, a := c.expectedError, span.StatusCode().String(); err != nil && !strings.EqualFold(e, a) { |
| 140 | + t.Errorf("Span Error is missing.") |
| 141 | + } |
| 142 | + |
| 143 | + if e, a := c.expectedStatusCode, span.Attributes()["http.status_code"].AsInt64(); e != int(a) { |
| 144 | + t.Errorf("expected status code to be %v, got %v", e, a) |
| 145 | + } |
| 146 | + |
| 147 | + if e, a := c.expectedRequestID, span.Attributes()["aws.request_id"].AsString(); !strings.EqualFold(e, a) { |
| 148 | + t.Errorf("expected request id to be %s, got %s", e, a) |
| 149 | + } |
| 150 | + |
| 151 | + if e, a := "Route 53", span.Attributes()["aws.service"].AsString(); !strings.EqualFold(e, a) { |
| 152 | + t.Errorf("expected service to be %s, got %s", e, a) |
| 153 | + } |
| 154 | + |
| 155 | + if e, a := c.expectedRegion, span.Attributes()["aws.region"].AsString(); !strings.EqualFold(e, a) { |
| 156 | + t.Errorf("expected region to be %s, got %s", e, a) |
| 157 | + } |
| 158 | + |
| 159 | + if e, a := "ChangeResourceRecordSets", span.Attributes()["aws.operation"].AsString(); !strings.EqualFold(e, a) { |
| 160 | + t.Errorf("expected operation to be %s, got %s", e, a) |
| 161 | + } |
| 162 | + }) |
| 163 | + |
| 164 | + } |
| 165 | +} |
0 commit comments