From deebc896d82727d2641492919eb4565bb2f08f45 Mon Sep 17 00:00:00 2001 From: ace-n Date: Mon, 10 Jun 2019 13:47:04 -0700 Subject: [PATCH 01/11] Use system tests + update sample + fix lint --- functions/background/index.js | 15 +++--- functions/background/package.json | 5 +- functions/background/test/index.test.js | 65 +++++++++++++------------ 3 files changed, 42 insertions(+), 43 deletions(-) diff --git a/functions/background/index.js b/functions/background/index.js index e149e55895..2d7ac6070d 100644 --- a/functions/background/index.js +++ b/functions/background/index.js @@ -22,13 +22,13 @@ const requestPromiseNative = require('request-promise-native'); * Background Cloud Function that returns a Promise. Note that we don't pass * a "callback" argument to the function. * - * @param {object} event The Cloud Functions event. - * @param {object} event.data The event data. + * @param {object} data The event data + * @param {object} data.endpoint The URL to send the request to. * @returns {Promise} */ -exports.helloPromise = event => { +exports.helloPromise = data => { return requestPromiseNative({ - uri: event.data.endpoint, + uri: data.endpoint, }); }; // [END functions_background_promise] @@ -38,12 +38,11 @@ exports.helloPromise = event => { * Background Cloud Function that returns synchronously. Note that we don't pass * a "callback" argument to the function. * - * @param {object} event The Cloud Functions event. - * @param {object} event.data The event data. + * @param {object} data The event data */ -exports.helloSynchronous = event => { +exports.helloSynchronous = data => { // This function returns synchronously - if (event.data.something === true) { + if (data.something === true) { return 'Something is true!'; } else { throw new Error('Something was not true!'); diff --git a/functions/background/package.json b/functions/background/package.json index 5dfbdbb94f..393b11d963 100644 --- a/functions/background/package.json +++ b/functions/background/package.json @@ -19,10 +19,9 @@ "request-promise-native": "^1.0.5" }, "devDependencies": { - "@google-cloud/nodejs-repo-tools": "^3.3.0", + "child-process-promise": "^2.2.1", "mocha": "^6.0.0", - "proxyquire": "^2.1.0", - "sinon": "^7.2.7" + "requestretry": "^4.0.0" }, "cloud-repo-tools": { "requiresKeyFile": true, diff --git a/functions/background/test/index.test.js b/functions/background/test/index.test.js index 6812c81982..4245d43bbc 100644 --- a/functions/background/test/index.test.js +++ b/functions/background/test/index.test.js @@ -15,49 +15,52 @@ 'use strict'; -const proxyquire = require('proxyquire').noCallThru(); -const sinon = require('sinon'); const assert = require('assert'); -const tools = require('@google-cloud/nodejs-repo-tools'); +const requestRetry = require('requestretry'); +const execPromise = require('child-process-promise').exec; +const path = require('path'); -function getSample() { - const requestPromiseNative = sinon.stub().returns(Promise.resolve('test')); +const program = require('..'); - return { - program: proxyquire('../', { - 'request-promise-native': requestPromiseNative, - }), - mocks: { - requestPromiseNative: requestPromiseNative, - }, - }; -} +const BASE_URL = process.env.BASE_URL || 'http://localhost:8080'; +const cwd = path.join(__dirname, '..'); -beforeEach(tools.stubConsole); -afterEach(tools.restoreConsole); +let ffProc; -it('should make a promise request', () => { - const sample = getSample(); +before(() => { + ffProc = execPromise( + `functions-framework --target=helloPromise --signature-type=event`, + {timeout: 1000, shell: true, cwd} + ); +}); + +after(async () => { + await ffProc; +}); + +it('should make a promise request', async () => { const event = { data: { - endpoint: 'foo.com', + endpoint: 'https://example.com', }, }; - return sample.program.helloPromise(event).then(result => { - assert.deepStrictEqual(sample.mocks.requestPromiseNative.firstCall.args, [ - {uri: 'foo.com'}, - ]); - assert.strictEqual(result, 'test'); + const response = await requestRetry({ + url: `${BASE_URL}/`, + method: 'POST', + body: event, + retryDelay: 200, + json: true, }); + + assert.strictEqual(response.statusCode, 200); + assert.ok(response.body.includes(`Example Domain`)); }); it('should return synchronously', () => { assert.strictEqual( - getSample().program.helloSynchronous({ - data: { - something: true, - }, + program.helloSynchronous({ + something: true, }), 'Something is true!' ); @@ -66,10 +69,8 @@ it('should return synchronously', () => { it('should throw an error', () => { assert.throws( () => { - getSample().program.helloSynchronous({ - data: { - something: false, - }, + program.helloSynchronous({ + something: false, }); }, Error, From bd09ed2739b049ce58517b217a68fb7052a21369 Mon Sep 17 00:00:00 2001 From: ace-n Date: Mon, 10 Jun 2019 14:32:25 -0700 Subject: [PATCH 02/11] Swap emulator with Node.js FF --- .kokoro/build.sh | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.kokoro/build.sh b/.kokoro/build.sh index 9a453242c9..3a2079c5ef 100755 --- a/.kokoro/build.sh +++ b/.kokoro/build.sh @@ -60,11 +60,7 @@ gcloud config set project $GCLOUD_PROJECT # Start functions emulator, if appropriate if [[ $PROJECT == functions/* ]]; then - export FUNCTIONS_LOG_PATH=$(pwd)/logs/cloud-functions-emulator.log - npm install -g @google-cloud/functions-emulator - touch "$FUNCTIONS_LOG_PATH" - functions config set logFile "$FUNCTIONS_LOG_PATH" - functions-emulator start + npm install -g @google-cloud/functions-framework fi npm test From 19631a485cae2a5349b8bca7f4259b5b3df7c335 Mon Sep 17 00:00:00 2001 From: ace-n Date: Mon, 10 Jun 2019 20:57:32 -0700 Subject: [PATCH 03/11] Put emulator back --- .kokoro/build.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.kokoro/build.sh b/.kokoro/build.sh index 3a2079c5ef..ebd996709d 100755 --- a/.kokoro/build.sh +++ b/.kokoro/build.sh @@ -61,6 +61,12 @@ gcloud config set project $GCLOUD_PROJECT # Start functions emulator, if appropriate if [[ $PROJECT == functions/* ]]; then npm install -g @google-cloud/functions-framework + + export FUNCTIONS_LOG_PATH=$(pwd)/logs/cloud-functions-emulator.log + npm install -g @google-cloud/functions-emulator + touch "$FUNCTIONS_LOG_PATH" + functions config set logFile "$FUNCTIONS_LOG_PATH" + functions-emulator start fi npm test From 7a79ac56297ae62be76aa1060c250045661cf452 Mon Sep 17 00:00:00 2001 From: ace-n Date: Mon, 10 Jun 2019 21:54:18 -0700 Subject: [PATCH 04/11] More context-sensitive emulator install (to avoid FF conflicts) --- .kokoro/build.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.kokoro/build.sh b/.kokoro/build.sh index ebd996709d..347bbae22f 100755 --- a/.kokoro/build.sh +++ b/.kokoro/build.sh @@ -58,10 +58,10 @@ export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/secrets-key.json gcloud auth activate-service-account --key-file "$GOOGLE_APPLICATION_CREDENTIALS" gcloud config set project $GCLOUD_PROJECT -# Start functions emulator, if appropriate -if [[ $PROJECT == functions/* ]]; then - npm install -g @google-cloud/functions-framework +npm install -g @google-cloud/functions-framework +# Start functions emulator, if appropriate +if [[ $PROJECT == functions/* ]] && grep --quiet functions-emulator **/package.json; then export FUNCTIONS_LOG_PATH=$(pwd)/logs/cloud-functions-emulator.log npm install -g @google-cloud/functions-emulator touch "$FUNCTIONS_LOG_PATH" From d58a5758669c28abd87a0e6c259647e95b39a814 Mon Sep 17 00:00:00 2001 From: ace-n Date: Tue, 11 Jun 2019 19:59:58 -0700 Subject: [PATCH 05/11] Tweak build cmd --- .kokoro/build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.kokoro/build.sh b/.kokoro/build.sh index 347bbae22f..82741fb449 100755 --- a/.kokoro/build.sh +++ b/.kokoro/build.sh @@ -61,7 +61,7 @@ gcloud config set project $GCLOUD_PROJECT npm install -g @google-cloud/functions-framework # Start functions emulator, if appropriate -if [[ $PROJECT == functions/* ]] && grep --quiet functions-emulator **/package.json; then +if [[ $PROJECT == functions/* ]] && grep --quiet functions-emulator package.json; then export FUNCTIONS_LOG_PATH=$(pwd)/logs/cloud-functions-emulator.log npm install -g @google-cloud/functions-emulator touch "$FUNCTIONS_LOG_PATH" From 314b6c782e243c248f6074b94b3d69eaa96ab779 Mon Sep 17 00:00:00 2001 From: ace-n Date: Wed, 12 Jun 2019 11:05:02 -0700 Subject: [PATCH 06/11] Attempt to fix failing test: add missing pkg + increase timeout --- functions/background/package.json | 1 + functions/background/test/index.test.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/functions/background/package.json b/functions/background/package.json index 393b11d963..6dea6943ca 100644 --- a/functions/background/package.json +++ b/functions/background/package.json @@ -19,6 +19,7 @@ "request-promise-native": "^1.0.5" }, "devDependencies": { + "@google-cloud/functions-framework": "^1.1.1", "child-process-promise": "^2.2.1", "mocha": "^6.0.0", "requestretry": "^4.0.0" diff --git a/functions/background/test/index.test.js b/functions/background/test/index.test.js index 4245d43bbc..1654347ca0 100644 --- a/functions/background/test/index.test.js +++ b/functions/background/test/index.test.js @@ -30,7 +30,7 @@ let ffProc; before(() => { ffProc = execPromise( `functions-framework --target=helloPromise --signature-type=event`, - {timeout: 1000, shell: true, cwd} + {timeout: 2000, shell: true, cwd} ); }); From 34883337d7c8c5a146be84a8d17114fcf09f3e18 Mon Sep 17 00:00:00 2001 From: ace-n Date: Wed, 12 Jun 2019 11:44:58 -0700 Subject: [PATCH 07/11] DEBUG: debug CI --- functions/background/test/index.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/functions/background/test/index.test.js b/functions/background/test/index.test.js index 1654347ca0..9825f378be 100644 --- a/functions/background/test/index.test.js +++ b/functions/background/test/index.test.js @@ -35,7 +35,8 @@ before(() => { }); after(async () => { - await ffProc; + const dbg = await ffProc; + console.log(dbg); }); it('should make a promise request', async () => { From 49dab6ef2b7ee3e452fcdab635a73f36f0439913 Mon Sep 17 00:00:00 2001 From: ace-n Date: Wed, 12 Jun 2019 11:55:37 -0700 Subject: [PATCH 08/11] DEBUG: print base URL --- functions/background/test/index.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/functions/background/test/index.test.js b/functions/background/test/index.test.js index 9825f378be..097beb8c88 100644 --- a/functions/background/test/index.test.js +++ b/functions/background/test/index.test.js @@ -23,6 +23,8 @@ const path = require('path'); const program = require('..'); const BASE_URL = process.env.BASE_URL || 'http://localhost:8080'; +console.log('DBG BASE URL', BASE_URL); + const cwd = path.join(__dirname, '..'); let ffProc; From 0dd50d342fc64fed244725ec0f2cd9ead171016d Mon Sep 17 00:00:00 2001 From: ace-n Date: Wed, 12 Jun 2019 12:12:39 -0700 Subject: [PATCH 09/11] FIX: move BASE_URL into emulator-only block --- .kokoro/build.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.kokoro/build.sh b/.kokoro/build.sh index 82741fb449..ffa0d7e83c 100755 --- a/.kokoro/build.sh +++ b/.kokoro/build.sh @@ -41,7 +41,6 @@ export SENDGRID_API_KEY=$(cat $KOKORO_GFILE_DIR/secrets-sendgrid-api-key.txt) # Configure GCF variables export FUNCTIONS_TOPIC=integration-tests-instance export FUNCTIONS_BUCKET=$GCLOUD_PROJECT -export BASE_URL="http://localhost:8010/${GCLOUD_PROJECT}/${GCF_REGION}" # Configure IoT variables export NODEJS_IOT_EC_PUBLIC_KEY=${KOKORO_GFILE_DIR}/ec_public.pem @@ -62,6 +61,8 @@ npm install -g @google-cloud/functions-framework # Start functions emulator, if appropriate if [[ $PROJECT == functions/* ]] && grep --quiet functions-emulator package.json; then + export BASE_URL="http://localhost:8010/${GCLOUD_PROJECT}/${GCF_REGION}" + export FUNCTIONS_LOG_PATH=$(pwd)/logs/cloud-functions-emulator.log npm install -g @google-cloud/functions-emulator touch "$FUNCTIONS_LOG_PATH" From 10cffc541766bd217e37c8071f416541980118ae Mon Sep 17 00:00:00 2001 From: ace-n Date: Wed, 12 Jun 2019 13:56:37 -0700 Subject: [PATCH 10/11] Revert dbg commits + handle timeout (for Linux systems) --- functions/background/test/index.test.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/functions/background/test/index.test.js b/functions/background/test/index.test.js index 097beb8c88..76e4715960 100644 --- a/functions/background/test/index.test.js +++ b/functions/background/test/index.test.js @@ -37,8 +37,16 @@ before(() => { }); after(async () => { - const dbg = await ffProc; - console.log(dbg); + try { + await ffProc; + } catch (err) { + // Timeouts always cause errors on Linux, so catch them + if (err.name && err.name === 'ChildProcessError') { + return; + } + + throw err; + } }); it('should make a promise request', async () => { From 1be7c1b9a2f77c3a651d09cb434a47e355848bef Mon Sep 17 00:00:00 2001 From: ace-n Date: Wed, 12 Jun 2019 14:01:54 -0700 Subject: [PATCH 11/11] Delete debug, take 2 --- functions/background/test/index.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/functions/background/test/index.test.js b/functions/background/test/index.test.js index 76e4715960..2ac797cfaa 100644 --- a/functions/background/test/index.test.js +++ b/functions/background/test/index.test.js @@ -23,7 +23,6 @@ const path = require('path'); const program = require('..'); const BASE_URL = process.env.BASE_URL || 'http://localhost:8080'; -console.log('DBG BASE URL', BASE_URL); const cwd = path.join(__dirname, '..');