-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathcompleteMpu.js
More file actions
235 lines (213 loc) · 7.41 KB
/
completeMpu.js
File metadata and controls
235 lines (213 loc) · 7.41 KB
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
const assert = require('assert');
const async = require('async');
const arsenal = require('arsenal');
const { promisify } = require('util');
const { ListObjectsCommand } = require('@aws-sdk/client-s3');
const { GCP, GcpUtils } = arsenal.storage.data.external.GCP;
const {
gcpMpuSetup,
genUniqID,
gcpRetry,
waitForBucketReady,
} = require('../../../utils/gcpUtils');
const { getRealAwsConfig } =
require('../../../../aws-node-sdk/test/support/awsConfig');
const {
CreateBucketCommand,
DeleteBucketCommand,
} = require('@aws-sdk/client-s3');
const credentialOne = 'gcpbackend';
const bucketNames = {
main: {
Name: `cldsrvci-completempu-${genUniqID()}`,
Type: 'MULTI_REGIONAL',
},
mpu: {
Name: `cldsrvci-mpu-completempu-${genUniqID()}`,
Type: 'MULTI_REGIONAL',
},
};
const numParts = 1024;
const partSize = 10;
const smallMD5 = '583c466f3f31d97b361adc60caea72f5-1';
const bigMD5 = '9c8a62e2c04a512ce348d8280497b49e-1024';
function gcpMpuSetupWrapper(params, callback) {
gcpMpuSetup(params, (err, result) => {
assert.ifError(err, `Unable to setup MPU test, error ${err}`);
const { uploadId, etagList } = result;
this.currentTest.uploadId = uploadId;
this.currentTest.etagList = etagList;
return callback();
});
}
function listObjectsPaginated(gcpClient, bucketName, cb) {
const objects = [];
let marker;
function _list() {
const params = { Bucket: bucketName };
if (marker) {
params.Marker = marker;
}
const command = new ListObjectsCommand(params);
return gcpClient.send(command)
.then(res => {
const contents = (res && res.Contents) || [];
objects.push(...contents);
const isTruncated = Boolean(res && res.IsTruncated);
if (!isTruncated) {
return cb(null, objects);
}
// AWS listObjects(V1) pagination: prefer NextMarker, fallback to last key.
marker = (res && res.NextMarker) ||
(contents.length ? contents[contents.length - 1].Key : undefined);
if (!marker) {
return cb(null, objects);
}
return _list();
})
.catch(err => cb(err));
}
return _list();
}
function emptyBucket(gcpClient, bucketName, cb) {
return listObjectsPaginated(gcpClient, bucketName, (err, objects) => {
if (err) {
return cb(err);
}
return async.eachLimit(objects, 20, (object, next) => {
const deleteParams = {
Bucket: bucketName,
Key: object.Key,
};
return gcpClient.deleteObject(deleteParams, next);
}, cb);
});
}
describe('GCP: Complete MPU', function testSuite() {
this.timeout(600000);
let config;
let gcpClient;
before(async () => {
config = getRealAwsConfig(credentialOne);
gcpClient = new GCP(config);
const buckets = Object.values(bucketNames);
await async.eachSeries(
buckets,
async bucket => {
const cmd = new CreateBucketCommand({ Bucket: bucket.Name });
await gcpRetry(gcpClient, cmd);
await waitForBucketReady(gcpClient, bucket.Name);
},
);
});
after(async () => {
const buckets = Object.values(bucketNames);
await async.eachSeries(
buckets,
async bucket => {
await promisify(emptyBucket)(gcpClient, bucket.Name);
const cmd = new DeleteBucketCommand({ Bucket: bucket.Name });
await gcpRetry(gcpClient, cmd);
},
);
});
describe('when MPU has 0 parts', () => {
beforeEach(function beforeFn(done) {
this.currentTest.key = `somekey-${genUniqID()}`;
gcpMpuSetupWrapper.call(this, {
gcpClient,
bucketNames,
key: this.currentTest.key,
partCount: 0, partSize,
}, done);
});
it('should return error if 0 parts are given in MPU complete',
function testFn(done) {
const params = {
Bucket: bucketNames.main.Name,
MPU: bucketNames.mpu.Name,
Key: this.test.key,
UploadId: this.test.uploadId,
MultipartUpload: { Parts: [] },
};
gcpClient.completeMultipartUpload(params, err => {
assert(err);
assert.strictEqual(err.code, 400);
return done();
});
});
});
describe('when MPU has 1 uploaded part', () => {
beforeEach(function beforeFn(done) {
this.currentTest.key = `somekey-${genUniqID()}`;
gcpMpuSetupWrapper.call(this, {
gcpClient,
bucketNames,
key: this.currentTest.key,
partCount: 1, partSize,
}, done);
});
it('should successfully complete MPU',
function testFn(done) {
const parts = GcpUtils.createMpuList({
Key: this.test.key,
UploadId: this.test.uploadId,
}, 'parts', 1).map(item => {
Object.assign(item, {
ETag: this.test.etagList[item.PartNumber - 1],
});
return item;
});
const params = {
Bucket: bucketNames.main.Name,
MPU: bucketNames.mpu.Name,
Key: this.test.key,
UploadId: this.test.uploadId,
MultipartUpload: { Parts: parts },
};
gcpClient.completeMultipartUpload(params, (err, res) => {
assert.equal(err, null,
`Expected success, but got error ${err}`);
assert.strictEqual(res.ETag, `"${smallMD5}"`);
return done();
});
});
});
describe('when MPU has 1024 uploaded parts', () => {
beforeEach(function beforeFn(done) {
this.currentTest.key = `somekey-${genUniqID()}`;
gcpMpuSetupWrapper.call(this, {
gcpClient,
bucketNames,
key: this.currentTest.key,
partCount: numParts, partSize,
}, done);
});
it('should successfully complete MPU',
function testFn(done) {
this.retries(1);
const parts = GcpUtils.createMpuList({
Key: this.test.key,
UploadId: this.test.uploadId,
}, 'parts', numParts).map(item => {
Object.assign(item, {
ETag: this.test.etagList[item.PartNumber - 1],
});
return item;
});
const params = {
Bucket: bucketNames.main.Name,
MPU: bucketNames.mpu.Name,
Key: this.test.key,
UploadId: this.test.uploadId,
MultipartUpload: { Parts: parts },
};
gcpClient.completeMultipartUpload(params, (err, res) => {
assert.equal(err, null,
`Expected success, but got error ${err}`);
assert.strictEqual(res.ETag, `"${bigMD5}"`);
return done();
});
});
});
});