Skip to content

Add code for managing emulators and running database/firestore emulator tests via yarn commands. #1435

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

Merged
merged 5 commits into from
Dec 20, 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 .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- script: yarn test:saucelabs --database-firestore-only
# TODO(yifanyang): Once we verify the emulator tests are reliable, we
# should make these tests blocking rather than allow failures.
- script: node scripts/testing/database-emulator-test.js
- script: yarn test:database:emulator
include:
- name: Node.js and Browser (Chrome) Test
stage: test
Expand All @@ -53,7 +53,7 @@ jobs:
if: type = push
- name: Database Node.js and Browser (Chrome) Test with Emulator
stage: test
script: node scripts/testing/database-emulator-test.js
script: yarn test:database:emulator
- stage: deploy
script: skip
# NPM Canary Build Config
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"test:coverage": "lcov-result-merger 'packages/**/lcov.info' | coveralls",
"test:setup": "node tools/config.js",
"pretest:saucelabs": "lerna run --parallel pretest",
"test:saucelabs": "karma start config/karma.saucelabs.js --single-run"
"test:saucelabs": "karma start config/karma.saucelabs.js --single-run",
"test:database:emulator": "ts-node scripts/emulator-testing/database-test-runner.ts",
"test:firestore:emulator": "ts-node scripts/emulator-testing/firestore-test-runner.ts"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -61,6 +63,8 @@
"prettier": "1.12.0",
"semver": "5.5.0",
"simple-git": "1.92.0",
"ts-node": "5.0.1",
"typescript": "2.8.1",
"yargs": "11.0.0"
}
}
29 changes: 23 additions & 6 deletions packages/firestore/test/integration/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,39 @@ declare const __karma__: any;

const PROJECT_CONFIG = require('../../../../../config/project.json');

const EMULATOR_PORT = process.env.FIRESTORE_EMULATOR_PORT;
const EMULATOR_PROJECT_ID = process.env.FIRESTORE_EMULATOR_PROJECT_ID;
export const USE_EMULATOR = !!EMULATOR_PORT;

const EMULATOR_FIRESTORE_SETTING = {
host: `localhost:${EMULATOR_PORT}`,
ssl: false,
timestampsInSnapshots: true
};

const PROD_FIRESTORE_SETTING = {
host: 'firestore.googleapis.com',
ssl: true,
timestampsInSnapshots: true
};

export const DEFAULT_SETTINGS = getDefaultSettings();

// tslint:disable-next-line:no-console
console.log(`Default Settings: ${JSON.stringify(DEFAULT_SETTINGS)}`);

function getDefaultSettings(): firestore.Settings {
const karma = typeof __karma__ !== 'undefined' ? __karma__ : undefined;
if (karma && karma.config.firestoreSettings) {
return karma.config.firestoreSettings;
} else {
return {
host: 'firestore.googleapis.com',
ssl: true,
timestampsInSnapshots: true
};
return USE_EMULATOR ? EMULATOR_FIRESTORE_SETTING : PROD_FIRESTORE_SETTING;
}
}

export const DEFAULT_PROJECT_ID = PROJECT_CONFIG.projectId;
export const DEFAULT_PROJECT_ID = USE_EMULATOR
? EMULATOR_PROJECT_ID
: PROJECT_CONFIG.projectId;
export const ALT_PROJECT_ID = 'test-db2';

function isIeOrEdge(): boolean {
Expand Down
50 changes: 50 additions & 0 deletions scripts/emulator-testing/database-test-runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* 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.
*/

import { spawn } from 'child-process-promise';
import * as path from 'path';

import { DatabaseEmulator } from './emulators/database-emulator';
import { ChildProcessPromise } from './emulators/emulator';

function runTest(port: number, namespace: string): ChildProcessPromise {
const options = {
cwd: path.resolve(__dirname, '../../packages/database'),
env: Object.assign({}, process.env, {
RTDB_EMULATOR_PORT: port,
RTDB_EMULATOR_NAMESPACE: namespace
}),
stdio: 'inherit'
};
return spawn('yarn', ['test'], options);
}

async function run(): Promise<void> {
const emulator = new DatabaseEmulator();
try {
await emulator.download();
await emulator.setUp();
await emulator.setPublicRules();
await runTest(emulator.port, emulator.namespace);
} finally {
await emulator.tearDown();
}
}

run().catch(err => {
console.error(err);
process.exitCode = 1;
});
54 changes: 54 additions & 0 deletions scripts/emulator-testing/emulators/database-emulator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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.
*/

import * as request from 'request';

import { Emulator } from './emulator';

export class DatabaseEmulator extends Emulator {
namespace: string;

constructor(port = 8088, namespace = 'test-emulator') {
super(port);
this.namespace = namespace;
this.binaryName = 'database-emulator.jar';
// Use locked version of emulator for test to be deterministic.
// The latest version can be found from database emulator doc:
// https://firebase.google.com/docs/database/security/test-rules-emulator
this.binaryUrl =
'https://storage.googleapis.com/firebase-preview-drop/emulator/firebase-database-emulator-v3.5.0.jar';
}

setPublicRules(): Promise<number> {
console.log('Setting rule {".read": true, ".write": true} to emulator ...');
return new Promise<number>((resolve, reject) => {
request.put(
{
uri: `http://localhost:${this.port}/.settings/rules.json?ns=${
this.namespace
}`,
headers: { Authorization: 'Bearer owner' },
body: '{ "rules": { ".read": true, ".write": true } }'
},
(error, response, body) => {
if (error) reject(error);
console.log(`Done setting public rule to emulator: ${body}.`);
resolve(response.statusCode);
}
);
});
}
}
109 changes: 109 additions & 0 deletions scripts/emulator-testing/emulators/emulator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* 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.
*/

import { spawn } from 'child-process-promise';
import { ChildProcess } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as request from 'request';
import * as tmp from 'tmp';

export interface ChildProcessPromise extends Promise<void> {
childProcess: ChildProcess;
}

export abstract class Emulator {
binaryName: string;
binaryUrl: string;
binaryPath: string;

emulator: ChildProcess;
port: number;

constructor(port: number) {
this.port = port;
}

download(): Promise<void> {
return new Promise<void>((resolve, reject) => {
tmp.dir((err, dir) => {
if (err) reject(err);

console.log(`Created temporary directory at [${dir}].`);
const filepath: string = path.resolve(dir, this.binaryName);
const writeStream: fs.WriteStream = fs.createWriteStream(filepath);

console.log(`Downloading emulator from [${this.binaryUrl}] ...`);
request(this.binaryUrl)
.pipe(writeStream)
.on('finish', () => {
console.log(`Saved emulator binary file to [${filepath}].`);
this.binaryPath = filepath;
resolve();
})
.on('error', reject);
});
});
}

setUp(): Promise<void> {
return new Promise<void>((resolve, reject) => {
const promise: ChildProcessPromise = spawn(
'java',
['-jar', path.basename(this.binaryPath), '--port', this.port],
{
cwd: path.dirname(this.binaryPath),
stdio: 'inherit'
}
);
promise.catch(reject);
this.emulator = promise.childProcess;

console.log(`Waiting for emulator to start up ...`);
const timeout = 10; // seconds
const start: number = Date.now();

const wait = (resolve, reject) => {
if (Date.now() - start > timeout * 1000) {
reject(`Emulator not ready after ${timeout}s. Exiting ...`);
} else {
console.log(`Ping emulator at [http://localhost:${this.port}] ...`);
request(`http://localhost:${this.port}`, (error, response) => {
if (error && error.code === 'ECONNREFUSED') {
setTimeout(wait, 1000, resolve, reject);
} else if (response) {
// Database and Firestore emulators will return 400 and 200 respectively.
// As long as we get a response back, it means the emulator is ready.
console.log('Emulator has started up successfully!');
resolve();
} else {
// This should not happen.
reject({ error, response });
}
});
}
};
setTimeout(wait, 1000, resolve, reject);
});
}

tearDown(): void {
if (this.emulator) {
console.log(`Shutting down emulator, pid: [${this.emulator.pid}] ...`);
this.emulator.kill();
}
}
}
32 changes: 32 additions & 0 deletions scripts/emulator-testing/emulators/firestore-emulator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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.
*/

import { Emulator } from './emulator';

export class FirestoreEmulator extends Emulator {
projectId: string;

constructor(port = 8087, projectId = 'test-emulator') {
super(port);
this.projectId = projectId;
this.binaryName = 'firestore-emulator.jar';
// Use locked version of emulator for test to be deterministic.
// The latest version can be found from firestore emulator doc:
// https://firebase.google.com/docs/firestore/security/test-rules-emulator
this.binaryUrl =
'https://storage.googleapis.com/firebase-preview-drop/emulator/cloud-firestore-emulator-v1.2.1.jar';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little concerned about the hard coded url. If we forgot to update the version, we will eventually become using an out dated emulator binary. @ryanpbrewster Is there any way to always get the latest binary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was some discussion before on specifying emulator versions.

The conclusion seemed to be that it's probably better to have a locked version and update the version manually, than to always get the latest version and have risk that test may fail surprisingly with the new version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. That sounds fine to me. Can you please add a comment about it and include a pointer to where the latest emulator can be found?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
}
51 changes: 51 additions & 0 deletions scripts/emulator-testing/firestore-test-runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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.
*/

import { spawn } from 'child-process-promise';
import * as path from 'path';

import { ChildProcessPromise } from './emulators/emulator';
import { FirestoreEmulator } from './emulators/firestore-emulator';

function runTest(port: number, projectId: string): ChildProcessPromise {
const options = {
cwd: path.resolve(__dirname, '../../packages/firestore'),
env: Object.assign({}, process.env, {
FIRESTORE_EMULATOR_PORT: port,
FIRESTORE_EMULATOR_PROJECT_ID: projectId
}),
stdio: 'inherit'
};
// TODO(b/113267261): Include browser test once WebChannel support is
// ready in Firestore emulator.
return spawn('yarn', ['test:node'], options);
}

async function run(): Promise<void> {
const emulator = new FirestoreEmulator();
try {
await emulator.download();
await emulator.setUp();
await runTest(emulator.port, emulator.projectId);
} finally {
await emulator.tearDown();
}
}

run().catch(err => {
console.error(err);
process.exitCode = 1;
});
3 changes: 3 additions & 0 deletions scripts/emulator-testing/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../config/tsconfig.base.json"
}
Loading