Skip to content

Add test for respecting manually set aws sdk headers #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor
.idea
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/aws/aws-xray-sdk-go

go 1.15

require (
github.com/DATA-DOG/go-sqlmock v1.4.1
github.com/aws/aws-sdk-go v1.17.12
github.com/davecgh/go-spew v0.0.0-20160907170601-6d212800a42e // indirect
github.com/pkg/errors v0.8.1
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0 // indirect
github.com/aws/aws-sdk-go v1.36.3
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.1.4
golang.org/x/net v0.0.0-20190311183353-d8887717615a
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
)
38 changes: 26 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
github.com/DATA-DOG/go-sqlmock v1.4.1 h1:ThlnYciV1iM/V0OSF/dtkqWb6xo5qITT1TJBG1MRDJM=
github.com/DATA-DOG/go-sqlmock v1.4.1/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/aws/aws-sdk-go v1.17.12 h1:jMFwRUaM0LcfdenfvbDLePNoWSoCdOHqF4RCvSB4xNQ=
github.com/aws/aws-sdk-go v1.17.12/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/davecgh/go-spew v0.0.0-20160907170601-6d212800a42e h1:PuhFC+IBoXcNHhkaUTRc+kNN0MtTMPFoWWBXyNMw7ww=
github.com/davecgh/go-spew v0.0.0-20160907170601-6d212800a42e/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0 h1:GD+A8+e+wFkqje55/2fOVnZPkoDIu1VooBWfNrnY8Uo=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/aws/aws-sdk-go v1.36.3 h1:KYpG5OegwW3xgOsMxy01nj/Td281yxi1Ha2lJQJs4tI=
github.com/aws/aws-sdk-go v1.36.3/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.1.4 h1:ToftOQTytwshuOSj6bDSolVUa3GINfJP/fg3OkkOzQQ=
github.com/stretchr/testify v1.1.4/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b h1:uwuIcX0g4Yl1NC5XAz37xsr2lTtcqevgzYNVt49waME=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
38 changes: 30 additions & 8 deletions xray/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ package xray
import (
"context"
"encoding/json"
"github.com/aws/aws-sdk-go/aws/request"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
Expand All @@ -28,7 +30,7 @@ func TestAWS(t *testing.T) {
// Runs a suite of tests against two different methods of registering
// handlers on an AWS client.

type test func(context.Context, *TestDaemon, *testing.T, *lambda.Lambda)
type test func(context.Context, *TestDaemon, *testing.T, *lambda.Lambda, *http.Request)
tests := []struct {
name string
test test
Expand All @@ -38,6 +40,7 @@ func TestAWS(t *testing.T) {
{"successful connection", testClientSuccessfulConnection, false},
{"without segment", testClientWithoutSegment, false},
{"test data race", testAWSDataRace, false},
{"test manual header", testRespectManuallyAddedHeaders, false},
}

onClient := func(s *session.Session) *lambda.Lambda {
Expand Down Expand Up @@ -83,16 +86,17 @@ func TestAWS(t *testing.T) {
defer td.Close()

t.Run(test.name, func(t *testing.T) {
session, cleanup := fakeSession(t, test.failConn)
rr := &http.Request{} // Used to inspect the request received by service
session, cleanup := fakeSession(t, test.failConn, rr)
defer cleanup()
test.test(ctx, td, t, cons.constructor(session))
test.test(ctx, td, t, cons.constructor(session), rr)
})
}
})
}
}

func fakeSession(t *testing.T, failConn bool) (*session.Session, func()) {
func fakeSession(t *testing.T, failConn bool, requestReceived *http.Request) (*session.Session, func()) {
cfg := &aws.Config{
Region: aws.String("fake-moon-1"),
Credentials: credentials.NewStaticCredentials("akid", "secret", "noop"),
Expand All @@ -102,6 +106,7 @@ func fakeSession(t *testing.T, failConn bool) (*session.Session, func()) {
if !failConn {
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b := []byte(`{}`)
requestReceived.Header = r.Header
w.WriteHeader(http.StatusOK)
w.Write(b)
}))
Expand All @@ -116,7 +121,7 @@ func fakeSession(t *testing.T, failConn bool) (*session.Session, func()) {
}
}

func testClientSuccessfulConnection(ctx context.Context, td *TestDaemon, t *testing.T, svc *lambda.Lambda) {
func testClientSuccessfulConnection(ctx context.Context, td *TestDaemon, t *testing.T, svc *lambda.Lambda, rr *http.Request) {
ctx, root := BeginSegment(ctx, "Test")
_, err := svc.ListFunctionsWithContext(ctx, &lambda.ListFunctionsInput{})
root.Close(nil)
Expand Down Expand Up @@ -147,6 +152,10 @@ func testClientSuccessfulConnection(ctx context.Context, td *TestDaemon, t *test
assert.Equal(t, "attempt", attemptSubseg.Name)
assert.Zero(t, attemptSubseg.openSegments)

// Test trace header created & received properly
assert.NotNil(t, rr.Header.Get(TraceIDHeaderKey))
assert.Equal(t, 3, len(strings.Split(rr.Header.Get(TraceIDHeaderKey), ";")))

// Connect subsegment will contain multiple child subsegments.
// The subsegment should fail since the endpoint is not valid,
// and should not be InProgress.
Expand All @@ -167,7 +176,7 @@ func testClientSuccessfulConnection(ctx context.Context, td *TestDaemon, t *test
}
}

func testClientFailedConnection(ctx context.Context, td *TestDaemon, t *testing.T, svc *lambda.Lambda) {
func testClientFailedConnection(ctx context.Context, td *TestDaemon, t *testing.T, svc *lambda.Lambda, rr *http.Request) {
ctx, root := BeginSegment(ctx, "Test")
_, err := svc.ListFunctionsWithContext(ctx, &lambda.ListFunctionsInput{})
root.Close(nil)
Expand Down Expand Up @@ -203,12 +212,12 @@ func testClientFailedConnection(ctx context.Context, td *TestDaemon, t *testing.
assert.NotEmpty(t, connectSubseg.Subsegments)
}

func testClientWithoutSegment(ctx context.Context, td *TestDaemon, t *testing.T, svc *lambda.Lambda) {
func testClientWithoutSegment(ctx context.Context, td *TestDaemon, t *testing.T, svc *lambda.Lambda, rr *http.Request) {
_, err := svc.ListFunctionsWithContext(ctx, &lambda.ListFunctionsInput{})
assert.NoError(t, err)
}

func testAWSDataRace(ctx context.Context, td *TestDaemon, t *testing.T, svc *lambda.Lambda) {
func testAWSDataRace(ctx context.Context, td *TestDaemon, t *testing.T, svc *lambda.Lambda, rr *http.Request) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ctx, seg := BeginSegment(ctx, "TestSegment")
Expand Down Expand Up @@ -236,3 +245,16 @@ func testAWSDataRace(ctx context.Context, td *TestDaemon, t *testing.T, svc *lam
wg.Wait()
seg.Close(nil)
}

func testRespectManuallyAddedHeaders(ctx context.Context, td *TestDaemon, t *testing.T, svc *lambda.Lambda, rr *http.Request) {
header := "custom-header"
headers := map[string]string{
TraceIDHeaderKey: header,
}

out, err := svc.ListFunctionsWithContext(ctx, &lambda.ListFunctionsInput{}, request.WithSetRequestHeaders(headers))
assert.Nil(t, err)
assert.NotNil(t, out)
assert.NotNil(t, rr.Header.Get(TraceIDHeaderKey))
assert.Equal(t, header, rr.Header.Get(TraceIDHeaderKey))
}