-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Adding info on how to wait for the Grid to be ready #773
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
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f3b2ced
starting wait-for-it
ddavison 7459984
starting wait-for-it
ddavison ac5165e
Merge branch 'wait-for-it' of github.com:SeleniumHQ/docker-selenium i…
diemol bfc15bf
Adding small script to check the Grid status, plus docs for HEALTHCHE…
diemol 9bda77d
Fixing typos.
diemol 8278ba0
Adding default vars for script
diemol 3c9a273
Fixing typo
diemol 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
<!-- Thanks for sending us a PR to improve this project! If you are adding a | ||
feature or fixing a bug, and this needs more documentation, please add it to your PR. --> | ||
|
||
|
||
- [ ] By placing an `X` in the preceding checkbox, I verify that I have signed the [Contributor License Agreement](https://github.com/SeleniumHQ/docker-selenium/blob/master/CONTRIBUTING.md#contributing-code-to-selenium) |
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,26 @@ | ||
#!/usr/bin/env bash | ||
# check-grid.sh | ||
|
||
set -e | ||
|
||
# process arguments | ||
while [[ $# -gt 0 ]] | ||
do | ||
case "$1" in | ||
--host) | ||
HOST="$2" | ||
if [[ ${HOST} == "" ]]; then HOST="localhost"; fi | ||
shift 2 | ||
;; | ||
--port) | ||
PORT="$2" | ||
if [[ ${PORT} == "" ]]; then PORT="4444"; fi | ||
shift 2 | ||
;; | ||
*) | ||
echoerr "Unknown argument: $1" | ||
;; | ||
esac | ||
done | ||
|
||
curl -sSL http://${HOST}:${PORT}/wd/hub/status | jq -r '.value.ready' | grep "true" || exit 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
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 |
---|---|---|
|
@@ -269,6 +269,101 @@ $ FF=$(docker run --rm --name=fx \ | |
|
||
_Note: Since a Docker container is not meant to preserve state and spawning a new one takes less than 3 seconds you will likely want to remove containers after each end-to-end test with_ `--rm` _command. You need to think of your Docker containers as single processes, not as running virtual machines, in case you are familiar with [Vagrant](https://www.vagrantup.com/)._ | ||
|
||
## Waiting for the Grid to be ready | ||
|
||
It is a good practice to check first if the Grid is up and ready to receive requests, this can be done by checking the `/wd/hub/status` endpoint. | ||
|
||
A Grid that is ready, composed by a hub and a node, could look like this: | ||
|
||
```json | ||
{ | ||
"status": 0, | ||
"value": { | ||
"ready": true, | ||
"message": "Hub has capacity", | ||
"build": { | ||
"revision": "aacccce0", | ||
"time": "2018-08-02T20:13:22.693Z", | ||
"version": "3.14.0" | ||
}, | ||
"os": { | ||
"arch": "amd64", | ||
"name": "Linux", | ||
"version": "4.9.93-linuxkit-aufs" | ||
}, | ||
"java": { | ||
"version": "1.8.0_181" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
The `"ready": true` value indicates that the Grid is ready to receive requests. This status can be polled through a | ||
script before running any test, or it can be added as a [HEALTHCHECK](https://docs.docker.com/engine/reference/run/#healthcheck) | ||
when the docker container is started. | ||
|
||
### Adding a [HEALTHCHECK](https://docs.docker.com/engine/reference/run/#healthcheck) to the Grid | ||
|
||
The script [check-grid.sh](Base/check-grid.sh), which is included in the images, can be used to poll the Grid status. | ||
|
||
This example checks the status of the Grid every 15 seconds, it has a timeout of 30 seconds when the check is done, | ||
and it retries up to 5 times until the container is marked as unhealthy. Please use adjusted values to fit your needs, | ||
(if needed) replace the `--host` and `--port` parameters for the ones used in your environment. | ||
|
||
``` bash | ||
$ docker network create grid | ||
$ docker run -d -p 4444:4444 --net grid --name selenium-hub \ | ||
--health-cmd='/opt/bin/check-grid.sh --host 0.0.0.0 --port 4444' \ | ||
--health-interval=15s --health-timeout=30s --health-retries=5 \ | ||
selenium/hub:3.14.0-arsenic | ||
$ docker run -d --net grid -e HUB_HOST=selenium-hub -v /dev/shm:/dev/shm selenium/node-chrome:3.14.0-arsenic | ||
$ docker run -d --net grid -e HUB_HOST=selenium-hub -v /dev/shm:/dev/shm selenium/node-firefox:3.14.0-arsenic | ||
``` | ||
**Note:** The `\` line delimiter won't work on Windows based terminals, try either `^` or a backtick. | ||
|
||
The container health status can be checked by doing `docker ps` and verifying the `(healthy)|(unhealthy)` status or by | ||
inspecting it in the following way: | ||
|
||
```bash | ||
$ docker inspect --format='{{json .State.Health.Status}}' selenium-hub | ||
"healthy" | ||
``` | ||
|
||
### Using a bash script to wait for the Grid | ||
|
||
A common problem known in docker is that a running container does not always mean that the application inside it is ready. | ||
A simple way to tackle this is by using a "wait-for-it" script, more information can be seen [here](https://docs.docker.com/compose/startup-order/). | ||
|
||
The following script is an example of how this can be done using bash, but the same principle applies if you want to do this with the programming language used to write the tests. | ||
|
||
```bash | ||
#!/bin/bash | ||
# wait-for-grid.sh | ||
|
||
set -e | ||
|
||
cmd="$@" | ||
|
||
while ! curl -sSL "http://localhost:4444/wd/hub/status" 2>&1 \ | ||
| jq -r '.value.ready' 2>&1 | grep "true" >/dev/null; do | ||
echo 'Waiting for the Grid' | ||
sleep 1 | ||
done | ||
|
||
>&2 echo "Selenium Grid is up - executing tests" | ||
exec $cmd | ||
``` | ||
**Note:** If needed, replace `localhost` and `4444` for the correct values in your environment. Also, this script is polling indefinitely, you might want | ||
to teak it and establish a timeout. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/teak/tweak/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @cgoldberg |
||
|
||
Let's say that the normal command to execute your tests is `mvn clean test`. Here is a way to use the above script and execute your tests: | ||
|
||
```bash | ||
$ ./wait-for-grid.sh mvn clean test | ||
``` | ||
|
||
Like this, the script will poll until the Grid is ready, and then your tests will start. | ||
|
||
## Debugging | ||
|
||
In the event you wish to visually see what the browser is doing you will want to run the `debug` variant of node or standalone images. A VNC server will run on port 5900. You are free to map that to any free external port that you wish. Keep in mind that you will only be able to run one node per port so if you wish to include a second node, or more, you will have to use different ports, the 5900 as the internal port will have to remain the same though as thats the VNC service on the node. The second example below shows how to run multiple nodes and with different VNC ports open: | ||
|
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.
Could you do
HOST="${2:-localhost}"
?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.
Good idea, I was just copying and pasting from other script. Just made the changes and I will push them now. Thanks @tnguyen14