|
| 1 | +// Copyright 2020 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 assert = require('assert'); |
| 18 | +const {execSync} = require('child_process'); |
| 19 | +const path = require('path'); |
| 20 | +const requestRetry = require('requestretry'); |
| 21 | +const uuid = require('uuid'); |
| 22 | +const {obtainTestInstance} = require('./util'); |
| 23 | +const {describe, it, before, after} = require('mocha'); |
| 24 | + |
| 25 | +const PORT = 9010; |
| 26 | +const BASE_URL = `http://localhost:${PORT}`; |
| 27 | +const cwd = path.join(__dirname, '..'); |
| 28 | + |
| 29 | +const TABLE_ID = `mobile-time-series-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules |
| 30 | + |
| 31 | +describe('functions', async () => { |
| 32 | + const instance = await obtainTestInstance(); |
| 33 | + const INSTANCE_ID = instance.id; |
| 34 | + let table; |
| 35 | + const TIMESTAMP = new Date(2019, 5, 1); |
| 36 | + TIMESTAMP.setUTCHours(0); |
| 37 | + |
| 38 | + let ffProc; |
| 39 | + |
| 40 | + before(async () => { |
| 41 | + table = instance.table(TABLE_ID); |
| 42 | + |
| 43 | + await table.create().catch(console.error); |
| 44 | + await table.createFamily('stats_summary').catch(console.error); |
| 45 | + |
| 46 | + const rowsToInsert = [ |
| 47 | + { |
| 48 | + key: 'phone#4c410523#20190501', |
| 49 | + data: { |
| 50 | + stats_summary: { |
| 51 | + connected_cell: { |
| 52 | + value: 1, |
| 53 | + timestamp: TIMESTAMP, |
| 54 | + }, |
| 55 | + connected_wifi: { |
| 56 | + value: 1, |
| 57 | + timestamp: TIMESTAMP, |
| 58 | + }, |
| 59 | + os_build: { |
| 60 | + value: 'PQ2A.190405.003', |
| 61 | + timestamp: TIMESTAMP, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + key: 'phone#4c410523#20190502', |
| 68 | + data: { |
| 69 | + stats_summary: { |
| 70 | + connected_cell: { |
| 71 | + value: 1, |
| 72 | + timestamp: TIMESTAMP, |
| 73 | + }, |
| 74 | + connected_wifi: { |
| 75 | + value: 1, |
| 76 | + timestamp: TIMESTAMP, |
| 77 | + }, |
| 78 | + os_build: { |
| 79 | + value: 'PQ2A.190405.004', |
| 80 | + timestamp: TIMESTAMP, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + key: 'phone#4c410523#20190505', |
| 87 | + data: { |
| 88 | + stats_summary: { |
| 89 | + connected_cell: { |
| 90 | + value: 0, |
| 91 | + timestamp: TIMESTAMP, |
| 92 | + }, |
| 93 | + connected_wifi: { |
| 94 | + value: 1, |
| 95 | + timestamp: TIMESTAMP, |
| 96 | + }, |
| 97 | + os_build: { |
| 98 | + value: 'PQ2A.190406.000', |
| 99 | + timestamp: TIMESTAMP, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + { |
| 105 | + key: 'phone#5c10102#20190501', |
| 106 | + data: { |
| 107 | + stats_summary: { |
| 108 | + connected_cell: { |
| 109 | + value: 1, |
| 110 | + timestamp: TIMESTAMP, |
| 111 | + }, |
| 112 | + connected_wifi: { |
| 113 | + value: 1, |
| 114 | + timestamp: TIMESTAMP, |
| 115 | + }, |
| 116 | + os_build: { |
| 117 | + value: 'PQ2A.190401.002', |
| 118 | + timestamp: TIMESTAMP, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + { |
| 124 | + key: 'phone#5c10102#20190502', |
| 125 | + data: { |
| 126 | + stats_summary: { |
| 127 | + connected_cell: { |
| 128 | + value: 1, |
| 129 | + timestamp: TIMESTAMP, |
| 130 | + }, |
| 131 | + connected_wifi: { |
| 132 | + value: 0, |
| 133 | + timestamp: TIMESTAMP, |
| 134 | + }, |
| 135 | + os_build: { |
| 136 | + value: 'PQ2A.190406.000', |
| 137 | + timestamp: TIMESTAMP, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + ]; |
| 143 | + |
| 144 | + await table.insert(rowsToInsert).catch(console.error); |
| 145 | + |
| 146 | + // Run the functions-framework instance to host functions locally |
| 147 | + // exec's 'timeout' param won't kill children of "shim" /bin/sh process |
| 148 | + // Workaround: include "& sleep <TIMEOUT>; kill $!" in executed command |
| 149 | + ffProc = execSync( |
| 150 | + `functions-framework --target=readRows --signature-type=http --port ${PORT} & sleep 2; kill $!`, |
| 151 | + {shell: true, cwd} |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + after(async () => { |
| 156 | + // Wait for the functions framework to stop |
| 157 | + await ffProc; |
| 158 | + }); |
| 159 | + |
| 160 | + it('should read one row', async () => { |
| 161 | + const response = await requestRetry({ |
| 162 | + url: `${BASE_URL}/readRows`, |
| 163 | + method: 'GET', |
| 164 | + body: { |
| 165 | + instanceId: INSTANCE_ID, |
| 166 | + tableId: TABLE_ID, |
| 167 | + }, |
| 168 | + retryDelay: 200, |
| 169 | + json: true, |
| 170 | + }); |
| 171 | + |
| 172 | + assert.strictEqual(response.statusCode, 200); |
| 173 | + assert.strictEqual( |
| 174 | + response.body, |
| 175 | + `rowkey: phone#4c410523#20190501, os_build: PQ2A.190405.003 |
| 176 | +rowkey: phone#4c410523#20190502, os_build: PQ2A.190405.004 |
| 177 | +rowkey: phone#4c410523#20190505, os_build: PQ2A.190406.000 |
| 178 | +rowkey: phone#5c10102#20190501, os_build: PQ2A.190401.002 |
| 179 | +rowkey: phone#5c10102#20190502, os_build: PQ2A.190406.000 |
| 180 | +` |
| 181 | + ); |
| 182 | + }); |
| 183 | +}); |
0 commit comments