forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3_storage_client_test.go
283 lines (259 loc) · 8.35 KB
/
s3_storage_client_test.go
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
// Copyright (c) 2021-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"os"
"path"
"strconv"
"testing"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/smithy-go"
)
type tcBucketPath struct {
in string
bucket string
s3path string
}
func TestExtractBucketNameAndPath(t *testing.T) {
s3util := new(snowflakeS3Client)
testcases := []tcBucketPath{
{"sfc-dev1-regression/test_sub_dir/", "sfc-dev1-regression", "test_sub_dir/"},
{"sfc-dev1-regression/dir/test_stg/test_sub_dir/", "sfc-dev1-regression", "dir/test_stg/test_sub_dir/"},
{"sfc-dev1-regression/", "sfc-dev1-regression", ""},
{"sfc-dev1-regression//", "sfc-dev1-regression", "/"},
{"sfc-dev1-regression///", "sfc-dev1-regression", "//"},
}
for _, test := range testcases {
s3Loc, err := s3util.extractBucketNameAndPath(test.in)
if err != nil {
t.Error(err)
}
if s3Loc.bucketName != test.bucket {
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.bucket, s3Loc.bucketName)
}
if s3Loc.s3Path != test.s3path {
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.s3path, s3Loc.s3Path)
}
}
}
type mockUploadObjectAPI func(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*manager.Uploader)) (*manager.UploadOutput, error)
func (m mockUploadObjectAPI) Upload(
ctx context.Context,
params *s3.PutObjectInput,
optFns ...func(*manager.Uploader)) (*manager.UploadOutput, error) {
return m(ctx, params, optFns...)
}
func TestUploadOneFileToS3WSAEConnAborted(t *testing.T) {
if !runningOnAWS() {
t.Skip("skipping non aws environment")
}
info := execResponseStageInfo{
Location: "sfc-customer-stage/rwyi-testacco/users/9220/",
LocationType: "S3",
}
initialParallel := int64(100)
dir, err := os.Getwd()
if err != nil {
t.Error(err)
}
s3Cli, err := new(snowflakeS3Client).createClient(&info, false)
if err != nil {
t.Error(err)
}
uploadMeta := fileMetadata{
name: "data1.txt.gz",
stageLocationType: "S3",
noSleepingTime: true,
parallel: initialParallel,
client: s3Cli,
sha256Digest: "123456789abcdef",
stageInfo: &info,
dstFileName: "data1.txt.gz",
srcFileName: path.Join(dir, "/test_data/put_get_1.txt"),
overwrite: true,
options: &SnowflakeFileTransferOptions{
MultiPartThreshold: dataSizeThreshold,
},
mockUploader: mockUploadObjectAPI(func(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*manager.Uploader)) (*manager.UploadOutput, error) {
return nil, &smithy.GenericAPIError{
Code: errNoWsaeconnaborted,
Message: "mock err, connection aborted",
}
}),
}
uploadMeta.realSrcFileName = uploadMeta.srcFileName
fi, err := os.Stat(uploadMeta.srcFileName)
if err != nil {
t.Error(err)
}
uploadMeta.uploadSize = fi.Size()
err = new(remoteStorageUtil).uploadOneFile(&uploadMeta)
if err == nil {
t.Error("should have raised an error")
}
if uploadMeta.lastMaxConcurrency == 0 {
t.Fatalf("expected concurrency. got: 0")
}
if uploadMeta.lastMaxConcurrency != int(initialParallel/defaultMaxRetry) {
t.Fatalf("expected last max concurrency to be: %v, got: %v",
int(initialParallel/defaultMaxRetry), uploadMeta.lastMaxConcurrency)
}
initialParallel = 4
uploadMeta.parallel = initialParallel
err = new(remoteStorageUtil).uploadOneFile(&uploadMeta)
if err == nil {
t.Error("should have raised an error")
}
if uploadMeta.lastMaxConcurrency == 0 {
t.Fatalf("expected no last max concurrency. got: %v",
uploadMeta.lastMaxConcurrency)
}
if uploadMeta.lastMaxConcurrency != 1 {
t.Fatalf("expected last max concurrency to be: 1, got: %v",
uploadMeta.lastMaxConcurrency)
}
}
func TestUploadOneFileToS3ConnReset(t *testing.T) {
info := execResponseStageInfo{
Location: "sfc-teststage/rwyitestacco/users/1234/",
LocationType: "S3",
}
initialParallel := int64(100)
dir, err := os.Getwd()
if err != nil {
t.Error(err)
}
s3Cli, err := new(snowflakeS3Client).createClient(&info, false)
if err != nil {
t.Error(err)
}
uploadMeta := fileMetadata{
name: "data1.txt.gz",
stageLocationType: "S3",
noSleepingTime: true,
parallel: initialParallel,
client: s3Cli,
sha256Digest: "123456789abcdef",
stageInfo: &info,
dstFileName: "data1.txt.gz",
srcFileName: path.Join(dir, "/test_data/put_get_1.txt"),
overwrite: true,
options: &SnowflakeFileTransferOptions{
MultiPartThreshold: dataSizeThreshold,
},
mockUploader: mockUploadObjectAPI(func(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*manager.Uploader)) (*manager.UploadOutput, error) {
return nil, &smithy.GenericAPIError{
Code: strconv.Itoa(-1),
Message: "mock err, connection aborted",
}
}),
}
uploadMeta.realSrcFileName = uploadMeta.srcFileName
fi, err := os.Stat(uploadMeta.srcFileName)
if err != nil {
t.Error(err)
}
uploadMeta.uploadSize = fi.Size()
err = new(remoteStorageUtil).uploadOneFile(&uploadMeta)
if err == nil {
t.Error("should have raised an error")
}
if uploadMeta.lastMaxConcurrency != 0 {
t.Fatalf("expected no concurrency. got: %v",
uploadMeta.lastMaxConcurrency)
}
}
func TestUploadFileWithS3UploadFailedError(t *testing.T) {
info := execResponseStageInfo{
Location: "sfc-teststage/rwyitestacco/users/1234/",
LocationType: "S3",
}
initialParallel := int64(100)
dir, err := os.Getwd()
if err != nil {
t.Error(err)
}
s3Cli, err := new(snowflakeS3Client).createClient(&info, false)
if err != nil {
t.Error(err)
}
uploadMeta := fileMetadata{
name: "data1.txt.gz",
stageLocationType: "S3",
noSleepingTime: true,
parallel: initialParallel,
client: s3Cli,
sha256Digest: "123456789abcdef",
stageInfo: &info,
dstFileName: "data1.txt.gz",
srcFileName: path.Join(dir, "/test_data/put_get_1.txt"),
overwrite: true,
options: &SnowflakeFileTransferOptions{
MultiPartThreshold: dataSizeThreshold,
},
mockUploader: mockUploadObjectAPI(func(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*manager.Uploader)) (*manager.UploadOutput, error) {
return nil, &smithy.GenericAPIError{
Code: expiredToken,
Message: "An error occurred (ExpiredToken) when calling the " +
"operation: The provided token has expired.",
}
}),
}
uploadMeta.realSrcFileName = uploadMeta.srcFileName
fi, err := os.Stat(uploadMeta.srcFileName)
if err != nil {
t.Error(err)
}
uploadMeta.uploadSize = fi.Size()
err = new(remoteStorageUtil).uploadOneFile(&uploadMeta)
if err != nil {
t.Error(err)
}
if uploadMeta.resStatus != renewToken {
t.Fatalf("expected %v result status, got: %v",
renewToken, uploadMeta.resStatus)
}
}
type mockHeaderAPI func(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error)
func (m mockHeaderAPI) HeadObject(
ctx context.Context,
params *s3.HeadObjectInput,
optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error) {
return m(ctx, params, optFns...)
}
func TestGetHeadExpiryError(t *testing.T) {
meta := fileMetadata{
client: s3.New(s3.Options{}),
stageInfo: &execResponseStageInfo{Location: ""},
mockHeader: mockHeaderAPI(func(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error) {
return nil, &smithy.GenericAPIError{
Code: expiredToken,
}
}),
}
if header, err := new(snowflakeS3Client).getFileHeader(&meta, "file.txt"); header != nil || err == nil {
t.Fatalf("expected null header, got: %v", header)
}
if meta.resStatus != renewToken {
t.Fatalf("expected %v result status, got: %v",
renewToken, meta.resStatus)
}
}
func TestGetHeaderUnexpectedError(t *testing.T) {
meta := fileMetadata{
client: s3.New(s3.Options{}),
stageInfo: &execResponseStageInfo{Location: ""},
mockHeader: mockHeaderAPI(func(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error) {
return nil, &smithy.GenericAPIError{
Code: "-1",
}
}),
}
if header, err := new(snowflakeS3Client).getFileHeader(&meta, "file.txt"); header != nil || err == nil {
t.Fatalf("expected null header, got: %v", header)
}
if meta.resStatus != errStatus {
t.Fatalf("expected %v result status, got: %v", errStatus, meta.resStatus)
}
}