-
Notifications
You must be signed in to change notification settings - Fork 940
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
Add code for managing emulators and running database/firestore emulator tests via yarn commands. #1435
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
df00b9f
Refactor code for managing database/firestore emulators.
yifanyang c6a2fd8
Intentionally allow one console.log statement in test.
yifanyang ba584ac
address feedback
yifanyang 8cc5e62
address feedback
yifanyang c10402b
fix
yifanyang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
); | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../config/tsconfig.base.json" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.