Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
60 changes: 60 additions & 0 deletions functions/helloworld/test/sample.integration.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_integration_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_integration_test]
78 changes: 78 additions & 0 deletions functions/helloworld/test/sample.integration.storage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* 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.
*/

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`));
});
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]
47 changes: 47 additions & 0 deletions functions/helloworld/test/sample.system.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_system_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_system_test]
77 changes: 77 additions & 0 deletions functions/helloworld/test/sample.system.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_system_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_system_test]
55 changes: 55 additions & 0 deletions functions/helloworld/test/sample.unit.http.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 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_unit_test]
const test = require(`ava`);
const sinon = require(`sinon`);
const uuid = require(`uuid`);

const helloHttp = require(`..`).helloHttp;

test(`helloHttp: should print a name`, t => {
// Initialize mocks
const name = uuid.v4();
const req = {
body: {
name: name
}
};
const res = { send: sinon.stub() };

// Call tested function
helloHttp(req, res);

// Verify behavior of tested function
t.true(res.send.calledOnce);
t.deepEqual(res.send.firstCall.args, [`Hello ${name}!`]);
});

test(`helloHttp: should print hello world`, t => {
// Initialize mocks
const req = {
body: {}
};
const res = { send: sinon.stub() };

// Call tested function
helloHttp(req, res);

// Verify behavior of tested function
t.true(res.send.calledOnce);
t.deepEqual(res.send.firstCall.args, [`Hello World!`]);
});
// [END functions_http_unit_test]
Loading