From 19cc802e1671ed7ef8d693d52eeebf63d29de256 Mon Sep 17 00:00:00 2001 From: Jani Patokallio Date: Wed, 3 Apr 2019 16:25:06 +1100 Subject: [PATCH 1/8] Add ability to stop/start multiple instances by label --- functions/scheduleinstance/index.js | 120 ++++++++++++------ functions/scheduleinstance/package.json | 2 +- functions/scheduleinstance/test/index.test.js | 66 +++++++++- 3 files changed, 141 insertions(+), 47 deletions(-) diff --git a/functions/scheduleinstance/index.js b/functions/scheduleinstance/index.js index 462ce87605..89973e3672 100644 --- a/functions/scheduleinstance/index.js +++ b/functions/scheduleinstance/index.js @@ -25,8 +25,11 @@ const compute = new Compute(); * * Expects a PubSub message with JSON-formatted event data containing the * following attributes: - * zone - the GCP zone the instance is located in. - * instance - the name of the instance. + * zone - the GCP zone the instances are located in. + * instance - the name of a single instance. + * label - the label of instances to start. + * + * Exactly one of instance or label must be specified. * * @param {!object} event Cloud Function PubSub message event. * @param {!object} callback Cloud Function PubSub callback indicating completion. @@ -37,25 +40,39 @@ exports.startInstancePubSub = (event, callback) => { const payload = _validatePayload( JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString()) ); - compute - .zone(payload.zone) - .vm(payload.instance) - .start() - .then(data => { - // Operation pending. - const operation = data[0]; - return operation.promise(); - }) - .then(() => { - // Operation complete. Instance successfully started. - const message = 'Successfully started instance ' + payload.instance; - console.log(message); - callback(null, message); - }) - .catch(err => { - console.log(err); - callback(err); + if(payload.instance) { + var filter = 'name eq ' + payload.instance + } else if(payload.label) { + var filter = 'labels.' + payload.label + } + var options = { + filter: filter + } + compute.getVMs(options).then(function(vms) { + vms[0].forEach(function(instance) { + if (payload.zone == instance.zone.id) { + compute + .zone(payload.zone) + .vm(instance.name) + .start() + .then(data => { + // Operation pending. + const operation = data[0]; + return operation.promise(); + }) + .then(() => { + // Operation complete. Instance successfully started. + const message = 'Successfully started instance ' + instance.name; + console.log(message); + callback(null, message); + }) + .catch(err => { + console.log(err); + callback(err); + }); + } }); + }); } catch (err) { console.log(err); callback(err); @@ -69,8 +86,11 @@ exports.startInstancePubSub = (event, callback) => { * * Expects a PubSub message with JSON-formatted event data containing the * following attributes: - * zone - the GCP zone the instance is located in. - * instance - the name of the instance. + * zone - the GCP zone the instances are located in. + * instance - the name of a single instance. + * label - the label of instances to start. + * + * Exactly one of instance or label must be specified. * * @param {!object} event Cloud Function PubSub message event. * @param {!object} callback Cloud Function PubSub callback indicating completion. @@ -81,25 +101,39 @@ exports.stopInstancePubSub = (event, callback) => { const payload = _validatePayload( JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString()) ); - compute - .zone(payload.zone) - .vm(payload.instance) - .stop() - .then(data => { - // Operation pending. - const operation = data[0]; - return operation.promise(); - }) - .then(() => { - // Operation complete. Instance successfully stopped. - const message = 'Successfully stopped instance ' + payload.instance; - console.log(message); - callback(null, message); - }) - .catch(err => { - console.log(err); - callback(err); + if(payload.instance) { + var filter = 'name eq ' + payload.instance + } else if(payload.label) { + var filter = 'labels.' + payload.label + } + var options = { + filter: filter + } + compute.getVMs(options).then(function(vms) { + vms[0].forEach(function(instance) { + if (payload.zone == instance.zone.id) { + compute + .zone(payload.zone) + .vm(instance.name) + .stop() + .then(data => { + // Operation pending. + const operation = data[0]; + return operation.promise(); + }) + .then(() => { + // Operation complete. Instance successfully stopped. + const message = 'Successfully stopped instance ' + instance.name; + console.log(message); + callback(null, message); + }) + .catch(err => { + console.log(err); + callback(err); + }); + } }); + }); } catch (err) { console.log(err); callback(err); @@ -116,8 +150,10 @@ exports.stopInstancePubSub = (event, callback) => { function _validatePayload(payload) { if (!payload.zone) { throw new Error(`Attribute 'zone' missing from payload`); - } else if (!payload.instance) { - throw new Error(`Attribute 'instance' missing from payload`); + } else if (!payload.instance && !payload.label) { + throw new Error(`Attribute 'instance' or 'label' must be specified in payload`); + } else if (payload.instance && payload.label) { + throw new Error(`Only one of attributes 'instance' and 'label' can be specified in payload`); } return payload; } diff --git a/functions/scheduleinstance/package.json b/functions/scheduleinstance/package.json index 0ad41bda3a..a8755b9286 100644 --- a/functions/scheduleinstance/package.json +++ b/functions/scheduleinstance/package.json @@ -1,6 +1,6 @@ { "name": "cloud-functions-schedule-instance", - "version": "0.0.1", + "version": "0.0.2", "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 a556288f59..309390367e 100644 --- a/functions/scheduleinstance/test/index.test.js +++ b/functions/scheduleinstance/test/index.test.js @@ -56,7 +56,7 @@ afterEach(tools.restoreConsole); /** Tests for startInstancePubSub */ -it('startInstancePubSub: should accept JSON-formatted event payload', async () => { +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'}; @@ -70,6 +70,20 @@ it('startInstancePubSub: should accept JSON-formatted event payload', async () = 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.startInstancePubSub(mocks.event, mocks.callback); + + const data = await sample.mocks.requestPromise(); + // The request was successfully sent. + assert.strictEqual(data, 'request sent'); +}); + it(`startInstancePubSub: should fail with missing 'zone' attribute`, () => { const mocks = getMocks(); const sample = getSample(); @@ -96,7 +110,22 @@ it(`startInstancePubSub: should fail with missing 'instance' attribute`, () => { assert.deepStrictEqual( mocks.callback.firstCall.args[0], - new Error(`Attribute 'instance' missing from payload`) + new Error(`Attribute 'instance' or 'label' must be specified in payload`) + ); +}); + +it(`startInstancePubSub: should fail when both 'instance' and 'label' attributes specified`, () => { + const mocks = getMocks(); + const sample = getSample(); + const pubsubData = {zone: 'test-zone', instance: 'test-instance', label: 'test-label'}; + mocks.event.data.data = Buffer.from(JSON.stringify(pubsubData)).toString( + 'base64' + ); + sample.program.startInstancePubSub(mocks.event, mocks.callback); + + assert.deepStrictEqual( + mocks.callback.firstCall.args[0], + new Error(`Only one of attributes 'instance' and 'label' can be specified in payload`) ); }); @@ -117,7 +146,7 @@ it('startInstancePubSub: should fail with empty event payload', () => { /** Tests for stopInstancePubSub */ -it('stopInstancePubSub: should accept JSON-formatted event payload', async () => { +it('stopInstancePubSub: should accept JSON-formatted event payloa with instance', async () => { const mocks = getMocks(); const sample = getSample(); const pubsubData = {zone: 'test-zone', instance: 'test-instance'}; @@ -131,6 +160,20 @@ it('stopInstancePubSub: should accept JSON-formatted event payload', async () => 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); + + const data = await sample.mocks.requestPromise(); + // The request was successfully sent. + assert.strictEqual(data, 'request sent'); +}); + it(`stopInstancePubSub: should fail with missing 'zone' attribute`, () => { const mocks = getMocks(); const sample = getSample(); @@ -157,7 +200,22 @@ it(`stopInstancePubSub: should fail with missing 'instance' attribute`, () => { assert.deepStrictEqual( mocks.callback.firstCall.args[0], - new Error(`Attribute 'instance' missing from payload`) + new Error(`Attribute 'instance' or 'label' must be specified in payload`) + ); +}); + +it(`stopInstancePubSub: should fail when both 'instance' and 'label' attributes specified`, () => { + const mocks = getMocks(); + const sample = getSample(); + const pubsubData = {zone: 'test-zone', instance: 'test-instance', label: 'test-label'}; + mocks.event.data.data = Buffer.from(JSON.stringify(pubsubData)).toString( + 'base64' + ); + sample.program.stopInstancePubSub(mocks.event, mocks.callback); + + assert.deepStrictEqual( + mocks.callback.firstCall.args[0], + new Error(`Only one of attributes 'instance' and 'label' can be specified in payload`) ); }); From d4fd556968862f0ade82cc6a3564853f3b2ae30b Mon Sep 17 00:00:00 2001 From: Jani Patokallio Date: Wed, 17 Apr 2019 12:54:50 +1000 Subject: [PATCH 2/8] Remove support for instance names --- functions/scheduleinstance/index.js | 23 ++--------- functions/scheduleinstance/test/index.test.js | 38 ++----------------- 2 files changed, 8 insertions(+), 53 deletions(-) diff --git a/functions/scheduleinstance/index.js b/functions/scheduleinstance/index.js index 89973e3672..bf1d4c131a 100644 --- a/functions/scheduleinstance/index.js +++ b/functions/scheduleinstance/index.js @@ -26,11 +26,8 @@ const compute = new Compute(); * Expects a PubSub message with JSON-formatted event data containing the * following attributes: * zone - the GCP zone the instances are located in. - * instance - the name of a single instance. * label - the label of instances to start. * - * Exactly one of instance or label must be specified. - * * @param {!object} event Cloud Function PubSub message event. * @param {!object} callback Cloud Function PubSub callback indicating completion. */ @@ -40,13 +37,8 @@ exports.startInstancePubSub = (event, callback) => { const payload = _validatePayload( JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString()) ); - if(payload.instance) { - var filter = 'name eq ' + payload.instance - } else if(payload.label) { - var filter = 'labels.' + payload.label - } var options = { - filter: filter + filter: 'labels.' + payload.label } compute.getVMs(options).then(function(vms) { vms[0].forEach(function(instance) { @@ -101,13 +93,8 @@ exports.stopInstancePubSub = (event, callback) => { const payload = _validatePayload( JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString()) ); - if(payload.instance) { - var filter = 'name eq ' + payload.instance - } else if(payload.label) { - var filter = 'labels.' + payload.label - } var options = { - filter: filter + filter: 'labels.' + payload.label } compute.getVMs(options).then(function(vms) { vms[0].forEach(function(instance) { @@ -150,10 +137,8 @@ exports.stopInstancePubSub = (event, callback) => { function _validatePayload(payload) { if (!payload.zone) { throw new Error(`Attribute 'zone' missing from payload`); - } else if (!payload.instance && !payload.label) { - throw new Error(`Attribute 'instance' or 'label' must be specified in payload`); - } else if (payload.instance && payload.label) { - throw new Error(`Only one of attributes 'instance' and 'label' can be specified in payload`); + } else if (!payload.label) { + throw new Error(`Attribute 'label' missing from payload`); } return payload; } diff --git a/functions/scheduleinstance/test/index.test.js b/functions/scheduleinstance/test/index.test.js index 309390367e..fc37dbc84c 100644 --- a/functions/scheduleinstance/test/index.test.js +++ b/functions/scheduleinstance/test/index.test.js @@ -99,7 +99,7 @@ it(`startInstancePubSub: should fail with missing 'zone' attribute`, () => { ); }); -it(`startInstancePubSub: should fail with missing 'instance' attribute`, () => { +it(`startInstancePubSub: should fail with missing 'label' attribute`, () => { const mocks = getMocks(); const sample = getSample(); const pubsubData = {zone: 'test-zone'}; @@ -110,22 +110,7 @@ it(`startInstancePubSub: should fail with missing 'instance' attribute`, () => { assert.deepStrictEqual( mocks.callback.firstCall.args[0], - new Error(`Attribute 'instance' or 'label' must be specified in payload`) - ); -}); - -it(`startInstancePubSub: should fail when both 'instance' and 'label' attributes specified`, () => { - const mocks = getMocks(); - const sample = getSample(); - const pubsubData = {zone: 'test-zone', instance: 'test-instance', label: 'test-label'}; - mocks.event.data.data = Buffer.from(JSON.stringify(pubsubData)).toString( - 'base64' - ); - sample.program.startInstancePubSub(mocks.event, mocks.callback); - - assert.deepStrictEqual( - mocks.callback.firstCall.args[0], - new Error(`Only one of attributes 'instance' and 'label' can be specified in payload`) + new Error(`Attribute 'label' missing from payload`) ); }); @@ -189,7 +174,7 @@ it(`stopInstancePubSub: should fail with missing 'zone' attribute`, () => { ); }); -it(`stopInstancePubSub: should fail with missing 'instance' attribute`, () => { +it(`stopInstancePubSub: should fail with missing 'label' attribute`, () => { const mocks = getMocks(); const sample = getSample(); const pubsubData = {zone: 'test-zone'}; @@ -200,22 +185,7 @@ it(`stopInstancePubSub: should fail with missing 'instance' attribute`, () => { assert.deepStrictEqual( mocks.callback.firstCall.args[0], - new Error(`Attribute 'instance' or 'label' must be specified in payload`) - ); -}); - -it(`stopInstancePubSub: should fail when both 'instance' and 'label' attributes specified`, () => { - const mocks = getMocks(); - const sample = getSample(); - const pubsubData = {zone: 'test-zone', instance: 'test-instance', label: 'test-label'}; - mocks.event.data.data = Buffer.from(JSON.stringify(pubsubData)).toString( - 'base64' - ); - sample.program.stopInstancePubSub(mocks.event, mocks.callback); - - assert.deepStrictEqual( - mocks.callback.firstCall.args[0], - new Error(`Only one of attributes 'instance' and 'label' can be specified in payload`) + new Error(`Attribute 'label' missing from payload`) ); }); From 61c7ade96dc8448673629978135028b79dc2ae79 Mon Sep 17 00:00:00 2001 From: Jani Patokallio Date: Wed, 22 May 2019 13:02:32 +1000 Subject: [PATCH 3/8] Fix linter errors --- functions/scheduleinstance/index.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/functions/scheduleinstance/index.js b/functions/scheduleinstance/index.js index bf1d4c131a..b8587012dd 100644 --- a/functions/scheduleinstance/index.js +++ b/functions/scheduleinstance/index.js @@ -29,7 +29,8 @@ const compute = new Compute(); * label - the label of instances to start. * * @param {!object} event Cloud Function PubSub message event. - * @param {!object} callback Cloud Function PubSub callback indicating completion. + * @param {!object} callback Cloud Function PubSub callback indicating + * completion. */ exports.startInstancePubSub = (event, callback) => { try { @@ -37,12 +38,12 @@ exports.startInstancePubSub = (event, callback) => { const payload = _validatePayload( JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString()) ); - var options = { - filter: 'labels.' + payload.label - } - compute.getVMs(options).then(function(vms) { - vms[0].forEach(function(instance) { - if (payload.zone == instance.zone.id) { + const options = { + filter: 'labels.' + payload.label, + }; + compute.getVMs(options).then(vms => { + vms[0].forEach(instance => { + if (payload.zone === instance.zone.id) { compute .zone(payload.zone) .vm(instance.name) @@ -93,12 +94,12 @@ exports.stopInstancePubSub = (event, callback) => { const payload = _validatePayload( JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString()) ); - var options = { - filter: 'labels.' + payload.label - } - compute.getVMs(options).then(function(vms) { - vms[0].forEach(function(instance) { - if (payload.zone == instance.zone.id) { + const options = { + filter: 'labels.' + payload.label, + }; + compute.getVMs(options).then(vms => { + vms[0].forEach(instance => { + if (payload.zone === instance.zone.id) { compute .zone(payload.zone) .vm(instance.name) @@ -132,7 +133,7 @@ exports.stopInstancePubSub = (event, callback) => { * Validates that a request payload contains the expected fields. * * @param {!object} payload the request payload to validate. - * @returns {!object} the payload object. + * @return {!object} the payload object. */ function _validatePayload(payload) { if (!payload.zone) { From 69e313c2f154c610dc1d3146769254203d2eba7c Mon Sep 17 00:00:00 2001 From: Jani Patokallio Date: Thu, 23 May 2019 11:13:49 +1000 Subject: [PATCH 4/8] Style and typo fixes --- functions/scheduleinstance/index.js | 8 ++------ functions/scheduleinstance/test/index.test.js | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/functions/scheduleinstance/index.js b/functions/scheduleinstance/index.js index b8587012dd..3e732237f9 100644 --- a/functions/scheduleinstance/index.js +++ b/functions/scheduleinstance/index.js @@ -38,9 +38,7 @@ exports.startInstancePubSub = (event, callback) => { const payload = _validatePayload( JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString()) ); - const options = { - filter: 'labels.' + payload.label, - }; + const options = {filter: `labels.${payload.label}`}; compute.getVMs(options).then(vms => { vms[0].forEach(instance => { if (payload.zone === instance.zone.id) { @@ -94,9 +92,7 @@ exports.stopInstancePubSub = (event, callback) => { const payload = _validatePayload( JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString()) ); - const options = { - filter: 'labels.' + payload.label, - }; + const options = {filter: `labels.${payload.label}`}; compute.getVMs(options).then(vms => { vms[0].forEach(instance => { if (payload.zone === instance.zone.id) { diff --git a/functions/scheduleinstance/test/index.test.js b/functions/scheduleinstance/test/index.test.js index fc37dbc84c..82afd576ba 100644 --- a/functions/scheduleinstance/test/index.test.js +++ b/functions/scheduleinstance/test/index.test.js @@ -131,7 +131,7 @@ it('startInstancePubSub: should fail with empty event payload', () => { /** Tests for stopInstancePubSub */ -it('stopInstancePubSub: should accept JSON-formatted event payloa with instance', async () => { +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'}; From 4086fd02f2ce305f3b2b7c6f1ea8f6f685b10ff3 Mon Sep 17 00:00:00 2001 From: Jani Patokallio Date: Tue, 4 Jun 2019 15:26:52 +0530 Subject: [PATCH 5/8] Update function signatures for NodeJS 8 --- functions/scheduleinstance/index.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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 => { From 84051f952a49a411ee1106a2b9203e47cb4dc363 Mon Sep 17 00:00:00 2001 From: Jani Patokallio Date: Tue, 4 Jun 2019 15:55:27 +0530 Subject: [PATCH 6/8] Bump up version number --- functions/scheduleinstance/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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.", From 61365e0134d58756d5c7d1ed0ad27c86d6553887 Mon Sep 17 00:00:00 2001 From: Jani Patokallio Date: Tue, 4 Jun 2019 15:56:01 +0530 Subject: [PATCH 7/8] Update mocks and signatures for NodeJS 8 --- functions/scheduleinstance/test/index.test.js | 69 ++++++------------- 1 file changed, 20 insertions(+), 49 deletions(-) diff --git a/functions/scheduleinstance/test/index.test.js b/functions/scheduleinstance/test/index.test.js index 82afd576ba..0af7a7a756 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,14 @@ 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( + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( 'base64' ); - sample.program.startInstancePubSub(mocks.event, mocks.callback); + sample.program.startInstancePubSub(mocks.event, mocks.context, mocks.callback); const data = await sample.mocks.requestPromise(); // The request was successfully sent. @@ -87,11 +72,11 @@ 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( + const pubsubData = {label: 'testkey=value'}; + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( 'base64' ); - sample.program.startInstancePubSub(mocks.event, mocks.callback); + sample.program.startInstancePubSub(mocks.event, mocks.context, mocks.callback); assert.deepStrictEqual( mocks.callback.firstCall.args[0], @@ -103,10 +88,10 @@ 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( + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( 'base64' ); - sample.program.startInstancePubSub(mocks.event, mocks.callback); + sample.program.startInstancePubSub(mocks.event, mocks.context, mocks.callback); assert.deepStrictEqual( mocks.callback.firstCall.args[0], @@ -118,10 +103,10 @@ 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( + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( 'base64' ); - sample.program.startInstancePubSub(mocks.event, mocks.callback); + sample.program.startInstancePubSub(mocks.event, mocks.context, mocks.callback); assert.deepStrictEqual( mocks.callback.firstCall.args[0], @@ -131,28 +116,14 @@ 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( + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( 'base64' ); - sample.program.stopInstancePubSub(mocks.event, mocks.callback); + sample.program.stopInstancePubSub(mocks.event, mocks.context, mocks.callback); const data = await sample.mocks.requestPromise(); // The request was successfully sent. @@ -162,11 +133,11 @@ 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( + const pubsubData = {label: 'testkey=value'}; + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( 'base64' ); - sample.program.stopInstancePubSub(mocks.event, mocks.callback); + sample.program.stopInstancePubSub(mocks.event, mocks.context, mocks.callback); assert.deepStrictEqual( mocks.callback.firstCall.args[0], @@ -178,10 +149,10 @@ 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( + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( 'base64' ); - sample.program.stopInstancePubSub(mocks.event, mocks.callback); + sample.program.stopInstancePubSub(mocks.event, mocks.context, mocks.callback); assert.deepStrictEqual( mocks.callback.firstCall.args[0], @@ -193,10 +164,10 @@ 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( + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( 'base64' ); - sample.program.stopInstancePubSub(mocks.event, mocks.callback); + sample.program.stopInstancePubSub(mocks.event,mocks.context, mocks.callback); assert.deepStrictEqual( mocks.callback.firstCall.args[0], From e8f636870ff59959b2498529ca39b3c9c2e0ea96 Mon Sep 17 00:00:00 2001 From: Jani Patokallio Date: Tue, 4 Jun 2019 16:17:33 +0530 Subject: [PATCH 8/8] Lint --- functions/scheduleinstance/test/index.test.js | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/functions/scheduleinstance/test/index.test.js b/functions/scheduleinstance/test/index.test.js index 0af7a7a756..eb324122a0 100644 --- a/functions/scheduleinstance/test/index.test.js +++ b/functions/scheduleinstance/test/index.test.js @@ -59,10 +59,12 @@ it('startInstancePubSub: should accept JSON-formatted event payload with label', const mocks = getMocks(); const sample = getSample(); const pubsubData = {zone: 'test-zone', label: 'testkey=value'}; - mocks.event.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.context, mocks.callback); const data = await sample.mocks.requestPromise(); // The request was successfully sent. @@ -73,10 +75,12 @@ it(`startInstancePubSub: should fail with missing 'zone' attribute`, () => { const mocks = getMocks(); const sample = getSample(); const pubsubData = {label: 'testkey=value'}; - mocks.event.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.context, mocks.callback); assert.deepStrictEqual( mocks.callback.firstCall.args[0], @@ -88,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 = 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.context, mocks.callback); assert.deepStrictEqual( mocks.callback.firstCall.args[0], @@ -103,10 +109,12 @@ it('startInstancePubSub: should fail with empty event payload', () => { const mocks = getMocks(); const sample = getSample(); const pubsubData = {}; - mocks.event.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.context, mocks.callback); assert.deepStrictEqual( mocks.callback.firstCall.args[0], @@ -120,9 +128,7 @@ it('startInstancePubSub: should accept JSON-formatted event payload with label', const mocks = getMocks(); const sample = getSample(); const pubsubData = {zone: 'test-zone', label: 'testkey=value'}; - mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( - 'base64' - ); + 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(); @@ -134,9 +140,7 @@ it(`stopInstancePubSub: should fail with missing 'zone' attribute`, () => { const mocks = getMocks(); const sample = getSample(); const pubsubData = {label: 'testkey=value'}; - mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( - 'base64' - ); + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64'); sample.program.stopInstancePubSub(mocks.event, mocks.context, mocks.callback); assert.deepStrictEqual( @@ -149,9 +153,7 @@ it(`stopInstancePubSub: should fail with missing 'label' attribute`, () => { const mocks = getMocks(); const sample = getSample(); const pubsubData = {zone: 'test-zone'}; - mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( - 'base64' - ); + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString('base64'); sample.program.stopInstancePubSub(mocks.event, mocks.context, mocks.callback); assert.deepStrictEqual( @@ -164,10 +166,8 @@ it('stopInstancePubSub: should fail with empty event payload', () => { const mocks = getMocks(); const sample = getSample(); const pubsubData = {}; - mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( - 'base64' - ); - sample.program.stopInstancePubSub(mocks.event,mocks.context, 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],