-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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 "shim" (mini-emulator) for GCF functions #623
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c2ca09a
Add shims + tests
e697c7a
Add missing region tags
8b21614
Merge branch 'master' into testing-snippets
fhinkel 1423e65
Merge branch 'master' into testing-snippets
JustinBeckwith 8da7428
Merge branch 'master' into testing-snippets
JustinBeckwith 21f26c5
Tweak shim region tags
f9d70d7
Remove extraneous line
569539e
Merge branch 'master' into testing-snippets
f8276a6
Merge branch 'master' into testing-snippets
JustinBeckwith 3f3bd74
Merge branch 'master' into testing-snippets
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/** | ||
* Copyright 2018, Google, Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const httpShim = (PORT) => { | ||
// [START functions_testing_shim_http] | ||
// Import dependencies | ||
const gcfCode = require('./index.js'); | ||
const express = require('express'); | ||
|
||
// TODO(developer): specify the port to use | ||
// const PORT = 3000; | ||
|
||
// Start local HTTP server | ||
const app = express(); | ||
const server = require(`http`).createServer(app); | ||
server.on('connection', socket => socket.unref()); | ||
server.listen(PORT); | ||
|
||
// Register HTTP handlers | ||
Object.keys(gcfCode).forEach(gcfFn => { | ||
// Handle a single HTTP request | ||
const handler = (req, res) => { | ||
gcfCode[gcfFn](req, res); | ||
server.close(); | ||
}; | ||
|
||
app.get(`/${gcfFn}`, handler); | ||
app.post(`/${gcfFn}`, handler); | ||
}); | ||
// [END functions_testing_shim_http] | ||
}; | ||
|
||
const pubsubShim = (gcfFn, topicName, subscriptionName) => { | ||
// [START functions_testing_shim_pubsub] | ||
// Import dependencies | ||
const Pubsub = require('@google-cloud/pubsub'); | ||
const pubsub = Pubsub(); | ||
|
||
// TODO(developer): specify a function to test | ||
// const gcfCode = require('./index.js'); | ||
// const gcfFn = gcfCode.YOUR_FUNCTION; | ||
|
||
// TODO(developer): specify an existing topic and subscription to use | ||
// const topicName = process.env.TOPIC || 'YOUR_TOPIC'; | ||
// const subscriptionName = process.env.SUBSCRIPTION || 'YOUR_SUBSCRIPTION'; | ||
|
||
// Subscribe to Pub/Sub topic | ||
const subscription = pubsub.topic(topicName).subscription(subscriptionName); | ||
|
||
// Handle a single Pub/Sub message | ||
const messageHandler = (msg) => { | ||
gcfFn({ data: msg }, () => { | ||
msg.ack(); | ||
subscription.removeListener(`message`, messageHandler); | ||
}); | ||
}; | ||
subscription.on(`message`, messageHandler); | ||
// [END functions_testing_shim_pubsub] | ||
}; | ||
|
||
const storageShim = (gcfFn, bucketName, topicName, subscriptionName) => { | ||
// [START functions_testing_shim_storage] | ||
// Import dependencies | ||
const Pubsub = require('@google-cloud/pubsub'); | ||
const Storage = require(`@google-cloud/storage`); | ||
const pubsub = Pubsub(); | ||
const storage = Storage(); | ||
|
||
// TODO(developer): specify a function to test | ||
// const gcfCode = require('./index.js'); | ||
// const gcfFn = gcfCode.YOUR_FUNCTION; | ||
|
||
// TODO(developer): specify a Cloud Storage bucket to monitor | ||
// const bucketName = 'YOUR_GCS_BUCKET' | ||
|
||
// TODO(developer): specify an existing topic and subscription to use | ||
// const topicName = process.env.TOPIC || 'YOUR_TOPIC'; | ||
// const subscriptionName = process.env.SUBSCRIPTION || 'YOUR_SUBSCRIPTION'; | ||
|
||
// Create notification on target bucket | ||
// Further info: https://cloud.google.com/storage/docs/reporting-changes | ||
const bucket = storage.bucket(bucketName); | ||
return bucket.createNotification(topicName) | ||
.then(data => data[0]) | ||
.then((notification) => new Promise(resolve => { | ||
// Subscribe to Pub/Sub topic | ||
const subscription = pubsub | ||
.topic(topicName) | ||
.subscription(subscriptionName); | ||
|
||
// Handle a single Pub/Sub message | ||
const messageHandler = (msg) => { | ||
const data = JSON.parse(Buffer.from(msg.data, 'base64').toString()); | ||
gcfFn({ data: data }, () => { | ||
msg.ack(); | ||
subscription.removeListener(`message`, messageHandler); | ||
resolve(notification); | ||
}); | ||
}; | ||
subscription.on(`message`, messageHandler); | ||
})) | ||
.then(notification => notification.delete()); // Delete notification | ||
// [END functions_testing_shim_storage] | ||
}; | ||
|
||
const gcfCodeGlobal = require('./index.js'); | ||
require(`yargs`) // eslint-disable-line | ||
.demandCommand(1) | ||
.command( | ||
'http <port>', | ||
'HTTP-triggered-function shim', | ||
{}, | ||
opts => httpShim(opts.port) | ||
) | ||
.command( | ||
'pubsub <functionName> <topic> <subscription>', | ||
'PubSub-triggered-function shim', | ||
{}, | ||
opts => pubsubShim( | ||
gcfCodeGlobal[opts.functionName], | ||
opts.topic, | ||
opts.subscription | ||
) | ||
) | ||
.command( | ||
'storage <functionName> <bucket> <topic> <subscription>', | ||
'Storage-triggered-function shim', | ||
{}, | ||
opts => storageShim( | ||
gcfCodeGlobal[opts.functionName], | ||
opts.bucket, | ||
opts.topic, | ||
opts.subscription | ||
) | ||
) | ||
.wrap(120) | ||
.help() | ||
.strict() | ||
.argv; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/** | ||
* Copyright 2018, Google, Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const test = require(`ava`); | ||
const Supertest = require(`supertest`); | ||
const tools = require(`@google-cloud/nodejs-repo-tools`); | ||
const Pubsub = require(`@google-cloud/pubsub`); | ||
const pubsub = Pubsub(); | ||
const Storage = require(`@google-cloud/storage`); | ||
const storage = Storage(); | ||
const uuid = require(`uuid`); | ||
const path = require(`path`); | ||
|
||
const PORT = process.env.PORT || 3000; | ||
const supertest = Supertest(`http://localhost:${PORT}`); | ||
const topicName = `gcf-shim-topic-${uuid.v4()}`; | ||
const subscriptionName = `gcf-shim-sub-${uuid.v4()}`; | ||
const bucketName = `gcf-shim-bucket-${uuid.v4()}`; | ||
|
||
const cmd = `node shim.js`; | ||
const cwd = path.join(__dirname, `..`); | ||
|
||
const bucket = storage.bucket(bucketName); | ||
let topic; | ||
let subscription; | ||
|
||
// Helper function | ||
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)); | ||
|
||
// Create test resources | ||
test.before(t => { | ||
return bucket.create() | ||
.then(data => { | ||
return pubsub.createTopic(topicName); | ||
}) | ||
.then(data => { | ||
topic = data[0]; | ||
return topic.createSubscription(subscriptionName); | ||
}) | ||
.then(data => { | ||
subscription = data[0]; | ||
return delay(500); | ||
}) | ||
.catch(console.error); | ||
}); | ||
|
||
// Tear down test resources | ||
test.after.always(t => { | ||
return bucket.deleteFiles({ force: true }) | ||
.then(subscription.delete()) | ||
.then(topic.delete()) | ||
.then(() => { | ||
try { | ||
return bucket.delete(); | ||
} catch (ex) { | ||
return bucket.delete(); | ||
} | ||
}) | ||
.catch(console.error); | ||
}); | ||
|
||
test(`shim: should handle HTTP`, async t => { | ||
const shim = tools.runAsync(`${cmd} http ${PORT}`, cwd); | ||
await delay(1000) | ||
.then(() => { | ||
// Send HTTP request | ||
return supertest | ||
.get(`/helloGET`) | ||
.expect(200) | ||
.expect((response) => { | ||
t.is(response.text, 'Hello World!'); | ||
}); | ||
}) | ||
.then(() => { return shim; }); // Stop shim | ||
}); | ||
|
||
test(`shim: should handle PubSub`, async t => { | ||
const name = uuid.v4(); | ||
const publisher = topic.publisher(); | ||
const shim = tools.runAsync(`${cmd} pubsub helloPubSub "${topicName}" "${subscriptionName}"`, cwd); | ||
|
||
// Publish to topic | ||
await publisher.publish(Buffer.from(name)) | ||
.then(delay(1000)) | ||
.then(() => { return shim; }) | ||
.then(output => { | ||
t.true(output.includes(`Hello, ${name}!`)); | ||
}); | ||
}); | ||
|
||
test(`shim: should handle GCS`, async t => { | ||
const shim = tools.runAsync(`${cmd} storage helloGCS "${bucketName}" "${topicName}" "${subscriptionName}"`, cwd); | ||
|
||
// Upload file to bucket | ||
await delay(10000) | ||
.then(() => { return bucket.upload(path.join(cwd, `test/test.txt`)); }) | ||
.then(() => { return shim; }) | ||
.then(output => { | ||
t.true(output.includes(`File test.txt uploaded.`)); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks you! 🙏