11const assert = require ( 'assert' ) ;
22const async = require ( 'async' ) ;
33const arsenal = require ( 'arsenal' ) ;
4+ const {
5+ ListObjectsCommand,
6+ HeadBucketCommand,
7+ CreateBucketCommand,
8+ DeleteBucketCommand,
9+ PutObjectCommand,
10+ DeleteObjectCommand,
11+ } = require ( '@aws-sdk/client-s3' ) ;
412const { GCP } = arsenal . storage . data . external . GCP ;
5- const { makeGcpRequest } = require ( '../../../utils/makeRequest' ) ;
6- const { gcpRequestRetry, genUniqID } = require ( '../../../utils/gcpUtils' ) ;
13+ const { genUniqID, gcpRetry } = require ( '../../../utils/gcpUtils' ) ;
714const { getRealAwsConfig } =
815 require ( '../../../../aws-node-sdk/test/support/awsConfig' ) ;
916const { listingHardLimit } = require ( '../../../../../../constants' ) ;
@@ -18,95 +25,90 @@ const gcpClient = new GCP(config);
1825function populateBucket ( createdObjects , callback ) {
1926 process . stdout . write (
2027 `Putting ${ createdObjects . length } objects into bucket\n` ) ;
21- async . mapLimit ( createdObjects , 10 ,
22- ( object , moveOn ) => {
23- makeGcpRequest ( {
24- method : 'PUT' ,
25- bucket : bucketName ,
26- objectKey : object ,
27- authCredentials : config . credentials ,
28- } , err => {
29- moveOn ( err ) ;
30- } ) ;
31- } , err => {
32- if ( err ) {
33- process . stdout
34- . write ( `err putting objects ${ err . code } \n` ) ;
28+ async . mapLimit (
29+ createdObjects ,
30+ 10 ,
31+ async object => {
32+ const command = new PutObjectCommand ( {
33+ Bucket : bucketName ,
34+ Key : object ,
35+ } ) ;
36+ await gcpClient . send ( command ) ;
37+ } ,
38+ err => {
39+ if ( err ) {
40+ process . stdout
41+ . write ( `err putting objects ${ err } \n` ) ;
42+ }
43+ return callback ( err ) ;
3544 }
36- return callback ( err ) ;
37- } ) ;
45+ ) ;
3846}
3947
4048function removeObjects ( createdObjects , callback ) {
4149 process . stdout . write (
4250 `Deleting ${ createdObjects . length } objects from bucket\n` ) ;
43- async . mapLimit ( createdObjects , 10 ,
44- ( object , moveOn ) => {
45- makeGcpRequest ( {
46- method : 'DELETE' ,
47- bucket : bucketName ,
48- objectKey : object ,
49- authCredentials : config . credentials ,
50- } , err => moveOn ( err ) ) ;
51- } , err => {
52- if ( err ) {
53- process . stdout
54- . write ( `err deleting objects ${ err . code } \n` ) ;
51+ async . mapLimit (
52+ createdObjects ,
53+ 10 ,
54+ async object => {
55+ const command = new DeleteObjectCommand ( {
56+ Bucket : bucketName ,
57+ Key : object ,
58+ } ) ;
59+ await gcpClient . send ( command ) ;
60+ } ,
61+ err => {
62+ if ( err ) {
63+ process . stdout
64+ . write ( `err deleting objects ${ err } \n` ) ;
65+ }
66+ return callback ( err ) ;
5567 }
56- return callback ( err ) ;
57- } ) ;
68+ ) ;
5869}
5970
6071describe ( 'GCP: GET Bucket' , function testSuite ( ) {
6172 this . timeout ( 180000 ) ;
6273
63- before ( done => {
64- gcpRequestRetry ( {
65- method : 'PUT' ,
66- bucket : bucketName ,
67- authCredentials : config . credentials ,
68- } , 0 , err => {
69- if ( err ) {
70- process . stdout . write ( `err in creating bucket ${ err } \n` ) ;
71- }
72- return done ( err ) ;
73- } ) ;
74+ before ( async ( ) => {
75+ await gcpRetry (
76+ gcpClient ,
77+ new CreateBucketCommand ( { Bucket : bucketName } ) ,
78+ ) ;
7479 } ) ;
7580
76- after ( done => {
77- gcpRequestRetry ( {
78- method : 'DELETE' ,
79- bucket : bucketName ,
80- authCredentials : config . credentials ,
81- } , 0 , err => {
82- if ( err ) {
83- process . stdout . write ( `err in deleting bucket ${ err } \n` ) ;
84- }
85- return done ( err ) ;
86- } ) ;
81+ after ( async ( ) => {
82+ await gcpRetry (
83+ gcpClient ,
84+ new DeleteBucketCommand ( { Bucket : bucketName } ) ,
85+ ) ;
8786 } ) ;
8887
8988 describe ( 'without existing bucket' , ( ) => {
90- it ( 'should return 404 and NoSuchBucket' , done => {
89+ it ( 'should return 404 and NoSuchBucket' , async ( ) => {
9190 const badBucketName = `nonexistingbucket-${ genUniqID ( ) } ` ;
92- gcpClient . getBucket ( {
93- Bucket : badBucketName ,
94- } , err => {
91+ try {
92+ const command = new HeadBucketCommand ( {
93+ Bucket : badBucketName ,
94+ } ) ;
95+ await gcpClient . send ( command ) ;
96+ assert . fail ( 'Expected NoSuchBucket error, but got success' ) ;
97+ } catch ( err ) {
9598 assert ( err ) ;
96- assert . strictEqual ( err . $metadata ?. httpStatusCode , 404 ) ;
97- assert . strictEqual ( err . name , 'NoSuchBucket' ) ;
98- return done ( ) ;
99- } ) ;
99+ const statusCode = err . $metadata && err . $metadata . httpStatusCode ;
100+ assert . strictEqual ( statusCode , 404 ) ;
101+ const errorName = err . name === 'NotFound' ? 'NoSuchBucket' : err . name ;
102+ assert . strictEqual ( errorName , 'NoSuchBucket' ) ;
103+ }
100104 } ) ;
101105
102- it ( 'should return 200' , done => {
103- gcpClient . listObjects ( {
106+ it ( 'should return 200' , async ( ) => {
107+ const command = new ListObjectsCommand ( {
104108 Bucket : bucketName ,
105- } , ( err , res ) => {
106- assert . equal ( err , null , `Expected success, but got ${ err } ` ) ;
107- assert . strictEqual ( res . $metadata ?. httpStatusCode , 200 ) ;
108- return done ( ) ;
109109 } ) ;
110+ const res = await gcpClient . send ( command ) ;
111+ assert . strictEqual ( res . $metadata ?. httpStatusCode , 200 ) ;
110112 } ) ;
111113 } ) ;
112114
@@ -119,27 +121,22 @@ describe('GCP: GET Bucket', function testSuite() {
119121
120122 after ( done => removeObjects ( createdObjects , done ) ) ;
121123
122- it ( `should list all ${ smallSize } created objects` , done => {
123- gcpClient . listObjects ( {
124+ it ( `should list all ${ smallSize } created objects` , async ( ) => {
125+ const command = new ListObjectsCommand ( {
124126 Bucket : bucketName ,
125- } , ( err , res ) => {
126- assert . equal ( err , null , `Expected success, but got ${ err } ` ) ;
127- assert . strictEqual ( res . Contents . length , smallSize ) ;
128- return done ( ) ;
129127 } ) ;
128+ const res = await gcpClient . send ( command ) ;
129+ assert . strictEqual ( res . Contents . length , smallSize ) ;
130130 } ) ;
131131
132132 describe ( 'with MaxKeys at 10' , ( ) => {
133- it ( 'should list MaxKeys number of objects' , done => {
134- gcpClient . listObjects ( {
133+ it ( 'should list MaxKeys number of objects' , async ( ) => {
134+ const command = new ListObjectsCommand ( {
135135 Bucket : bucketName ,
136136 MaxKeys : 10 ,
137- } , ( err , res ) => {
138- assert . equal ( err , null ,
139- `Expected success, but got ${ err } ` ) ;
140- assert . strictEqual ( res . Contents . length , 10 ) ;
141- return done ( ) ;
142137 } ) ;
138+ const res = await gcpClient . send ( command ) ;
139+ assert . strictEqual ( res . Contents . length , 10 ) ;
143140 } ) ;
144141 } ) ;
145142 } ) ;
@@ -152,15 +149,12 @@ describe('GCP: GET Bucket', function testSuite() {
152149
153150 after ( done => removeObjects ( createdObjects , done ) ) ;
154151
155- it ( 'should list at max 1000 of objects created' , done => {
156- gcpClient . listObjects ( {
152+ it ( 'should list at max 1000 of objects created' , async ( ) => {
153+ const command = new ListObjectsCommand ( {
157154 Bucket : bucketName ,
158- } , ( err , res ) => {
159- assert . equal ( err , null , `Expected success, but got ${ err } ` ) ;
160- assert . strictEqual ( res . Contents . length ,
161- listingHardLimit ) ;
162- return done ( ) ;
163155 } ) ;
156+ const res = await gcpClient . send ( command ) ;
157+ assert . strictEqual ( res . Contents . length , listingHardLimit ) ;
164158 } ) ;
165159
166160 describe ( 'with MaxKeys at 1001' , ( ) => {
@@ -175,17 +169,13 @@ describe('GCP: GET Bucket', function testSuite() {
175169 //
176170 // Actual behavior: it returns a list longer than 1000 objects when
177171 // max-keys is greater than 1000
178- it . skip ( 'should list at max 1000, ignoring MaxKeys' , done => {
179- gcpClient . listObjects ( {
172+ it . skip ( 'should list at max 1000, ignoring MaxKeys' , async ( ) => {
173+ const command = new ListObjectsCommand ( {
180174 Bucket : bucketName ,
181175 MaxKeys : 1001 ,
182- } , ( err , res ) => {
183- assert . equal ( err , null ,
184- `Expected success, but got ${ err } ` ) ;
185- assert . strictEqual ( res . Contents . length ,
186- listingHardLimit ) ;
187- return done ( ) ;
188176 } ) ;
177+ const res = await gcpClient . send ( command ) ;
178+ assert . strictEqual ( res . Contents . length , listingHardLimit ) ;
189179 } ) ;
190180 } ) ;
191181 } ) ;
0 commit comments