github.com/aavshr/aws-sdk-go@v1.41.3/service/rds/customizations_test.go (about)

     1  //go:build go1.9
     2  // +build go1.9
     3  
     4  package rds
     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 TestCopyDBSnapshotNoPanic(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.CopyDBSnapshotRequest(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  		opCopyDBSnapshot: {
    45  			Req: func() *request.Request {
    46  				req, _ := svc.CopyDBSnapshotRequest(&CopyDBSnapshotInput{
    47  					SourceRegion:               aws.String("us-west-1"),
    48  					SourceDBSnapshotIdentifier: aws.String("foo"),
    49  					TargetDBSnapshotIdentifier: aws.String("bar"),
    50  				})
    51  				return req
    52  			}(),
    53  			Assert: assertAsRegexMatch(fmt.Sprintf(regexPattern,
    54  				opCopyDBSnapshot, targetRegion)),
    55  		},
    56  		opCreateDBInstanceReadReplica: {
    57  			Req: func() *request.Request {
    58  				req, _ := svc.CreateDBInstanceReadReplicaRequest(
    59  					&CreateDBInstanceReadReplicaInput{
    60  						SourceRegion:               aws.String("us-west-1"),
    61  						SourceDBInstanceIdentifier: aws.String("foo"),
    62  						DBInstanceIdentifier:       aws.String("bar"),
    63  					})
    64  				return req
    65  			}(),
    66  			Assert: assertAsRegexMatch(fmt.Sprintf(regexPattern,
    67  				opCreateDBInstanceReadReplica, targetRegion)),
    68  		},
    69  		opCopyDBClusterSnapshot: {
    70  			Req: func() *request.Request {
    71  				req, _ := svc.CopyDBClusterSnapshotRequest(
    72  					&CopyDBClusterSnapshotInput{
    73  						SourceRegion:                      aws.String("us-west-1"),
    74  						SourceDBClusterSnapshotIdentifier: aws.String("foo"),
    75  						TargetDBClusterSnapshotIdentifier: aws.String("bar"),
    76  					})
    77  				return req
    78  			}(),
    79  			Assert: assertAsRegexMatch(fmt.Sprintf(regexPattern,
    80  				opCopyDBClusterSnapshot, targetRegion)),
    81  		},
    82  		opCreateDBCluster: {
    83  			Req: func() *request.Request {
    84  				req, _ := svc.CreateDBClusterRequest(
    85  					&CreateDBClusterInput{
    86  						SourceRegion:        aws.String("us-west-1"),
    87  						DBClusterIdentifier: aws.String("foo"),
    88  						Engine:              aws.String("bar"),
    89  					})
    90  				return req
    91  			}(),
    92  			Assert: assertAsRegexMatch(fmt.Sprintf(regexPattern,
    93  				opCreateDBCluster, targetRegion)),
    94  		},
    95  		opStartDBInstanceAutomatedBackupsReplication: {
    96  			Req: func() *request.Request {
    97  				req, _ := svc.StartDBInstanceAutomatedBackupsReplicationRequest(
    98  					&StartDBInstanceAutomatedBackupsReplicationInput{
    99  						SourceRegion:        aws.String("us-west-1"),
   100  						SourceDBInstanceArn: aws.String("bar"),
   101  					})
   102  				return req
   103  			}(),
   104  			Assert: assertAsRegexMatch(fmt.Sprintf(regexPattern,
   105  				opStartDBInstanceAutomatedBackupsReplication, targetRegion)),
   106  		},
   107  		opCopyDBSnapshot + " same region": {
   108  			Req: func() *request.Request {
   109  				req, _ := svc.CopyDBSnapshotRequest(&CopyDBSnapshotInput{
   110  					SourceRegion:               aws.String("us-west-2"),
   111  					SourceDBSnapshotIdentifier: aws.String("foo"),
   112  					TargetDBSnapshotIdentifier: aws.String("bar"),
   113  				})
   114  				return req
   115  			}(),
   116  			Assert: assertAsEmpty(),
   117  		},
   118  		opCreateDBInstanceReadReplica + " same region": {
   119  			Req: func() *request.Request {
   120  				req, _ := svc.CreateDBInstanceReadReplicaRequest(&CreateDBInstanceReadReplicaInput{
   121  					SourceRegion:               aws.String("us-west-2"),
   122  					SourceDBInstanceIdentifier: aws.String("foo"),
   123  					DBInstanceIdentifier:       aws.String("bar"),
   124  				})
   125  				return req
   126  			}(),
   127  			Assert: assertAsEmpty(),
   128  		},
   129  		opCopyDBClusterSnapshot + " same region": {
   130  			Req: func() *request.Request {
   131  				req, _ := svc.CopyDBClusterSnapshotRequest(
   132  					&CopyDBClusterSnapshotInput{
   133  						SourceRegion:                      aws.String("us-west-2"),
   134  						SourceDBClusterSnapshotIdentifier: aws.String("foo"),
   135  						TargetDBClusterSnapshotIdentifier: aws.String("bar"),
   136  					})
   137  				return req
   138  			}(),
   139  			Assert: assertAsEmpty(),
   140  		},
   141  		opCreateDBCluster + " same region": {
   142  			Req: func() *request.Request {
   143  				req, _ := svc.CreateDBClusterRequest(
   144  					&CreateDBClusterInput{
   145  						SourceRegion:        aws.String("us-west-2"),
   146  						DBClusterIdentifier: aws.String("foo"),
   147  						Engine:              aws.String("bar"),
   148  					})
   149  				return req
   150  			}(),
   151  			Assert: assertAsEmpty(),
   152  		},
   153  		opStartDBInstanceAutomatedBackupsReplication + " same region": {
   154  			Req: func() *request.Request {
   155  				req, _ := svc.StartDBInstanceAutomatedBackupsReplicationRequest(
   156  					&StartDBInstanceAutomatedBackupsReplicationInput{
   157  						SourceRegion:        aws.String("us-west-2"),
   158  						SourceDBInstanceArn: aws.String("bar"),
   159  					})
   160  				return req
   161  			}(),
   162  			Assert: assertAsEmpty(),
   163  		},
   164  		opCopyDBSnapshot + " presignURL set": {
   165  			Req: func() *request.Request {
   166  				req, _ := svc.CopyDBSnapshotRequest(&CopyDBSnapshotInput{
   167  					SourceRegion:               aws.String("us-west-1"),
   168  					SourceDBSnapshotIdentifier: aws.String("foo"),
   169  					TargetDBSnapshotIdentifier: aws.String("bar"),
   170  					PreSignedUrl:               aws.String("mockPresignedURL"),
   171  				})
   172  				return req
   173  			}(),
   174  			Assert: assertAsEqual("mockPresignedURL"),
   175  		},
   176  		opCreateDBInstanceReadReplica + " presignURL set": {
   177  			Req: func() *request.Request {
   178  				req, _ := svc.CreateDBInstanceReadReplicaRequest(&CreateDBInstanceReadReplicaInput{
   179  					SourceRegion:               aws.String("us-west-1"),
   180  					SourceDBInstanceIdentifier: aws.String("foo"),
   181  					DBInstanceIdentifier:       aws.String("bar"),
   182  					PreSignedUrl:               aws.String("mockPresignedURL"),
   183  				})
   184  				return req
   185  			}(),
   186  			Assert: assertAsEqual("mockPresignedURL"),
   187  		},
   188  		opCopyDBClusterSnapshot + " presignURL set": {
   189  			Req: func() *request.Request {
   190  				req, _ := svc.CopyDBClusterSnapshotRequest(
   191  					&CopyDBClusterSnapshotInput{
   192  						SourceRegion:                      aws.String("us-west-1"),
   193  						SourceDBClusterSnapshotIdentifier: aws.String("foo"),
   194  						TargetDBClusterSnapshotIdentifier: aws.String("bar"),
   195  						PreSignedUrl:                      aws.String("mockPresignedURL"),
   196  					})
   197  				return req
   198  			}(),
   199  			Assert: assertAsEqual("mockPresignedURL"),
   200  		},
   201  		opCreateDBCluster + " presignURL set": {
   202  			Req: func() *request.Request {
   203  				req, _ := svc.CreateDBClusterRequest(
   204  					&CreateDBClusterInput{
   205  						SourceRegion:        aws.String("us-west-1"),
   206  						DBClusterIdentifier: aws.String("foo"),
   207  						Engine:              aws.String("bar"),
   208  						PreSignedUrl:        aws.String("mockPresignedURL"),
   209  					})
   210  				return req
   211  			}(),
   212  			Assert: assertAsEqual("mockPresignedURL"),
   213  		},
   214  		opStartDBInstanceAutomatedBackupsReplication + " presignURL set": {
   215  			Req: func() *request.Request {
   216  				req, _ := svc.StartDBInstanceAutomatedBackupsReplicationRequest(
   217  					&StartDBInstanceAutomatedBackupsReplicationInput{
   218  						SourceRegion:        aws.String("us-west-1"),
   219  						KmsKeyId:            aws.String("foo"),
   220  						SourceDBInstanceArn: aws.String("bar"),
   221  						PreSignedUrl:        aws.String("mockPresignedURL"),
   222  					})
   223  				return req
   224  			}(),
   225  			Assert: assertAsEqual("mockPresignedURL"),
   226  		},
   227  	}
   228  
   229  	for name, c := range cases {
   230  		t.Run(name, func(t *testing.T) {
   231  			if err := c.Req.Sign(); err != nil {
   232  				t.Fatalf("expect no error, got %v", err)
   233  			}
   234  			b, _ := ioutil.ReadAll(c.Req.HTTPRequest.Body)
   235  			q, _ := url.ParseQuery(string(b))
   236  
   237  			u, _ := url.QueryUnescape(q.Get("PreSignedUrl"))
   238  
   239  			c.Assert(t, u)
   240  		})
   241  	}
   242  }
   243  
   244  func TestPresignWithSourceNotSet(t *testing.T) {
   245  	reqs := map[string]*request.Request{}
   246  	svc := New(unit.Session, &aws.Config{Region: aws.String("us-west-2")})
   247  
   248  	reqs[opCopyDBSnapshot], _ = svc.CopyDBSnapshotRequest(&CopyDBSnapshotInput{
   249  		SourceDBSnapshotIdentifier: aws.String("foo"),
   250  		TargetDBSnapshotIdentifier: aws.String("bar"),
   251  	})
   252  
   253  	for _, req := range reqs {
   254  		_, err := req.Presign(5 * time.Minute)
   255  		if err != nil {
   256  			t.Fatal(err)
   257  		}
   258  	}
   259  }
   260  
   261  func assertAsRegexMatch(exp string) func(*testing.T, string) {
   262  	return func(t *testing.T, v string) {
   263  		t.Helper()
   264  
   265  		if re, a := regexp.MustCompile(exp), v; !re.MatchString(a) {
   266  			t.Errorf("expect %s to match %s", re, a)
   267  		}
   268  	}
   269  }
   270  
   271  func assertAsEmpty() func(*testing.T, string) {
   272  	return func(t *testing.T, v string) {
   273  		t.Helper()
   274  
   275  		if len(v) != 0 {
   276  			t.Errorf("expect empty, got %v", v)
   277  		}
   278  	}
   279  }
   280  
   281  func assertAsEqual(expect string) func(*testing.T, string) {
   282  	return func(t *testing.T, v string) {
   283  		t.Helper()
   284  
   285  		if e, a := expect, v; e != a {
   286  			t.Errorf("expect %v, got %v", e, a)
   287  		}
   288  	}
   289  }