|
| 1 | +/** |
| 2 | + * Copyright 2018, Google, Inc. |
| 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 | + |
| 16 | +// [START functions_storage_system_test] |
| 17 | +const Storage = require(`@google-cloud/storage`); |
| 18 | +const storage = Storage(); |
| 19 | +const uuid = require(`uuid`); |
| 20 | +const test = require(`ava`); |
| 21 | +const path = require(`path`); |
| 22 | +const childProcess = require(`child_process`); |
| 23 | +const localFileName = `test.txt`; |
| 24 | + |
| 25 | +// Use unique GCS filename to avoid conflicts between concurrent test runs |
| 26 | +const gcsFileName = `test-${uuid.v4()}.txt`; |
| 27 | + |
| 28 | +const bucketName = process.env.BUCKET_NAME; |
| 29 | +const bucket = storage.bucket(bucketName); |
| 30 | +const baseCmd = `gcloud beta functions`; |
| 31 | + |
| 32 | +test.serial(`helloGCS: should print uploaded message`, async (t) => { |
| 33 | + t.plan(1); |
| 34 | + const startTime = new Date(Date.now()).toISOString(); |
| 35 | + |
| 36 | + // Upload file |
| 37 | + const filepath = path.join(__dirname, localFileName); |
| 38 | + await bucket.upload(filepath, { |
| 39 | + destination: gcsFileName |
| 40 | + }); |
| 41 | + |
| 42 | + // Wait for consistency |
| 43 | + await new Promise(resolve => setTimeout(resolve, 15000)); |
| 44 | + |
| 45 | + // Check logs |
| 46 | + const logs = childProcess.execSync(`${baseCmd} logs read helloGCS --start-time ${startTime}`).toString(); |
| 47 | + t.true(logs.includes(`File ${gcsFileName} uploaded`)); |
| 48 | +}); |
| 49 | + |
| 50 | +test.serial(`helloGCS: should print metadata updated message`, async (t) => { |
| 51 | + t.plan(1); |
| 52 | + const startTime = new Date(Date.now()).toISOString(); |
| 53 | + |
| 54 | + // Update file metadata |
| 55 | + const file = bucket.file(gcsFileName); |
| 56 | + await file.setMetadata(gcsFileName, { foo: `bar` }); |
| 57 | + |
| 58 | + // Wait for consistency |
| 59 | + await new Promise(resolve => setTimeout(resolve, 15000)); |
| 60 | + |
| 61 | + // Check logs |
| 62 | + const logs = childProcess.execSync(`${baseCmd} logs read helloGCS --start-time ${startTime}`).toString(); |
| 63 | + t.true(logs.includes(`File ${gcsFileName} metadata updated`)); |
| 64 | +}); |
| 65 | + |
| 66 | +test.serial(`helloGCS: should print deleted message`, async (t) => { |
| 67 | + t.plan(1); |
| 68 | + const startTime = new Date(Date.now()).toISOString(); |
| 69 | + |
| 70 | + // Delete file |
| 71 | + bucket.deleteFiles(); |
| 72 | + |
| 73 | + // Wait for consistency |
| 74 | + await new Promise(resolve => setTimeout(resolve, 15000)); |
| 75 | + |
| 76 | + // Check logs |
| 77 | + const logs = childProcess.execSync(`${baseCmd} logs read helloGCS --start-time ${startTime}`).toString(); |
| 78 | + t.true(logs.includes(`File ${gcsFileName} deleted`)); |
| 79 | +}); |
| 80 | +// [START functions_storage_system_test] |
0 commit comments