Skip to content

Commit 8e8ac14

Browse files
committed
Minor refactor.
Added Dockerfile to create image for dhis2-system-test. This will make it easier to automate system test execution. System test should only start once application (database and web) is ready to accept connections.
1 parent f640558 commit 8e8ac14

File tree

5 files changed

+188
-7
lines changed

5 files changed

+188
-7
lines changed

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM node:4.4.5-slim
2+
3+
ENV DOCKERIZE_VERSION v0.2.0
4+
5+
RUN apt-get update && apt-get install -y wget && npm install chakram --save-dev && npm install -g mocha
6+
7+
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
8+
&& tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
9+
10+
ENV NODE_PATH /usr/local/lib/node_modules
11+
12+
WORKDIR /dhis2
13+
14+
COPY dhis2.js system-test/

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ http://dareid.github.io/chakram/
99
Prerequisites
1010
-------------
1111
* [Install Docker](http://docs.docker.com/engine/installation/ "Documentation")
12-
* [Install npm](http://https://docs.npmjs.com/getting-started/what-is-npm/ "Documentation")
13-
* [Chakram Getting started](http://https://github.com/dareid/chakram#getting-started/ "Documentation")
12+
* [Install npm](https://docs.npmjs.com/getting-started/what-is-npm/ "Documentation")
13+
* [Chakram Getting started](https://github.com/dareid/chakram#getting-started/ "Documentation")
1414

1515
Execute
1616
-------------

dhis2.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
var chakram = require('chakram'),
22
expect = chakram.expect,
3-
host = 'localhost',
4-
port = '8085';
3+
//host = 'localhost',
4+
//port = '8085';
5+
host = 'dhis2-web',
6+
port = '8080';
57

68
var authorization = {
79
headers: {
@@ -11,7 +13,7 @@ var authorization = {
1113
};
1214

1315
describe("DHIS2 - User Module - Create User", function () {
14-
16+
// TODO: the second time the test is executed it fails. Delete user if user exists before create?
1517
it("should create a new User", function () {
1618
var johnDoe = {
1719
"firstName": "John",

docker-compose.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,20 @@ services:
1212
- "5432:5432"
1313
web:
1414
container_name: dhis2-web
15-
image: pgracio/dhis2-web:latest
15+
image: pgracio/dhis2-web:2.23-tomcat7-jre8
1616
environment:
1717
JAVA_OPTS: "-Xmx1024m -Xms4000m"
1818
links:
1919
- database
2020
ports:
21-
- "8085:8080"
21+
- "8085:8080"
22+
depends_on:
23+
- database
24+
entrypoint: ./wait-for-it.sh -t 0 database:5432 --
25+
command: catalina.sh run # https://github.com/docker/compose/issues/3140
26+
system-test:
27+
container_name: dhis2-system-test
28+
image: pgracio/dhis2-system-test
29+
depends_on:
30+
- web
31+
command: dockerize "-wait" "http://dhis2-web:8080" "-timeout" "120s" "mocha" "system-test/dhis2.js"

wait-for-it.sh

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#!/usr/bin/env bash
2+
# https://github.com/vishnubob/wait-for-it
3+
# Use this script to test if a given TCP host/port are available
4+
5+
cmdname=$(basename $0)
6+
7+
echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }
8+
9+
usage()
10+
{
11+
cat << USAGE >&2
12+
Usage:
13+
$cmdname host:port [-s] [-t timeout] [-- command args]
14+
-h HOST | --host=HOST Host or IP under test
15+
-p PORT | --port=PORT TCP port under test
16+
Alternatively, you specify the host and port as host:port
17+
-s | --strict Only execute subcommand if the test succeeds
18+
-q | --quiet Don't output any status messages
19+
-t TIMEOUT | --timeout=TIMEOUT
20+
Timeout in seconds, zero for no timeout
21+
-- COMMAND ARGS Execute command with args after the test finishes
22+
USAGE
23+
exit 1
24+
}
25+
wait_for()
26+
{
27+
if [[ $TIMEOUT -gt 0 ]]; then
28+
echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"
29+
else
30+
echoerr "$cmdname: waiting for $HOST:$PORT without a timeout"
31+
fi
32+
start_ts=$(date +%s)
33+
while :
34+
do
35+
(echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1
36+
result=$?
37+
if [[ $result -eq 0 ]]; then
38+
end_ts=$(date +%s)
39+
echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
40+
break
41+
fi
42+
sleep 1
43+
done
44+
return $result
45+
}
46+
wait_for_wrapper()
47+
{
48+
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
49+
if [[ $QUIET -eq 1 ]]; then
50+
timeout $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
51+
else
52+
timeout $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
53+
fi
54+
PID=$!
55+
trap "kill -INT -$PID" INT
56+
wait $PID
57+
RESULT=$?
58+
if [[ $RESULT -ne 0 ]]; then
59+
echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"
60+
fi
61+
return $RESULT
62+
}
63+
# process arguments
64+
while [[ $# -gt 0 ]]
65+
do
66+
case "$1" in
67+
*:* )
68+
hostport=(${1//:/ })
69+
HOST=${hostport[0]}
70+
PORT=${hostport[1]}
71+
shift 1
72+
;;
73+
--child)
74+
CHILD=1
75+
shift 1
76+
;;
77+
-q | --quiet)
78+
QUIET=1
79+
shift 1
80+
;;
81+
-s | --strict)
82+
STRICT=1
83+
shift 1
84+
;;
85+
-h)
86+
HOST="$2"
87+
if [[ $HOST == "" ]]; then break; fi
88+
shift 2
89+
;;
90+
--host=*)
91+
HOST="${1#*=}"
92+
shift 1
93+
;;
94+
-p)
95+
PORT="$2"
96+
if [[ $PORT == "" ]]; then break; fi
97+
shift 2
98+
;;
99+
--port=*)
100+
PORT="${1#*=}"
101+
shift 1
102+
;;
103+
-t)
104+
TIMEOUT="$2"
105+
if [[ $TIMEOUT == "" ]]; then break; fi
106+
shift 2
107+
;;
108+
--timeout=*)
109+
TIMEOUT="${1#*=}"
110+
shift 1
111+
;;
112+
--)
113+
shift
114+
CLI="$@"
115+
break
116+
;;
117+
--help)
118+
usage
119+
;;
120+
*)
121+
echoerr "Unknown argument: $1"
122+
usage
123+
;;
124+
esac
125+
done
126+
if [[ "$HOST" == "" || "$PORT" == "" ]]; then
127+
echoerr "Error: you need to provide a host and port to test."
128+
usage
129+
fi
130+
TIMEOUT=${TIMEOUT:-15}
131+
STRICT=${STRICT:-0}
132+
CHILD=${CHILD:-0}
133+
QUIET=${QUIET:-0}
134+
if [[ $CHILD -gt 0 ]]; then
135+
wait_for
136+
RESULT=$?
137+
exit $RESULT
138+
else
139+
if [[ $TIMEOUT -gt 0 ]]; then
140+
wait_for_wrapper
141+
RESULT=$?
142+
else
143+
wait_for
144+
RESULT=$?
145+
fi
146+
fi
147+
if [[ $CLI != "" ]]; then
148+
if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then
149+
echoerr "$cmdname: strict mode, refusing to execute subprocess"
150+
exit $RESULT
151+
fi
152+
exec $CLI
153+
else
154+
exit $RESULT
155+
fi

0 commit comments

Comments
 (0)