Skip to content

Commit

Permalink
Add samples for the new Test Lab triggers!! (#588)
Browse files Browse the repository at this point in the history
* Add test lab quickstart and slack sample (#28)
  • Loading branch information
jhuleatt committed Jul 24, 2019
1 parent 5214ef7 commit 91ddb52
Show file tree
Hide file tree
Showing 13 changed files with 365 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -58,6 +58,10 @@ This quickstart sample demonstrates using **Cloud Functions** triggered by **Pub

This quickstart sample demonstrates using **Cloud Functions** triggered by **Crashlytics**. The functions send an email when there is a new issue in an important conversion flow.

### [Test Lab trigger quickstart: Log when a matrix completes](/quickstarts/test-complete)

This quickstart demonstrates how to trigger a function in response to the
completion of a test matrix in **Firebase Test Lab**.

<a name="environment"></a>
## Development Environment Samples and Boilerplates
Expand Down
65 changes: 65 additions & 0 deletions quickstarts/test-complete/.gitignore
@@ -0,0 +1,65 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
firebase-debug.log*

# Firebase cache
.firebase/

# Firebase config

# Uncomment this if you'd like others to create their own Firebase project.
# For a team working on the same Firebase project(s), it is recommended to leave
# it commented so all members can deploy to the same project(s) in .firebaserc.
# .firebaserc

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
31 changes: 31 additions & 0 deletions quickstarts/test-complete/README.md
@@ -0,0 +1,31 @@
# Firebase Test Lab Trigger - Quickstart

This quickstart demonstrates how to trigger a function in response to the
completion of a test matrix in **Firebase Test Lab**.

## Setting up the sample

1. Clone or download this repo and open the `quickstarts/test-complete`
directory.
1. You must have the Firebase CLI installed. If you don't have it install it
with `npm install -g firebase-tools` and then configure it with
`firebase login`.
1. Configure the CLI locally by using `firebase use --add` and select your
project in the list.
1. Install Cloud Functions dependencies locally by running:
`cd functions; npm install; cd -`

## Deploy and test

1. Deploy your function using `firebase deploy --only functions`
1. Navigate to the
[Test Lab](https://console.firebase.google.com/u/0/project/_/testlab/histories)
section of the Firebase Console and start a test.
1. Once the test finishes running,
[view the functions logs](https://console.firebase.google.com/u/0/project/_/functions/logs?severity=DEBUG)
for your project, and check that the test run status was logged.

## Next Steps

To see how to post to Slack instead of just `console.log`-ing, check out
[this sample](https://github.com/firebase/functions-samples/tree/master/testlab-to-slack).
1 change: 1 addition & 0 deletions quickstarts/test-complete/firebase.json
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions quickstarts/test-complete/functions/.gitignore
@@ -0,0 +1 @@
node_modules/
12 changes: 12 additions & 0 deletions quickstarts/test-complete/functions/index.js
@@ -0,0 +1,12 @@
const functions = require('firebase-functions');

exports.logTestComplete = functions.testLab
.testMatrix()
.onComplete(testMatrix => {
const { testMatrixId, createTime, state, outcomeSummary } = testMatrix;

console.log(
`TEST ${testMatrixId} (created at ${createTime}): ${state}. ${outcomeSummary ||
''}`
);
});
22 changes: 22 additions & 0 deletions quickstarts/test-complete/functions/package.json
@@ -0,0 +1,22 @@
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"dependencies": {
"firebase-admin": "^8.0.0",
"firebase-functions": "^3.2.0"
},
"devDependencies": {
"firebase-functions-test": "^0.1.6"
},
"private": true
}
65 changes: 65 additions & 0 deletions testlab-to-slack/.gitignore
@@ -0,0 +1,65 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
firebase-debug.log*

# Firebase cache
.firebase/

# Firebase config

# Uncomment this if you'd like others to create their own Firebase project.
# For a team working on the same Firebase project(s), it is recommended to leave
# it commented so all members can deploy to the same project(s) in .firebaserc.
.firebaserc

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
56 changes: 56 additions & 0 deletions testlab-to-slack/README.md
@@ -0,0 +1,56 @@
# Post Test Lab Results to Slack channel

This sample demonstrates how to post to a Slack channel in response to the
completion of a test matrix in **Firebase Test Lab**. The message will look like
this:

![example Slack message with Test Lab status](https://i.imgur.com/9DTL19x.png)

## Setting up the sample

1. [Create a Slack app](https://api.slack.com/slack-apps#creating_apps)
1. Visit Slack's [_Your Apps_](https://api.slack.com/apps) page, select your
app, and click _OAuth & Permissions_ on the left side of the page.

1. Under the _Scopes_ header, type in `chat:write:bot` and add the scope to
your app. Click _Save Changes_

1. Under the _OAuth Tokens & Redirect URLs_ header, click the _Install App_
button. Once you've installed the app to your Workspace and you're
returned to the page with _OAuth Tokens & Redirect URLs_, copy the _OAuth
Access Token_ that was automatically created when your app was installed.
You'll need this later so your function can authenticate with Slack.

1. [Get the ID](https://stackoverflow.com/questions/40940327/what-is-the-simplest-way-to-find-a-slack-team-id-and-a-channel-id)
of the channel you want your Slack app to post to. You'll need this later so
your function can post to the correct channel.
1. Clone or download this repo and open this directory in a terminal:

```shell
cd testlab-to-slack
```

1. You must have the latest Firebase CLI installed. If you don't have it,
install it with `npm install -g firebase-tools` and then sign in with
`firebase login`.
1. Configure the CLI locally by using `firebase use --add` and select your
project in the list.
1. Install Cloud Functions dependencies locally by running:
`cd functions; npm install; cd -`
1. Set the following environment variables so that the function can
authenticate with Slack and post to the correct room:

```
firebase functions:config:set slack.token="YOUR_SLACK_OAUTH_TOKEN" slack.channelid="ID_OF_YOUR_SLACK_CHANNEL"
```

## Deploy and test

This sample comes with a web-based UI for testing the function. To test it out:

1. Deploy your function using `firebase deploy --only functions`
1. Navigate to the
[Test Lab](https://console.firebase.google.com/u/0/project/_/testlab/histories)
section of the Firebase Console and start a test.
1. Once the test finishes running, check your Slack channel and view the new
post!
1 change: 1 addition & 0 deletions testlab-to-slack/firebase.json
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions testlab-to-slack/functions/.gitignore
@@ -0,0 +1 @@
node_modules/
83 changes: 83 additions & 0 deletions testlab-to-slack/functions/index.js
@@ -0,0 +1,83 @@
const functions = require('firebase-functions');

const slackAPI = 'https://slack.com/api/chat.postMessage';
const slackToken = functions.config().slack.token;
const slackChannelId = functions.config().slack.channelid;
const axios = require('axios');

function postToSlack(title, details) {
return axios.post(
slackAPI,
{
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: title
}
},
{
type: 'divider'
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: details
}
}
],
channel: slackChannelId
},
{
headers: {
Authorization: 'Bearer ' + slackToken
}
}
);
}

function getSlackmoji(term) {
switch (term) {
case 'SUCCESS':
return ':tada:';
case 'FAILURE':
return ':broken_heart:';
case 'INCONCLUSIVE':
return ':question:';
case 'SKIPPED':
return ':arrow_heading_down:';
case 'VALIDATING':
return ':thought_balloon:';
case 'PENDING':
return ':soon:';
case 'FINISHED':
return ':white_check_mark:';
case 'ERROR':
return ':red_circle:';
case 'INVALID':
return ':large_orange_diamond:';
default:
return '';
}
}

exports.postTestResultsToSlack = functions.testLab
.testMatrix()
.onComplete(async testMatrix => {
const { testMatrixId, state, outcomeSummary } = testMatrix;

const title = `${getSlackmoji(state)} ${getSlackmoji(
outcomeSummary
)} ${testMatrixId}`;

const details = `Status: *${state}* ${getSlackmoji(
state
)}\nOutcome: *${outcomeSummary}* ${getSlackmoji(outcomeSummary)}
`;

const slackResponse = await postToSlack(title, details);

console.log(JSON.stringify(slackResponse.data));
});
23 changes: 23 additions & 0 deletions testlab-to-slack/functions/package.json
@@ -0,0 +1,23 @@
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"dependencies": {
"axios": "^0.19.0",
"firebase-admin": "^8.0.0",
"firebase-functions": "^3.2.0"
},
"devDependencies": {
"firebase-functions-test": "^0.1.6"
},
"private": true
}

0 comments on commit 91ddb52

Please sign in to comment.