Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions functions/helloworld/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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