Skip to content

Commit 1b2786e

Browse files
committed
Refactoring, tests and fix for SERVERNUM.
- Add a common place for functions to be used in entry_point.sh scripts. - Refactor the calls to "echo $DISPLAY | sed..." into a function named get_server_num. - Add tests for this function. - Fix regex used in sed call.
1 parent e961ce7 commit 1b2786e

File tree

15 files changed

+120
-12
lines changed

15 files changed

+120
-12
lines changed

NodeBase/Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ RUN apt-get update -qqy \
2323
#==============================
2424
# Scripts to run Selenium Node
2525
#==============================
26-
COPY entry_point.sh /opt/bin/entry_point.sh
26+
COPY \
27+
entrypoint.sh \
28+
functions.sh \
29+
/opt/bin/
2730
RUN chmod +x /opt/bin/entry_point.sh
2831

2932
#============================

NodeBase/Dockerfile.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ RUN apt-get update -qqy \
2222
#==============================
2323
# Scripts to run Selenium Node
2424
#==============================
25-
COPY entry_point.sh /opt/bin/entry_point.sh
25+
COPY \
26+
entry_point.sh \
27+
functions.sh \
28+
/opt/bin/
2629
RUN chmod +x /opt/bin/entry_point.sh
2730

2831
#============================

NodeBase/entry_point.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/bin/bash
2+
3+
source /opt/bin/functions.sh
4+
25
export GEOMETRY="$SCREEN_WIDTH""x""$SCREEN_HEIGHT""x""$SCREEN_DEPTH"
36

47
if [ ! -e /opt/selenium/config.json ]; then
@@ -28,7 +31,7 @@ fi
2831

2932
# TODO: Look into http://www.seleniumhq.org/docs/05_selenium_rc.jsp#browser-side-logs
3033

31-
SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
34+
SERVERNUM=$(get_server_num)
3235
xvfb-run -n $SERVERNUM --server-args="-screen 0 $GEOMETRY -ac +extension RANDR" \
3336
java ${JAVA_OPTS} -jar /opt/selenium/selenium-server-standalone.jar \
3437
-role node \

NodeBase/functions.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
# https://github.com/SeleniumHQ/docker-selenium/issues/184
4+
function get_server_num() {
5+
echo $(echo $DISPLAY | sed -r -e 's/([^:]+)?:([0-9]+)(\.[0-9]+)?/\2/')
6+
}
7+

NodeBase/test-functions.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bats
2+
3+
source "$BATS_TEST_DIRNAME"/functions.sh
4+
5+
# Tests for function get_server_num
6+
#
7+
# Test data from http://askubuntu.com/questions/432255/what-is-display-environment-variable
8+
@test 'get_server_num of :99.1' {
9+
10+
export DISPLAY=':99.1'
11+
expected_result='99'
12+
result="$(get_server_num)"
13+
echo "result: $result"
14+
[ "$result" == "$expected_result" ]
15+
}
16+
17+
@test 'get_server_num of :0' {
18+
19+
export DISPLAY=':0'
20+
expected_result='0'
21+
result="$(get_server_num)"
22+
echo "result: $result"
23+
[ "$result" == "$expected_result" ]
24+
}
25+
26+
@test 'get_server_num of localhost:4' {
27+
28+
export DISPLAY='localhost:4'
29+
expected_result='4'
30+
result="$(get_server_num)"
31+
echo "result: $result"
32+
[ "$result" == "$expected_result" ]
33+
}
34+
35+
@test 'get_server_num of google.com:0' {
36+
37+
export DISPLAY='google.com:0'
38+
expected_result='0'
39+
result="$(get_server_num)"
40+
echo "result: $result"
41+
[ "$result" == "$expected_result" ]
42+
}
43+
44+
@test 'get_server_num of google.com:99.1' {
45+
46+
export DISPLAY='google.com:99.1'
47+
expected_result='99'
48+
result="$(get_server_num)"
49+
echo "result: $result"
50+
[ "$result" == "$expected_result" ]
51+
}
52+

NodeChromeDebug/entry_point.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/bin/bash
2+
3+
source /opt/bin/functions.sh
4+
25
export GEOMETRY="$SCREEN_WIDTH""x""$SCREEN_HEIGHT""x""$SCREEN_DEPTH"
36

47
if [ ! -e /opt/selenium/config.json ]; then
@@ -28,7 +31,7 @@ fi
2831

2932
# TODO: Look into http://www.seleniumhq.org/docs/05_selenium_rc.jsp#browser-side-logs
3033

31-
SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
34+
SERVERNUM=$(get_server_num)
3235
env | cut -f 1 -d "=" | sort > asroot
3336
sudo -E -u seluser -i env | cut -f 1 -d "=" | sort > asseluser
3437
sudo -E -i -u seluser \

NodeFirefoxDebug/entry_point.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/bin/bash
2+
3+
source /opt/bin/functions.sh
4+
25
export GEOMETRY="$SCREEN_WIDTH""x""$SCREEN_HEIGHT""x""$SCREEN_DEPTH"
36

47
if [ ! -e /opt/selenium/config.json ]; then
@@ -28,7 +31,7 @@ fi
2831

2932
# TODO: Look into http://www.seleniumhq.org/docs/05_selenium_rc.jsp#browser-side-logs
3033

31-
SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
34+
SERVERNUM=$(get_server_num)
3235
env | cut -f 1 -d "=" | sort > asroot
3336
sudo -E -u seluser -i env | cut -f 1 -d "=" | sort > asseluser
3437
sudo -E -i -u seluser \

Standalone/entry_point.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/bin/bash
2+
3+
source /opt/bin/functions.sh
4+
25
export GEOMETRY="$SCREEN_WIDTH""x""$SCREEN_HEIGHT""x""$SCREEN_DEPTH"
36

47
function shutdown {
@@ -10,7 +13,7 @@ if [ ! -z "$SE_OPTS" ]; then
1013
echo "appending selenium options: ${SE_OPTS}"
1114
fi
1215

13-
SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
16+
SERVERNUM=$(get_server_num)
1417
xvfb-run -n $SERVERNUM --server-args="-screen 0 $GEOMETRY -ac +extension RANDR" \
1518
java ${JAVA_OPTS} -jar /opt/selenium/selenium-server-standalone.jar \
1619
${SE_OPTS} &

StandaloneChrome/entry_point.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/bin/bash
2+
3+
source /opt/bin/functions.sh
4+
25
export GEOMETRY="$SCREEN_WIDTH""x""$SCREEN_HEIGHT""x""$SCREEN_DEPTH"
36

47
function shutdown {
@@ -10,7 +13,7 @@ if [ ! -z "$SE_OPTS" ]; then
1013
echo "appending selenium options: ${SE_OPTS}"
1114
fi
1215

13-
SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
16+
SERVERNUM=$(get_server_num)
1417
xvfb-run -n $SERVERNUM --server-args="-screen 0 $GEOMETRY -ac +extension RANDR" \
1518
java ${JAVA_OPTS} -jar /opt/selenium/selenium-server-standalone.jar \
1619
${SE_OPTS} &

StandaloneChromeDebug/entry_point.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
2-
source "/opt/bin/clear_x_locks.sh"
2+
3+
source /opt/bin/clear_x_locks.sh
4+
source /opt/bin/functions.sh
35

46
export GEOMETRY="$SCREEN_WIDTH""x""$SCREEN_HEIGHT""x""$SCREEN_DEPTH"
57

@@ -12,7 +14,7 @@ if [ ! -z "$SE_OPTS" ]; then
1214
echo "appending selenium options: ${SE_OPTS}"
1315
fi
1416

15-
SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
17+
SERVERNUM=$(get_server_num)
1618
env | cut -f 1 -d "=" | sort > asroot
1719
sudo -E -u seluser -i env | cut -f 1 -d "=" | sort > asseluser
1820
sudo -E -i -u seluser \

StandaloneDebug/entry_point.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/bin/bash
2+
3+
source /opt/bin/functions.sh
4+
25
export GEOMETRY="$SCREEN_WIDTH""x""$SCREEN_HEIGHT""x""$SCREEN_DEPTH"
36

47
function shutdown {
@@ -10,7 +13,7 @@ if [ ! -z "$SE_OPTS" ]; then
1013
echo "appending selenium options: ${SE_OPTS}"
1114
fi
1215

13-
SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
16+
SERVERNUM=$(get_server_num)
1417
env | cut -f 1 -d "=" | sort > asroot
1518
sudo -E -u seluser -i env | cut -f 1 -d "=" | sort > asseluser
1619
sudo -E -i -u seluser \

StandaloneFirefox/entry_point.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/bin/bash
2+
3+
source /opt/bin/functions.sh
4+
25
export GEOMETRY="$SCREEN_WIDTH""x""$SCREEN_HEIGHT""x""$SCREEN_DEPTH"
36

47
function shutdown {
@@ -10,7 +13,7 @@ if [ ! -z "$SE_OPTS" ]; then
1013
echo "appending selenium options: ${SE_OPTS}"
1114
fi
1215

13-
SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
16+
SERVERNUM=$(get_server_num)
1417
xvfb-run -n $SERVERNUM --server-args="-screen 0 $GEOMETRY -ac +extension RANDR" \
1518
java ${JAVA_OPTS} -jar /opt/selenium/selenium-server-standalone.jar \
1619
${SE_OPTS} &

StandaloneFirefoxDebug/entry_point.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/bin/bash
2+
3+
source /opt/bin/functions.sh
4+
25
export GEOMETRY="$SCREEN_WIDTH""x""$SCREEN_HEIGHT""x""$SCREEN_DEPTH"
36

47
function shutdown {
@@ -10,7 +13,7 @@ if [ ! -z "$SE_OPTS" ]; then
1013
echo "appending selenium options: ${SE_OPTS}"
1114
fi
1215

13-
SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
16+
SERVERNUM=$(get_server_num)
1417
env | cut -f 1 -d "=" | sort > asroot
1518
sudo -E -u seluser -i env | cut -f 1 -d "=" | sort > asseluser
1619
sudo -E -i -u seluser \

circle.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ machine:
33
- docker
44
environment:
55
DELETE_CONTAINERS: false
6+
67
dependencies:
78
override:
9+
# Install bats for testing shell scripts.
10+
- git clone https://github.com/sstephenson/bats.git && cd bats && sudo ./install.sh /usr/local
811
- docker info
912
- make build
1013

test.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ if [ -n "$1" ] && [ $1 == 'debug' ]; then
55
DEBUG='-debug'
66
fi
77

8+
# Due to the dependency GNU sed, we're skipping this part when running
9+
# on Mac OS X.
10+
if [ "$(uname)" != 'Darwin' ] ; then
11+
echo 'Testing shell functions...'
12+
which bats > /dev/null 2>&1
13+
if [ $? -ne 0 ] ; then
14+
echo "Could not find 'bats'. Please install it first, e.g., following https://github.com/sstephenson/bats#installing-bats-from-source."
15+
exit 1
16+
fi
17+
NodeBase/test-functions.sh || exit 1
18+
else
19+
echo 'Skipping shell functions test on Mac OS X.'
20+
fi
21+
822
echo Building test container image
923
docker build -t selenium/test:local ./Test
1024

0 commit comments

Comments
 (0)