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}
61MACHINE=${MACHINE:-versatilepb}
62
63# Determine the architecture
64ARCH=$(uname -m)
65
66# Determine the prefix of the Dockerfile's base image and the QEMU_ARCH variable
67case ${ARCH} in
68  "ppc64le")
69    DOCKER_BASE="ppc64le/"
70    QEMU_ARCH="ppc64le-linux"
71    ;;
72  "x86_64")
73    DOCKER_BASE=""
74    QEMU_ARCH="x86_64-linux"
75    ;;
76  *)
77    echo "Unsupported system architecture(${ARCH}) found for docker image"
78    exit 1
79esac
80
81# Set the location of the qemu binary relative to UPSTREAM_WORKSPACE
82QEMU_BIN=${QEMU_BIN:-./tmp/sysroots/${QEMU_ARCH}/usr/bin/qemu-system-arm}
83
84# Get the base directory of the openbmc-build-scripts repo so we can return
85DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
86
87# Create the base Docker image for QEMU and Robot
88. "$DIR/scripts/build-qemu-robot-docker.sh" "$DOCKER_IMG_NAME"
89
90# Copy the scripts to start and verify QEMU in the workspace
91cp $DIR/scripts/boot-qemu* ${UPSTREAM_WORKSPACE}
92
93################################################################################
94
95if [[ ${LAUNCH} == "local" ]]; then
96
97  # Start QEMU docker instance
98  # root in docker required to open up the https/ssh ports
99  obmc_qemu_docker=$(docker run --detach \
100                                --user root \
101                                --env HOME=${OBMC_BUILD_DIR} \
102                                --env QEMU_RUN_TIMER=${QEMU_RUN_TIMER} \
103                                --env QEMU_ARCH=${QEMU_ARCH} \
104                                --env QEMU_BIN=${QEMU_BIN} \
105                                --env MACHINE=${MACHINE} \
106                                --workdir "${OBMC_BUILD_DIR}"           \
107                                --volume "${UPSTREAM_WORKSPACE}":"${OBMC_BUILD_DIR}" \
108                                --tty \
109                                ${DOCKER_IMG_NAME} ${OBMC_BUILD_DIR}/boot-qemu-test.exp)
110
111  # We can use default ports because we're going to have the 2
112  # docker instances talk over their private network
113  DOCKER_SSH_PORT=22
114  DOCKER_HTTPS_PORT=443
115  DOCKER_QEMU_IP_ADDR="$(docker inspect $obmc_qemu_docker |  \
116                       grep -m 1 "IPAddress\":" | cut -d '"' -f 4)"
117
118  #Now wait for the OpenBMC QEMU Docker instance to get to standby
119  delay=5
120  attempt=$(( $QEMU_LOGIN_TIMER / $delay ))
121  while [ $attempt -gt 0 ]; do
122    attempt=$(( $attempt - 1 ))
123    echo "Waiting for qemu to get to standby (attempt: $attempt)..."
124    result=$(docker logs $obmc_qemu_docker)
125    if grep -q 'OPENBMC-READY' <<< $result ; then
126      echo "QEMU is ready!"
127      # Give QEMU a few secs to stablize
128      sleep $delay
129      break
130    fi
131      sleep $delay
132  done
133
134  if [ "$attempt" -eq 0 ]; then
135    echo "Timed out waiting for QEMU, exiting"
136    exit 1
137  fi
138
139  # Now run the Robot test (Tests commented out until they are working again)
140
141  # Timestamp for job
142  #echo "Robot Test started, $(date)"
143
144  #mkdir -p ${WORKSPACE}
145  #cd ${WORKSPACE}
146
147  # Copy in the script which will execute the Robot tests
148  #cp $DIR/scripts/run-robot.sh ${WORKSPACE}
149
150  # Run the Docker container to execute the Robot test cases
151  # The test results will be put in ${WORKSPACE}
152  #docker run --rm \
153  #           --user root \
154  #           --env HOME=${HOME} \
155  #           --env IP_ADDR=${DOCKER_QEMU_IP_ADDR} \
156  #           --env SSH_PORT=${DOCKER_SSH_PORT} \
157  #           --env HTTPS_PORT=${DOCKER_HTTPS_PORT} \
158  #           --workdir ${HOME} \
159  #           --volume ${WORKSPACE}:${HOME} \
160  #           --tty \
161  #           ${DOCKER_IMG_NAME} ${HOME}/run-robot.sh
162
163  # Now stop the QEMU Docker image
164  docker stop $obmc_qemu_docker
165
166elif [[ ${LAUNCH} == "k8s" ]]; then
167  # Package the Upstream into an image based off the one created by the build-qemu-robot.sh
168  # Dockerfile = $( cat << EOF
169  imgname=$DOCKER_IMG_NAME
170  cd $DIR
171  source ./kubernetes/kubernetes-launch.sh QEMU-launch false false deployment
172
173  # Xcat Launch (NYI)
174
175  # source ./kubernetes/kubernetes-launch.sh XCAT-launch true true
176
177else
178  echo "LAUNCH variable invalid, Exiting"
179  exit 1
180fi
181