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,31 +19,25 @@ 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 ) {
22+ await bucketUtils . empty ( Bucket , true ) ;
23+ await bucketUtils . deleteOne ( Bucket ) ;
24+ } catch ( error ) {
2025 process . stdout
21- . write ( `Error emptying and deleting bucket: ${ err } \n` ) ;
26+ . write ( `Error emptying and deleting bucket: ${ error } \n` ) ;
2227 // ignore the error and continue
2328 }
2429}
2530
2631async function cleanAllBuckets ( bucketUtils , s3 ) {
27- let listingLoop = true ;
28- let ContinuationToken ;
29-
3032 process . stdout . write ( 'Try cleaning all buckets before running the test\n' ) ;
3133
32- while ( listingLoop ) {
33- const list = await s3 . listBuckets ( { ContinuationToken } ) . promise ( ) ;
34- ContinuationToken = list . ContinuationToken ;
35- listingLoop = ! ! ContinuationToken ;
34+ // ListBuckets doesn't support pagination, it returns all buckets at once
35+ const list = await s3 . send ( new ListBucketsCommand ( { } ) ) ;
3636
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- }
37+ if ( list . Buckets && list . Buckets . length ) {
38+ process . stdout
39+ . write ( `Found ${ list . Buckets . length } buckets to clean:\n${
40+ JSON . stringify ( list . Buckets , null , 2 ) } \n`) ;
4241
4342 // clean sequentially to avoid overloading
4443 for ( const bucket of list . Buckets ) {
@@ -49,25 +48,25 @@ async function cleanAllBuckets(bucketUtils, s3) {
4948
5049describeFn ( 'GET Service - AWS.S3.listBuckets' , function getService ( ) {
5150 this . timeout ( 600000 ) ;
51+ let unauthenticatedBucketUtil ;
5252
5353 describe ( 'When user is unauthorized' , ( ) => {
54- let s3 ;
55- let config ;
5654
5755 beforeEach ( ( ) => {
58- config = getConfig ( 'default' ) ;
59- s3 = new S3 ( config ) ;
56+ const config = getConfig ( 'default' ) ;
57+ unauthenticatedBucketUtil = new BucketUtility ( 'default' , config , true ) ;
6058 } ) ;
6159
62- it ( 'should return 403 and AccessDenied' , done => {
63- s3 . makeUnauthenticatedRequest ( 'listBuckets' , error => {
64- assert ( error ) ;
60+ it ( 'should return 403 and AccessDenied' , async ( ) => {
61+ const s3Unauth = unauthenticatedBucketUtil . s3 ;
6562
66- assert . strictEqual ( error . statusCode , 403 ) ;
67- assert . strictEqual ( error . code , 'AccessDenied' ) ;
68-
69- done ( ) ;
70- } ) ;
63+ try {
64+ await s3Unauth . send ( new ListBucketsCommand ( { } ) ) ;
65+ throw new Error ( 'Should have thrown an error' ) ;
66+ } catch ( error ) {
67+ assert . strictEqual ( error . $metadata ?. httpStatusCode || error . statusCode , 403 ) ;
68+ assert . strictEqual ( error . name , 'AccessDenied' ) ;
69+ }
7170 } ) ;
7271 } ) ;
7372
@@ -76,46 +75,46 @@ describeFn('GET Service - AWS.S3.listBuckets', function getService() {
7675 let testFn ;
7776
7877 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- } ) ;
78+ testFn = async function testFn ( config , code , statusCode ) {
79+ const s3 = new S3Client ( config ) ;
80+ try {
81+ await s3 . send ( new ListBucketsCommand ( { } ) ) ;
82+ throw new Error ( 'Should have thrown an error' ) ;
83+ } catch ( err ) {
84+ assert . strictEqual ( err . $metadata ?. httpStatusCode || err . statusCode , statusCode ) ;
85+ assert . strictEqual ( err . name , code ) ;
86+ }
8987 } ;
9088 } ) ;
9189
9290 it ( 'should return 403 and InvalidAccessKeyId ' +
93- 'if accessKeyId is invalid' , done => {
91+ 'if accessKeyId is invalid' , async ( ) => {
9492 const invalidAccess = getConfig ( 'default' ,
9593 Object . assign ( { } ,
9694 {
97- credentials : null ,
98- accessKeyId : 'wrong' ,
99- secretAccessKey : 'wrong again' ,
95+ credentials : {
96+ accessKeyId : 'wrong' ,
97+ secretAccessKey : 'wrong again' ,
98+ } ,
10099 } ,
101100 sigCfg
102101 )
103102 ) ;
104103 const expectedCode = 'InvalidAccessKeyId' ;
105104 const expectedStatus = 403 ;
106105
107- testFn ( invalidAccess , expectedCode , expectedStatus , done ) ;
106+ await testFn ( invalidAccess , expectedCode , expectedStatus ) ;
108107 } ) ;
109108
110109 it ( 'should return 403 and SignatureDoesNotMatch ' +
111- 'if credential is polluted' , done => {
110+ 'if credential is polluted' , async ( ) => {
112111 const pollutedConfig = getConfig ( 'default' , sigCfg ) ;
113112 pollutedConfig . credentials . secretAccessKey = 'wrong' ;
114113
115114 const expectedCode = 'SignatureDoesNotMatch' ;
116115 const expectedStatus = 403 ;
117116
118- testFn ( pollutedConfig , expectedCode , expectedStatus , done ) ;
117+ await testFn ( pollutedConfig , expectedCode , expectedStatus ) ;
119118 } ) ;
120119 } ) ;
121120
@@ -131,42 +130,46 @@ describeFn('GET Service - AWS.S3.listBuckets', function getService() {
131130 before ( done => {
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+ moveOn ( err ) ;
147+ } ) ;
150148 } ,
151149 err => {
152150 if ( err ) {
153- process . stdout . write ( `err creating buckets: ${ err } ` ) ;
151+ process . stdout . write ( `err creating buckets: ${ err } \n` ) ;
152+ return next ( err ) ;
154153 }
155- next ( err ) ;
154+ return next ( err ) ;
156155 } )
157156 ] , done ) ;
158157 } ) ;
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 ( ( ) => {
171+ moveOn ( ) ;
172+ } ) ;
170173 } ,
171174 err => {
172175 if ( err ) {
@@ -178,12 +181,18 @@ describeFn('GET Service - AWS.S3.listBuckets', function getService() {
178181
179182 it ( 'should list buckets concurrently' , done => {
180183 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- } ) ;
184+ s3 . send ( new ListBucketsCommand ( { } ) )
185+ . then ( result => {
186+ // Filter for our test buckets only
187+ const ourBuckets = result . Buckets . filter ( bucket =>
188+ bucket . Name . startsWith ( 'getservicebuckets-' )
189+ ) ;
190+ assert . equal ( ourBuckets . length ,
191+ createdBuckets . length ,
192+ 'Created buckets are missing in response' ) ;
193+ next ( ) ;
194+ } )
195+ . catch ( next ) ;
187196 } ,
188197 err => {
189198 assert . ifError ( err , `error listing buckets: ${ err } ` ) ;
@@ -192,8 +201,7 @@ describeFn('GET Service - AWS.S3.listBuckets', function getService() {
192201 } ) ;
193202
194203 it ( 'should list buckets' , done => {
195- s3
196- . listBuckets ( ) . promise ( )
204+ s3 . send ( new ListBucketsCommand ( { } ) )
197205 . then ( data => {
198206 const isValidResponse = tv4 . validate ( data , svcSchema ) ;
199207 if ( ! isValidResponse ) {
@@ -230,19 +238,17 @@ describeFn('GET Service - AWS.S3.listBuckets', function getService() {
230238 . catch ( done ) ;
231239 } ) ;
232240
233- const filterFn = bucket => createdBuckets . indexOf ( bucket . name ) > - 1 ;
241+ const filterFn = bucket => createdBuckets . indexOf ( bucket . Name ) > - 1 ;
234242
235243 describe ( 'two accounts are given' , ( ) => {
236244 let anotherS3 ;
237245
238246 before ( ( ) => {
239- anotherS3 = new S3 ( getConfig ( 'lisa' ) ) ;
240- anotherS3 . config . setPromisesDependency ( Promise ) ;
247+ anotherS3 = new S3Client ( getConfig ( 'lisa' ) ) ;
241248 } ) ;
242249
243250 it ( 'should not return other accounts bucket list' , done => {
244- anotherS3
245- . listBuckets ( ) . promise ( )
251+ anotherS3 . send ( new ListBucketsCommand ( { } ) )
246252 . then ( data => {
247253 const hasSameBuckets = data . Buckets
248254 . filter ( filterFn )
0 commit comments