@@ -19,15 +19,22 @@ set -e
1919
2020KERNEL_IMAGE=%kernelimage%
2121DISK_IMAGE=%diskimage%
22- BAZEL_QEMU_TEST_RUNNER_PATH=%testrunnerinsideqemu %
22+ BAZEL_RUN_SSHD_PATH=%runsshd %
2323RUN_QEMU_SCRIPT=%runqemuscript%
2424
2525# Create a tmp directory that serves as the /test_fs sanbox dir inside qemu.
2626tmpdir_for_sandbox=$( mktemp -d)
2727
28+ # shellcheck disable=SC2317
2829function cleanup {
2930 retval=$?
3031 rm -rf " ${tmpdir_for_sandbox:? } " || true
32+
33+ if [[ -n " ${qemu_pid} " ]]; then
34+ kill " ${qemu_pid} " & > /dev/null || true
35+ wait " ${qemu_pid} " || true
36+ qemu_pid=" "
37+ fi
3138 exit " ${retval} "
3239}
3340
@@ -56,6 +63,10 @@ function path_qemu_sandbox() {
5663 echo " $v "
5764}
5865
66+ function qemu_is_running() {
67+ kill -0 " ${qemu_pid} " & > /dev/null
68+ }
69+
5970
6071# We need to write and transform the environment variables so that they have the "correct"
6172# paths when run inside of qemu.
@@ -104,34 +115,89 @@ qemu_warnings_file=$(strip_pwd_from_path "${TEST_WARNINGS_OUTPUT_FILE}")
104115qemu_testlogs_dir=" ${qemu_warnings_file/ ${qemu_warnings_file##*/ testlogs/ } } "
105116cp -afL " ${testlogs_dir} /" " ${tmpdir_for_sandbox} /${qemu_testlogs_dir} /"
106117
107- # Copy the test runner and test cmd into the sandbox.
108- test_runner_file=" ${tmpdir_for_sandbox} /test_runner_inside_qemu.sh"
118+ # Create test tmp dir.
119+ if [[ -n " ${TEST_TMPDIR} " ]]; then
120+ mkdir -p " $( path_qemu_sandbox " ${TEST_TMPDIR} " ) "
121+ fi
122+
123+ # Copy the test cmd into the sandbox.
109124test_base=${PWD// " ${OLDPWD} " / \/ test_fs}
110125test_cmd_path=" ${tmpdir_for_sandbox} /test_cmd.sh"
111126test_cmd_path_in_qemu=" /test_fs/test_cmd.sh"
112127
113- echo " #!/bin/bash -e" > " ${test_cmd_path} "
114- echo " ${@: 1} " >> " ${test_cmd_path} "
128+ cat << EOF > "${test_cmd_path} "
129+ #!/bin/bash -e
130+ source /test_fs/test_env.sh
131+ cd ${test_base}
132+ export TESTING_UNDER_QEMU=true
133+ ${@: 1}
134+ EOF
115135chmod +x " ${test_cmd_path} "
116136
117137printf " export test_base=%s\n" " ${test_base} " >> " ${test_env_file} "
118138printf " export test_exec_path=%s\n" " ${test_cmd_path_in_qemu} " >> " ${test_env_file} "
119139
120- cp -afL " ${BAZEL_QEMU_TEST_RUNNER_PATH} " " ${test_runner_file} "
140+ # Setup ssh.
141+ ssh_priv_key=" ${tmpdir_for_sandbox} /ssh_key"
142+ ssh_pub_key_inside_qemu=" /test_fs/ssh_key.pub"
143+ ssh-keygen -f " ${ssh_priv_key} " -N ' ' -b 1024 > /dev/null
121144
122- # Launch the qemu test.
123- retval=0
124- (exec env - \
145+ ssh_env_file=" ${tmpdir_for_sandbox} /ssh_env.sh"
146+ echo " #!/bin/bash" > " ${ssh_env_file} "
147+ echo " export SSH_PUB_KEY=${ssh_pub_key_inside_qemu} " >> " ${ssh_env_file} "
148+
149+ run_sshd_file=" ${tmpdir_for_sandbox} /run_sshd.sh"
150+ cp -afL " ${BAZEL_RUN_SSHD_PATH} " " ${run_sshd_file} "
151+
152+ monitor_sock=" mon.sock"
153+ # Launch qemu.
154+ env - \
125155 QEMU_TEST_FS_PATH=" ${tmpdir_for_sandbox} " \
126156 QEMU_KERNEL_IMAGE=" ${KERNEL_IMAGE} " \
127157 QEMU_DISK_BASE_RO=" ${PWD} /${DISK_IMAGE} " \
128- " ${RUN_QEMU_SCRIPT} " ) || retval=$?
158+ MONITOR_SOCK=" ${monitor_sock} " \
159+ " ${RUN_QEMU_SCRIPT} " & > " qemu.log" &
160+ qemu_pid=" $! "
161+
162+ echo " QEMU logs available at: $( pwd) /qemu.log"
163+ echo " QEMU monitor available at unix socket: $( pwd) /${monitor_sock} "
164+
165+
166+ echo ' Waiting for QEMU to boot'
167+ while qemu_is_running && ! echo " info usernet" | netcat -NU " ${monitor_sock} " & > /dev/null; do
168+ sleep 1
169+ done
170+
171+ host_ssh_port=" $( echo " info usernet" | netcat -NU " ${monitor_sock} " | grep " HOST_FORWARD" | awk ' {print $4}' ) "
172+ ssh_opts=(
173+ -q
174+ -i " ${ssh_priv_key} "
175+ -o " StrictHostKeyChecking=no"
176+ -o " UserKnownHostsFile=/dev/null"
177+ -p " ${host_ssh_port} "
178+ root@localhost
179+ )
180+
181+ # Wait for qemu to boot and ssh to be ready
182+ echo ' Waiting for SSH to come online'
183+ while qemu_is_running && ! ssh " ${ssh_opts[@]} " ' echo test' & > /dev/null; do
184+ sleep 1
185+ done
186+
187+ if ! qemu_is_running; then
188+ echo ' QEMU failed to boot'
189+ cat -v qemu.log
190+ echo " Log available in sandbox at: $( pwd) /qemu.log"
191+ exit 3
192+ fi
193+
194+ retval=0
195+ ssh " ${ssh_opts[@]} " ' /bin/bash -c ' " ${test_cmd_path_in_qemu} " || retval=$?
129196
130197# We use a known path to find the testlogs directory so that we can copy the results back from qemu.
131198testlogs_dir=" ${TEST_WARNINGS_OUTPUT_FILE%%/ testlogs/* } /testlogs/"
132199qemu_warnings_file=$( strip_pwd_from_path " ${TEST_WARNINGS_OUTPUT_FILE} " )
133200qemu_testlogs_dir=" ${qemu_warnings_file/ ${qemu_warnings_file##*/ testlogs/ } } "
134-
135201cp -afL " ${tmpdir_for_sandbox} /${qemu_testlogs_dir} /" " ${testlogs_dir} /"
136202
137- exit $ retval
203+ exit " ${ retval} "
0 commit comments