Skip to content

Commit

Permalink
Refactoring, tests and fix for SERVERNUM.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
kayabendroth committed Apr 17, 2016
1 parent e961ce7 commit 1b2786e
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 12 deletions.
5 changes: 4 additions & 1 deletion NodeBase/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ RUN apt-get update -qqy \
#==============================
# Scripts to run Selenium Node
#==============================
COPY entry_point.sh /opt/bin/entry_point.sh
COPY \
entrypoint.sh \
functions.sh \
/opt/bin/
RUN chmod +x /opt/bin/entry_point.sh

#============================
Expand Down
5 changes: 4 additions & 1 deletion NodeBase/Dockerfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ RUN apt-get update -qqy \
#==============================
# Scripts to run Selenium Node
#==============================
COPY entry_point.sh /opt/bin/entry_point.sh
COPY \
entry_point.sh \
functions.sh \
/opt/bin/
RUN chmod +x /opt/bin/entry_point.sh

#============================
Expand Down
5 changes: 4 additions & 1 deletion NodeBase/entry_point.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash

source /opt/bin/functions.sh

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

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

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

SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
SERVERNUM=$(get_server_num)
xvfb-run -n $SERVERNUM --server-args="-screen 0 $GEOMETRY -ac +extension RANDR" \
java ${JAVA_OPTS} -jar /opt/selenium/selenium-server-standalone.jar \
-role node \
Expand Down
7 changes: 7 additions & 0 deletions NodeBase/functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# https://github.com/SeleniumHQ/docker-selenium/issues/184
function get_server_num() {
echo $(echo $DISPLAY | sed -r -e 's/([^:]+)?:([0-9]+)(\.[0-9]+)?/\2/')
}

52 changes: 52 additions & 0 deletions NodeBase/test-functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bats

source "$BATS_TEST_DIRNAME"/functions.sh

# Tests for function get_server_num
#
# Test data from http://askubuntu.com/questions/432255/what-is-display-environment-variable
@test 'get_server_num of :99.1' {

export DISPLAY=':99.1'
expected_result='99'
result="$(get_server_num)"
echo "result: $result"
[ "$result" == "$expected_result" ]
}

@test 'get_server_num of :0' {

export DISPLAY=':0'
expected_result='0'
result="$(get_server_num)"
echo "result: $result"
[ "$result" == "$expected_result" ]
}

@test 'get_server_num of localhost:4' {

export DISPLAY='localhost:4'
expected_result='4'
result="$(get_server_num)"
echo "result: $result"
[ "$result" == "$expected_result" ]
}

@test 'get_server_num of google.com:0' {

export DISPLAY='google.com:0'
expected_result='0'
result="$(get_server_num)"
echo "result: $result"
[ "$result" == "$expected_result" ]
}

@test 'get_server_num of google.com:99.1' {

export DISPLAY='google.com:99.1'
expected_result='99'
result="$(get_server_num)"
echo "result: $result"
[ "$result" == "$expected_result" ]
}

5 changes: 4 additions & 1 deletion NodeChromeDebug/entry_point.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash

source /opt/bin/functions.sh

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

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

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

SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
SERVERNUM=$(get_server_num)
env | cut -f 1 -d "=" | sort > asroot
sudo -E -u seluser -i env | cut -f 1 -d "=" | sort > asseluser
sudo -E -i -u seluser \
Expand Down
5 changes: 4 additions & 1 deletion NodeFirefoxDebug/entry_point.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash

source /opt/bin/functions.sh

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

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

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

SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
SERVERNUM=$(get_server_num)
env | cut -f 1 -d "=" | sort > asroot
sudo -E -u seluser -i env | cut -f 1 -d "=" | sort > asseluser
sudo -E -i -u seluser \
Expand Down
5 changes: 4 additions & 1 deletion Standalone/entry_point.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash

source /opt/bin/functions.sh

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

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

SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
SERVERNUM=$(get_server_num)
xvfb-run -n $SERVERNUM --server-args="-screen 0 $GEOMETRY -ac +extension RANDR" \
java ${JAVA_OPTS} -jar /opt/selenium/selenium-server-standalone.jar \
${SE_OPTS} &
Expand Down
5 changes: 4 additions & 1 deletion StandaloneChrome/entry_point.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash

source /opt/bin/functions.sh

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

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

SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
SERVERNUM=$(get_server_num)
xvfb-run -n $SERVERNUM --server-args="-screen 0 $GEOMETRY -ac +extension RANDR" \
java ${JAVA_OPTS} -jar /opt/selenium/selenium-server-standalone.jar \
${SE_OPTS} &
Expand Down
6 changes: 4 additions & 2 deletions StandaloneChromeDebug/entry_point.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash
source "/opt/bin/clear_x_locks.sh"

source /opt/bin/clear_x_locks.sh
source /opt/bin/functions.sh

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

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

SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
SERVERNUM=$(get_server_num)
env | cut -f 1 -d "=" | sort > asroot
sudo -E -u seluser -i env | cut -f 1 -d "=" | sort > asseluser
sudo -E -i -u seluser \
Expand Down
5 changes: 4 additions & 1 deletion StandaloneDebug/entry_point.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash

source /opt/bin/functions.sh

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

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

SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
SERVERNUM=$(get_server_num)
env | cut -f 1 -d "=" | sort > asroot
sudo -E -u seluser -i env | cut -f 1 -d "=" | sort > asseluser
sudo -E -i -u seluser \
Expand Down
5 changes: 4 additions & 1 deletion StandaloneFirefox/entry_point.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash

source /opt/bin/functions.sh

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

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

SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
SERVERNUM=$(get_server_num)
xvfb-run -n $SERVERNUM --server-args="-screen 0 $GEOMETRY -ac +extension RANDR" \
java ${JAVA_OPTS} -jar /opt/selenium/selenium-server-standalone.jar \
${SE_OPTS} &
Expand Down
5 changes: 4 additions & 1 deletion StandaloneFirefoxDebug/entry_point.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash

source /opt/bin/functions.sh

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

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

SERVERNUM=$(echo $DISPLAY | sed 's/:\([0-9][0-9]*\).[0-9][0-9]*/\1/')
SERVERNUM=$(get_server_num)
env | cut -f 1 -d "=" | sort > asroot
sudo -E -u seluser -i env | cut -f 1 -d "=" | sort > asseluser
sudo -E -i -u seluser \
Expand Down
3 changes: 3 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ machine:
- docker
environment:
DELETE_CONTAINERS: false

dependencies:
override:
# Install bats for testing shell scripts.
- git clone https://github.com/sstephenson/bats.git && cd bats && sudo ./install.sh /usr/local
- docker info
- make build

Expand Down
14 changes: 14 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ if [ -n "$1" ] && [ $1 == 'debug' ]; then
DEBUG='-debug'
fi

# Due to the dependency GNU sed, we're skipping this part when running
# on Mac OS X.
if [ "$(uname)" != 'Darwin' ] ; then
echo 'Testing shell functions...'
which bats > /dev/null 2>&1
if [ $? -ne 0 ] ; then
echo "Could not find 'bats'. Please install it first, e.g., following https://github.com/sstephenson/bats#installing-bats-from-source."
exit 1
fi
NodeBase/test-functions.sh || exit 1
else
echo 'Skipping shell functions test on Mac OS X.'
fi

echo Building test container image
docker build -t selenium/test:local ./Test

Expand Down

3 comments on commit 1b2786e

@andrerom
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kayabendroth This breaks the images with /opt/bin/entry_point.sh: line 3: /opt/bin/functions.sh: No such file or directory

see: #208

@gundal
Copy link

@gundal gundal commented on 1b2786e Apr 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ +1

functions.sh does not exist in any of the docker images besides nodebase

NodeBase/functions.sh

!/bin/bash

#184

function get_server_num() {
echo $(echo $DISPLAY | sed -r -e 's/([^:]+)?:([0-9]+)(.[0-9]+)?/\2/')
}

@kayabendroth
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This broke the latest images b/c of the way the images are built currently. I didn't want to merge those changes back into the 2.53.0 branch right away.

Improving the image build process in order to avoid changes that break latest is on the list already.

Please sign in to comment.