File: customizations_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.49.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312,636 kB
  • sloc: makefile: 120
file content (294 lines) | stat: -rw-r--r-- 9,865 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//go:build go1.7
// +build go1.7

package ec2_test

import (
	"bytes"
	"context"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"regexp"
	"strconv"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	sdkclient "github.com/aws/aws-sdk-go/aws/client"
	"github.com/aws/aws-sdk-go/aws/endpoints"
	"github.com/aws/aws-sdk-go/aws/request"
	"github.com/aws/aws-sdk-go/awstesting/unit"
	"github.com/aws/aws-sdk-go/service/ec2"
)

func TestCopySnapshotPresignedURL(t *testing.T) {
	svc := ec2.New(unit.Session, &aws.Config{Region: aws.String("us-west-2")})

	func() {
		defer func() {
			if r := recover(); r != nil {
				t.Fatalf("expect CopySnapshotRequest with nill")
			}
		}()
		// Doesn't panic on nil input
		req, _ := svc.CopySnapshotRequest(nil)
		req.Sign()
	}()

	req, _ := svc.CopySnapshotRequest(&ec2.CopySnapshotInput{
		SourceRegion:     aws.String("us-west-1"),
		SourceSnapshotId: aws.String("snap-id"),
	})
	req.Sign()

	b, _ := ioutil.ReadAll(req.HTTPRequest.Body)
	q, _ := url.ParseQuery(string(b))
	u, _ := url.QueryUnescape(q.Get("PresignedUrl"))
	if e, a := "us-west-2", q.Get("DestinationRegion"); e != a {
		t.Errorf("expect %v, got %v", e, a)
	}
	if e, a := "us-west-1", q.Get("SourceRegion"); e != a {
		t.Errorf("expect %v, got %v", e, a)
	}

	r := regexp.MustCompile(`^https://ec2\.us-west-1\.amazonaws\.com/.+&DestinationRegion=us-west-2`)
	if !r.MatchString(u) {
		t.Errorf("expect %v to match, got %v", r.String(), u)
	}
}

func TestCopySnapshotPresignedURLConfig(t *testing.T) {
	const (
		inputKmsKeyId     = "KMS_KEY_ID"
		inputSnapshotId   = "SNAPSHOT_ID"
		clientRegion      = endpoints.UsEast1RegionID
		inputSourceRegion = endpoints.UsWest2RegionID
	)
	cases := map[string]struct {
		Encrypted         bool
		DestinationRegion string
		KmsKeyId          string
	}{
		// Not Encrypted
		"Not Encrypted": {},
		// Not Encrypted with KmsKeyId
		"Not Encrypted with KmsKeyId": {
			KmsKeyId: inputKmsKeyId,
		},
		// Not Encrypted with DestinationRegion
		"Not Encrypted with DestinationRegion": {
			DestinationRegion: endpoints.UsEast2RegionID,
		},
		// Not Encrypted with KmsKeyId and DestinationRegion
		"Not Encrypted with KmsKeyId and DestinationRegion": {
			KmsKeyId:          inputKmsKeyId,
			DestinationRegion: endpoints.UsEast2RegionID,
		},
		// Encrypted
		"Encrypted": {
			Encrypted: true,
		},
		// Encrypted with KmsKeyId
		"Encrypted with KmsKeyId": {
			Encrypted: true,
			KmsKeyId:  inputKmsKeyId,
		},
		// Encrypted with DestinationRegion
		"Encrypted with DestinationRegion": {
			Encrypted:         true,
			DestinationRegion: endpoints.UsEast2RegionID,
		},
		// Encrypted with KmsKeyId and DestinationRegion
		"Encrypted with KmsKeyId and DestinationRegion": {
			Encrypted:         true,
			KmsKeyId:          inputKmsKeyId,
			DestinationRegion: endpoints.UsEast2RegionID,
		},
	}

	for name, config := range cases {
		t.Run(name, func(t *testing.T) {
			t.Log(name)

			// Set up new client
			svc := ec2.New(unit.Session, &aws.Config{
				Region: aws.String(clientRegion),
			})

			// Base input
			input := ec2.CopySnapshotInput{
				SourceRegion:     aws.String(inputSourceRegion),
				SourceSnapshotId: aws.String(inputSnapshotId),
			}

			// Add input from test case config
			if config.Encrypted != false {
				input.Encrypted = &config.Encrypted
			}
			if config.DestinationRegion != "" {
				input.DestinationRegion = &config.DestinationRegion
			}
			if config.KmsKeyId != "" {
				input.KmsKeyId = &config.KmsKeyId
			}

			// Execute request
			req, _ := svc.CopySnapshotRequest(&input)
			req.Sign()

			// Parse request
			body, _ := ioutil.ReadAll(req.HTTPRequest.Body)
			query, _ := url.ParseQuery(string(body))

			// Test Body SourceRegion
			sourceRegion := query.Get("SourceRegion")
			if sourceRegion == "" {
				t.Errorf("SourceRegion should always be sent in the request")
			}
			if sourceRegion != inputSourceRegion {
				t.Errorf("SourceRegion should be `%v`, but found `%v`", inputSourceRegion, sourceRegion)
			}
			// Test Body SourceSnapshotId
			sourceSnapshotId := query.Get("SourceSnapshotId")
			if sourceSnapshotId == "" {
				t.Errorf("SourceSnapshotId should always be sent in the request")
			}
			if sourceSnapshotId != inputSnapshotId {
				t.Errorf("SourceSnapshotId should be `%v`, but found `%v`", inputSnapshotId, sourceSnapshotId)
			}
			// Test Body Encrypted
			encrypted := query.Get("Encrypted")
			if config.Encrypted && strconv.FormatBool(config.Encrypted) != encrypted {
				t.Errorf("Encrypted should be `%v`, but found `%v`", config.Encrypted, encrypted)
			}
			if !config.Encrypted && encrypted != "" {
				t.Errorf("Encrypted should be empty, but found `%v`", encrypted)
			}
			// Test Body DestinationRegion
			destinationRegion := query.Get("DestinationRegion")
			if destinationRegion != clientRegion {
				t.Errorf("DestinationRegion should always be equal to the client region `%v`, but found `%v`", clientRegion, destinationRegion)
			}
			if destinationRegion == "" {
				t.Errorf("DestinationRegion should never empty")
			}
			// Test Body KmsKeyId
			kmsKeyId := query.Get("KmsKeyId")
			if config.KmsKeyId != "" && config.KmsKeyId != kmsKeyId {
				t.Errorf("KmsKeyId should be `%v`, but found `%v`", config.KmsKeyId, kmsKeyId)
			}
			if config.KmsKeyId == "" && kmsKeyId != "" {
				t.Errorf("KmsKeyId should be empty, but found `%v`", kmsKeyId)
			}

			// Assert PresignedUrl
			presignedUrl, _ := url.QueryUnescape(query.Get("PresignedUrl"))
			if presignedUrl == "" {
				t.Errorf("PresignedUrl should always be sent in the request")
			}
			// Test PresignedUrl EC2 URL
			baseEc2UrlRegex := regexp.MustCompile(fmt.Sprintf(`^https://ec2\.%s\.amazonaws\.com/`, inputSourceRegion))
			if !baseEc2UrlRegex.MatchString(presignedUrl) {
				t.Errorf("Expected PresignedUrl to match `%v`, but found `%v`", baseEc2UrlRegex.String(), presignedUrl)
			}

			presignedUrlQuery, _ := url.ParseQuery(presignedUrl)
			// Test PresignedUrl SourceRegion
			presignedUrlSourceRegion := presignedUrlQuery.Get("SourceRegion")
			if presignedUrlSourceRegion == "" {
				t.Errorf("PresignedUrl SourceRegion should always be sent in the request")
			}
			if presignedUrlSourceRegion != inputSourceRegion {
				t.Errorf("PresignedUrl SourceRegion should be `%v`, but found `%v`", inputSourceRegion, presignedUrlSourceRegion)
			}
			// Test PresignedUrl SourceSnapshotId
			presignedUrlSourceSnapshotId := presignedUrlQuery.Get("SourceSnapshotId")
			if presignedUrlSourceSnapshotId == "" {
				t.Errorf("PresignedUrl SourceSnapshotId should always be sent in the request")
			}
			if presignedUrlSourceSnapshotId != inputSnapshotId {
				t.Errorf("PresignedUrl SourceSnapshotId should be `%v`, but found `%v`", inputSnapshotId, presignedUrlSourceSnapshotId)
			}
			// Test PresignedUrl Encrypted
			presignedUrlEncrypted := query.Get("Encrypted")
			if config.Encrypted && strconv.FormatBool(config.Encrypted) != presignedUrlEncrypted {
				t.Errorf("PresignedUrl Encrypted should be `%v`, but found `%v`", config.Encrypted, presignedUrlEncrypted)
			}
			if !config.Encrypted && presignedUrlEncrypted != "" {
				t.Errorf("PresignedUrl Encrypted should be empty, but found `%v`", presignedUrlEncrypted)
			}
			// Test PresignedUrl DestinationRegion
			presignedUrlDestinationRegion := presignedUrlQuery.Get("DestinationRegion")
			if presignedUrlDestinationRegion != clientRegion {
				t.Errorf("PresignedUrl DestinationRegion should always be equal to the client region `%v`, but found `%v`", clientRegion, presignedUrlDestinationRegion)
			}
			// Test PresignedUrl KmsKeyId
			presignedUrlKmsKeyId := query.Get("KmsKeyId")
			if config.KmsKeyId != "" && config.KmsKeyId != presignedUrlKmsKeyId {
				t.Errorf("PresignedUrl KmsKeyId should be `%v`, but found `%v`", config.KmsKeyId, presignedUrlKmsKeyId)
			}
			if config.KmsKeyId == "" && presignedUrlKmsKeyId != "" {
				t.Errorf("PresignedUrl KmsKeyId should be empty, but found `%v`", presignedUrlKmsKeyId)
			}
			// Test PresignedUrl X-Amz-Credential
			presignedUrlAmzCredential := presignedUrlQuery.Get("X-Amz-Credential")
			amzCredentialRegex := regexp.MustCompile(fmt.Sprintf(`^\w{4}/\d{8}/%s/ec2/aws4_request$`, inputSourceRegion))
			if !amzCredentialRegex.MatchString(presignedUrlAmzCredential) {
				t.Errorf("Expected PresignedUrl X-Amz-Credential to match `%v`, but found `%v`", amzCredentialRegex.String(), presignedUrlAmzCredential)
			}
		})
	}
}

func TestNoCustomRetryerWithMaxRetries(t *testing.T) {
	cases := map[string]struct {
		Config           aws.Config
		ExpectMaxRetries int
	}{
		"With custom retrier": {
			Config: aws.Config{
				Retryer: sdkclient.DefaultRetryer{
					NumMaxRetries: 10,
				},
			},
			ExpectMaxRetries: 10,
		},
		"with max retries": {
			Config: aws.Config{
				MaxRetries: aws.Int(10),
			},
			ExpectMaxRetries: 10,
		},
		"no options set": {
			ExpectMaxRetries: sdkclient.DefaultRetryerMaxNumRetries,
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			client := ec2.New(unit.Session, &aws.Config{
				DisableParamValidation: aws.Bool(true),
			}, c.Config.Copy())
			client.ModifyNetworkInterfaceAttributeWithContext(context.Background(), nil, checkRetryerMaxRetries(t, c.ExpectMaxRetries))
			client.AssignPrivateIpAddressesWithContext(context.Background(), nil, checkRetryerMaxRetries(t, c.ExpectMaxRetries))
		})
	}

}

func checkRetryerMaxRetries(t *testing.T, maxRetries int) func(*request.Request) {
	return func(r *request.Request) {
		r.Handlers.Send.Clear()
		r.Handlers.Send.PushBack(func(rr *request.Request) {
			if e, a := maxRetries, rr.Retryer.MaxRetries(); e != a {
				t.Errorf("%s, expect %v max retries, got %v", rr.Operation.Name, e, a)
			}
			rr.HTTPResponse = &http.Response{
				StatusCode: 200,
				Header:     http.Header{},
				Body:       ioutil.NopCloser(&bytes.Buffer{}),
			}
		})
	}
}