Skip to content

Commit

Permalink
Add generic background function for GCS + fix typo in existing tests (#…
Browse files Browse the repository at this point in the history
…565)

* Add generic background function for GCS + fix typo in existing tests

* Create "context" variable
  • Loading branch information
Ace Nassri authored Feb 9, 2018
1 parent 2373cda commit 2523dfc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
23 changes: 23 additions & 0 deletions functions/helloworld/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 19 additions & 3 deletions functions/helloworld/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`));
});
});
Expand All @@ -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();
Expand All @@ -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`));
});
});
Expand Down
2 changes: 2 additions & 0 deletions functions/helloworld/test/updateFunctions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2523dfc

Please sign in to comment.