Skip to content

Commit ad13d5d

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 e41e0b8 commit ad13d5d

3 files changed

Lines changed: 130 additions & 97 deletions

File tree

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

Lines changed: 79 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +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+
credentials: { accessKeyId: '', secretAccessKey: '' },
20+
forcePathStyle: true,
21+
signer: { sign: async request => request },
22+
});
23+
}
24+
else {
25+
this.s3 = new S3Client({
26+
...s3Config,
27+
maxAttempts: 0,
28+
});
29+
}
1630
}
1731

1832
bucketExists(bucketName) {
19-
return this.s3
20-
.headBucket({ Bucket: bucketName }).promise()
33+
return this.s3.send(new HeadBucketCommand({ Bucket: bucketName }))
2134
.then(() => true)
2235
.catch(err => {
23-
if (err.code === 'NotFound') {
36+
if (err.name === 'NotFound') {
2437
return false;
2538
}
2639
throw err;
2740
});
2841
}
2942

3043
createOne(bucketName) {
31-
return this.s3
32-
.createBucket({ Bucket: bucketName }).promise()
33-
.then(() => bucketName);
44+
return this.s3.send(new CreateBucketCommand({ Bucket: bucketName }))
45+
.then(() => bucketName)
46+
.catch(err => {
47+
throw err;
48+
});
3449
}
3550

3651
createOneWithLock(bucketName) {
37-
return this.s3.createBucket({
52+
return this.s3.send(new CreateBucketCommand({
3853
Bucket: bucketName,
3954
ObjectLockEnabledForBucket: true,
40-
}).promise()
41-
.then(() => bucketName);
55+
}))
56+
.then(() => bucketName)
57+
.catch(err => {
58+
throw err;
59+
});
4260
}
4361

4462
createMany(bucketNames) {
4563
const promises = bucketNames.map(
4664
bucketName => this.createOne(bucketName)
4765
);
48-
4966
return Promise.all(promises);
5067
}
51-
5268
createRandom(nBuckets = 1) {
5369
if (nBuckets === 1) {
5470
const bucketName = projectFixture.generateBucketName();
55-
5671
return this.createOne(bucketName);
5772
}
58-
5973
const bucketNames = projectFixture
6074
.generateManyBucketNames(nBuckets)
61-
.sort(() => 0.5 - Math.random()); // Simply shuffle array
62-
75+
.sort(() => 0.5 - Math.random());
6376
return this.createMany(bucketNames);
6477
}
6578

6679
deleteOne(bucketName) {
67-
return this.s3
68-
.deleteBucket({ Bucket: bucketName }).promise();
80+
return this.s3.send(new DeleteBucketCommand({ Bucket: bucketName }))
81+
.catch(err => {
82+
throw err;
83+
});
6984
}
7085

7186
deleteMany(bucketNames) {
7287
const promises = bucketNames.map(
7388
bucketName => this.deleteOne(bucketName)
7489
);
75-
7690
return Promise.all(promises);
7791
}
78-
7992
/**
8093
* Recursively delete all versions of all objects within the bucket
8194
* @param bucketName
8295
* @returns {Promise.<T>}
8396
*/
84-
8597
empty(bucketName) {
8698
const param = {
8799
Bucket: bucketName,
88100
};
89101

90-
return this.s3
91-
.listObjectVersions(param).promise()
92-
.then(data =>
93-
Promise.all(
94-
data.Versions
95-
.filter(object => !object.Key.endsWith('/'))
96-
// remove all objects
102+
return this.s3.send(new ListObjectVersionsCommand(param))
103+
.then(data => Promise.all(
104+
(data.Versions || [])
105+
.filter(object => !object.Key.endsWith('/'))
106+
.map(object =>
107+
this.s3.send(new DeleteObjectCommand({
108+
Bucket: bucketName,
109+
Key: object.Key,
110+
VersionId: object.VersionId,
111+
}))
112+
.then(() => object)
113+
)
114+
.concat((data.Versions || [])
115+
.filter(object => object.Key.endsWith('/'))
97116
.map(object =>
98-
this.s3.deleteObject({
99-
Bucket: bucketName,
100-
Key: object.Key,
101-
VersionId: object.VersionId,
102-
}).promise()
103-
.then(() => object)
104-
)
105-
.concat(data.Versions
106-
.filter(object => object.Key.endsWith('/'))
107-
// remove all directories
108-
.map(object =>
109-
this.s3.deleteObject({
110-
Bucket: bucketName,
111-
Key: object.Key,
112-
VersionId: object.VersionId,
113-
}).promise()
117+
this.s3.send(new DeleteObjectCommand({
118+
Bucket: bucketName,
119+
Key: object.Key,
120+
VersionId: object.VersionId,
121+
}))
114122
.then(() => object)
115-
)
116123
)
117-
.concat(data.DeleteMarkers
118-
.map(object =>
119-
this.s3.deleteObject({
120-
Bucket: bucketName,
121-
Key: object.Key,
122-
VersionId: object.VersionId,
123-
}).promise()
124-
.then(() => object)))
125-
)
126-
);
124+
)
125+
.concat((data.DeleteMarkers || [])
126+
.map(object =>
127+
this.s3.send(new DeleteObjectCommand({
128+
Bucket: bucketName,
129+
Key: object.Key,
130+
VersionId: object.VersionId,
131+
}))
132+
.then(() => object)
133+
))
134+
));
127135
}
128136

129137
emptyMany(bucketNames) {
130-
const promises = bucketNames.map(
131-
bucketName => this.empty(bucketName)
138+
const promises = bucketNames.map(
139+
bucketName => this.empty(bucketName)
132140
);
133141

134142
return Promise.all(promises);
135143
}
136-
137144
emptyIfExists(bucketName) {
138145
return this.bucketExists(bucketName)
139146
.then(exists => {
@@ -143,19 +150,19 @@ class BucketUtility {
143150
return undefined;
144151
});
145152
}
146-
147153
emptyManyIfExists(bucketNames) {
148154
const promises = bucketNames.map(
149155
bucketName => this.emptyIfExists(bucketName)
150156
);
151-
152157
return Promise.all(promises);
153158
}
154159

155160
getOwner() {
156-
return this.s3
157-
.listBuckets().promise()
158-
.then(data => data.Owner);
161+
return this.s3.send(new ListBucketsCommand({}))
162+
.then(data => data.Owner)
163+
.catch(err => {
164+
throw err;
165+
});
159166
}
160167
}
161168

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)