|
| 1 | +// Copyright 2019 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 | +const {exec} = require('child_process'); |
| 16 | +const {request} = require('gaxios'); |
| 17 | +const assert = require('assert'); |
| 18 | +const sinon = require('sinon'); |
| 19 | +const waitPort = require('wait-port'); |
| 20 | +const {InstancesClient} = require('@google-cloud/compute'); |
| 21 | +const sample = require('../index.js'); |
| 22 | + |
| 23 | +const {BILLING_ACCOUNT} = process.env; |
| 24 | + |
| 25 | +describe('functions/billing tests', () => { |
| 26 | + let projectId; |
| 27 | + before(async () => { |
| 28 | + const client = new InstancesClient(); |
| 29 | + projectId = await client.getProjectId(); |
| 30 | + }); |
| 31 | + after(async () => { |
| 32 | + // Re-enable billing using the sample file itself |
| 33 | + // Invoking the file directly is more concise vs. re-implementing billing setup here |
| 34 | + const jsonData = { |
| 35 | + billingAccountName: `billingAccounts/${BILLING_ACCOUNT}`, |
| 36 | + projectName: `projects/${projectId}`, |
| 37 | + }; |
| 38 | + const encodedData = Buffer.from(JSON.stringify(jsonData)).toString( |
| 39 | + 'base64' |
| 40 | + ); |
| 41 | + const pubsubMessage = {data: encodedData, attributes: {}}; |
| 42 | + await require('../').startBilling(pubsubMessage); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('notifies Slack', () => { |
| 46 | + let ffProc; |
| 47 | + const PORT = 8080; |
| 48 | + const BASE_URL = `http://localhost:${PORT}`; |
| 49 | + |
| 50 | + before(async () => { |
| 51 | + console.log('Starting functions-framework process...'); |
| 52 | + ffProc = exec( |
| 53 | + `npx functions-framework --target=notifySlack --signature-type=event --port ${PORT}` |
| 54 | + ); |
| 55 | + await waitPort({host: 'localhost', port: PORT}); |
| 56 | + console.log('functions-framework process started and listening!'); |
| 57 | + }); |
| 58 | + |
| 59 | + after(() => { |
| 60 | + console.log('Ending functions-framework process...'); |
| 61 | + ffProc.kill(); |
| 62 | + console.log('functions-framework process stopped.'); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('functions_billing_slack', () => { |
| 66 | + it('should notify Slack when budget is exceeded', async () => { |
| 67 | + const jsonData = {costAmount: 500, budgetAmount: 400}; |
| 68 | + const encodedData = Buffer.from(JSON.stringify(jsonData)).toString( |
| 69 | + 'base64' |
| 70 | + ); |
| 71 | + const pubsubMessage = {data: encodedData, attributes: {}}; |
| 72 | + |
| 73 | + const response = await request({ |
| 74 | + url: `${BASE_URL}/notifySlack`, |
| 75 | + method: 'POST', |
| 76 | + data: {data: pubsubMessage}, |
| 77 | + }); |
| 78 | + |
| 79 | + assert.strictEqual(response.status, 200); |
| 80 | + assert.strictEqual( |
| 81 | + response.data, |
| 82 | + 'Slack notification sent successfully' |
| 83 | + ); |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('disables billing', () => { |
| 89 | + let ffProc; |
| 90 | + const PORT = 8081; |
| 91 | + const BASE_URL = `http://localhost:${PORT}`; |
| 92 | + |
| 93 | + before(async () => { |
| 94 | + console.log('Starting functions-framework process...'); |
| 95 | + ffProc = exec( |
| 96 | + `npx functions-framework --target=stopBilling --signature-type=event --port ${PORT}` |
| 97 | + ); |
| 98 | + await waitPort({host: 'localhost', port: PORT}); |
| 99 | + console.log('functions-framework process started and listening!'); |
| 100 | + }); |
| 101 | + |
| 102 | + after(() => { |
| 103 | + console.log('Ending functions-framework process...'); |
| 104 | + ffProc.kill(); |
| 105 | + console.log('functions-framework process stopped.'); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('functions_billing_stop', () => { |
| 109 | + xit('should disable billing when budget is exceeded', async () => { |
| 110 | + // Use functions framework to ensure sample follows GCF specification |
| 111 | + // (Invoking it directly works too, but DOES NOT ensure GCF compatibility) |
| 112 | + const jsonData = {costAmount: 500, budgetAmount: 400}; |
| 113 | + const encodedData = Buffer.from(JSON.stringify(jsonData)).toString( |
| 114 | + 'base64' |
| 115 | + ); |
| 116 | + const pubsubMessage = {data: encodedData, attributes: {}}; |
| 117 | + |
| 118 | + const response = await request({ |
| 119 | + url: `${BASE_URL}/stopBilling`, |
| 120 | + method: 'POST', |
| 121 | + data: {data: pubsubMessage}, |
| 122 | + }); |
| 123 | + |
| 124 | + assert.strictEqual(response.status, 200); |
| 125 | + assert.ok(response.data.includes('Billing disabled')); |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + describe('shuts down GCE instances', () => { |
| 131 | + describe('functions_billing_limit', () => { |
| 132 | + it('should attempt to shut down GCE instances when budget is exceeded', async () => { |
| 133 | + const jsonData = {costAmount: 500, budgetAmount: 400}; |
| 134 | + const encodedData = Buffer.from(JSON.stringify(jsonData)).toString( |
| 135 | + 'base64' |
| 136 | + ); |
| 137 | + const pubsubMessage = {data: encodedData, attributes: {}}; |
| 138 | + // Mock GCE (because real GCE instances take too long to start/stop) |
| 139 | + const instances = [{name: 'test-instance-1', status: 'RUNNING'}]; |
| 140 | + const listStub = sinon |
| 141 | + .stub(sample.getInstancesClient(), 'list') |
| 142 | + .resolves([instances]); |
| 143 | + const stopStub = sinon |
| 144 | + .stub(sample.getInstancesClient(), 'stop') |
| 145 | + .resolves({}); |
| 146 | + await sample.limitUse(pubsubMessage); |
| 147 | + assert.strictEqual(listStub.calledOnce, true); |
| 148 | + assert.ok(stopStub.calledOnce); |
| 149 | + }); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments