21585: Updates installer's Terraform code to require IMDSv2 on service nodes.
[arvados.git] / services / workbench2 / 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     set +e +o pipefail
11     kill ${arvboot_PID} ${consume_stdout_PID} ${wb2_PID} ${consume_wb2_stdout_PID}
12     wait ${arvboot_PID} ${consume_stdout_PID} ${wb2_PID} ${consume_wb2_stdout_PID} || true
13     echo >&2 "done"
14 }
15
16 random_free_port() {
17     while port=$(shuf -n1 -i $(cat /proc/sys/net/ipv4/ip_local_port_range | tr '\011' '-'))
18     netstat -atun | grep -q ":$port\s" ; do
19         continue
20     done
21     echo $port
22 }
23
24 usage() {
25     echo "Usage: ${0} [options]"
26     echo "Options:"
27     echo "  -i            Run Cypress in interactive mode."
28     echo "  -a PATH       Arvados dir. If PATH doesn't exist, a repo clone is downloaded there."
29     echo "  -w PATH       Workbench2 dir. Default: Current working directory"
30     exit 0
31 }
32
33 # Allow self-signed certs on 'wait-on'
34 export NODE_TLS_REJECT_UNAUTHORIZED=0
35
36 ARVADOS_DIR="unset"
37 CYPRESS_MODE="run"
38 WB2_DIR=`pwd`
39
40 while getopts "ia:w:" o; do
41     case "${o}" in
42         i)
43             # Interactive mode
44             CYPRESS_MODE="open"
45             ;;
46         a)
47             ARVADOS_DIR=${OPTARG}
48             ;;
49         w)
50             WB2_DIR=${OPTARG}
51             ;;
52         *)
53             echo "Invalid Option: -$OPTARG" 1>&2
54             usage
55             ;;
56     esac
57 done
58 shift $((OPTIND-1))
59
60 if [ "${ARVADOS_DIR}" = "unset" ]; then
61   echo "ARVADOS_DIR is unset, using git working dir"
62   ARVADOS_DIR=$(env -C "$WB2_DIR" git rev-parse --show-toplevel)
63 fi
64
65 echo "ARVADOS_DIR is ${ARVADOS_DIR}"
66
67 ARVADOS_LOG=${ARVADOS_DIR}/arvados.log
68 ARVADOS_CONF=${WB2_DIR}/tools/arvados_config.yml
69 VOCABULARY_CONF=${WB2_DIR}/tools/example-vocabulary.json
70
71 if [ ! -f "${WB2_DIR}/src/index.tsx" ]; then
72     echo "ERROR: '${WB2_DIR}' isn't workbench2's directory"
73     usage
74 fi
75
76 if [ ! -f ${ARVADOS_CONF} ]; then
77     echo "ERROR: Arvados config file ${ARVADOS_CONF} not found"
78     exit 1
79 fi
80
81 if [ -f "${WB2_DIR}/public/config.json" ]; then
82     echo "ERROR: Please move public/config.json file out of the way"
83     exit 1
84 fi
85
86 GOPATH="$(go env GOPATH)"
87
88 if [ ! -x ${GOPATH}/bin/arvados-server ]; then
89     echo "Building & installing arvados-server..."
90     cd ${ARVADOS_DIR}
91     GOFLAGS=-buildvcs=false go mod download || exit 1
92     cd cmd/arvados-server
93     GOFLAGS=-buildvcs=false go install
94     cd -
95
96     echo "Installing dev dependencies..."
97     ${GOPATH}/bin/arvados-server install -type test || exit 1
98 fi
99
100 echo "Launching arvados in test mode..."
101 TMPSUBDIR=$(mktemp -d -p /tmp | cut -d \/ -f3) # Removes the /tmp/ part for the regex below
102 TMPDIR=/tmp/${TMPSUBDIR}
103 cp ${VOCABULARY_CONF} ${TMPDIR}/voc.json
104 cp ${ARVADOS_CONF} ${TMPDIR}/arvados.yml
105 sed -i "s/VocabularyPath: \".*\"/VocabularyPath: \"\/tmp\/${TMPSUBDIR}\/voc.json\"/" ${TMPDIR}/arvados.yml
106 coproc arvboot (${GOPATH}/bin/arvados-server boot \
107     -type test \
108     -source "${ARVADOS_DIR}" \
109     -config ${TMPDIR}/arvados.yml \
110     -no-workbench1 \
111     -no-workbench2 \
112     -own-temporary-database \
113     -timeout 20m 2> ${ARVADOS_LOG})
114 trap cleanup ERR EXIT
115
116 read controllerInfo <&"${arvboot[0]}" || exit 1
117 controllerURL=`echo "$controllerInfo" | awk '{print $1;}'`;
118 echo "Arvados up and running at ${controllerURL}"
119 IFS='/' ; read -ra controllerHostPort <<< "${controllerURL}" ; unset IFS
120 controllerHostPort=${controllerHostPort[2]}
121
122 # Copy coproc's stdout to stderr, to ensure `arvados-server boot`
123 # doesn't get blocked trying to write stdout.
124 exec 7<&"${arvboot[0]}"; coproc consume_stdout (cat <&7 >&2)
125
126 cd ${WB2_DIR}
127 echo "Launching workbench2..."
128 WB2_PORT=`random_free_port`
129 coproc wb2 (PORT=${WB2_PORT} \
130     REACT_APP_ARVADOS_API_HOST=${controllerHostPort} \
131     yarn start)
132 exec 8<&"${wb2[0]}"; coproc consume_wb2_stdout (cat <&8 >&2)
133
134 # Wait for workbench2 to be up.
135 # Using https-get to avoid false positive 'ready' detection.
136 yarn run wait-on --timeout 300000 https-get://127.0.0.1:${WB2_PORT} || exit 1
137
138 echo "Running tests..."
139 CYPRESS_system_token=systemusertesttoken1234567890aoeuidhtnsqjkxbmwvzpy \
140     CYPRESS_controller_url=${controllerURL} \
141     CYPRESS_BASE_URL=https://127.0.0.1:${WB2_PORT} \
142     yarn run cypress ${CYPRESS_MODE} "$@"