-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add integration test for http server
To keep our repository well-tested, this commit introduces an e2e test for the http server rule. This is usually not done for http servers/rules within Bazel, but it feels valuable and also serves as a great example of how Chromium tests could work within Bazel, with RBE etc.
- Loading branch information
1 parent
b066f7b
commit 3548719
Showing
8 changed files
with
205 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,98 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {runfiles} from '@bazel/runfiles'; | ||
import {Builder, By, WebDriver} from 'selenium-webdriver'; | ||
import {Options as ChromeOptions, ServiceBuilder} from 'selenium-webdriver/chrome'; | ||
|
||
import * as waitOn from 'wait-on'; | ||
import * as childProcess from 'child_process'; | ||
|
||
/** | ||
* Test script that will start the test http server binary in a background process. | ||
* Once the server is available and listening, Chromium from `bazel/browsers` is | ||
* launched through Selenium to ensure that the environment variable inlining, | ||
* actual resolution of JavaScript resources and `index.html` works as expected. | ||
*/ | ||
async function runTest() { | ||
const [chromiumRootpath, chromedriverRootpath] = process.argv.slice(2); | ||
|
||
// Resolve chromium, chromedriver and the server binary to disk paths. | ||
const chromiumPath = runfiles.resolveWorkspaceRelative(chromiumRootpath); | ||
const chromedriverPath = runfiles.resolveWorkspaceRelative(chromedriverRootpath); | ||
const serverBinPath = runfiles.resolveWorkspaceRelative('bazel/http-server/test/server'); | ||
|
||
const serverPort = 1234; | ||
const serverHost = `127.0.0.1:${serverPort}`; | ||
|
||
// Start test http server in background | ||
const serverProcess = childProcess.spawn(serverBinPath, ['--port', `${serverPort}`], { | ||
env: {...process.env, GOOGLE_MAPS_API_KEY: 'myPersonalSecret'}, | ||
stdio: 'inherit', | ||
}); | ||
|
||
// Ensure the process gets killed, if the test terminates early. | ||
process.on('exit', () => serverProcess.kill()); | ||
|
||
// Keep track of potentially launched webdriver instance, so that | ||
// we can kill it when the test code errors unexpectedly. | ||
let driver: WebDriver | null = null; | ||
|
||
try { | ||
// Wait for server to be ready, regardless of status code (404/200 or else) | ||
await waitOn({ | ||
resources: [`http-get://${serverHost}`], | ||
headers: { | ||
'accept': 'text/html', | ||
}, | ||
}); | ||
|
||
const service = new ServiceBuilder(chromedriverPath); | ||
const options = new ChromeOptions() | ||
.setChromeBinaryPath(chromiumPath) | ||
.headless() | ||
.addArguments('--no-sandbox'); | ||
|
||
driver = await new Builder() | ||
.forBrowser('chrome') | ||
.setChromeOptions(options) | ||
.setChromeService(service) | ||
.build(); | ||
|
||
await driver.get(`http://${serverHost}`); | ||
|
||
let bodyText = await driver.findElement(By.css('body')).getText(); | ||
|
||
// Assert that the variable is inlined, and that the `index.html` renders. | ||
if (bodyText !== 'Works My key: myPersonalSecret') { | ||
throw Error(`Unexpected body: ${bodyText}`); | ||
} | ||
|
||
console.log('Valid text for index file: ', bodyText); | ||
|
||
await driver.get(`http://${serverHost}/not-found.txt`); | ||
|
||
bodyText = await driver.findElement(By.css('body')).getText(); | ||
|
||
if (bodyText !== 'Not found - Error 404') { | ||
throw Error(`Unexpected text when requesting unknown resource: ${bodyText}`); | ||
} | ||
|
||
console.log('Valid text for unknown resource:', bodyText); | ||
} finally { | ||
await driver?.quit(); | ||
|
||
// Kill server process if we exit normally (no script termination). | ||
serverProcess.kill(); | ||
} | ||
} | ||
|
||
runTest().catch((e) => { | ||
console.error(e); | ||
process.exit(1); | ||
}); |
This file contains 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 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 |
---|---|---|
|
@@ -1206,6 +1206,18 @@ | |
resolved "https://registry.yarnpkg.com/@gar/promisify/-/promisify-1.1.2.tgz#30aa825f11d438671d585bd44e7fd564535fc210" | ||
integrity sha512-82cpyJyKRoQoRi+14ibCeGPu0CwypgtBAdBhq1WfvagpCZNKqwXbKwXllYSMG91DhmG4jt9gN8eP6lGOtozuaw== | ||
|
||
"@hapi/hoek@^9.0.0": | ||
version "9.2.1" | ||
resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-9.2.1.tgz#9551142a1980503752536b5050fd99f4a7f13b17" | ||
integrity sha512-gfta+H8aziZsm8pZa0vj04KO6biEiisppNgA1kbJvFrrWu9Vm7eaUEy76DIxsuTaWvti5fkJVhllWc6ZTE+Mdw== | ||
|
||
"@hapi/topo@^5.0.0": | ||
version "5.1.0" | ||
resolved "https://registry.yarnpkg.com/@hapi/topo/-/topo-5.1.0.tgz#dc448e332c6c6e37a4dc02fd84ba8d44b9afb012" | ||
integrity sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg== | ||
dependencies: | ||
"@hapi/hoek" "^9.0.0" | ||
|
||
"@istanbuljs/load-nyc-config@^1.0.0": | ||
version "1.1.0" | ||
resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced" | ||
|
@@ -1573,6 +1585,23 @@ | |
colors "~1.2.1" | ||
string-argv "~0.3.1" | ||
|
||
"@sideway/address@^4.1.3": | ||
version "4.1.3" | ||
resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.3.tgz#d93cce5d45c5daec92ad76db492cc2ee3c64ab27" | ||
integrity sha512-8ncEUtmnTsMmL7z1YPB47kPUq7LpKWJNFPsRzHiIajGC5uXlWGn+AmkYPcHNl8S4tcEGx+cnORnNYaw2wvL+LQ== | ||
dependencies: | ||
"@hapi/hoek" "^9.0.0" | ||
|
||
"@sideway/formula@^3.0.0": | ||
version "3.0.0" | ||
resolved "https://registry.yarnpkg.com/@sideway/formula/-/formula-3.0.0.tgz#fe158aee32e6bd5de85044be615bc08478a0a13c" | ||
integrity sha512-vHe7wZ4NOXVfkoRb8T5otiENVlT7a3IAiw7H5M2+GO+9CDgcVUUsX1zalAztCmwyOr2RUTGJdgB+ZvSVqmdHmg== | ||
|
||
"@sideway/pinpoint@^2.0.0": | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/@sideway/pinpoint/-/pinpoint-2.0.0.tgz#cff8ffadc372ad29fd3f78277aeb29e632cc70df" | ||
integrity sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ== | ||
|
||
"@tootallnate/once@2": | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-2.0.0.tgz#f544a148d3ab35801c1f633a7441fd87c2e484bf" | ||
|
@@ -1966,6 +1995,13 @@ | |
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.4.tgz#bd86a43617df0594787d38b735f55c805becf1bc" | ||
integrity sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw== | ||
|
||
"@types/wait-on@^5.3.1": | ||
version "5.3.1" | ||
resolved "https://registry.yarnpkg.com/@types/wait-on/-/wait-on-5.3.1.tgz#bc5520d1d8b90b9caab1bef23315685ded73320d" | ||
integrity sha512-2FFOKCF/YydrMUaqg+fkk49qf0e5rDgwt6aQsMzFQzbS419h2gNOXyiwp/o2yYy27bi/C1z+HgfncryjGzlvgQ== | ||
dependencies: | ||
"@types/node" "*" | ||
|
||
"@types/which@^2.0.1": | ||
version "2.0.1" | ||
resolved "https://registry.yarnpkg.com/@types/which/-/which-2.0.1.tgz#27ecd67f915b7c3d6ba552135bb1eecd66e63501" | ||
|
@@ -2463,7 +2499,7 @@ aws4@^1.8.0: | |
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.11.0.tgz#d61f46d83b2519250e2784daf5b09479a8b41c59" | ||
integrity sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA== | ||
|
||
[email protected]: | ||
[email protected], axios@^0.21.1: | ||
version "0.21.4" | ||
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575" | ||
integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg== | ||
|
@@ -5057,6 +5093,17 @@ jju@~1.4.0: | |
resolved "https://registry.yarnpkg.com/jju/-/jju-1.4.0.tgz#a3abe2718af241a2b2904f84a625970f389ae32a" | ||
integrity sha1-o6vicYryQaKykE+EpiWXDzia4yo= | ||
|
||
joi@^17.4.0: | ||
version "17.5.0" | ||
resolved "https://registry.yarnpkg.com/joi/-/joi-17.5.0.tgz#7e66d0004b5045d971cf416a55fb61d33ac6e011" | ||
integrity sha512-R7hR50COp7StzLnDi4ywOXHrBrgNXuUUfJWIR5lPY5Bm/pOD3jZaTwpluUXVLRWcoWZxkrHBBJ5hLxgnlehbdw== | ||
dependencies: | ||
"@hapi/hoek" "^9.0.0" | ||
"@hapi/topo" "^5.0.0" | ||
"@sideway/address" "^4.1.3" | ||
"@sideway/formula" "^3.0.0" | ||
"@sideway/pinpoint" "^2.0.0" | ||
|
||
js-tokens@^4.0.0: | ||
version "4.0.0" | ||
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" | ||
|
@@ -6975,7 +7022,7 @@ rxjs@^5.5.6: | |
dependencies: | ||
symbol-observable "1.0.1" | ||
|
||
rxjs@^7.2.0, rxjs@^7.4.0: | ||
rxjs@^7.1.0, rxjs@^7.2.0, rxjs@^7.4.0: | ||
version "7.5.2" | ||
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.5.2.tgz#11e4a3a1dfad85dbf7fb6e33cbba17668497490b" | ||
integrity sha512-PwDt186XaL3QN5qXj/H9DGyHhP3/RYYgZZwqBv9Tv8rsAaiwFH1IsJJlcgD37J7UW5a6O67qX0KWKS3/pu0m4w== | ||
|
@@ -8093,6 +8140,17 @@ w3c-xmlserializer@^3.0.0: | |
dependencies: | ||
xml-name-validator "^4.0.0" | ||
|
||
wait-on@^6.0.0: | ||
version "6.0.0" | ||
resolved "https://registry.yarnpkg.com/wait-on/-/wait-on-6.0.0.tgz#7e9bf8e3d7fe2daecbb7a570ac8ca41e9311c7e7" | ||
integrity sha512-tnUJr9p5r+bEYXPUdRseolmz5XqJTTj98JgOsfBn7Oz2dxfE2g3zw1jE+Mo8lopM3j3et/Mq1yW7kKX6qw7RVw== | ||
dependencies: | ||
axios "^0.21.1" | ||
joi "^17.4.0" | ||
lodash "^4.17.21" | ||
minimist "^1.2.5" | ||
rxjs "^7.1.0" | ||
|
||
watchpack@^2.3.1: | ||
version "2.3.1" | ||
resolved "https://registry.yarnpkg.com/watchpack/-/watchpack-2.3.1.tgz#4200d9447b401156eeca7767ee610f8809bc9d25" | ||
|