diff --git a/functions/scheduleinstance/index.js b/functions/scheduleinstance/index.js index 3e732237f9..961aede759 100644 --- a/functions/scheduleinstance/index.js +++ b/functions/scheduleinstance/index.js @@ -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 => { @@ -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 => { diff --git a/functions/scheduleinstance/package.json b/functions/scheduleinstance/package.json index 0e95cdf863..10bee75a2b 100644 --- a/functions/scheduleinstance/package.json +++ b/functions/scheduleinstance/package.json @@ -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.", diff --git a/functions/scheduleinstance/test/index.test.js b/functions/scheduleinstance/test/index.test.js index 82afd576ba..eb324122a0 100644 --- a/functions/scheduleinstance/test/index.test.js +++ b/functions/scheduleinstance/test/index.test.js @@ -38,15 +38,14 @@ function getSample() { function getMocks() { const event = { - data: { - data: {}, - }, + data: {}, }; const callback = sinon.spy(); return { event: event, + context: {}, callback: callback, }; } @@ -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. @@ -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], @@ -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], @@ -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], @@ -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. @@ -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], @@ -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], @@ -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],