-
Notifications
You must be signed in to change notification settings - Fork 367
/
buckets.test.js
411 lines (349 loc) · 13.3 KB
/
buckets.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const {Storage} = require('@google-cloud/storage');
const {assert} = require('chai');
const {after, it} = require('mocha');
const cp = require('child_process');
const uuid = require('uuid');
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const storage = new Storage();
const samplesTestBucketPrefix = `nodejs-storage-samples-${uuid.v4()}`;
const bucketName = `${samplesTestBucketPrefix}-a`;
const bucketNameDualRegion = `${samplesTestBucketPrefix}-b`;
const bucketNameDualRegionTurbo = `${samplesTestBucketPrefix}-c`;
const bucketNameWithClassAndLocation = `${samplesTestBucketPrefix}-d`;
const bucketNameAutoclass = `${samplesTestBucketPrefix}-e`;
const defaultKmsKeyName = process.env.GOOGLE_CLOUD_KMS_KEY_ASIA;
const bucket = storage.bucket(bucketName);
const bucketWithClassAndLocation = storage.bucket(
bucketNameWithClassAndLocation
);
const dualRegionBucket = storage.bucket(bucketNameDualRegion);
const dualRegionBucketTurbo = storage.bucket(bucketNameDualRegionTurbo);
const PUBLIC_ACCESS_PREVENTION_INHERITED = 'inherited';
const PUBLIC_ACCESS_PREVENTION_ENFORCED = 'enforced';
const DUAL_REGION = {
LOCATION: 'US',
REGIONS: ['US-EAST1', 'US-WEST1'],
};
const RPO_ASYNC_TURBO = 'ASYNC_TURBO';
const RPO_DEFAULT = 'DEFAULT';
async function deleteAllBucketsAsync() {
const [buckets] = await storage.getBuckets({prefix: samplesTestBucketPrefix});
for (const bucket of buckets) {
await bucket.deleteFiles({force: true});
await bucket.delete({ignoreNotFound: true});
}
}
after(deleteAllBucketsAsync);
it('should create a bucket', async () => {
const output = execSync(`node createNewBucket.js ${bucketName}`);
assert.match(output, new RegExp(`Bucket ${bucketName} created`));
const [exists] = await bucket.exists();
assert.strictEqual(exists, true);
});
it('should list buckets', () => {
const output = execSync('node listBuckets.js');
assert.match(output, /Buckets:/);
assert.match(output, new RegExp(bucketName));
});
it('should get bucket metadata', async () => {
const output = execSync(`node bucketMetadata.js ${bucketName}`);
assert.include(output, bucketName);
});
it('should disable autoclass', async () => {
await storage.createBucket(bucketNameAutoclass, {
autoclass: {
enabled: true,
},
});
const output = execSync(
`node setAutoclass.js ${bucketNameAutoclass} ${false}`
);
assert.include(output, 'Autoclass');
});
it('should get autoclass', async () => {
const output = execSync(`node getAutoclass.js ${bucketNameAutoclass}`);
assert.include(output, 'Autoclass enabled is set to false');
});
it('should set a buckets default KMS key', async () => {
const output = execSync(
`node enableDefaultKMSKey.js ${bucketName} ${defaultKmsKeyName}`
);
assert.include(
output,
`Default KMS key for ${bucketName} was set to ${defaultKmsKeyName}`
);
const metadata = await bucket.getMetadata();
assert.strictEqual(
metadata[0].encryption.defaultKmsKeyName,
defaultKmsKeyName
);
});
it('should remove a buckets default KMS key', async () => {
const output = execSync(`node removeDefaultKMSKey.js ${bucketName}`);
assert.include(output, `Default KMS key was removed from ${bucketName}`);
const [metadata] = await bucket.getMetadata();
assert.ok(!metadata.encryption);
});
it("should enable a bucket's uniform bucket-level access", async () => {
const output = execSync(
`node enableUniformBucketLevelAccess.js ${bucketName}`
);
assert.match(
output,
new RegExp(`Uniform bucket-level access was enabled for ${bucketName}`)
);
const metadata = await bucket.getMetadata();
assert.strictEqual(
metadata[0].iamConfiguration.uniformBucketLevelAccess.enabled,
true
);
});
it("should get a bucket's uniform bucket-level access metadata", async () => {
const output = execSync(`node getUniformBucketLevelAccess.js ${bucketName}`);
assert.match(
output,
new RegExp(`Uniform bucket-level access is enabled for ${bucketName}`)
);
const [metadata] = await bucket.getMetadata();
assert.ok(metadata.iamConfiguration.uniformBucketLevelAccess.enabled);
assert.strictEqual(
metadata.iamConfiguration.uniformBucketLevelAccess.lockedTime !== null,
true
);
});
it("should disable a bucket's uniform bucket-level access", async () => {
const output = execSync(
`node disableUniformBucketLevelAccess.js ${bucketName}`
);
assert.match(
output,
new RegExp(`Uniform bucket-level access was disabled for ${bucketName}`)
);
const metadata = await bucket.getMetadata();
assert.strictEqual(
metadata[0].iamConfiguration.uniformBucketLevelAccess.enabled,
false
);
});
it('should configure a bucket cors', async () => {
execSync(
`node configureBucketCors.js ${bucketName} 3600 POST http://example.appspot.com content-type`
);
await bucket.getMetadata();
assert.deepStrictEqual(bucket.metadata.cors[0], {
origin: ['http://example.appspot.com'],
method: ['POST'],
responseHeader: ['content-type'],
maxAgeSeconds: 3600,
});
});
it('should remove a bucket cors configuration', async () => {
const output = execSync(`node removeBucketCors.js ${bucketName}`);
assert.include(
output,
`Removed CORS configuration from bucket ${bucketName}`
);
await bucket.getMetadata();
assert.ok(!bucket.metadata.cors);
});
it('should set public access prevention to enforced', async () => {
const output = execSync(
`node setPublicAccessPreventionEnforced.js ${bucketName}`
);
assert.match(
output,
new RegExp(`Public access prevention is set to enforced for ${bucketName}.`)
);
const metadata = await bucket.getMetadata();
assert.strictEqual(
metadata[0].iamConfiguration.publicAccessPrevention,
PUBLIC_ACCESS_PREVENTION_ENFORCED
);
});
it("should get a bucket's public access prevention metadata", async () => {
await storage.bucket(bucketName).setMetadata({
iamConfiguration: {
publicAccessPrevention: PUBLIC_ACCESS_PREVENTION_ENFORCED,
},
});
const output = execSync(`node getPublicAccessPrevention.js ${bucketName}`);
assert.match(
output,
new RegExp(`Public access prevention is enforced for ${bucketName}.`)
);
const [metadata] = await bucket.getMetadata();
assert.ok(metadata.iamConfiguration.publicAccessPrevention);
});
it('should set public access prevention to inherited', async () => {
const output = execSync(
`node setPublicAccessPreventionInherited.js ${bucketName}`
);
assert.match(
output,
new RegExp(`Public access prevention is 'inherited' for ${bucketName}.`)
);
const metadata = await bucket.getMetadata();
assert.strictEqual(
metadata[0].iamConfiguration.publicAccessPrevention,
PUBLIC_ACCESS_PREVENTION_INHERITED
);
});
it('should create a dual-region bucket', async () => {
const output = execSync(
`node createBucketWithDualRegion.js ${bucketNameDualRegion} ${DUAL_REGION.LOCATION} ${DUAL_REGION.REGIONS[0]} ${DUAL_REGION.REGIONS[1]}`
);
// Ensure the sample outputs the desired result
assert.include(output, bucketNameDualRegion);
assert.include(output, DUAL_REGION.LOCATION);
assert.include(output, DUAL_REGION.REGIONS[0]);
assert.include(output, DUAL_REGION.REGIONS[1]);
assert.include(output, 'dual-region');
// Make API request for further verification
const [exists] = await dualRegionBucket.exists();
assert.strictEqual(exists, true);
const [metadata] = await dualRegionBucket.getMetadata();
assert.strictEqual(metadata.location, DUAL_REGION.LOCATION);
assert(metadata.customPlacementConfig);
assert(Array.isArray(metadata.customPlacementConfig.dataLocations));
const dataLocations = metadata.customPlacementConfig.dataLocations;
assert(dataLocations.includes(DUAL_REGION.REGIONS[0]));
assert(dataLocations.includes(DUAL_REGION.REGIONS[1]));
assert.strictEqual(metadata.locationType, 'dual-region');
});
it('should create a dual-region bucket with turbo replication enabled', async () => {
const output = execSync(
`node createBucketWithTurboReplication.js ${bucketNameDualRegionTurbo}`
);
assert.match(
output,
new RegExp(
`${bucketNameDualRegionTurbo} created with the recovery point objective \\(RPO\\) set to ASYNC_TURBO in NAM4.`
)
);
const [exists] = await dualRegionBucketTurbo.exists();
assert.strictEqual(exists, true);
});
it("should get a bucket's RPO metadata", async () => {
await storage.bucket(bucketNameDualRegionTurbo).setMetadata({
rpo: RPO_ASYNC_TURBO,
});
const output = execSync(`node getRPO.js ${bucketNameDualRegionTurbo}`);
assert.match(
output,
new RegExp(`RPO is ASYNC_TURBO for ${bucketNameDualRegionTurbo}.`)
);
const metadata = await dualRegionBucketTurbo.getMetadata();
assert.strictEqual(metadata[0].rpo, RPO_ASYNC_TURBO);
});
it("should set a bucket's RPO to ASYNC_TURBO", async () => {
const output = execSync(
`node setRPOAsyncTurbo.js ${bucketNameDualRegionTurbo}`
);
assert.match(
output,
new RegExp(`Turbo replication enabled for ${bucketNameDualRegionTurbo}.`)
);
const metadata = await dualRegionBucketTurbo.getMetadata();
assert.strictEqual(metadata[0].rpo, RPO_ASYNC_TURBO);
});
it("should set a bucket's RPO to DEFAULT", async () => {
const output = execSync(`node setRPODefault.js ${bucketNameDualRegionTurbo}`);
assert.match(
output,
new RegExp(`Turbo replication disabled for ${bucketNameDualRegionTurbo}.`)
);
const metadata = await dualRegionBucketTurbo.getMetadata();
assert.strictEqual(metadata[0].rpo, RPO_DEFAULT);
});
it("should add a bucket's website configuration", async () => {
const output = execSync(
`node addBucketWebsiteConfiguration.js ${bucketName} http://example.com http://example.com/404.html`
);
assert.include(
output,
`Static website bucket ${bucketName} is set up to use http://example.com as the index page and http://example.com/404.html as the 404 page`
);
const [metadata] = await bucket.getMetadata();
assert.deepStrictEqual(metadata.website, {
mainPageSuffix: 'http://example.com',
notFoundPage: 'http://example.com/404.html',
});
});
it('should make bucket publicly readable', async () => {
const output = execSync(`node makeBucketPublic.js ${bucketName}`);
assert.match(
output,
new RegExp(`Bucket ${bucketName} is now publicly readable`)
);
const [policy] = await bucket.iam.getPolicy();
const objectViewerBinding = policy.bindings.filter(binding => {
return binding.role === 'roles/storage.legacyBucketReader';
})[0];
assert(objectViewerBinding.members.includes('allUsers'));
});
it("should enable a bucket's versioning", async () => {
const output = execSync(`node enableBucketVersioning.js ${bucketName}`);
assert.include(output, `Versioning is enabled for bucket ${bucketName}`);
await bucket.getMetadata();
assert.strictEqual(bucket.metadata.versioning.enabled, true);
});
it("should disable a bucket's versioning", async () => {
const output = execSync(`node disableBucketVersioning.js ${bucketName}`);
assert.include(output, `Versioning is disabled for bucket ${bucketName}`);
await bucket.getMetadata();
assert.strictEqual(bucket.metadata.versioning.enabled, false);
});
it('should add label to bucket', async () => {
const output = execSync(
`node addBucketLabel.js ${bucketName} labelone labelonevalue`
);
assert.include(output, `Added label to bucket ${bucketName}`);
const [labels] = await storage.bucket(bucketName).getLabels();
assert.isTrue('labelone' in labels);
});
it('should remove label to bucket', async () => {
const output = execSync(`node removeBucketLabel.js ${bucketName} labelone`);
assert.include(output, `Removed labels from bucket ${bucketName}`);
const [labels] = await storage.bucket(bucketName).getLabels();
assert.isFalse('labelone' in labels);
});
it("should change a bucket's default storage class", async () => {
const output = execSync(
`node changeDefaultStorageClass.js ${bucketName} coldline`
);
assert.include(output, `${bucketName} has been set to coldline`);
const [metadata] = await bucket.getMetadata();
assert.strictEqual(metadata.storageClass, 'COLDLINE');
});
it('should create bucket with storage class and location', async () => {
const output = execSync(
`node createBucketWithStorageClassAndLocation.js ${bucketNameWithClassAndLocation} coldline ASIA`
);
assert.include(
output,
`${bucketNameWithClassAndLocation} created with coldline class in ASIA`
);
const [metadata] = await bucketWithClassAndLocation.getMetadata();
assert.strictEqual(metadata.storageClass, 'COLDLINE');
assert.strictEqual(metadata.location, 'ASIA');
});
it('should delete a bucket', async () => {
const output = execSync(`node deleteBucket.js ${bucketName}`);
assert.match(output, new RegExp(`Bucket ${bucketName} deleted`));
const [exists] = await bucket.exists();
assert.strictEqual(exists, false);
});