Skip to content

Commit 4427dcf

Browse files
committed
delete operation related bucket tests migration
Issue: CLDSRV-724
1 parent fd26585 commit 4427dcf

File tree

7 files changed

+291
-259
lines changed

7 files changed

+291
-259
lines changed
Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
const assert = require('assert');
22
const { errors } = require('arsenal');
3-
const { S3 } = require('aws-sdk');
3+
const { S3Client,
4+
CreateBucketCommand,
5+
DeleteBucketCommand,
6+
DeleteBucketLifecycleCommand,
7+
PutBucketLifecycleConfigurationCommand,
8+
GetBucketLifecycleConfigurationCommand } = require('@aws-sdk/client-s3');
49

510
const getConfig = require('../support/config');
6-
const BucketUtility = require('../../lib/utility/bucket-util');
711

812
const bucket = 'lifecycledeletetestbucket';
913
const basicRule = {
@@ -16,17 +20,16 @@ const basicRule = {
1620
};
1721

1822
// Check for the expected error response code and status code.
19-
function assertError(err, expectedErr, cb) {
23+
function assertError(err, expectedErr) {
2024
if (expectedErr === null) {
2125
assert.strictEqual(err, null, `expected no error but got '${err}'`);
2226
} else {
23-
assert.strictEqual(err.code, expectedErr, 'incorrect error response ' +
24-
`code: should be '${expectedErr}' but got '${err.code}'`);
25-
assert.strictEqual(err.statusCode, errors[expectedErr].code,
27+
assert.strictEqual(err.name, expectedErr, 'incorrect error response ' +
28+
`code: should be '${expectedErr}' but got '${err.Code}'`);
29+
assert.strictEqual(err.$metadata.httpStatusCode, errors[expectedErr].code,
2630
'incorrect error status code: should be 400 but got ' +
27-
`'${err.statusCode}'`);
31+
`'${err.$metadata.httpStatusCode}'`);
2832
}
29-
cb();
3033
}
3134

3235
describe('aws-sdk test delete bucket lifecycle', () => {
@@ -35,43 +38,51 @@ describe('aws-sdk test delete bucket lifecycle', () => {
3538

3639
before(done => {
3740
const config = getConfig('default', { signatureVersion: 'v4' });
38-
s3 = new S3(config);
39-
otherAccountS3 = new BucketUtility('lisa', {}).s3;
41+
s3 = new S3Client(config);
42+
const otherAccountConfig = getConfig('lisa', {});
43+
otherAccountS3 = new S3Client(otherAccountConfig);
4044
return done();
4145
});
4246

43-
it('should return NoSuchBucket error if bucket does not exist', done => {
44-
s3.deleteBucketLifecycle({ Bucket: bucket }, err =>
45-
assertError(err, 'NoSuchBucket', done));
47+
it('should return NoSuchBucket error if bucket does not exist', async () => {
48+
try {
49+
await s3.send(new DeleteBucketLifecycleCommand({ Bucket: bucket }));
50+
// Should not reach here
51+
throw new Error('Expected NoSuchBucket error');
52+
} catch (err) {
53+
assertError(err, 'NoSuchBucket');
54+
}
4655
});
4756

4857
describe('config rules', () => {
49-
beforeEach(done => s3.createBucket({ Bucket: bucket }, done));
58+
beforeEach(() => s3.send(new CreateBucketCommand({ Bucket: bucket })));
5059

51-
afterEach(done => s3.deleteBucket({ Bucket: bucket }, done));
60+
afterEach(() => s3.send(new DeleteBucketCommand({ Bucket: bucket })));
5261

53-
it('should return AccessDenied if user is not bucket owner', done => {
54-
otherAccountS3.deleteBucketLifecycle({ Bucket: bucket },
55-
err => assertError(err, 'AccessDenied', done));
62+
it('should return AccessDenied if user is not bucket owner', async () => {
63+
try {
64+
await otherAccountS3.send(new DeleteBucketLifecycleCommand({ Bucket: bucket }));
65+
// Should not reach here
66+
throw new Error('Expected AccessDenied error');
67+
} catch (err) {
68+
assertError(err, 'AccessDenied');
69+
}
5670
});
5771

58-
it('should return no error if no lifecycle config on bucket', done => {
59-
s3.deleteBucketLifecycle({ Bucket: bucket }, err =>
60-
assertError(err, null, done));
61-
});
72+
it('should return no error if no lifecycle config on bucket', () => s3.send(new
73+
DeleteBucketLifecycleCommand({ Bucket: bucket })));
6274

63-
it('should delete lifecycle configuration from bucket', done => {
75+
it('should delete lifecycle configuration from bucket', async () => {
6476
const params = { Bucket: bucket,
6577
LifecycleConfiguration: { Rules: [basicRule] } };
66-
s3.putBucketLifecycleConfiguration(params, err => {
67-
assert.equal(err, null);
68-
s3.deleteBucketLifecycle({ Bucket: bucket }, err => {
69-
assert.equal(err, null);
70-
s3.getBucketLifecycleConfiguration({ Bucket: bucket },
71-
err =>
72-
assertError(err, 'NoSuchLifecycleConfiguration', done));
73-
});
74-
});
78+
await s3.send(new PutBucketLifecycleConfigurationCommand(params));
79+
await s3.send(new DeleteBucketLifecycleCommand({ Bucket: bucket }));
80+
try {
81+
await s3.send(new GetBucketLifecycleConfigurationCommand({ Bucket: bucket }));
82+
throw new Error('Expected NoSuchLifecycleConfiguration error');
83+
} catch (err) {
84+
assertError(err, 'NoSuchLifecycleConfiguration');
85+
}
7586
});
7687
});
7788
});
Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const assert = require('assert');
22
const { errors } = require('arsenal');
3-
const { S3 } = require('aws-sdk');
3+
const { S3Client,
4+
CreateBucketCommand,
5+
DeleteBucketCommand,
6+
DeleteBucketPolicyCommand,
7+
PutBucketPolicyCommand,
8+
GetBucketPolicyCommand } = require('@aws-sdk/client-s3');
49

510
const getConfig = require('../support/config');
611
const BucketUtility = require('../../lib/utility/bucket-util');
@@ -18,17 +23,16 @@ const bucketPolicy = {
1823
};
1924

2025
// Check for the expected error response code and status code.
21-
function assertError(err, expectedErr, cb) {
26+
function assertError(err, expectedErr) {
2227
if (expectedErr === null) {
2328
assert.strictEqual(err, null, `expected no error but got '${err}'`);
2429
} else {
25-
assert.strictEqual(err.code, expectedErr, 'incorrect error response ' +
26-
`code: should be '${expectedErr}' but got '${err.code}'`);
27-
assert.strictEqual(err.statusCode, errors[expectedErr].code,
30+
assert.strictEqual(err.name, expectedErr, 'incorrect error response ' +
31+
`code: should be '${expectedErr}' but got '${err.name}'`);
32+
assert.strictEqual(err.$metadata.httpStatusCode, errors[expectedErr].code,
2833
'incorrect error status code: should be 400 but got ' +
29-
`'${err.statusCode}'`);
34+
`'${err.$metadata.httpStatusCode}'`);
3035
}
31-
cb();
3236
}
3337

3438
describe('aws-sdk test delete bucket policy', () => {
@@ -37,42 +41,48 @@ describe('aws-sdk test delete bucket policy', () => {
3741

3842
before(done => {
3943
const config = getConfig('default', { signatureVersion: 'v4' });
40-
s3 = new S3(config);
44+
s3 = new S3Client(config);
4145
otherAccountS3 = new BucketUtility('lisa', {}).s3;
4246
return done();
4347
});
4448

45-
it('should return NoSuchBucket error if bucket does not exist', done => {
46-
s3.deleteBucketPolicy({ Bucket: bucket }, err =>
47-
assertError(err, 'NoSuchBucket', done));
49+
it('should return NoSuchBucket error if bucket does not exist', async () => {
50+
try {
51+
await s3.send(new DeleteBucketPolicyCommand({ Bucket: bucket }));
52+
throw new Error('Expected NoSuchBucket error');
53+
} catch (err) {
54+
assertError(err, 'NoSuchBucket');
55+
}
4856
});
4957

5058
describe('policy rules', () => {
51-
beforeEach(done => s3.createBucket({ Bucket: bucket }, done));
59+
beforeEach(() => s3.send(new CreateBucketCommand({ Bucket: bucket })));
5260

53-
afterEach(done => s3.deleteBucket({ Bucket: bucket }, done));
61+
afterEach(() => s3.send(new DeleteBucketCommand({ Bucket: bucket })));
5462

55-
it('should return MethodNotAllowed if user is not bucket owner', done => {
56-
otherAccountS3.deleteBucketPolicy({ Bucket: bucket },
57-
err => assertError(err, 'MethodNotAllowed', done));
63+
it('should return MethodNotAllowed if user is not bucket owner', async () => {
64+
try {
65+
await otherAccountS3.send(new DeleteBucketPolicyCommand({ Bucket: bucket }));
66+
throw new Error('Expected MethodNotAllowed error');
67+
} catch (err) {
68+
assertError(err, 'MethodNotAllowed');
69+
}
5870
});
5971

60-
it('should return no error if no policy on bucket', done => {
61-
s3.deleteBucketPolicy({ Bucket: bucket }, err =>
62-
assertError(err, null, done));
72+
it('should return no error if no policy on bucket', () => {
73+
s3.send(new DeleteBucketPolicyCommand({ Bucket: bucket }));
6374
});
6475

65-
it('should delete policy from bucket', done => {
76+
it('should delete policy from bucket', async () => {
6677
const params = { Bucket: bucket, Policy: JSON.stringify(bucketPolicy) };
67-
s3.putBucketPolicy(params, err => {
68-
assert.equal(err, null);
69-
s3.deleteBucketPolicy({ Bucket: bucket }, err => {
70-
assert.equal(err, null);
71-
s3.getBucketPolicy({ Bucket: bucket },
72-
err =>
73-
assertError(err, 'NoSuchBucketPolicy', done));
74-
});
75-
});
78+
await s3.send(new PutBucketPolicyCommand(params));
79+
await s3.send(new DeleteBucketPolicyCommand({ Bucket: bucket }));
80+
try {
81+
await s3.send(new GetBucketPolicyCommand({ Bucket: bucket }));
82+
throw new Error('Expected NoSuchBucketPolicy error');
83+
} catch (err) {
84+
assertError(err, 'NoSuchBucketPolicy');
85+
}
7686
});
7787
});
7888
});

tests/functional/aws-node-sdk/test/bucket/deleteBucketQuota.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const AWS = require('aws-sdk');
2-
const S3 = AWS.S3;
1+
const { S3Client,
2+
CreateBucketCommand,
3+
DeleteBucketCommand } = require('@aws-sdk/client-s3');
34
const assert = require('assert');
45
const getConfig = require('../support/config');
56
const sendRequest = require('../quota/tooling').sendRequest;
@@ -12,20 +13,18 @@ describe('Test delete bucket quota', () => {
1213

1314
before(() => {
1415
const config = getConfig('default', { signatureVersion: 'v4' });
15-
s3 = new S3(config);
16-
AWS.config.update(config);
16+
s3 = new S3Client(config);
1717
});
1818

19-
beforeEach(done => s3.createBucket({ Bucket: bucket }, done));
19+
beforeEach(() => s3.send(new CreateBucketCommand({ Bucket: bucket })));
2020

21-
afterEach(done => s3.deleteBucket({ Bucket: bucket }, done));
21+
afterEach(() => s3.send(new DeleteBucketCommand({ Bucket: bucket })));
2222

2323
it('should delete the bucket quota', async () => {
2424
try {
2525
await sendRequest('DELETE', '127.0.0.1:8000', `/${bucket}/?quota=true`);
26-
assert.ok(true);
2726
} catch (err) {
28-
assert.fail(`Expected no error, but got ${err}`);
27+
assert.fail(`Unexpected error: ${err}`);
2928
}
3029
});
3130

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const assert = require('assert');
2-
const { S3 } = require('aws-sdk');
3-
const { series } = require('async');
2+
const { S3Client,
3+
CreateBucketCommand,
4+
DeleteBucketCommand,
5+
PutBucketVersioningCommand,
6+
PutBucketReplicationCommand,
7+
DeleteBucketReplicationCommand,
8+
GetBucketReplicationCommand } = require('@aws-sdk/client-s3');
49
const { errorInstances } = require('arsenal');
510

611
const getConfig = require('../support/config');
@@ -25,72 +30,69 @@ describe('aws-node-sdk test deleteBucketReplication', () => {
2530
let otherAccountS3;
2631
const config = getConfig('default', { signatureVersion: 'v4' });
2732

28-
function putVersioningOnBucket(bucket, cb) {
29-
return s3.putBucketVersioning({
33+
function putVersioningOnBucket(bucket) {
34+
return s3.send(new PutBucketVersioningCommand({
3035
Bucket: bucket,
3136
VersioningConfiguration: { Status: 'Enabled' },
32-
}, cb);
37+
}));
3338
}
3439

35-
function putReplicationOnBucket(bucket, cb) {
36-
return s3.putBucketReplication({
40+
function putReplicationOnBucket(bucket) {
41+
return s3.send(new PutBucketReplicationCommand({
3742
Bucket: bucket,
3843
ReplicationConfiguration: replicationConfig,
39-
}, cb);
44+
}));
4045
}
4146

42-
function deleteReplicationAndCheckResponse(bucket, cb) {
43-
return s3.deleteBucketReplication({ Bucket: bucket }, (err, data) => {
44-
assert.strictEqual(err, null);
45-
assert.deepStrictEqual(data, {});
46-
return cb();
47-
});
47+
function deleteReplicationAndCheckResponse(bucket) {
48+
return s3.send(new DeleteBucketReplicationCommand({ Bucket: bucket }))
49+
.then(data => {
50+
assert.deepStrictEqual(data.$metadata.httpStatusCode, 204);
51+
});
4852
}
4953

50-
beforeEach(done => {
51-
s3 = new S3(config);
54+
beforeEach(() => {
55+
s3 = new S3Client(config);
5256
otherAccountS3 = new BucketUtility('lisa', {}).s3;
53-
return s3.createBucket({ Bucket: bucket }, done);
57+
return s3.send(new CreateBucketCommand({ Bucket: bucket }));
5458
});
5559

56-
afterEach(done => s3.deleteBucket({ Bucket: bucket }, done));
60+
afterEach(() => s3.send(new DeleteBucketCommand({ Bucket: bucket })));
5761

58-
it('should return empty object if bucket has no replication config', done =>
59-
deleteReplicationAndCheckResponse(bucket, done));
62+
it('should return empty object if bucket has no replication config',
63+
() => deleteReplicationAndCheckResponse(bucket));
6064

61-
it('should delete a bucket replication config when it has one', done =>
62-
series([
63-
next => putVersioningOnBucket(bucket, next),
64-
next => putReplicationOnBucket(bucket, next),
65-
next => deleteReplicationAndCheckResponse(bucket, next),
66-
], done));
65+
it('should delete a bucket replication config when it has one', async () => {
66+
await putVersioningOnBucket(bucket);
67+
await putReplicationOnBucket(bucket);
68+
await deleteReplicationAndCheckResponse(bucket);
69+
});
6770

6871
it('should return ReplicationConfigurationNotFoundError if getting ' +
69-
'replication config after it has been deleted', done =>
70-
series([
71-
next => putVersioningOnBucket(bucket, next),
72-
next => putReplicationOnBucket(bucket, next),
73-
next => s3.getBucketReplication({ Bucket: bucket }, (err, data) => {
74-
if (err) {
75-
return next(err);
76-
}
77-
assert.deepStrictEqual(data, {
78-
ReplicationConfiguration: replicationConfig,
79-
});
80-
return next();
81-
}),
82-
next => deleteReplicationAndCheckResponse(bucket, next),
83-
next => s3.getBucketReplication({ Bucket: bucket }, err => {
84-
assert(errorInstances.ReplicationConfigurationNotFoundError.is[err.code]);
85-
return next();
86-
}),
87-
], done));
72+
'replication config after it has been deleted', async () => {
73+
await putVersioningOnBucket(bucket);
74+
await putReplicationOnBucket(bucket);
75+
76+
const data = await s3.send(new GetBucketReplicationCommand({ Bucket: bucket }));
77+
assert.deepStrictEqual(data.ReplicationConfiguration, replicationConfig);
8878

89-
it('should return AccessDenied if user is not bucket owner', done =>
90-
otherAccountS3.deleteBucketReplication({ Bucket: bucket }, err => {
91-
assert(err);
92-
assert.strictEqual(err.code, 'AccessDenied');
93-
assert.strictEqual(err.statusCode, 403);
94-
return done();
95-
}));
79+
await deleteReplicationAndCheckResponse(bucket);
80+
81+
try {
82+
await s3.send(new GetBucketReplicationCommand({ Bucket: bucket }));
83+
assert.fail('Expected ReplicationConfigurationNotFoundError');
84+
} catch (err) {
85+
assert(errorInstances.ReplicationConfigurationNotFoundError.is[err.name]);
86+
}
87+
});
88+
89+
it('should return AccessDenied if user is not bucket owner', async () => {
90+
try {
91+
await otherAccountS3.send(new DeleteBucketReplicationCommand({ Bucket: bucket }));
92+
assert.fail('Expected AccessDenied error');
93+
} catch (err) {
94+
assert.strictEqual(err.name, 'AccessDenied');
95+
assert.strictEqual(err.$metadata.httpStatusCode, 403);
96+
}
97+
});
9698
});

0 commit comments

Comments
 (0)