|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +const fs = require('fs'); |
| 18 | +const path = require('path'); |
| 19 | +const {Storage} = require('@google-cloud/storage'); |
| 20 | +const {assert} = require('chai'); |
| 21 | +const {before, after, it, describe} = require('mocha'); |
| 22 | +const cp = require('child_process'); |
| 23 | +const uuid = require('uuid'); |
| 24 | +const {promisify} = require('util'); |
| 25 | + |
| 26 | +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
| 27 | + |
| 28 | +const storage = new Storage(); |
| 29 | +const cwd = path.join(__dirname, '..'); |
| 30 | +const bucketName = generateName(); |
| 31 | +const bucket = storage.bucket(bucketName); |
| 32 | +const fileName = 'test.txt'; |
| 33 | +const filePath = path.join(cwd, 'resources', fileName); |
| 34 | +const downloadFilePath = path.join(cwd, 'downloaded.txt'); |
| 35 | + |
| 36 | +describe('file', () => { |
| 37 | + before(async () => { |
| 38 | + await bucket.create(); |
| 39 | + }); |
| 40 | + |
| 41 | + after(async () => { |
| 42 | + await promisify(fs.unlink)(downloadFilePath).catch(console.error); |
| 43 | + // Try deleting all files twice, just to make sure |
| 44 | + await bucket.deleteFiles({force: true}).catch(console.error); |
| 45 | + await bucket.deleteFiles({force: true}).catch(console.error); |
| 46 | + await bucket.delete().catch(console.error); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('Object Contexts', () => { |
| 50 | + const contextFile = bucket.file(fileName); |
| 51 | + |
| 52 | + beforeEach(async () => { |
| 53 | + await bucket.upload(filePath, {destination: fileName}); |
| 54 | + |
| 55 | + await contextFile.setMetadata({ |
| 56 | + contexts: { |
| 57 | + custom: { |
| 58 | + 'team-owner': {value: 'storage-team'}, |
| 59 | + priority: {value: 'high'}, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should set object contexts', async () => { |
| 66 | + const output = execSync( |
| 67 | + `node setObjectContexts.js ${bucketName} ${fileName}` |
| 68 | + ); |
| 69 | + // Verify Initial Creation |
| 70 | + assert.include(output, `Updated Object Contexts for ${fileName}:`); |
| 71 | + assert.include(output, '"team-owner":'); |
| 72 | + assert.include(output, '"value": "storage-team"'); |
| 73 | + assert.include(output, '"priority":'); |
| 74 | + assert.include(output, '"value": "high"'); |
| 75 | + assert.include(output, '"createTime":'); |
| 76 | + assert.include(output, '"updateTime":'); |
| 77 | + |
| 78 | + const [metadata] = await contextFile.getMetadata(); |
| 79 | + const customContexts = metadata.contexts?.custom || {}; |
| 80 | + |
| 81 | + assert.strictEqual(customContexts['priority']?.value, 'high'); |
| 82 | + assert.strictEqual(customContexts['team-owner']?.value, 'storage-team'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should get object contexts', async () => { |
| 86 | + const output = execSync( |
| 87 | + `node getObjectContexts.js ${bucketName} ${fileName}` |
| 88 | + ); |
| 89 | + assert.include(output, `Object Contexts for ${fileName}:`); |
| 90 | + assert.include(output, 'Key: priority'); |
| 91 | + assert.include(output, 'Value: high'); |
| 92 | + assert.include(output, 'Key: team-owner'); |
| 93 | + assert.include(output, 'Value: storage-team'); |
| 94 | + |
| 95 | + const [metadata] = await contextFile.getMetadata(); |
| 96 | + const customContexts = metadata.contexts?.custom || {}; |
| 97 | + |
| 98 | + assert.strictEqual(customContexts['priority'].value, 'high'); |
| 99 | + assert.strictEqual(customContexts['team-owner'].value, 'storage-team'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should list objects with context filters', async () => { |
| 103 | + const noContextFileName = `no-context-${fileName}`; |
| 104 | + await bucket.upload(filePath, {destination: noContextFileName}); |
| 105 | + // Ensure it has no contexts |
| 106 | + await bucket |
| 107 | + .file(noContextFileName) |
| 108 | + .setMetadata({contexts: {custom: null}}); |
| 109 | + |
| 110 | + const output = execSync(`node listObjectContexts.js ${bucketName}`); |
| 111 | + |
| 112 | + // Testing Existence of Value Pair (contexts."key"="val") |
| 113 | + assert.include( |
| 114 | + output, |
| 115 | + 'Files matching filter [contexts."priority"="high"]' |
| 116 | + ); |
| 117 | + assert.include(output, ` - ${fileName}`); |
| 118 | + |
| 119 | + // Testing Existence of Key (contexts."key":*) |
| 120 | + assert.include(output, 'Files with the "team-owner" context key'); |
| 121 | + assert.include(output, ` - ${fileName}`); |
| 122 | + |
| 123 | + // Testing Absence of Value Pair (-contexts."key"="val") |
| 124 | + assert.include( |
| 125 | + output, |
| 126 | + 'Files matching absence of value pair [-contexts."priority"="high"]' |
| 127 | + ); |
| 128 | + assert.include(output, ` - ${noContextFileName}`); |
| 129 | + |
| 130 | + // Testing Absence of Key (-contexts."key":*) |
| 131 | + assert.include( |
| 132 | + output, |
| 133 | + 'Files matching absence of key regardless of value [-contexts."team-owner":*]' |
| 134 | + ); |
| 135 | + assert.include(output, ` - ${noContextFileName}`); |
| 136 | + |
| 137 | + const [files] = await bucket.getFiles(); |
| 138 | + const targetFile = files.find(f => f.name === fileName); |
| 139 | + |
| 140 | + assert.exists(targetFile); |
| 141 | + const [metadata] = await targetFile.getMetadata(); |
| 142 | + |
| 143 | + // Verify the state that the list filter is supposed to find |
| 144 | + assert.strictEqual(metadata.contexts?.custom?.priority?.value, 'high'); |
| 145 | + |
| 146 | + await bucket.file(noContextFileName).delete(); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
| 150 | + |
| 151 | +function generateName() { |
| 152 | + return `nodejs-storage-samples-${uuid.v4()}`; |
| 153 | +} |
0 commit comments