11const assert = require ( 'assert' ) ;
22const tv4 = require ( 'tv4' ) ;
33const async = require ( 'async' ) ;
4- const { S3 } = require ( 'aws-sdk' ) ;
4+ const {
5+ S3Client,
6+ ListBucketsCommand,
7+ CreateBucketCommand,
8+ DeleteBucketCommand,
9+ } = require ( '@aws-sdk/client-s3' ) ;
510
611const BucketUtility = require ( '../../lib/utility/bucket-util' ) ;
712const getConfig = require ( '../support/config' ) ;
@@ -14,60 +19,53 @@ const describeFn = process.env.AWS_ON_AIR
1419
1520async function cleanBucket ( bucketUtils , s3 , Bucket ) {
1621 try {
17- await bucketUtils . empty ( Bucket ) ;
18- await s3 . deleteBucket ( { Bucket } ) . promise ( ) ;
19- } catch ( err ) {
20- process . stdout
21- . write ( `Error emptying and deleting bucket: ${ err } \n` ) ;
22- // ignore the error and continue
22+ await bucketUtils . empty ( Bucket , true ) ;
23+ await bucketUtils . deleteOne ( Bucket ) ;
24+ } catch ( error ) {
25+ console . error ( `Error cleaning bucket ${ Bucket } :` , error ) ;
2326 }
2427}
2528
2629async function cleanAllBuckets ( bucketUtils , s3 ) {
27- let listingLoop = true ;
28- let ContinuationToken ;
29-
3030 process . stdout . write ( 'Try cleaning all buckets before running the test\n' ) ;
3131
32- while ( listingLoop ) {
33- const list = await s3 . listBuckets ( { ContinuationToken } ) . promise ( ) ;
34- ContinuationToken = list . ContinuationToken ;
35- listingLoop = ! ! ContinuationToken ;
32+ // ListBuckets doesn't support pagination, it returns all buckets at once
33+ const list = await s3 . send ( new ListBucketsCommand ( { } ) ) ;
3634
37- if ( list . Buckets . length ) {
38- process . stdout
39- . write ( `Found ${ list . Buckets . length } buckets to clean:\n${
40- JSON . stringify ( list . Buckets , null , 2 ) } \n`) ;
41- }
35+ if ( list . Buckets && list . Buckets . length ) {
36+ process . stdout
37+ . write ( `Found ${ list . Buckets . length } buckets to clean:\n${
38+ JSON . stringify ( list . Buckets , null , 2 ) } \n`) ;
4239
4340 // clean sequentially to avoid overloading
4441 for ( const bucket of list . Buckets ) {
42+ console . log ( `Cleaning bucket: ${ bucket . Name } ` ) ;
4543 await cleanBucket ( bucketUtils , s3 , bucket . Name ) ;
4644 }
4745 }
4846}
4947
5048describeFn ( 'GET Service - AWS.S3.listBuckets' , function getService ( ) {
5149 this . timeout ( 600000 ) ;
50+ let unauthenticatedBucketUtil ;
5251
5352 describe ( 'When user is unauthorized' , ( ) => {
54- let s3 ;
55- let config ;
5653
5754 beforeEach ( ( ) => {
58- config = getConfig ( 'default' ) ;
59- s3 = new S3 ( config ) ;
55+ const config = getConfig ( 'default' ) ;
56+ unauthenticatedBucketUtil = new BucketUtility ( 'default' , config , true ) ;
6057 } ) ;
6158
62- it ( 'should return 403 and AccessDenied' , done => {
63- s3 . makeUnauthenticatedRequest ( 'listBuckets' , error => {
64- assert ( error ) ;
65-
66- assert . strictEqual ( error . statusCode , 403 ) ;
67- assert . strictEqual ( error . code , 'AccessDenied' ) ;
59+ it ( 'should return 403 and AccessDenied' , async ( ) => {
60+ const s3Unauth = unauthenticatedBucketUtil . s3
6861
69- done ( ) ;
70- } ) ;
62+ try {
63+ await s3Unauth . send ( new ListBucketsCommand ( { } ) ) ;
64+ throw new Error ( 'Should have thrown an error' ) ;
65+ } catch ( error ) {
66+ assert . strictEqual ( error . $metadata ?. httpStatusCode || error . statusCode , 403 ) ;
67+ assert . strictEqual ( error . name , 'AccessDenied' ) ;
68+ }
7169 } ) ;
7270 } ) ;
7371
@@ -76,46 +74,46 @@ describeFn('GET Service - AWS.S3.listBuckets', function getService() {
7674 let testFn ;
7775
7876 before ( ( ) => {
79- testFn = function testFn ( config , code , statusCode , done ) {
80- const s3 = new S3 ( config ) ;
81- s3 . listBuckets ( ( err , data ) => {
82- assert ( err ) ;
83- assert . ifError ( data ) ;
84-
85- assert . strictEqual ( err . statusCode , statusCode ) ;
86- assert . strictEqual ( err . code , code ) ;
87- done ( ) ;
88- } ) ;
77+ testFn = async function testFn ( config , code , statusCode ) {
78+ const s3 = new S3Client ( config ) ;
79+ try {
80+ await s3 . send ( new ListBucketsCommand ( { } ) ) ;
81+ throw new Error ( 'Should have thrown an error' ) ;
82+ } catch ( err ) {
83+ assert . strictEqual ( err . $metadata ?. httpStatusCode || err . statusCode , statusCode ) ;
84+ assert . strictEqual ( err . name , code ) ;
85+ }
8986 } ;
9087 } ) ;
9188
9289 it ( 'should return 403 and InvalidAccessKeyId ' +
93- 'if accessKeyId is invalid' , done => {
90+ 'if accessKeyId is invalid' , async ( ) => {
9491 const invalidAccess = getConfig ( 'default' ,
9592 Object . assign ( { } ,
9693 {
97- credentials : null ,
98- accessKeyId : 'wrong' ,
99- secretAccessKey : 'wrong again' ,
94+ credentials : {
95+ accessKeyId : 'wrong' ,
96+ secretAccessKey : 'wrong again' ,
97+ } ,
10098 } ,
10199 sigCfg
102100 )
103101 ) ;
104102 const expectedCode = 'InvalidAccessKeyId' ;
105103 const expectedStatus = 403 ;
106104
107- testFn ( invalidAccess , expectedCode , expectedStatus , done ) ;
105+ await testFn ( invalidAccess , expectedCode , expectedStatus ) ;
108106 } ) ;
109107
110108 it ( 'should return 403 and SignatureDoesNotMatch ' +
111- 'if credential is polluted' , done => {
109+ 'if credential is polluted' , async ( ) => {
112110 const pollutedConfig = getConfig ( 'default' , sigCfg ) ;
113111 pollutedConfig . credentials . secretAccessKey = 'wrong' ;
114112
115113 const expectedCode = 'SignatureDoesNotMatch' ;
116114 const expectedStatus = 403 ;
117115
118- testFn ( pollutedConfig , expectedCode , expectedStatus , done ) ;
116+ await testFn ( pollutedConfig , expectedCode , expectedStatus ) ;
119117 } ) ;
120118 } ) ;
121119
@@ -129,24 +127,25 @@ describeFn('GET Service - AWS.S3.listBuckets', function getService() {
129127 . map ( i => `getservicebuckets-${ i } ` ) ;
130128
131129 before ( done => {
130+ console . log ( 'sigCfg' , sigCfg ) ;
132131 bucketUtil = new BucketUtility ( 'default' , sigCfg ) ;
133132 s3 = bucketUtil . s3 ;
134- s3 . config . update ( { maxRetries : 0 } ) ;
135- s3 . config . update ( { httpOptions : { timeout : 0 } } ) ;
136133 async . series ( [
137- // if other tests failed to delete their buckets, listings might be wrong
138- // try toclean all buckets before running the test
139- next => cleanAllBuckets ( bucketUtil , s3 ) . then ( next ) . catch ( next ) ,
134+ next => cleanAllBuckets ( bucketUtil , s3 ) . then ( ( ) => next ( ) ) . catch ( next ) ,
140135 next =>
141136 async . eachLimit ( createdBuckets , 10 , ( bucketName , moveOn ) => {
142- s3 . createBucket ( { Bucket : bucketName } , err => {
143- if ( bucketName . endsWith ( '000' ) ) {
144- // log to keep ci alive
145- process . stdout
146- . write ( `creating bucket: ${ bucketName } \n` ) ;
147- }
148- moveOn ( err ) ;
149- } ) ;
137+ s3 . send ( new CreateBucketCommand ( { Bucket : bucketName } ) )
138+ . then ( ( ) => {
139+ if ( bucketName . endsWith ( '000' ) ) {
140+ process . stdout
141+ . write ( `creating bucket: ${ bucketName } \n` ) ;
142+ }
143+ moveOn ( ) ;
144+ } )
145+ . catch ( err => {
146+ console . log ( 'Error creating bucket:' , err ) ;
147+ moveOn ( err ) ;
148+ } ) ;
150149 } ,
151150 err => {
152151 if ( err ) {
@@ -159,14 +158,16 @@ describeFn('GET Service - AWS.S3.listBuckets', function getService() {
159158
160159 after ( done => {
161160 async . eachLimit ( createdBuckets , 10 , ( bucketName , moveOn ) => {
162- s3 . deleteBucket ( { Bucket : bucketName } , err => {
163- if ( bucketName . endsWith ( '000' ) ) {
164- // log to keep ci alive
165- process . stdout
166- . write ( `deleting bucket: ${ bucketName } \n` ) ;
167- }
168- moveOn ( err ) ;
169- } ) ;
161+ s3 . send ( new DeleteBucketCommand ( { Bucket : bucketName } ) )
162+ . then ( ( ) => {
163+ if ( bucketName . endsWith ( '000' ) ) {
164+ // log to keep ci alive
165+ process . stdout
166+ . write ( `deleting bucket: ${ bucketName } \n` ) ;
167+ }
168+ moveOn ( ) ;
169+ } )
170+ . catch ( moveOn ) ;
170171 } ,
171172 err => {
172173 if ( err ) {
@@ -176,14 +177,16 @@ describeFn('GET Service - AWS.S3.listBuckets', function getService() {
176177 } ) ;
177178 } ) ;
178179
179- it ( 'should list buckets concurrently' , done => {
180+ it . only ( 'should list buckets concurrently' , done => {
180181 async . times ( 20 , ( n , next ) => {
181- s3 . listBuckets ( ( err , result ) => {
182- assert . equal ( result . Buckets . length ,
183- createdBuckets . length ,
184- 'Created buckets are missing in response' ) ;
185- next ( err ) ;
186- } ) ;
182+ s3 . send ( new ListBucketsCommand ( { } ) )
183+ . then ( result => {
184+ assert . equal ( result . Buckets . length ,
185+ createdBuckets . length ,
186+ 'Created buckets are missing in response' ) ;
187+ next ( ) ;
188+ } )
189+ . catch ( next ) ;
187190 } ,
188191 err => {
189192 assert . ifError ( err , `error listing buckets: ${ err } ` ) ;
@@ -192,8 +195,7 @@ describeFn('GET Service - AWS.S3.listBuckets', function getService() {
192195 } ) ;
193196
194197 it ( 'should list buckets' , done => {
195- s3
196- . listBuckets ( ) . promise ( )
198+ s3 . send ( new ListBucketsCommand ( { } ) )
197199 . then ( data => {
198200 const isValidResponse = tv4 . validate ( data , svcSchema ) ;
199201 if ( ! isValidResponse ) {
@@ -230,19 +232,17 @@ describeFn('GET Service - AWS.S3.listBuckets', function getService() {
230232 . catch ( done ) ;
231233 } ) ;
232234
233- const filterFn = bucket => createdBuckets . indexOf ( bucket . name ) > - 1 ;
235+ const filterFn = bucket => createdBuckets . indexOf ( bucket . Name ) > - 1 ;
234236
235237 describe ( 'two accounts are given' , ( ) => {
236238 let anotherS3 ;
237239
238240 before ( ( ) => {
239- anotherS3 = new S3 ( getConfig ( 'lisa' ) ) ;
240- anotherS3 . config . setPromisesDependency ( Promise ) ;
241+ anotherS3 = new S3Client ( getConfig ( 'lisa' ) ) ;
241242 } ) ;
242243
243244 it ( 'should not return other accounts bucket list' , done => {
244- anotherS3
245- . listBuckets ( ) . promise ( )
245+ anotherS3 . send ( new ListBucketsCommand ( { } ) )
246246 . then ( data => {
247247 const hasSameBuckets = data . Buckets
248248 . filter ( filterFn )
0 commit comments