1#!/bin/bash -xe
2###############################################################################
3#
4# This script is for starting QEMU against the input build and running the
5# robot CI test suite against it.(ROBOT CI TEST CURRENTLY WIP)
6#
7###############################################################################
8#
9# Parameters used by the script:
10#  UPSTREAM_WORKSPACE = The directory from which the QEMU components are being
11#                       imported from. Generally, this is the build directory
12#                       that is generated by the OpenBMC build-setup.sh script
13#                       when run with "target=qemu".
14#                       Example: /home/builder/workspace/openbmc-build/build.
15#
16# Optional Variables:
17#
18#  WORKSPACE          = Path of the workspace directory where some intermediate
19#                       files will be saved to.
20#  QEMU_RUN_TIMER     = Defaults to 300, a timer for the QEMU container.
21#  QEMU_LOGIN_TIMER   = Defaults to 180, a timer for the QEMU container to reach
22#                       login.
23#  DOCKER_IMG_NAME    = Defaults to openbmc/ubuntu-robot-qemu, the name the
24#                       Docker image will be tagged with when built.
25#  OBMC_BUILD_DIR     = Defaults to /tmp/openbmc/build, the path to the
26#                       directory where the UPSTREAM_WORKSPACE build files will
27#                       be mounted to. Since the build containers have been
28#                       changed to use /tmp as the parent directory for their
29#                       builds, move the mounting location to be the same to
30#                       resolve issues with file links or referrals to exact
31#                       paths in the original build directory. If the build
32#                       directory was changed in the build-setup.sh run, this
33#                       variable should also be changed. Otherwise, the default
34#                       should be used.
35#  LAUNCH             = Used to determine how to launch the qemu robot test
36#                       containers. The options are "local", and "k8s". It will
37#                       default to local which will launch a single container
38#                       to do the runs. If specified k8s will launch a group of
39#                       containers into a kubernetes cluster using the helper
40#                       script.
41#  QEMU_BIN           = Location of qemu-system-arm binary to use when starting
42#                       QEMU relative to upstream workspace.  Default is
43#                       ./tmp/sysroots/${QEMU_ARCH}/usr/bin/qemu-system-arm
44#                       which is the default location when doing a bitbake
45#                       of obmc-phosphor-image
46#
47#  MACHINE            = Machine to run test against. The options are "witherspoon",
48#                       "palmetto", "romulus", or undefined (default).  Default
49#                       will use the versatilepb model.
50###############################################################################
51
52set -uo pipefail
53
54QEMU_RUN_TIMER=${QEMU_RUN_TIMER:-300}
55QEMU_LOGIN_TIMER=${QEMU_LOGIN_TIMER:-180}
56WORKSPACE=${WORKSPACE:-${HOME}/${RANDOM}${RANDOM}}
57DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-openbmc/ubuntu-robot-qemu}
58OBMC_BUILD_DIR=${OBMC_BUILD_DIR:-/tmp/openbmc/build}
59UPSTREAM_WORKSPACE=${UPSTREAM_WORKSPACE:-${1}}
60LAUNCH=${LAUNCH:-local}
61DEFAULT_MACHINE=versatilepb
62MACHINE=${MACHINE:-${DEFAULT_MACHINE}}
63
64# The automated test suite needs a real machine type so
65# if we're using versatilepb for our qemu start parameter
66# then we need to just let our run-robot use the default
67if [[ "$MACHINE" == "$DEFAULT_MACHINE" ]]; then
68    MACHINE_QEMU=
69else
70    MACHINE_QEMU=${MACHINE}
71fi
72
73# Determine the architecture
74ARCH=$(uname -m)
75
76# Determine the prefix of the Dockerfile's base image and the QEMU_ARCH variable
77case ${ARCH} in
78  "ppc64le")
79    QEMU_ARCH="ppc64le-linux"
80    ;;
81  "x86_64")
82    QEMU_ARCH="x86_64-linux"
83    ;;
84  "aarch64")
85    QEMU_ARCH="arm64-linux"
86    ;;
87  *)
88    echo "Unsupported system architecture(${ARCH}) found for docker image"
89    exit 1
90esac
91
92# Set the location of the qemu binary relative to UPSTREAM_WORKSPACE
93QEMU_BIN=${QEMU_BIN:-./tmp/sysroots/${QEMU_ARCH}/usr/bin/qemu-system-arm}
94
95# Get the base directory of the openbmc-build-scripts repo so we can return
96DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
97
98# Create the base Docker image for QEMU and Robot
99# shellcheck source=scripts/build-qemu-robot-docker.sh
100. "$DIR/scripts/build-qemu-robot-docker.sh" "$DOCKER_IMG_NAME"
101
102# Copy the scripts to start and verify QEMU in the workspace
103cp "$DIR"/scripts/boot-qemu* "${UPSTREAM_WORKSPACE}"
104
105################################################################################
106
107if [[ ${LAUNCH} == "local" ]]; then
108
109  # Start QEMU docker instance
110  # root in docker required to open up the https/ssh ports
111  obmc_qemu_docker=$(docker run --detach \
112                                --rm \
113                                --user root \
114                                --env HOME="${OBMC_BUILD_DIR}" \
115                                --env QEMU_RUN_TIMER="${QEMU_RUN_TIMER}" \
116                                --env QEMU_ARCH="${QEMU_ARCH}" \
117                                --env QEMU_BIN="${QEMU_BIN}" \
118                                --env MACHINE="${MACHINE}" \
119                                --workdir "${OBMC_BUILD_DIR}"           \
120                                --volume "${UPSTREAM_WORKSPACE}:${OBMC_BUILD_DIR}:ro" \
121                                --tty \
122                                "${DOCKER_IMG_NAME}" "${OBMC_BUILD_DIR}"/boot-qemu-test.exp)
123
124  # We can use default ports because we're going to have the 2
125  # docker instances talk over their private network
126  DOCKER_SSH_PORT=22
127  DOCKER_HTTPS_PORT=443
128
129  # This docker command intermittently asserts a SIGPIPE which
130  # causes the whole script to fail. The IP address comes through
131  # fine on these errors so just ignore the SIGPIPE
132  trap '' PIPE
133
134  DOCKER_QEMU_IP_ADDR="$(docker inspect "$obmc_qemu_docker" |  \
135                       grep "IPAddress\":" | tail -n1 | cut -d '"' -f 4)"
136
137  #Now wait for the OpenBMC QEMU Docker instance to get to standby
138  delay=5
139  attempt=$(( QEMU_LOGIN_TIMER / delay ))
140  while [ $attempt -gt 0 ]; do
141    attempt=$(( attempt - 1 ))
142    echo "Waiting for qemu to get to standby (attempt: $attempt)..."
143    result=$(docker logs "$obmc_qemu_docker")
144    if grep -q 'OPENBMC-READY' <<< "$result" ; then
145      echo "QEMU is ready!"
146      # Give QEMU a few secs to stabilize
147      sleep $delay
148      break
149    fi
150      sleep $delay
151  done
152
153  if [ "$attempt" -eq 0 ]; then
154    echo "Timed out waiting for QEMU, exiting"
155    exit 1
156  fi
157
158  # Now run the Robot test (Tests commented out until they are working again)
159
160  # Timestamp for job
161  echo "Robot Test started, $(date)"
162
163  mkdir -p "${WORKSPACE}"
164  cd "${WORKSPACE}"
165
166  # Copy in the script which will execute the Robot tests
167  cp "$DIR"/scripts/run-robot.sh "${WORKSPACE}"
168
169  # Run the Docker container to execute the Robot test cases
170  # The test results will be put in ${WORKSPACE}
171  docker run --rm \
172             --env HOME="${HOME}" \
173             --env IP_ADDR="${DOCKER_QEMU_IP_ADDR}" \
174             --env SSH_PORT="${DOCKER_SSH_PORT}" \
175             --env HTTPS_PORT="${DOCKER_HTTPS_PORT}" \
176             --env MACHINE="${MACHINE_QEMU}" \
177             --workdir "${HOME}" \
178             --volume "${WORKSPACE}":"${HOME}" \
179             --tty \
180             "${DOCKER_IMG_NAME}" "${HOME}"/run-robot.sh
181
182  # Now stop the QEMU Docker image
183  docker stop "$obmc_qemu_docker"
184
185else
186  echo "LAUNCH variable invalid, Exiting"
187  exit 1
188fi
189