Skip to content

Commit 2523dfc

Browse files
author
Ace Nassri
authored
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
1 parent 2373cda commit 2523dfc

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

functions/helloworld/index.js

+23
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,29 @@ exports.helloGCS = (event, callback) => {
9494
};
9595
// [END functions_helloworld_storage]
9696

97+
// [START functions_helloworld_storage_generic]
98+
/**
99+
* Generic background Cloud Function to be triggered by Cloud Storage.
100+
*
101+
* @param {object} event The Cloud Functions event.
102+
* @param {function} callback The callback function.
103+
*/
104+
exports.helloGCSGeneric = (event, callback) => {
105+
const file = event.data;
106+
const context = event.context;
107+
108+
console.log(`Event ${context.eventId}`);
109+
console.log(` Event Type: ${context.eventType}`);
110+
console.log(` Bucket: ${file.bucket}`);
111+
console.log(` File: ${file.name}`);
112+
console.log(` Metageneration: ${file.metageneration}`);
113+
console.log(` Created: ${file.timeCreated}`);
114+
console.log(` Updated: ${file.updated}`);
115+
116+
callback();
117+
};
118+
// [END functions_helloworld_storage_generic]
119+
97120
// [START functions_helloworld_error]
98121
/**
99122
* Background Cloud Function that throws an error.

functions/helloworld/test/index.test.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ test.serial(`helloGCS: should print uploaded message`, async (t) => {
129129

130130
// Check logs
131131
await tools.tryTest(async (assert) => {
132-
const logs = await tools.runAsync(`${baseCmd} logs read helloPubSub --start-time ${startTime}`);
132+
const logs = await tools.runAsync(`${baseCmd} logs read helloGCS --start-time ${startTime}`);
133133
assert(logs.includes(`File ${fileName} uploaded`));
134134
});
135135
});
@@ -143,11 +143,27 @@ test.serial(`helloGCS: should print metadata updated message`, async (t) => {
143143

144144
// Check logs
145145
await tools.tryTest(async (assert) => {
146-
const logs = await tools.runAsync(`${baseCmd} logs read helloPubSub --start-time ${startTime}`);
146+
const logs = await tools.runAsync(`${baseCmd} logs read helloGCS --start-time ${startTime}`);
147147
assert(logs.includes(`File ${fileName} metadata updated`));
148148
});
149149
});
150150

151+
test.serial(`helloGCSGeneric: should print event details`, async (t) => {
152+
t.plan(0);
153+
const startTime = new Date(Date.now()).toISOString();
154+
155+
// Update file metadata
156+
await bucket.setMetadata(fileName, { foo: `baz` });
157+
158+
// Check logs
159+
await tools.tryTest(async (assert) => {
160+
const logs = await tools.runAsync(`${baseCmd} logs read helloGCSGeneric --start-time ${startTime}`);
161+
assert(logs.includes(`Bucket: ${bucketName}`));
162+
assert(logs.includes(`File: ${fileName}`));
163+
assert(logs.includes(`Event type: google.storage.object.metadataUpdate`));
164+
});
165+
});
166+
151167
test.serial(`helloGCS: should print deleted message`, async (t) => {
152168
t.plan(0);
153169
const startTime = new Date(Date.now()).toISOString();
@@ -157,7 +173,7 @@ test.serial(`helloGCS: should print deleted message`, async (t) => {
157173

158174
// Check logs
159175
await tools.tryTest(async (assert) => {
160-
const logs = await tools.runAsync(`${baseCmd} logs read helloPubSub --start-time ${startTime}`);
176+
const logs = await tools.runAsync(`${baseCmd} logs read helloGCS --start-time ${startTime}`);
161177
assert(logs.includes(`File ${fileName} deleted`));
162178
});
163179
});

functions/helloworld/test/updateFunctions.sh

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ ${FUNCTIONS_CMD} deploy helloPubSub --trigger-topic $FUNCTIONS_TOPIC
1111
echo '-----------------------------'
1212
${FUNCTIONS_CMD} deploy helloGCS --trigger-bucket $FUNCTIONS_BUCKET
1313
echo '-----------------------------'
14+
${FUNCTIONS_CMD} deploy helloGCSGeneric --trigger-bucket $FUNCTIONS_BUCKET
15+
echo '-----------------------------'
1416
${FUNCTIONS_CMD} deploy helloError --trigger-topic $FUNCTIONS_TOPIC
1517
echo '-----------------------------'
1618
${FUNCTIONS_CMD} deploy helloError2 --trigger-topic $FUNCTIONS_TOPIC

0 commit comments

Comments
 (0)