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#  DOCKER_IMG_NAME    = Defaults to openbmc/ubuntu-robot-qemu, the name the
22#                       Docker image will be tagged with when built.
23#  OBMC_BUILD_DIR     = Defaults to /tmp/openbmc/build, the path to the
24#                       directory where the UPSTREAM_WORKSPACE build files will
25#                       be mounted to. Since the build containers have been
26#                       changed to use /tmp as the parent directory for their
27#                       builds, move the mounting location to be the same to
28#                       resolve issues with file links or referrals to exact
29#                       paths in the original build directory. If the build
30#                       directory was changed in the build-setup.sh run, this
31#                       variable should also be changed. Otherwise, the default
32#                       should be used.
33#
34###############################################################################
35
36set -uo pipefail
37
38QEMU_RUN_TIMER=${QEMU_RUN_TIMER:-300}
39WORKSPACE=${WORKSPACE:-${HOME}/${RANDOM}${RANDOM}}
40DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-openbmc/ubuntu-robot-qemu}
41OBMC_BUILD_DIR=${OBMC_BUILD_DIR:-/tmp/openbmc/build}
42UPSTREAM_WORKSPACE=${UPSTREAM_WORKSPACE:-${1}}
43
44# Determine the architecture
45ARCH=$(uname -m)
46
47# Determine the prefix of the Dockerfile's base image and the QEMU_ARCH variable
48case ${ARCH} in
49  "ppc64le")
50    DOCKER_BASE="ppc64le/"
51    QEMU_ARCH="ppc64le-linux"
52    ;;
53  "x86_64")
54    DOCKER_BASE=""
55    QEMU_ARCH="x86_64-linux"
56    ;;
57  *)
58    echo "Unsupported system architecture(${ARCH}) found for docker image"
59    exit 1
60esac
61
62# Get the base directory of the openbmc-build-scripts repo so we can return
63DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
64
65# Create the base Docker image for QEMU and Robot
66. "$DIR/scripts/build-qemu-robot-docker.sh" "$DOCKER_IMG_NAME"
67
68# Copy the scripts to start and verify QEMU in the workspace
69cp $DIR/scripts/boot-qemu* ${UPSTREAM_WORKSPACE}
70
71# Move into the upstream workspace directory
72cd ${UPSTREAM_WORKSPACE}
73
74# Start QEMU docker instance
75# root in docker required to open up the https/ssh ports
76obmc_qemu_docker=$(docker run --detach \
77                              --user root \
78                              --env HOME=${OBMC_BUILD_DIR} \
79                              --env QEMU_RUN_TIMER=${QEMU_RUN_TIMER} \
80                              --env QEMU_ARCH=${QEMU_ARCH} \
81                              --workdir "${OBMC_BUILD_DIR}"           \
82                              --volume "${UPSTREAM_WORKSPACE}":"${OBMC_BUILD_DIR}" \
83                              --tty \
84                              ${DOCKER_IMG_NAME} ${OBMC_BUILD_DIR}/boot-qemu-test.exp)
85
86
87# We can use default ports because we're going to have the 2
88# docker instances talk over their private network
89DOCKER_SSH_PORT=22
90DOCKER_HTTPS_PORT=443
91DOCKER_QEMU_IP_ADDR="$(docker inspect $obmc_qemu_docker |  \
92                      grep -m 1 "IPAddress\":" | cut -d '"' -f 4)"
93
94# Now wait for the OpenBMC QEMU Docker instance to get to standby
95attempt=60
96while [ $attempt -gt 0 ]; do
97    attempt=$(( $attempt - 1 ))
98    echo "Waiting for qemu to get to standby (attempt: $attempt)..."
99    result=$(docker logs $obmc_qemu_docker)
100    if grep -q 'OPENBMC-READY' <<< $result ; then
101        echo "QEMU is ready!"
102        # Give QEMU a few secs to stablize
103        sleep 5
104        break
105    fi
106    sleep 2
107done
108
109if [ "$attempt" -eq 0 ]; then
110    echo "Timed out waiting for QEMU, exiting"
111    exit 1
112fi
113
114# Now run the Robot test
115
116# Timestamp for job
117echo "Robot Test started, $(date)"
118
119mkdir -p ${WORKSPACE}
120cd ${WORKSPACE}
121
122# Copy in the script which will execute the Robot tests
123cp $DIR/scripts/run-robot.sh ${WORKSPACE}
124
125# Run the Docker container to execute the Robot test cases
126# The test results will be put in ${WORKSPACE}
127docker run --rm \
128           --user root \
129           --env HOME=${HOME} \
130           --env IP_ADDR=${DOCKER_QEMU_IP_ADDR} \
131           --env SSH_PORT=${DOCKER_SSH_PORT} \
132           --env HTTPS_PORT=${DOCKER_HTTPS_PORT} \
133           --workdir ${HOME} \
134           --volume ${WORKSPACE}:${HOME} \
135           --tty \
136           ${DOCKER_IMG_NAME} ${HOME}/run-robot.sh
137
138# Now stop the QEMU Docker image
139docker stop $obmc_qemu_docker
140