From 679efe431910cd2277701f49be9f41319ba59bed Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Fri, 9 Feb 2018 14:55:52 -0800 Subject: [PATCH 1/2] Add generic background function for GCS + fix typo in existing tests --- functions/helloworld/index.js | 22 ++++++++++++++++++++ functions/helloworld/test/index.test.js | 22 +++++++++++++++++--- functions/helloworld/test/updateFunctions.sh | 2 ++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/functions/helloworld/index.js b/functions/helloworld/index.js index b9101d4b5b..f2cc907de3 100644 --- a/functions/helloworld/index.js +++ b/functions/helloworld/index.js @@ -94,6 +94,28 @@ 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; + + console.log(`Event ${event.context.eventId}`); + console.log(` Event Type: ${event.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 From 8f7d33e72184e36cc6cb1c42aa533afcc27b0ac3 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Fri, 9 Feb 2018 15:40:04 -0800 Subject: [PATCH 2/2] Create "context" variable --- functions/helloworld/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/functions/helloworld/index.js b/functions/helloworld/index.js index f2cc907de3..3eda309167 100644 --- a/functions/helloworld/index.js +++ b/functions/helloworld/index.js @@ -103,9 +103,10 @@ exports.helloGCS = (event, callback) => { */ exports.helloGCSGeneric = (event, callback) => { const file = event.data; + const context = event.context; - console.log(`Event ${event.context.eventId}`); - console.log(` Event Type: ${event.context.eventType}`); + 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}`);