From 2523dfc0a131a3036252cd79908b307cb32473d0 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Fri, 9 Feb 2018 15:40:42 -0800 Subject: [PATCH] Add generic background function for GCS + fix typo in existing tests (#565) * Add generic background function for GCS + fix typo in existing tests * Create "context" variable --- functions/helloworld/index.js | 23 ++++++++++++++++++++ functions/helloworld/test/index.test.js | 22 ++++++++++++++++--- functions/helloworld/test/updateFunctions.sh | 2 ++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/functions/helloworld/index.js b/functions/helloworld/index.js index b9101d4b5b..3eda309167 100644 --- a/functions/helloworld/index.js +++ b/functions/helloworld/index.js @@ -94,6 +94,29 @@ exports.helloGCS = (event, callback) => { }; // [END functions_helloworld_storage] +// [START functions_helloworld_storage_generic] +/** + * Generic background Cloud Function to be triggered by Cloud Storage. + * + * @param {object} event The Cloud Functions event. + * @param {function} callback The callback function. + */ +exports.helloGCSGeneric = (event, callback) => { + const file = event.data; + const context = event.context; + + console.log(`Event ${context.eventId}`); + console.log(` Event Type: ${context.eventType}`); + console.log(` Bucket: ${file.bucket}`); + console.log(` File: ${file.name}`); + console.log(` Metageneration: ${file.metageneration}`); + console.log(` Created: ${file.timeCreated}`); + console.log(` Updated: ${file.updated}`); + + callback(); +}; +// [END functions_helloworld_storage_generic] + // [START functions_helloworld_error] /** * Background Cloud Function that throws an error. diff --git a/functions/helloworld/test/index.test.js b/functions/helloworld/test/index.test.js index 5033955c89..360d5945d4 100644 --- a/functions/helloworld/test/index.test.js +++ b/functions/helloworld/test/index.test.js @@ -129,7 +129,7 @@ test.serial(`helloGCS: should print uploaded message`, async (t) => { // Check logs await tools.tryTest(async (assert) => { - const logs = await tools.runAsync(`${baseCmd} logs read helloPubSub --start-time ${startTime}`); + const logs = await tools.runAsync(`${baseCmd} logs read helloGCS --start-time ${startTime}`); assert(logs.includes(`File ${fileName} uploaded`)); }); }); @@ -143,11 +143,27 @@ test.serial(`helloGCS: should print metadata updated message`, async (t) => { // Check logs await tools.tryTest(async (assert) => { - const logs = await tools.runAsync(`${baseCmd} logs read helloPubSub --start-time ${startTime}`); + const logs = await tools.runAsync(`${baseCmd} logs read helloGCS --start-time ${startTime}`); assert(logs.includes(`File ${fileName} metadata updated`)); }); }); +test.serial(`helloGCSGeneric: should print event details`, async (t) => { + t.plan(0); + const startTime = new Date(Date.now()).toISOString(); + + // Update file metadata + await bucket.setMetadata(fileName, { foo: `baz` }); + + // Check logs + await tools.tryTest(async (assert) => { + const logs = await tools.runAsync(`${baseCmd} logs read helloGCSGeneric --start-time ${startTime}`); + assert(logs.includes(`Bucket: ${bucketName}`)); + assert(logs.includes(`File: ${fileName}`)); + assert(logs.includes(`Event type: google.storage.object.metadataUpdate`)); + }); +}); + test.serial(`helloGCS: should print deleted message`, async (t) => { t.plan(0); const startTime = new Date(Date.now()).toISOString(); @@ -157,7 +173,7 @@ test.serial(`helloGCS: should print deleted message`, async (t) => { // Check logs await tools.tryTest(async (assert) => { - const logs = await tools.runAsync(`${baseCmd} logs read helloPubSub --start-time ${startTime}`); + const logs = await tools.runAsync(`${baseCmd} logs read helloGCS --start-time ${startTime}`); assert(logs.includes(`File ${fileName} deleted`)); }); }); diff --git a/functions/helloworld/test/updateFunctions.sh b/functions/helloworld/test/updateFunctions.sh index cae716cd60..594005811a 100644 --- a/functions/helloworld/test/updateFunctions.sh +++ b/functions/helloworld/test/updateFunctions.sh @@ -11,6 +11,8 @@ ${FUNCTIONS_CMD} deploy helloPubSub --trigger-topic $FUNCTIONS_TOPIC echo '-----------------------------' ${FUNCTIONS_CMD} deploy helloGCS --trigger-bucket $FUNCTIONS_BUCKET echo '-----------------------------' +${FUNCTIONS_CMD} deploy helloGCSGeneric --trigger-bucket $FUNCTIONS_BUCKET +echo '-----------------------------' ${FUNCTIONS_CMD} deploy helloError --trigger-topic $FUNCTIONS_TOPIC echo '-----------------------------' ${FUNCTIONS_CMD} deploy helloError2 --trigger-topic $FUNCTIONS_TOPIC