Skip to content

Commit bfc15bf

Browse files
committed
Adding small script to check the Grid status, plus docs for HEALTHCHECK, fixes #691
1 parent ac5165e commit bfc15bf

File tree

9 files changed

+144
-187
lines changed

9 files changed

+144
-187
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
## Meta -
2+
3+
<!-- Please fill out the whole template so we have enough information to help you fix
4+
the issue as fast as possible. -->
5+
6+
<!-- If the template is not complete or deleted, we might close the issue right away
7+
since we will lack the needed information to troubleshoot it. -->
8+
29
Image(s):
310
<!-- node-chrome? hub? standalone-firefox? -->
411
Docker-Selenium Image Version(s):
@@ -21,3 +28,9 @@ https://bugzilla.mozilla.org/buglist.cgi?product=Testing&component=Marionette
2128
## Expected Behavior -
2229

2330
## Actual Behavior -
31+
32+
## Steps to reproduce -
33+
<!--
34+
Please be sure to include an SSCCE (Short, Self Contained, Correct [compilable] example) http://sscce.org/
35+
If you can't provide a link to the page, consider creating a reproducible page on https://jsfiddle.net/
36+
-->

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
<!-- Thanks for sending us a PR to improve this project! If you are adding a
2+
feature or fixing a bug, and this needs more documentation, please add it to your PR. -->
3+
4+
15
- [ ] 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)

Base/Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ RUN apt-get -qqy update \
2525
sudo \
2626
unzip \
2727
wget \
28+
jq \
29+
curl \
2830
&& rm -rf /var/lib/apt/lists/* /var/cache/apt/* \
2931
&& sed -i 's/securerandom\.source=file:\/dev\/random/securerandom\.source=file:\/dev\/urandom/' ./usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security/java.security
3032

@@ -54,8 +56,9 @@ RUN mkdir -p /opt/bin
5456
#======================================
5557
# Add intermediate service discovery
5658
#======================================
57-
COPY wait-for-it.sh \
59+
COPY check-grid.sh \
5860
/opt/bin/
61+
RUN chmod +x /opt/bin/check-grid.sh
5962

6063
#===================================================
6164
# Run the following commands as non-privileged user

Base/check-grid.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
# check-grid.sh
3+
4+
set -e
5+
6+
# process arguments
7+
while [[ $# -gt 0 ]]
8+
do
9+
case "$1" in
10+
--host)
11+
HOST="$2"
12+
if [[ ${HOST} == "" ]]; then HOST="localhost"; fi
13+
shift 2
14+
;;
15+
--port)
16+
PORT="$2"
17+
if [[ ${PORT} == "" ]]; then PORT="4444"; fi
18+
shift 2
19+
;;
20+
*)
21+
echoerr "Unknown argument: $1"
22+
;;
23+
esac
24+
done
25+
26+
curl -sSL http://${HOST}:${PORT}/wd/hub/status | jq -r '.value.ready' | grep "true" || exit 1

Base/wait-for-it.sh

Lines changed: 0 additions & 177 deletions
This file was deleted.

Hub/Dockerfile.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ ENV GRID_HUB_HOST "0.0.0.0"
3030
COPY generate_config \
3131
entry_point.sh \
3232
/opt/bin/
33-
# Running this command as sudo just to avoid the message:
34-
# To run a command as administrator (user "root"), use "sudo <command>". See "man sudo_root" for details.
35-
# When logging into the container
33+
3634
RUN /opt/bin/generate_config > /opt/selenium/config.json
3735

3836
CMD ["/opt/bin/entry_point.sh"]

Hub/entry_point.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,5 @@ java ${JAVA_OPTS} -jar /opt/selenium/selenium-server-standalone.jar \
2525
${SE_OPTS} &
2626
NODE_PID=$!
2727

28-
/opt/bin/wait-for-it.sh -h 127.0.0.1 -p $GRID_HUB_PORT -t 30 -- echo "Hub is up and running."
29-
3028
trap shutdown SIGTERM SIGINT
3129
wait $NODE_PID

NodeBase/entry_point.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ SERVERNUM=$(get_server_num)
4646

4747
rm -f /tmp/.X*lock
4848

49-
/opt/bin/wait-for-it.sh -h $HUB_PORT_4444_TCP_ADDR -p $HUB_PORT_4444_TCP_PORT -t 30 -s -- echo "Hub is ready for connections"
50-
5149
xvfb-run -n $SERVERNUM --server-args="-screen 0 $GEOMETRY -ac +extension RANDR" \
5250
java ${JAVA_OPTS} -jar /opt/selenium/selenium-server-standalone.jar \
5351
-role node \
@@ -57,7 +55,5 @@ xvfb-run -n $SERVERNUM --server-args="-screen 0 $GEOMETRY -ac +extension RANDR"
5755
${SE_OPTS} &
5856
NODE_PID=$!
5957

60-
/opt/bin/wait-for-it.sh -h 127.0.0.1 -p $NODE_PORT -t 30 -s -- echo "Node is up."
61-
6258
trap shutdown SIGTERM SIGINT
6359
wait $NODE_PID

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,102 @@ $ FF=$(docker run --rm --name=fx \
269269

270270
_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/)._
271271

272+
## Waiting for the Grid to be ready
273+
274+
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.
275+
276+
A Grid that is ready, composed by a hub and a node, could look like this:
277+
278+
```json
279+
{
280+
"status": 0,
281+
"value": {
282+
"ready": true,
283+
"message": "Hub has capacity",
284+
"build": {
285+
"revision": "aacccce0",
286+
"time": "2018-08-02T20:13:22.693Z",
287+
"version": "3.14.0"
288+
},
289+
"os": {
290+
"arch": "amd64",
291+
"name": "Linux",
292+
"version": "4.9.93-linuxkit-aufs"
293+
},
294+
"java": {
295+
"version": "1.8.0_181"
296+
}
297+
}
298+
}
299+
```
300+
301+
The `"ready": true` value indicates that the Grid is ready to receive requests. This status can be polled through a
302+
script before running any test, or it can be added as a [HEALTHCHECK](https://docs.docker.com/engine/reference/run/#healthcheck)
303+
when the docker container is started.
304+
305+
### Adding a [HEALTHCHECK](https://docs.docker.com/engine/reference/run/#healthcheck) to the Grid
306+
307+
The script [check-grid.sh](Base/check-grid.sh), which is included in the images, can be used to poll the Grid status.
308+
309+
This example checks the status of the Grid every 15 seconds, with has a timeout of 30 seconds when the check is done,
310+
and it retries up to 5 times until the container is marked as unhealthy. Please use values that adjust to your needs,
311+
additionally (if needed) replace the `--host` and `--port` parameters for the ones you have in your configuration.
312+
313+
``` bash
314+
$ docker network create grid
315+
$ docker run -d -p 4444:4444 --net grid --name selenium-hub \
316+
--health-cmd='/opt/bin/check-grid.sh --host 0.0.0.0 --port 4444' \
317+
--health-interval=15s --health-timeout=30s --health-retries=5 \
318+
selenium/hub:3.14.0-arsenic
319+
$ docker run -d --net grid -e HUB_HOST=selenium-hub -v /dev/shm:/dev/shm selenium/node-chrome:3.14.0-arsenic
320+
$ docker run -d --net grid -e HUB_HOST=selenium-hub -v /dev/shm:/dev/shm selenium/node-firefox:3.14.0-arsenic
321+
```
322+
**Note:** The `\` line delimiter won't work on Windows based terminals, try either `^` or `\``.
323+
324+
The container health status can be checked by doing `docker ps` and verifying the `(healthy)|(unhealthy)` status or by
325+
inspecting it in the following way:
326+
327+
```bash
328+
$ docker inspect --format='{{json .State.Health.Status}}' selenium-hub
329+
"healthy"
330+
```
331+
332+
### Using a bash script to wait for the Grid
333+
334+
A common problem known in docker is that a running container does not always mean that the application inside it is ready.
335+
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/).
336+
337+
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 in a method implemented
338+
in the programming language used to run the tests.
339+
340+
```bash
341+
#!/bin/bash
342+
# wait-for-grid.sh
343+
344+
set -e
345+
346+
cmd="$@"
347+
348+
while ! curl -sSL "http://localhost:4444/wd/hub/status" 2>&1 \
349+
| jq -r '.value.ready' 2>&1 | grep "true" >/dev/null; do
350+
echo 'Waiting for the Grid'
351+
sleep 1
352+
done
353+
354+
>&2 echo "Selenium Grid is up - executing tests"
355+
exec $cmd
356+
```
357+
**Note:** If needed, replace `localhost` and `4444` for the correct values in your environment. Also, this script is polling indefinitely, you might want
358+
to teak it and establish a timeout.
359+
360+
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:
361+
362+
```bash
363+
$ ./wait-for-grid.sh mvn clean test
364+
```
365+
366+
Like this, the script will poll until the Grid is ready, and then your tests will start.
367+
272368
## Debugging
273369

274370
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:

0 commit comments

Comments
 (0)