|
| 1 | +// Copyright 2018 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +const proxyquire = require('proxyquire').noCallThru(); |
| 18 | +const sinon = require('sinon'); |
| 19 | +const assert = require('assert'); |
| 20 | + |
| 21 | +const getSample = () => { |
| 22 | + const requestPromise = sinon |
| 23 | + .stub() |
| 24 | + .returns(new Promise((resolve) => resolve('request sent'))); |
| 25 | + |
| 26 | + return { |
| 27 | + program: proxyquire('../', { |
| 28 | + 'request-promise': requestPromise, |
| 29 | + }), |
| 30 | + mocks: { |
| 31 | + requestPromise: requestPromise, |
| 32 | + }, |
| 33 | + }; |
| 34 | +}; |
| 35 | + |
| 36 | +const getMocks = () => { |
| 37 | + const event = { |
| 38 | + data: {}, |
| 39 | + }; |
| 40 | + |
| 41 | + const callback = sinon.spy(); |
| 42 | + |
| 43 | + return { |
| 44 | + event: event, |
| 45 | + context: {}, |
| 46 | + callback: callback, |
| 47 | + }; |
| 48 | +}; |
| 49 | +const stubConsole = function () { |
| 50 | + sinon.stub(console, `error`); |
| 51 | + sinon.stub(console, `log`); |
| 52 | +}; |
| 53 | + |
| 54 | +//Restore console |
| 55 | +const restoreConsole = function () { |
| 56 | + console.log.restore(); |
| 57 | + console.error.restore(); |
| 58 | +}; |
| 59 | +beforeEach(stubConsole); |
| 60 | +afterEach(restoreConsole); |
| 61 | + |
| 62 | +/** Tests for startInstancePubSub */ |
| 63 | +describe('functions_start_instance_pubsub', () => { |
| 64 | + it('startInstancePubSub: should accept JSON-formatted event payload with label', async () => { |
| 65 | + const mocks = getMocks(); |
| 66 | + const sample = getSample(); |
| 67 | + const pubsubData = {zone: 'test-zone', label: 'testkey=value'}; |
| 68 | + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( |
| 69 | + 'base64' |
| 70 | + ); |
| 71 | + sample.program.startInstancePubSub( |
| 72 | + mocks.event, |
| 73 | + mocks.context, |
| 74 | + mocks.callback |
| 75 | + ); |
| 76 | + |
| 77 | + const data = await sample.mocks.requestPromise(); |
| 78 | + // The request was successfully sent. |
| 79 | + assert.strictEqual(data, 'request sent'); |
| 80 | + }); |
| 81 | + |
| 82 | + it(`startInstancePubSub: should fail with missing 'zone' attribute`, () => { |
| 83 | + const mocks = getMocks(); |
| 84 | + const sample = getSample(); |
| 85 | + const pubsubData = {label: 'testkey=value'}; |
| 86 | + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( |
| 87 | + 'base64' |
| 88 | + ); |
| 89 | + sample.program.startInstancePubSub( |
| 90 | + mocks.event, |
| 91 | + mocks.context, |
| 92 | + mocks.callback |
| 93 | + ); |
| 94 | + |
| 95 | + assert.deepStrictEqual( |
| 96 | + mocks.callback.firstCall.args[0], |
| 97 | + new Error(`Attribute 'zone' missing from payload`) |
| 98 | + ); |
| 99 | + }); |
| 100 | + |
| 101 | + it(`startInstancePubSub: should fail with missing 'label' attribute`, () => { |
| 102 | + const mocks = getMocks(); |
| 103 | + const sample = getSample(); |
| 104 | + const pubsubData = {zone: 'test-zone'}; |
| 105 | + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( |
| 106 | + 'base64' |
| 107 | + ); |
| 108 | + sample.program.startInstancePubSub( |
| 109 | + mocks.event, |
| 110 | + mocks.context, |
| 111 | + mocks.callback |
| 112 | + ); |
| 113 | + |
| 114 | + assert.deepStrictEqual( |
| 115 | + mocks.callback.firstCall.args[0], |
| 116 | + new Error(`Attribute 'label' missing from payload`) |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + it('startInstancePubSub: should fail with empty event payload', () => { |
| 121 | + const mocks = getMocks(); |
| 122 | + const sample = getSample(); |
| 123 | + const pubsubData = {}; |
| 124 | + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( |
| 125 | + 'base64' |
| 126 | + ); |
| 127 | + sample.program.startInstancePubSub( |
| 128 | + mocks.event, |
| 129 | + mocks.context, |
| 130 | + mocks.callback |
| 131 | + ); |
| 132 | + |
| 133 | + assert.deepStrictEqual( |
| 134 | + mocks.callback.firstCall.args[0], |
| 135 | + new Error(`Attribute 'zone' missing from payload`) |
| 136 | + ); |
| 137 | + }); |
| 138 | +}); |
| 139 | + |
| 140 | +/** Tests for stopInstancePubSub */ |
| 141 | +describe('functions_stop_instance_pubsub', () => { |
| 142 | + it('stopInstancePubSub: should accept JSON-formatted event payload with label', async () => { |
| 143 | + const mocks = getMocks(); |
| 144 | + const sample = getSample(); |
| 145 | + const pubsubData = {zone: 'test-zone', label: 'testkey=value'}; |
| 146 | + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( |
| 147 | + 'base64' |
| 148 | + ); |
| 149 | + sample.program.stopInstancePubSub( |
| 150 | + mocks.event, |
| 151 | + mocks.context, |
| 152 | + mocks.callback |
| 153 | + ); |
| 154 | + |
| 155 | + const data = await sample.mocks.requestPromise(); |
| 156 | + // The request was successfully sent. |
| 157 | + assert.strictEqual(data, 'request sent'); |
| 158 | + }); |
| 159 | + |
| 160 | + it(`stopInstancePubSub: should fail with missing 'zone' attribute`, () => { |
| 161 | + const mocks = getMocks(); |
| 162 | + const sample = getSample(); |
| 163 | + const pubsubData = {label: 'testkey=value'}; |
| 164 | + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( |
| 165 | + 'base64' |
| 166 | + ); |
| 167 | + sample.program.stopInstancePubSub( |
| 168 | + mocks.event, |
| 169 | + mocks.context, |
| 170 | + mocks.callback |
| 171 | + ); |
| 172 | + |
| 173 | + assert.deepStrictEqual( |
| 174 | + mocks.callback.firstCall.args[0], |
| 175 | + new Error(`Attribute 'zone' missing from payload`) |
| 176 | + ); |
| 177 | + }); |
| 178 | + |
| 179 | + it(`stopInstancePubSub: should fail with missing 'label' attribute`, () => { |
| 180 | + const mocks = getMocks(); |
| 181 | + const sample = getSample(); |
| 182 | + const pubsubData = {zone: 'test-zone'}; |
| 183 | + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( |
| 184 | + 'base64' |
| 185 | + ); |
| 186 | + sample.program.stopInstancePubSub( |
| 187 | + mocks.event, |
| 188 | + mocks.context, |
| 189 | + mocks.callback |
| 190 | + ); |
| 191 | + |
| 192 | + assert.deepStrictEqual( |
| 193 | + mocks.callback.firstCall.args[0], |
| 194 | + new Error(`Attribute 'label' missing from payload`) |
| 195 | + ); |
| 196 | + }); |
| 197 | + |
| 198 | + it('stopInstancePubSub: should fail with empty event payload', () => { |
| 199 | + const mocks = getMocks(); |
| 200 | + const sample = getSample(); |
| 201 | + const pubsubData = {}; |
| 202 | + mocks.event.data = Buffer.from(JSON.stringify(pubsubData)).toString( |
| 203 | + 'base64' |
| 204 | + ); |
| 205 | + sample.program.stopInstancePubSub( |
| 206 | + mocks.event, |
| 207 | + mocks.context, |
| 208 | + mocks.callback |
| 209 | + ); |
| 210 | + |
| 211 | + assert.deepStrictEqual( |
| 212 | + mocks.callback.firstCall.args[0], |
| 213 | + new Error(`Attribute 'zone' missing from payload`) |
| 214 | + ); |
| 215 | + }); |
| 216 | +}); |
0 commit comments