github.com/aavshr/aws-sdk-go@v1.41.3/service/docdb/customizations_test.go (about) 1 //go:build go1.9 2 // +build go1.9 3 4 package docdb 5 6 import ( 7 "fmt" 8 "io/ioutil" 9 "net/url" 10 "regexp" 11 "testing" 12 "time" 13 14 "github.com/aavshr/aws-sdk-go/aws" 15 "github.com/aavshr/aws-sdk-go/aws/request" 16 "github.com/aavshr/aws-sdk-go/awstesting" 17 "github.com/aavshr/aws-sdk-go/awstesting/unit" 18 ) 19 20 func TestCopyDBClusterSnapshotRequestNoPanic(t *testing.T) { 21 svc := New(unit.Session, &aws.Config{Region: aws.String("us-west-2")}) 22 23 f := func() { 24 // Doesn't panic on nil input 25 req, _ := svc.CopyDBClusterSnapshotRequest(nil) 26 req.Sign() 27 } 28 if paniced, p := awstesting.DidPanic(f); paniced { 29 t.Errorf("expect no panic, got %v", p) 30 } 31 } 32 33 func TestPresignCrossRegionRequest(t *testing.T) { 34 const targetRegion = "us-west-2" 35 36 svc := New(unit.Session, &aws.Config{Region: aws.String(targetRegion)}) 37 38 const regexPattern = `^https://rds.us-west-1\.amazonaws\.com/\?Action=%s.+?DestinationRegion=%s.+` 39 40 cases := map[string]struct { 41 Req *request.Request 42 Assert func(*testing.T, string) 43 }{ 44 opCopyDBClusterSnapshot: { 45 Req: func() *request.Request { 46 req, _ := svc.CopyDBClusterSnapshotRequest( 47 &CopyDBClusterSnapshotInput{ 48 SourceRegion: aws.String("us-west-1"), 49 SourceDBClusterSnapshotIdentifier: aws.String("foo"), 50 TargetDBClusterSnapshotIdentifier: aws.String("bar"), 51 }) 52 return req 53 }(), 54 Assert: assertAsRegexMatch(fmt.Sprintf(regexPattern, 55 opCopyDBClusterSnapshot, targetRegion)), 56 }, 57 opCreateDBCluster: { 58 Req: func() *request.Request { 59 req, _ := svc.CreateDBClusterRequest( 60 &CreateDBClusterInput{ 61 SourceRegion: aws.String("us-west-1"), 62 DBClusterIdentifier: aws.String("foo"), 63 Engine: aws.String("bar"), 64 MasterUsername: aws.String("user"), 65 MasterUserPassword: aws.String("password"), 66 }) 67 return req 68 }(), 69 Assert: assertAsRegexMatch(fmt.Sprintf(regexPattern, 70 opCreateDBCluster, targetRegion)), 71 }, 72 opCopyDBClusterSnapshot + " same region": { 73 Req: func() *request.Request { 74 req, _ := svc.CopyDBClusterSnapshotRequest( 75 &CopyDBClusterSnapshotInput{ 76 SourceRegion: aws.String("us-west-2"), 77 SourceDBClusterSnapshotIdentifier: aws.String("foo"), 78 TargetDBClusterSnapshotIdentifier: aws.String("bar"), 79 }) 80 return req 81 }(), 82 Assert: assertAsEmpty(), 83 }, 84 opCreateDBCluster + " same region": { 85 Req: func() *request.Request { 86 req, _ := svc.CreateDBClusterRequest( 87 &CreateDBClusterInput{ 88 SourceRegion: aws.String("us-west-2"), 89 DBClusterIdentifier: aws.String("foo"), 90 Engine: aws.String("bar"), 91 MasterUsername: aws.String("user"), 92 MasterUserPassword: aws.String("password"), 93 }) 94 return req 95 }(), 96 Assert: assertAsEmpty(), 97 }, 98 opCopyDBClusterSnapshot + " presignURL set": { 99 Req: func() *request.Request { 100 req, _ := svc.CopyDBClusterSnapshotRequest( 101 &CopyDBClusterSnapshotInput{ 102 SourceRegion: aws.String("us-west-1"), 103 SourceDBClusterSnapshotIdentifier: aws.String("foo"), 104 TargetDBClusterSnapshotIdentifier: aws.String("bar"), 105 PreSignedUrl: aws.String("mockPresignedURL"), 106 }) 107 return req 108 }(), 109 Assert: assertAsEqual("mockPresignedURL"), 110 }, 111 opCreateDBCluster + " presignURL set": { 112 Req: func() *request.Request { 113 req, _ := svc.CreateDBClusterRequest( 114 &CreateDBClusterInput{ 115 SourceRegion: aws.String("us-west-1"), 116 DBClusterIdentifier: aws.String("foo"), 117 Engine: aws.String("bar"), 118 PreSignedUrl: aws.String("mockPresignedURL"), 119 MasterUsername: aws.String("user"), 120 MasterUserPassword: aws.String("password"), 121 }) 122 return req 123 }(), 124 Assert: assertAsEqual("mockPresignedURL"), 125 }, 126 } 127 128 for name, c := range cases { 129 t.Run(name, func(t *testing.T) { 130 if err := c.Req.Sign(); err != nil { 131 t.Fatalf("expect no error, got %v", err) 132 } 133 b, _ := ioutil.ReadAll(c.Req.HTTPRequest.Body) 134 q, _ := url.ParseQuery(string(b)) 135 136 u, _ := url.QueryUnescape(q.Get("PreSignedUrl")) 137 138 c.Assert(t, u) 139 }) 140 } 141 } 142 143 func TestPresignWithSourceNotSet(t *testing.T) { 144 reqs := map[string]*request.Request{} 145 svc := New(unit.Session, &aws.Config{Region: aws.String("us-west-2")}) 146 147 reqs[opCopyDBClusterSnapshot], _ = svc.CopyDBClusterSnapshotRequest(&CopyDBClusterSnapshotInput{ 148 SourceDBClusterSnapshotIdentifier: aws.String("foo"), 149 TargetDBClusterSnapshotIdentifier: aws.String("bar"), 150 }) 151 152 for _, req := range reqs { 153 _, err := req.Presign(5 * time.Minute) 154 if err != nil { 155 t.Fatal(err) 156 } 157 } 158 } 159 160 func assertAsRegexMatch(exp string) func(*testing.T, string) { 161 return func(t *testing.T, v string) { 162 t.Helper() 163 164 if re, a := regexp.MustCompile(exp), v; !re.MatchString(a) { 165 t.Errorf("expect %s to match %s", re, a) 166 } 167 } 168 } 169 170 func assertAsEmpty() func(*testing.T, string) { 171 return func(t *testing.T, v string) { 172 t.Helper() 173 174 if len(v) != 0 { 175 t.Errorf("expect empty, got %v", v) 176 } 177 } 178 } 179 180 func assertAsEqual(expect string) func(*testing.T, string) { 181 return func(t *testing.T, v string) { 182 t.Helper() 183 184 if e, a := expect, v; e != a { 185 t.Errorf("expect %v, got %v", e, a) 186 } 187 } 188 }