Skip to content

Commit 5ecbaaf

Browse files
committed
utilities adaptation to comply with sdk migration
As test files depend on these utilities, they needed to be adapted to match the sdk v3 requirements. Issue: CLDSRV-724
1 parent 66458c8 commit 5ecbaaf

File tree

3 files changed

+137
-116
lines changed

3 files changed

+137
-116
lines changed

tests/functional/aws-node-sdk/lib/utility/bucket-util.js

Lines changed: 86 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,146 @@
1-
const AWS = require('aws-sdk');
2-
AWS.config.logger = console;
3-
const { S3 } = require('aws-sdk');
1+
const {
2+
S3Client,
3+
HeadBucketCommand,
4+
CreateBucketCommand,
5+
DeleteBucketCommand,
6+
ListObjectVersionsCommand,
7+
DeleteObjectCommand,
8+
ListBucketsCommand,
9+
} = require('@aws-sdk/client-s3');
410
const projectFixture = require('../fixtures/project');
511
const getConfig = require('../../test/support/config');
612

713
class BucketUtility {
8-
constructor(profile = 'default', config = {}) {
14+
constructor(profile = 'default', config = {}, unauthenticated = false) {
915
const s3Config = getConfig(profile, config);
10-
11-
this.s3 = new S3(s3Config);
12-
this.s3.config.setPromisesDependency(Promise);
13-
this.s3.config.update({
14-
maxRetries: 0,
15-
});
16+
if (unauthenticated) {
17+
this.s3 = new S3Client({
18+
...s3Config,
19+
maxAttempts: 0,
20+
credentials: { accessKeyId: '', secretAccessKey: '' },
21+
forcePathStyle: true,
22+
signer: { sign: async request => request },
23+
});
24+
}
25+
else {
26+
this.s3 = new S3Client({
27+
...s3Config,
28+
maxAttempts: 0,
29+
});
30+
}
1631
}
1732

1833
bucketExists(bucketName) {
19-
return this.s3
20-
.headBucket({ Bucket: bucketName })
21-
.promise()
34+
return this.s3.send(new HeadBucketCommand({ Bucket: bucketName }))
2235
.then(() => true)
2336
.catch(err => {
24-
if (err.code === 'NotFound') {
37+
if (err.name === 'NotFound') {
2538
return false;
2639
}
2740
throw err;
2841
});
2942
}
3043

3144
createOne(bucketName) {
32-
return this.s3
33-
.createBucket({ Bucket: bucketName })
34-
.promise()
35-
.then(() => bucketName);
45+
return this.s3.send(new CreateBucketCommand({ Bucket: bucketName }))
46+
.then(() => bucketName)
47+
.catch(err => {
48+
throw err;
49+
});
3650
}
3751

3852
createOneWithLock(bucketName) {
39-
return this.s3
40-
.createBucket({
41-
Bucket: bucketName,
42-
ObjectLockEnabledForBucket: true,
43-
})
44-
.promise()
45-
.then(() => bucketName);
53+
return this.s3.send(new CreateBucketCommand({
54+
Bucket: bucketName,
55+
ObjectLockEnabledForBucket: true,
56+
})).then(() => bucketName);
4657
}
4758

4859
createMany(bucketNames) {
4960
const promises = bucketNames.map(bucketName =>
5061
this.createOne(bucketName),
5162
);
52-
5363
return Promise.all(promises);
5464
}
5565

5666
createRandom(nBuckets = 1) {
5767
if (nBuckets === 1) {
5868
const bucketName = projectFixture.generateBucketName();
59-
6069
return this.createOne(bucketName);
6170
}
62-
6371
const bucketNames = projectFixture
6472
.generateManyBucketNames(nBuckets)
65-
.sort(() => 0.5 - Math.random()); // Simply shuffle array
66-
73+
.sort(() => 0.5 - Math.random());
6774
return this.createMany(bucketNames);
6875
}
6976

7077
deleteOne(bucketName) {
71-
return this.s3.deleteBucket({ Bucket: bucketName }).promise();
78+
return this.s3.send(new DeleteBucketCommand({ Bucket: bucketName }));
7279
}
7380

7481
deleteMany(bucketNames) {
7582
const promises = bucketNames.map(bucketName =>
7683
this.deleteOne(bucketName),
7784
);
78-
7985
return Promise.all(promises);
8086
}
81-
87+
8288
/**
8389
* Recursively delete all versions of all objects within the bucket
8490
* @param bucketName
8591
* @returns {Promise.<T>}
8692
*/
87-
88-
async empty(bucketName, BypassGovernanceRetention = false) {
93+
empty(bucketName, BypassGovernanceRetention = false) {
8994
const param = {
9095
Bucket: bucketName,
9196
};
9297

93-
const listedObjects = await this.s3.listObjectVersions(param).promise();
94-
95-
for (const version of listedObjects.Versions) {
96-
if (version.Key.endsWith('/')) {
97-
continue;
98-
}
99-
100-
await this.s3
101-
.deleteObject({
102-
Bucket: bucketName,
103-
Key: version.Key,
104-
VersionId: version.VersionId,
105-
...(BypassGovernanceRetention && {
106-
BypassGovernanceRetention,
107-
}),
108-
})
109-
.promise();
110-
}
111-
112-
for (const version of listedObjects.Versions) {
113-
if (!version.Key.endsWith('/')) {
114-
continue;
115-
}
116-
117-
await this.s3
118-
.deleteObject({
119-
Bucket: bucketName,
120-
Key: version.Key,
121-
VersionId: version.VersionId,
122-
...(BypassGovernanceRetention && {
123-
BypassGovernanceRetention,
124-
}),
125-
})
126-
.promise();
127-
}
128-
129-
for (const marker of listedObjects.DeleteMarkers) {
130-
await this.s3
131-
.deleteObject({
132-
Bucket: bucketName,
133-
Key: marker.Key,
134-
VersionId: marker.VersionId,
135-
...(BypassGovernanceRetention && {
136-
BypassGovernanceRetention,
137-
}),
138-
})
139-
.promise();
140-
}
98+
return this.s3.send(new ListObjectVersionsCommand(param))
99+
.then(data => Promise.all(
100+
(data.Versions || [])
101+
.filter(object => !object.Key.endsWith('/'))
102+
.map(object =>
103+
this.s3.send(new DeleteObjectCommand({
104+
Bucket: bucketName,
105+
Key: object.Key,
106+
VersionId: object.VersionId,
107+
...(BypassGovernanceRetention && { BypassGovernanceRetention }),
108+
})).then(() => object)
109+
)
110+
.concat((data.Versions || [])
111+
.filter(object => object.Key.endsWith('/'))
112+
.map(object =>
113+
this.s3.send(new DeleteObjectCommand({
114+
Bucket: bucketName,
115+
Key: object.Key,
116+
VersionId: object.VersionId,
117+
...(BypassGovernanceRetention && { BypassGovernanceRetention }),
118+
}))
119+
.then(() => object)
120+
)
121+
)
122+
.concat((data.DeleteMarkers || [])
123+
.map(object =>
124+
this.s3.send(new DeleteObjectCommand({
125+
Bucket: bucketName,
126+
Key: object.Key,
127+
VersionId: object.VersionId,
128+
...(BypassGovernanceRetention && { BypassGovernanceRetention }),
129+
}))
130+
.then(() => object)
131+
)
132+
)
133+
)
134+
);
141135
}
142136

143137
emptyMany(bucketNames) {
144-
const promises = bucketNames.map(bucketName => this.empty(bucketName));
145-
138+
const promises = bucketNames.map(
139+
bucketName => this.empty(bucketName)
140+
);
146141
return Promise.all(promises);
147142
}
148-
143+
149144
emptyIfExists(bucketName) {
150145
return this.bucketExists(bucketName).then(exists => {
151146
if (exists) {
@@ -159,15 +154,15 @@ class BucketUtility {
159154
const promises = bucketNames.map(bucketName =>
160155
this.emptyIfExists(bucketName),
161156
);
162-
163157
return Promise.all(promises);
164158
}
165159

166160
getOwner() {
167-
return this.s3
168-
.listBuckets()
169-
.promise()
170-
.then(data => data.Owner);
161+
return this.s3.send(new ListBucketsCommand({}))
162+
.then(data => data.Owner)
163+
.catch(err => {
164+
throw err;
165+
});
171166
}
172167
}
173168

tests/functional/aws-node-sdk/test/support/awsConfig.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const AWS = require('aws-sdk');
1+
const { fromIni } = require('@aws-sdk/credential-providers');
22
const fs = require('fs');
33
const path = require('path');
44
const { config } = require('../../../../../lib/Config');
55
const https = require('https');
66
const http = require('http');
7+
78
function getAwsCredentials(profile, credFile) {
89
const filename = path.join(process.env.HOME, credFile);
910

@@ -14,7 +15,7 @@ function getAwsCredentials(profile, credFile) {
1415
throw new Error(msg);
1516
}
1617

17-
return new AWS.SharedIniFileCredentials({ profile, filename });
18+
return fromIni({ profile, filepath: filename });
1819
}
1920

2021
function getRealAwsConfig(location) {
@@ -26,19 +27,18 @@ function getRealAwsConfig(location) {
2627
const params = {
2728
endpoint: gcpEndpoint ?
2829
`${proto}://${gcpEndpoint}` : `${proto}://${awsEndpoint}`,
29-
signatureVersion: 'v4',
3030
};
3131
if (config.locationConstraints[location].type === 'gcp') {
3232
params.mainBucket = bucketName;
3333
params.mpuBucket = mpuBucketName;
3434
}
3535
if (useHTTPS) {
36-
params.httpOptions = {
37-
agent: new https.Agent({ keepAlive: true }),
36+
params.requestHandler = {
37+
httpsAgent: new https.Agent({ keepAlive: true }),
3838
};
3939
} else {
40-
params.httpOptions = {
41-
agent: new http.Agent({ keepAlive: true }),
40+
params.requestHandler = {
41+
httpAgent: new http.Agent({ keepAlive: true }),
4242
};
4343
}
4444
if (credentialsProfile) {
@@ -48,13 +48,12 @@ function getRealAwsConfig(location) {
4848
return params;
4949
}
5050
if (pathStyle) {
51-
params.s3ForcePathStyle = true;
52-
}
53-
if (!useHTTPS) {
54-
params.sslEnabled = false;
51+
params.forcePathStyle = true;
5552
}
56-
params.accessKeyId = locCredentials.accessKey;
57-
params.secretAccessKey = locCredentials.secretKey;
53+
params.credentials = {
54+
accessKeyId: locCredentials.accessKey,
55+
secretAccessKey: locCredentials.secretKey,
56+
};
5857
return params;
5958
}
6059

tests/functional/aws-node-sdk/test/support/config.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const https = require('https');
2-
const AWS = require('aws-sdk');
2+
const http = require('http');
3+
const { NodeHttpHandler } = require('@smithy/node-http-handler');
34

45
const { getCredentials } = require('./credentials');
56
const { getAwsCredentials } = require('./awsConfig');
@@ -19,19 +20,45 @@ if (ssl && ssl.ca) {
1920

2021
const DEFAULT_GLOBAL_OPTIONS = {
2122
httpOptions,
22-
apiVersions: { s3: '2006-03-01' },
23-
signatureCache: false,
24-
sslEnabled: ssl !== undefined,
2523
};
24+
2625
const DEFAULT_MEM_OPTIONS = {
2726
endpoint: `${transport}://127.0.0.1:8000`,
28-
s3ForcePathStyle: true,
27+
port: 8000,
28+
forcePathStyle: true,
29+
region: 'us-east-1',
30+
maxAttempts: 3,
31+
requestHandler: new NodeHttpHandler({
32+
connectionTimeout: 5000,
33+
socketTimeout: 5000,
34+
httpAgent: new (ssl ? https : http).Agent({
35+
maxSockets: 200,
36+
keepAlive: true,
37+
keepAliveMsecs: 1000,
38+
}),
39+
}),
40+
};
41+
42+
const DEFAULT_AWS_OPTIONS = {
43+
region: 'us-east-1',
44+
maxAttempts: 3,
45+
requestHandler: new NodeHttpHandler({
46+
connectionTimeout: 5000,
47+
socketTimeout: 5000,
48+
httpAgent: new https.Agent({
49+
maxSockets: 200,
50+
keepAlive: true,
51+
keepAliveMsecs: 1000,
52+
}),
53+
}),
2954
};
30-
const DEFAULT_AWS_OPTIONS = {};
3155

3256
function _getMemCredentials(profile) {
3357
const { accessKeyId, secretAccessKey } = getCredentials(profile);
34-
return new AWS.Credentials(accessKeyId, secretAccessKey);
58+
return {
59+
accessKeyId,
60+
secretAccessKey,
61+
};
3562
}
3663

3764
function _getMemConfig(profile, config) {
@@ -58,11 +85,11 @@ function _getAwsConfig(profile, config) {
5885
return awsConfig;
5986
}
6087

61-
function getConfig(profile = 'default', config = {}) {
62-
const fn = process.env.AWS_ON_AIR && process.env.AWS_ON_AIR === 'true'
63-
? _getAwsConfig : _getMemConfig;
64-
65-
return fn.apply(this, [profile, config]);
88+
function getConfig(profile, config) {
89+
if (process.env.AWS_ON_AIR) {
90+
return _getAwsConfig(profile, config);
91+
}
92+
return _getMemConfig(profile, config);
6693
}
6794

6895
module.exports = getConfig;

0 commit comments

Comments
 (0)