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

     1  //go:build go1.7
     2  // +build go1.7
     3  
     4  package ec2_test
     5  
     6  import (
     7  	"bytes"
     8  	"context"
     9  	"io/ioutil"
    10  	"net/http"
    11  	"net/url"
    12  	"regexp"
    13  	"testing"
    14  
    15  	"github.com/aavshr/aws-sdk-go/aws"
    16  	sdkclient "github.com/aavshr/aws-sdk-go/aws/client"
    17  	"github.com/aavshr/aws-sdk-go/aws/request"
    18  	"github.com/aavshr/aws-sdk-go/awstesting/unit"
    19  	"github.com/aavshr/aws-sdk-go/service/ec2"
    20  )
    21  
    22  func TestCopySnapshotPresignedURL(t *testing.T) {
    23  	svc := ec2.New(unit.Session, &aws.Config{Region: aws.String("us-west-2")})
    24  
    25  	func() {
    26  		defer func() {
    27  			if r := recover(); r != nil {
    28  				t.Fatalf("expect CopySnapshotRequest with nill")
    29  			}
    30  		}()
    31  		// Doesn't panic on nil input
    32  		req, _ := svc.CopySnapshotRequest(nil)
    33  		req.Sign()
    34  	}()
    35  
    36  	req, _ := svc.CopySnapshotRequest(&ec2.CopySnapshotInput{
    37  		SourceRegion:     aws.String("us-west-1"),
    38  		SourceSnapshotId: aws.String("snap-id"),
    39  	})
    40  	req.Sign()
    41  
    42  	b, _ := ioutil.ReadAll(req.HTTPRequest.Body)
    43  	q, _ := url.ParseQuery(string(b))
    44  	u, _ := url.QueryUnescape(q.Get("PresignedUrl"))
    45  	if e, a := "us-west-2", q.Get("DestinationRegion"); e != a {
    46  		t.Errorf("expect %v, got %v", e, a)
    47  	}
    48  	if e, a := "us-west-1", q.Get("SourceRegion"); e != a {
    49  		t.Errorf("expect %v, got %v", e, a)
    50  	}
    51  
    52  	r := regexp.MustCompile(`^https://ec2\.us-west-1\.amazonaws\.com/.+&DestinationRegion=us-west-2`)
    53  	if !r.MatchString(u) {
    54  		t.Errorf("expect %v to match, got %v", r.String(), u)
    55  	}
    56  }
    57  
    58  func TestNoCustomRetryerWithMaxRetries(t *testing.T) {
    59  	cases := map[string]struct {
    60  		Config           aws.Config
    61  		ExpectMaxRetries int
    62  	}{
    63  		"With custom retrier": {
    64  			Config: aws.Config{
    65  				Retryer: sdkclient.DefaultRetryer{
    66  					NumMaxRetries: 10,
    67  				},
    68  			},
    69  			ExpectMaxRetries: 10,
    70  		},
    71  		"with max retries": {
    72  			Config: aws.Config{
    73  				MaxRetries: aws.Int(10),
    74  			},
    75  			ExpectMaxRetries: 10,
    76  		},
    77  		"no options set": {
    78  			ExpectMaxRetries: sdkclient.DefaultRetryerMaxNumRetries,
    79  		},
    80  	}
    81  
    82  	for name, c := range cases {
    83  		t.Run(name, func(t *testing.T) {
    84  			client := ec2.New(unit.Session, &aws.Config{
    85  				DisableParamValidation: aws.Bool(true),
    86  			}, c.Config.Copy())
    87  			client.ModifyNetworkInterfaceAttributeWithContext(context.Background(), nil, checkRetryerMaxRetries(t, c.ExpectMaxRetries))
    88  			client.AssignPrivateIpAddressesWithContext(context.Background(), nil, checkRetryerMaxRetries(t, c.ExpectMaxRetries))
    89  		})
    90  	}
    91  
    92  }
    93  
    94  func checkRetryerMaxRetries(t *testing.T, maxRetries int) func(*request.Request) {
    95  	return func(r *request.Request) {
    96  		r.Handlers.Send.Clear()
    97  		r.Handlers.Send.PushBack(func(rr *request.Request) {
    98  			if e, a := maxRetries, rr.Retryer.MaxRetries(); e != a {
    99  				t.Errorf("%s, expect %v max retries, got %v", rr.Operation.Name, e, a)
   100  			}
   101  			rr.HTTPResponse = &http.Response{
   102  				StatusCode: 200,
   103  				Header:     http.Header{},
   104  				Body:       ioutil.NopCloser(&bytes.Buffer{}),
   105  			}
   106  		})
   107  	}
   108  }