Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions sample tests #553

Merged
merged 23 commits into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ dependencies:
# Run your tests
test:
override:
- functions start && cd functions/datastore && npm run system-test
- functions start && cd functions/helloworld && npm run system-test
- functions start && cd functions/datastore && npm run system-test && functions stop
- functions start && cd functions/helloworld && npm run test && functions stop
- samples test run --cmd nyc -- --cache ava --verbose -T 30s 'functions/background/test/**/*.test.js'
- samples test run --cmd nyc -- --cache ava --verbose -T 30s 'functions/gcs/test/**/*.test.js'
- samples test run --cmd nyc -- --cache ava --verbose -T 30s 'functions/http/test/**/*.test.js'
Expand Down
10 changes: 10 additions & 0 deletions functions/ci_cd/cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
steps:
- name: 'gcr.io/cloud-builders/yarn'
args: ['install']
dir: 'functions/autodeploy'
- name: 'gcr.io/cloud-builders/npm'
args: ['test']
dir: 'functions/autodeploy'
- name: 'gcr.io/cloud-builders/gcloud'
args: ['beta', 'functions', 'deploy', '[YOUR_FUNCTION_NAME]', '[YOUR_FUNCTION_TRIGGER]']
dir: 'functions/autodeploy'
5 changes: 2 additions & 3 deletions functions/helloworld/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
"lint": "repo-tools lint",
"pretest": "npm run lint",
"e2e-test": "export FUNCTIONS_CMD='gcloud beta functions' && sh test/updateFunctions.sh && BASE_URL=\"https://$GCF_REGION-$GCLOUD_PROJECT.cloudfunctions.net/\" ava -T 20s --verbose test/*.test.js",
"system-test": "export FUNCTIONS_CMD='functions' && sh test/updateFunctions.sh && export BASE_URL=\"http://localhost:8010/$GCLOUD_PROJECT/$GCF_REGION\" && ava -T 20s --verbose test/*.test.js",
"test": "npm run system-test"
"test": "export FUNCTIONS_CMD='functions' && sh test/updateFunctions.sh && export BASE_URL=\"http://localhost:8010/$GCLOUD_PROJECT/$GCF_REGION\" && ava -T 20s --verbose test/index.test.js test/*unit*test.js test/*integration*test.js",
"system-test": "export FUNCTIONS_CMD='functions' && sh test/updateFunctions.sh && export BASE_URL=\"http://localhost:8010/$GCLOUD_PROJECT/$GCF_REGION\" && ava -T 20s --verbose test/*.test.js"
},
"dependencies": {
"@google-cloud/debug-agent": "2.3.0",
"pug": "2.0.0-rc.4",
"safe-buffer": "5.1.1"
},
"devDependencies": {
"@google-cloud/functions-emulator": "^1.0.0-alpha.29",
"@google-cloud/nodejs-repo-tools": "2.1.3",
"@google-cloud/pubsub": "^0.15.0",
"@google-cloud/storage": "^1.5.0",
Expand Down
41 changes: 41 additions & 0 deletions functions/helloworld/test/sample.integration.http.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START functions_http_integration_test]
const test = require(`ava`);
const Supertest = require(`supertest`);
const supertest = Supertest(process.env.BASE_URL);

test.cb(`helloHttp: should print a name`, (t) => {
supertest
.post(`/helloHttp`)
.send({ name: 'John' })
.expect(200)
.expect((response) => {
t.is(response.text, 'Hello John!');
})
.end(t.end);
});

test.cb(`helloHttp: should print hello world`, (t) => {
supertest
.get(`/helloHttp`)
.expect(200)
.expect((response) => {
t.is(response.text, `Hello World!`);
})
.end(t.end);
});
// [END functions_http_integration_test]
47 changes: 47 additions & 0 deletions functions/helloworld/test/sample.integration.pubsub.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START functions_pubsub_integration_test]
const childProcess = require(`child_process`);
const test = require(`ava`);
const uuid = require(`uuid`);

test(`helloPubSub: should print a name`, async (t) => {
t.plan(1);
const startTime = new Date(Date.now()).toISOString();
const name = uuid.v4();

// Mock Pub/Sub call, as the emulator doesn't listen to Pub/Sub topics
const encodedName = Buffer.from(name).toString(`base64`);
const data = JSON.stringify({ data: encodedName });
childProcess.execSync(`functions call helloPubSub --data '${data}'`);

// Check the emulator's logs
const logs = childProcess.execSync(`functions logs read helloPubSub --start-time ${startTime}`).toString();
t.true(logs.includes(`Hello, ${name}!`));
});

test(`helloPubSub: should print hello world`, async (t) => {
t.plan(1);
const startTime = new Date(Date.now()).toISOString();

// Mock Pub/Sub call, as the emulator doesn't listen to Pub/Sub topics
childProcess.execSync(`functions call helloPubSub --data {}`);

// Check the emulator's logs
const logs = childProcess.execSync(`functions logs read helloPubSub --start-time ${startTime}`).toString();
t.true(logs.includes(`Hello, World!`));
});
// [END functions_pubsub_integration_test]
77 changes: 77 additions & 0 deletions functions/helloworld/test/sample.integration.storage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START functions_storage_integration_test]
const childProcess = require(`child_process`);
const test = require(`ava`);
const uuid = require(`uuid`);

test(`helloGCS: should print uploaded message`, async (t) => {
t.plan(1);
const startTime = new Date(Date.now()).toISOString();
const filename = uuid.v4(); // Use a unique filename to avoid conflicts

// Mock GCS call, as the emulator doesn't listen to GCS buckets
const data = JSON.stringify({
name: filename,
resourceState: 'exists',
metageneration: '1'
});

childProcess.execSync(`functions call helloGCS --data '${data}'`);

// Check the emulator's logs
const logs = childProcess.execSync(`functions logs read helloGCS --start-time ${startTime}`).toString();
t.true(logs.includes(`File ${filename} uploaded.`));
});

test(`helloGCS: should print metadata updated message`, async (t) => {
t.plan(1);
const startTime = new Date(Date.now()).toISOString();
const filename = uuid.v4(); // Use a unique filename to avoid conflicts

// Mock GCS call, as the emulator doesn't listen to GCS buckets
const data = JSON.stringify({
name: filename,
resourceState: 'exists',
metageneration: '2'
});

childProcess.execSync(`functions call helloGCS --data '${data}'`);

// Check the emulator's logs
const logs = childProcess.execSync(`functions logs read helloGCS --start-time ${startTime}`).toString();
t.true(logs.includes(`File ${filename} metadata updated.`));
});

test(`helloGCS: should print deleted message`, async (t) => {
t.plan(1);
const startTime = new Date(Date.now()).toISOString();
const filename = uuid.v4(); // Use a unique filename to avoid conflicts

// Mock GCS call, as the emulator doesn't listen to GCS buckets
const data = JSON.stringify({
name: filename,
resourceState: 'not_exists',
metageneration: '3'
});

childProcess.execSync(`functions call helloGCS --data '${data}'`);

// Check the emulator's logs
const logs = childProcess.execSync(`functions logs read helloGCS --start-time ${startTime}`).toString();
t.true(logs.includes(`File ${filename} deleted.`));
});
// [END functions_storage_integration_test]
41 changes: 41 additions & 0 deletions functions/helloworld/test/sample.system.http.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START functions_http_system_test]
const test = require(`ava`);
const Supertest = require(`supertest`);
const supertest = Supertest(process.env.BASE_URL);

test.cb(`helloHttp: should print a name`, (t) => {
supertest
.post(`/helloHttp`)
.send({ name: 'John' })
.expect(200)
.expect((response) => {
t.is(response.text, 'Hello John!');
})
.end(t.end);
});

test.cb(`helloHttp: should print hello world`, (t) => {
supertest
.get(`/helloHttp`)
.expect(200)
.expect((response) => {
t.is(response.text, `Hello World!`);
})
.end(t.end);
});
// [END functions_http_system_test]
60 changes: 60 additions & 0 deletions functions/helloworld/test/sample.system.pubsub.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START functions_pubsub_system_test]
const childProcess = require(`child_process`);
const test = require(`ava`);
const uuid = require(`uuid`);
const Pubsub = require(`@google-cloud/pubsub`);
const pubsub = Pubsub();

const topicName = process.env.FUNCTIONS_TOPIC;
const baseCmd = `gcloud beta functions`;

test(`helloPubSub: should print a name`, async (t) => {
t.plan(1);
const startTime = new Date(Date.now()).toISOString();
const name = uuid.v4();

// Publish to pub/sub topic
const topic = pubsub.topic(topicName);
const publisher = topic.publisher();
await publisher.publish(Buffer.from(name));

// Wait for logs to become consistent
await new Promise(resolve => setTimeout(resolve, 15000));

// Check logs after a delay
const logs = childProcess.execSync(`${baseCmd} logs read helloPubSub --start-time ${startTime}`).toString();
t.true(logs.includes(`Hello, ${name}!`));
});

test(`helloPubSub: should print hello world`, async (t) => {
t.plan(1);
const startTime = new Date(Date.now()).toISOString();

// Publish to pub/sub topic
const topic = pubsub.topic(topicName);
const publisher = topic.publisher();
await publisher.publish(Buffer.from(''), { a: 'b' });

// Wait for logs to become consistent
await new Promise(resolve => setTimeout(resolve, 15000));

// Check logs after a delay
const logs = childProcess.execSync(`${baseCmd} logs read helloPubSub --start-time ${startTime}`).toString();
t.true(logs.includes('Hello, World!'));
});
// [END functions_pubsub_system_test]
80 changes: 80 additions & 0 deletions functions/helloworld/test/sample.system.storage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START functions_storage_system_test]
const Storage = require(`@google-cloud/storage`);
const storage = Storage();
const uuid = require(`uuid`);
const test = require(`ava`);
const path = require(`path`);
const childProcess = require(`child_process`);
const localFileName = `test.txt`;

// Use unique GCS filename to avoid conflicts between concurrent test runs
const gcsFileName = `test-${uuid.v4()}.txt`;

const bucketName = process.env.BUCKET_NAME;
const bucket = storage.bucket(bucketName);
const baseCmd = `gcloud beta functions`;

test.serial(`helloGCS: should print uploaded message`, async (t) => {
t.plan(1);
const startTime = new Date(Date.now()).toISOString();

// Upload file
const filepath = path.join(__dirname, localFileName);
await bucket.upload(filepath, {
destination: gcsFileName
});

// Wait for consistency
await new Promise(resolve => setTimeout(resolve, 15000));

// Check logs
const logs = childProcess.execSync(`${baseCmd} logs read helloGCS --start-time ${startTime}`).toString();
t.true(logs.includes(`File ${gcsFileName} uploaded`));
});

test.serial(`helloGCS: should print metadata updated message`, async (t) => {
t.plan(1);
const startTime = new Date(Date.now()).toISOString();

// Update file metadata
const file = bucket.file(gcsFileName);
await file.setMetadata(gcsFileName, { foo: `bar` });

// Wait for consistency
await new Promise(resolve => setTimeout(resolve, 15000));

// Check logs
const logs = childProcess.execSync(`${baseCmd} logs read helloGCS --start-time ${startTime}`).toString();
t.true(logs.includes(`File ${gcsFileName} metadata updated`));
});

test.serial(`helloGCS: should print deleted message`, async (t) => {
t.plan(1);
const startTime = new Date(Date.now()).toISOString();

// Delete file
bucket.deleteFiles();

// Wait for consistency
await new Promise(resolve => setTimeout(resolve, 15000));

// Check logs
const logs = childProcess.execSync(`${baseCmd} logs read helloGCS --start-time ${startTime}`).toString();
t.true(logs.includes(`File ${gcsFileName} deleted`));
});
// [START functions_storage_system_test]
Loading