16029: Add 'make integration-tests-in-docker'
[arvados-workbench2.git] / tools / run-integration-tests.sh
1 #!/bin/bash
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 set -e -o pipefail
7
8 cleanup() {
9     set -x
10     kill ${arvboot_PID} ${consume_stdout_PID} ${wb2_PID} ${consume_wb2_stdout_PID}
11     wait ${arvboot_PID} ${consume_stdout_PID} ${wb2_PID} ${consume_wb2_stdout_PID} || true
12     if [ "${CLEANUP_ARVADOS_DIR}" -eq "1" ]; then
13         rm -rf ${ARVADOS_DIR}
14     fi
15     echo >&2 "done"
16 }
17
18 random_free_port() {
19     while port=$(shuf -n1 -i $(cat /proc/sys/net/ipv4/ip_local_port_range | tr '\011' '-'))
20     netstat -atun | grep -q ":$port\s" ; do
21         continue
22     done
23     echo $port
24 }
25
26 usage() {
27     echo "Usage: ${0} [options]"
28     echo "Options:"
29     echo "  -i            Run Cypress in interactive mode."
30     echo "  -a PATH       Arvados dir. If PATH doesn't exist, a repo clone is downloaded there."
31     echo "  -w PATH       Workbench2 dir. Default: Current working directory"
32     exit 0
33 }
34
35 # Allow self-signed certs on 'wait-on'
36 export NODE_TLS_REJECT_UNAUTHORIZED=0
37
38 CLEANUP_ARVADOS_DIR=0
39 CYPRESS_MODE="run"
40 ARVADOS_DIR=`mktemp -d`
41 WB2_DIR=`pwd`
42
43 while getopts "ia:w:" o; do
44     case "${o}" in
45         i)
46             # Interactive mode
47             CYPRESS_MODE="open"
48             ;;
49         a)
50             ARVADOS_DIR=${OPTARG}
51             ;;
52         w)
53             WB2_DIR=${OPTARG}
54             ;;
55         *)
56             echo "Invalid Option: -$OPTARG" 1>&2
57             usage
58             ;;
59     esac
60 done
61 shift $((OPTIND-1))
62
63 ARVADOS_LOG=${ARVADOS_DIR}/arvados.log
64 ARVADOS_CONF=${WB2_DIR}/tools/arvados_config.yml
65
66 if [ ! -f "${WB2_DIR}/src/index.tsx" ]; then
67     echo "ERROR: '${WB2_DIR}' isn't workbench2's directory"
68     usage
69 fi
70
71 if [ ! -f ${ARVADOS_CONF} ]; then
72     echo "ERROR: Arvados config file ${ARVADOS_CONF} not found"
73     exit 1
74 fi
75
76 if [ -f "${WB2_DIR}/public/config.json" ]; then
77     echo "ERROR: Please move public/config.json file out of the way"
78     exit 1
79 fi
80
81 if [ ! -d "${ARVADOS_DIR}/.git" ]; then
82     mkdir -p ${ARVADOS_DIR} || exit 1
83     CLEANUP_ARVADOS_DIR=1
84     echo "Downloading arvados..."
85     git clone https://git.arvados.org/arvados.git ${ARVADOS_DIR} || exit 1
86 fi
87
88 echo "Building & installing arvados-server..."
89 cd ${ARVADOS_DIR}
90 go mod download || exit 1
91 cd cmd/arvados-server
92 go install
93 cd -
94
95 echo "Installing dev dependencies..."
96 ~/go/bin/arvados-server install -type test || exit 1
97
98 echo "Launching arvados in test mode..."
99 coproc arvboot (~/go/bin/arvados-server boot \
100     -type test \
101     -config ${ARVADOS_CONF} \
102     -own-temporary-database \
103     -timeout 20m 2> ${ARVADOS_LOG})
104 trap cleanup ERR EXIT
105
106 read controllerURL <&"${arvboot[0]}" || exit 1
107 echo "Arvados up and running at ${controllerURL}"
108 IFS='/' ; read -ra controllerHostPort <<< "${controllerURL}" ; unset IFS
109 controllerHostPort=${controllerHostPort[2]}
110
111 # Copy coproc's stdout to stderr, to ensure `arvados-server boot`
112 # doesn't get blocked trying to write stdout.
113 exec 7<&"${arvboot[0]}"; coproc consume_stdout (cat <&7 >&2)
114
115 cd ${WB2_DIR}
116 echo "Launching workbench2..."
117 WB2_PORT=`random_free_port`
118 coproc wb2 (PORT=${WB2_PORT} \
119     REACT_APP_ARVADOS_API_HOST=${controllerHostPort} \
120     yarn start)
121 exec 8<&"${wb2[0]}"; coproc consume_wb2_stdout (cat <&8 >&2)
122
123 # Wait for workbench2 to be up.
124 # Using https-get to avoid false positive 'ready' detection.
125 yarn run wait-on --timeout 300000 https-get://localhost:${WB2_PORT} || exit 1
126
127 echo "Running tests..."
128 CYPRESS_system_token=systemusertesttoken1234567890aoeuidhtnsqjkxbmwvzpy \
129     CYPRESS_controller_url=${controllerURL} \
130     CYPRESS_BASE_URL=https://localhost:${WB2_PORT} \
131     yarn run cypress ${CYPRESS_MODE}