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

Update function signatures for NodeJS 8 (#1318) #1335

Merged
merged 11 commits into from
Jun 4, 2019
10 changes: 4 additions & 6 deletions functions/scheduleinstance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ const compute = new Compute();
* @param {!object} callback Cloud Function PubSub callback indicating
* completion.
*/
exports.startInstancePubSub = (event, callback) => {
exports.startInstancePubSub = (event, context, callback) => {
try {
const pubsubMessage = event.data;
const payload = _validatePayload(
JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString())
JSON.parse(Buffer.from(event.data, 'base64').toString())
);
const options = {filter: `labels.${payload.label}`};
compute.getVMs(options).then(vms => {
Expand Down Expand Up @@ -86,11 +85,10 @@ exports.startInstancePubSub = (event, callback) => {
* @param {!object} event Cloud Function PubSub message event.
* @param {!object} callback Cloud Function PubSub callback indicating completion.
*/
exports.stopInstancePubSub = (event, callback) => {
exports.stopInstancePubSub = (event, context, callback) => {
try {
const pubsubMessage = event.data;
const payload = _validatePayload(
JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString())
JSON.parse(Buffer.from(event.data, 'base64').toString())
);
const options = {filter: `labels.${payload.label}`};
compute.getVMs(options).then(vms => {
Expand Down
2 changes: 1 addition & 1 deletion functions/scheduleinstance/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cloud-functions-schedule-instance",
"version": "0.0.2",
"version": "0.1.0",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
Expand Down
93 changes: 32 additions & 61 deletions functions/scheduleinstance/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ function getSample() {

function getMocks() {
const event = {
data: {
data: {},
},
data: {},
};

const callback = sinon.spy();

return {
event: event,
context: {},
callback: callback,
};
}
Expand All @@ -56,28 +55,16 @@ afterEach(tools.restoreConsole);

/** Tests for startInstancePubSub */

it('startInstancePubSub: should accept JSON-formatted event payload with instance', async () => {
const mocks = getMocks();
const sample = getSample();
const pubsubData = {zone: 'test-zone', instance: 'test-instance'};
mocks.event.data.data = Buffer.from(JSON.stringify(pubsubData)).toString(
'base64'
);
sample.program.startInstancePubSub(mocks.event, mocks.callback);

const data = await sample.mocks.requestPromise();
// The request was successfully sent.
assert.strictEqual(data, 'request sent');
});

it('startInstancePubSub: should accept JSON-formatted event payload with label', async () => {
const mocks = getMocks();
const sample = getSample();
const pubsubData = {zone: 'test-zone', label: 'testkey=value'};
mocks.event.data.data = Buffer.from(JSON.stringify(pubsubData)).toString(
'base64'
mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64');
sample.program.startInstancePubSub(
mocks.event,
mocks.context,
mocks.callback
);
sample.program.startInstancePubSub(mocks.event, mocks.callback);

const data = await sample.mocks.requestPromise();
// The request was successfully sent.
Expand All @@ -87,11 +74,13 @@ it('startInstancePubSub: should accept JSON-formatted event payload with label',
it(`startInstancePubSub: should fail with missing 'zone' attribute`, () => {
const mocks = getMocks();
const sample = getSample();
const pubsubData = {instance: 'test-instance'};
mocks.event.data.data = Buffer.from(JSON.stringify(pubsubData)).toString(
'base64'
const pubsubData = {label: 'testkey=value'};
mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64');
sample.program.startInstancePubSub(
mocks.event,
mocks.context,
mocks.callback
);
sample.program.startInstancePubSub(mocks.event, mocks.callback);

assert.deepStrictEqual(
mocks.callback.firstCall.args[0],
Expand All @@ -103,10 +92,12 @@ it(`startInstancePubSub: should fail with missing 'label' attribute`, () => {
const mocks = getMocks();
const sample = getSample();
const pubsubData = {zone: 'test-zone'};
mocks.event.data.data = Buffer.from(JSON.stringify(pubsubData)).toString(
'base64'
mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64');
sample.program.startInstancePubSub(
mocks.event,
mocks.context,
mocks.callback
);
sample.program.startInstancePubSub(mocks.event, mocks.callback);

assert.deepStrictEqual(
mocks.callback.firstCall.args[0],
Expand All @@ -118,10 +109,12 @@ it('startInstancePubSub: should fail with empty event payload', () => {
const mocks = getMocks();
const sample = getSample();
const pubsubData = {};
mocks.event.data.data = Buffer.from(JSON.stringify(pubsubData)).toString(
'base64'
mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64');
sample.program.startInstancePubSub(
mocks.event,
mocks.context,
mocks.callback
);
sample.program.startInstancePubSub(mocks.event, mocks.callback);

assert.deepStrictEqual(
mocks.callback.firstCall.args[0],
Expand All @@ -131,28 +124,12 @@ it('startInstancePubSub: should fail with empty event payload', () => {

/** Tests for stopInstancePubSub */

it('stopInstancePubSub: should accept JSON-formatted event payload with instance', async () => {
const mocks = getMocks();
const sample = getSample();
const pubsubData = {zone: 'test-zone', instance: 'test-instance'};
mocks.event.data.data = Buffer.from(JSON.stringify(pubsubData)).toString(
'base64'
);
sample.program.stopInstancePubSub(mocks.event, mocks.callback);

const data = await sample.mocks.requestPromise();
// The request was successfully sent.
assert.strictEqual(data, 'request sent');
});

it('startInstancePubSub: should accept JSON-formatted event payload with label', async () => {
const mocks = getMocks();
const sample = getSample();
const pubsubData = {zone: 'test-zone', label: 'testkey=value'};
mocks.event.data.data = Buffer.from(JSON.stringify(pubsubData)).toString(
'base64'
);
sample.program.stopInstancePubSub(mocks.event, mocks.callback);
mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64');
sample.program.stopInstancePubSub(mocks.event, mocks.context, mocks.callback);

const data = await sample.mocks.requestPromise();
// The request was successfully sent.
Expand All @@ -162,11 +139,9 @@ it('startInstancePubSub: should accept JSON-formatted event payload with label',
it(`stopInstancePubSub: should fail with missing 'zone' attribute`, () => {
const mocks = getMocks();
const sample = getSample();
const pubsubData = {instance: 'test-instance'};
mocks.event.data.data = Buffer.from(JSON.stringify(pubsubData)).toString(
'base64'
);
sample.program.stopInstancePubSub(mocks.event, mocks.callback);
const pubsubData = {label: 'testkey=value'};
mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64');
sample.program.stopInstancePubSub(mocks.event, mocks.context, mocks.callback);

assert.deepStrictEqual(
mocks.callback.firstCall.args[0],
Expand All @@ -178,10 +153,8 @@ it(`stopInstancePubSub: should fail with missing 'label' attribute`, () => {
const mocks = getMocks();
const sample = getSample();
const pubsubData = {zone: 'test-zone'};
mocks.event.data.data = Buffer.from(JSON.stringify(pubsubData)).toString(
'base64'
);
sample.program.stopInstancePubSub(mocks.event, mocks.callback);
mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64');
sample.program.stopInstancePubSub(mocks.event, mocks.context, mocks.callback);

assert.deepStrictEqual(
mocks.callback.firstCall.args[0],
Expand All @@ -193,10 +166,8 @@ it('stopInstancePubSub: should fail with empty event payload', () => {
const mocks = getMocks();
const sample = getSample();
const pubsubData = {};
mocks.event.data.data = Buffer.from(JSON.stringify(pubsubData)).toString(
'base64'
);
sample.program.stopInstancePubSub(mocks.event, mocks.callback);
mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64');
sample.program.stopInstancePubSub(mocks.event, mocks.context, mocks.callback);

assert.deepStrictEqual(
mocks.callback.firstCall.args[0],
Expand Down