10205e8daSAndrew Geissler#!/bin/bash -xe
274d2aba9SAlanny Lopez###############################################################################
30205e8daSAndrew Geissler#
474d2aba9SAlanny Lopez# This script is for starting QEMU against the input build and running the
574d2aba9SAlanny Lopez# robot CI test suite against it.(ROBOT CI TEST CURRENTLY WIP)
674d2aba9SAlanny Lopez#
774d2aba9SAlanny Lopez###############################################################################
874d2aba9SAlanny Lopez#
974d2aba9SAlanny Lopez# Parameters used by the script:
1074d2aba9SAlanny Lopez#  UPSTREAM_WORKSPACE = The directory from which the QEMU components are being
1174d2aba9SAlanny Lopez#                       imported from. Generally, this is the build directory
1274d2aba9SAlanny Lopez#                       that is generated by the OpenBMC build-setup.sh script
1374d2aba9SAlanny Lopez#                       when run with "target=qemu".
1474d2aba9SAlanny Lopez#                       Example: /home/builder/workspace/openbmc-build/build.
1574d2aba9SAlanny Lopez#
1674d2aba9SAlanny Lopez# Optional Variables:
1774d2aba9SAlanny Lopez#
1874d2aba9SAlanny Lopez#  WORKSPACE          = Path of the workspace directory where some intermediate
1974d2aba9SAlanny Lopez#                       files will be saved to.
2074d2aba9SAlanny Lopez#  QEMU_RUN_TIMER     = Defaults to 300, a timer for the QEMU container.
2174d2aba9SAlanny Lopez#  DOCKER_IMG_NAME    = Defaults to openbmc/ubuntu-robot-qemu, the name the
2274d2aba9SAlanny Lopez#                       Docker image will be tagged with when built.
2374d2aba9SAlanny Lopez#  OBMC_BUILD_DIR     = Defaults to /tmp/openbmc/build, the path to the
2474d2aba9SAlanny Lopez#                       directory where the UPSTREAM_WORKSPACE build files will
2574d2aba9SAlanny Lopez#                       be mounted to. Since the build containers have been
2674d2aba9SAlanny Lopez#                       changed to use /tmp as the parent directory for their
2774d2aba9SAlanny Lopez#                       builds, move the mounting location to be the same to
2874d2aba9SAlanny Lopez#                       resolve issues with file links or referrals to exact
2974d2aba9SAlanny Lopez#                       paths in the original build directory. If the build
3074d2aba9SAlanny Lopez#                       directory was changed in the build-setup.sh run, this
3174d2aba9SAlanny Lopez#                       variable should also be changed. Otherwise, the default
3274d2aba9SAlanny Lopez#                       should be used.
3307b4d5b2SAlanny Lopez#  LAUNCH             = Used to determine how to launch the qemu robot test
3407b4d5b2SAlanny Lopez#                       containers. The options are "local", and "k8s". It will
3507b4d5b2SAlanny Lopez#                       default to local which will launch a single container
3607b4d5b2SAlanny Lopez#                       to do the runs. If specified k8s will launch a group of
3707b4d5b2SAlanny Lopez#                       containers into a kubernetes cluster using the helper
3807b4d5b2SAlanny Lopez#                       script.
39*7a88f29eSAndrew Geissler#  QEMU_BIN           = Location of qemu-system-arm binary to use when starting
40*7a88f29eSAndrew Geissler#                       QEMU relative to upstream workspace.  Default is
41*7a88f29eSAndrew Geissler#                       ./tmp/sysroots/${QEMU_ARCH}/usr/bin/qemu-system-arm
42*7a88f29eSAndrew Geissler#                       which is the default location when doing a bitbake
43*7a88f29eSAndrew Geissler#                       of obmc-phosphor-image
4474d2aba9SAlanny Lopez#
4574d2aba9SAlanny Lopez###############################################################################
460205e8daSAndrew Geissler
470205e8daSAndrew Geisslerset -uo pipefail
480205e8daSAndrew Geissler
4974d2aba9SAlanny LopezQEMU_RUN_TIMER=${QEMU_RUN_TIMER:-300}
501df680a1SAndrew GeisslerWORKSPACE=${WORKSPACE:-${HOME}/${RANDOM}${RANDOM}}
5174d2aba9SAlanny LopezDOCKER_IMG_NAME=${DOCKER_IMG_NAME:-openbmc/ubuntu-robot-qemu}
5274d2aba9SAlanny LopezOBMC_BUILD_DIR=${OBMC_BUILD_DIR:-/tmp/openbmc/build}
5374d2aba9SAlanny LopezUPSTREAM_WORKSPACE=${UPSTREAM_WORKSPACE:-${1}}
5407b4d5b2SAlanny LopezLAUNCH=${LAUNCH:-local}
550205e8daSAndrew Geissler
5674d2aba9SAlanny Lopez# Determine the architecture
5774d2aba9SAlanny LopezARCH=$(uname -m)
580205e8daSAndrew Geissler
5974d2aba9SAlanny Lopez# Determine the prefix of the Dockerfile's base image and the QEMU_ARCH variable
6074d2aba9SAlanny Lopezcase ${ARCH} in
6174d2aba9SAlanny Lopez  "ppc64le")
620205e8daSAndrew Geissler    DOCKER_BASE="ppc64le/"
630205e8daSAndrew Geissler    QEMU_ARCH="ppc64le-linux"
6474d2aba9SAlanny Lopez    ;;
6574d2aba9SAlanny Lopez  "x86_64")
660205e8daSAndrew Geissler    DOCKER_BASE=""
670205e8daSAndrew Geissler    QEMU_ARCH="x86_64-linux"
6874d2aba9SAlanny Lopez    ;;
6974d2aba9SAlanny Lopez  *)
7074d2aba9SAlanny Lopez    echo "Unsupported system architecture(${ARCH}) found for docker image"
7174d2aba9SAlanny Lopez    exit 1
7274d2aba9SAlanny Lopezesac
730205e8daSAndrew Geissler
74*7a88f29eSAndrew Geissler# Set the location of the qemu binary relative to UPSTREAM_WORKSPACE
75*7a88f29eSAndrew GeisslerQEMU_BIN=${QEMU_BIN:-./tmp/sysroots/${QEMU_ARCH}/usr/bin/qemu-system-arm}
76*7a88f29eSAndrew Geissler
7774d2aba9SAlanny Lopez# Get the base directory of the openbmc-build-scripts repo so we can return
7874d2aba9SAlanny LopezDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7974d2aba9SAlanny Lopez
8074d2aba9SAlanny Lopez# Create the base Docker image for QEMU and Robot
810205e8daSAndrew Geissler. "$DIR/scripts/build-qemu-robot-docker.sh" "$DOCKER_IMG_NAME"
820205e8daSAndrew Geissler
830205e8daSAndrew Geissler# Copy the scripts to start and verify QEMU in the workspace
840205e8daSAndrew Geisslercp $DIR/scripts/boot-qemu* ${UPSTREAM_WORKSPACE}
850205e8daSAndrew Geissler
8607b4d5b2SAlanny Lopez################################################################################
8707b4d5b2SAlanny Lopez
8807b4d5b2SAlanny Lopezif [[ ${LAUNCH} == "local" ]]; then
8974d2aba9SAlanny Lopez
900205e8daSAndrew Geissler  # Start QEMU docker instance
910205e8daSAndrew Geissler  # root in docker required to open up the https/ssh ports
9210a193c9SLeonel Gonzalez  obmc_qemu_docker=$(docker run --detach \
930205e8daSAndrew Geissler                                --user root \
9474d2aba9SAlanny Lopez                                --env HOME=${OBMC_BUILD_DIR} \
950205e8daSAndrew Geissler                                --env QEMU_RUN_TIMER=${QEMU_RUN_TIMER} \
960205e8daSAndrew Geissler                                --env QEMU_ARCH=${QEMU_ARCH} \
97*7a88f29eSAndrew Geissler                                --env QEMU_BIN=${QEMU_BIN} \
9874d2aba9SAlanny Lopez                                --workdir "${OBMC_BUILD_DIR}"           \
9974d2aba9SAlanny Lopez                                --volume "${UPSTREAM_WORKSPACE}":"${OBMC_BUILD_DIR}" \
1000205e8daSAndrew Geissler                                --tty \
10174d2aba9SAlanny Lopez                                ${DOCKER_IMG_NAME} ${OBMC_BUILD_DIR}/boot-qemu-test.exp)
10274d2aba9SAlanny Lopez
1030205e8daSAndrew Geissler  # We can use default ports because we're going to have the 2
1040205e8daSAndrew Geissler  # docker instances talk over their private network
1050205e8daSAndrew Geissler  DOCKER_SSH_PORT=22
1060205e8daSAndrew Geissler  DOCKER_HTTPS_PORT=443
1070205e8daSAndrew Geissler  DOCKER_QEMU_IP_ADDR="$(docker inspect $obmc_qemu_docker |  \
1080205e8daSAndrew Geissler                       grep -m 1 "IPAddress\":" | cut -d '"' -f 4)"
1090205e8daSAndrew Geissler
11074d2aba9SAlanny Lopez  #Now wait for the OpenBMC QEMU Docker instance to get to standby
1110205e8daSAndrew Geissler  attempt=60
1120205e8daSAndrew Geissler  while [ $attempt -gt 0 ]; do
1130205e8daSAndrew Geissler    attempt=$(( $attempt - 1 ))
1140205e8daSAndrew Geissler    echo "Waiting for qemu to get to standby (attempt: $attempt)..."
1150205e8daSAndrew Geissler    result=$(docker logs $obmc_qemu_docker)
1160205e8daSAndrew Geissler    if grep -q 'OPENBMC-READY' <<< $result ; then
1170205e8daSAndrew Geissler      echo "QEMU is ready!"
1180205e8daSAndrew Geissler      # Give QEMU a few secs to stablize
1190205e8daSAndrew Geissler      sleep 5
1200205e8daSAndrew Geissler      break
1210205e8daSAndrew Geissler    fi
1220205e8daSAndrew Geissler      sleep 2
1230205e8daSAndrew Geissler  done
1240205e8daSAndrew Geissler
1250205e8daSAndrew Geissler  if [ "$attempt" -eq 0 ]; then
1260205e8daSAndrew Geissler    echo "Timed out waiting for QEMU, exiting"
1270205e8daSAndrew Geissler    exit 1
1280205e8daSAndrew Geissler  fi
1290205e8daSAndrew Geissler
13007b4d5b2SAlanny Lopez  # Now run the Robot test (Tests commented out until they are working again)
1310205e8daSAndrew Geissler
1320205e8daSAndrew Geissler  # Timestamp for job
13307b4d5b2SAlanny Lopez  #echo "Robot Test started, $(date)"
1340205e8daSAndrew Geissler
13507b4d5b2SAlanny Lopez  #mkdir -p ${WORKSPACE}
13607b4d5b2SAlanny Lopez  #cd ${WORKSPACE}
1370205e8daSAndrew Geissler
13874d2aba9SAlanny Lopez  # Copy in the script which will execute the Robot tests
13907b4d5b2SAlanny Lopez  #cp $DIR/scripts/run-robot.sh ${WORKSPACE}
1400205e8daSAndrew Geissler
14174d2aba9SAlanny Lopez  # Run the Docker container to execute the Robot test cases
1420205e8daSAndrew Geissler  # The test results will be put in ${WORKSPACE}
14307b4d5b2SAlanny Lopez  #docker run --rm \
14407b4d5b2SAlanny Lopez  #           --user root \
14507b4d5b2SAlanny Lopez  #           --env HOME=${HOME} \
14607b4d5b2SAlanny Lopez  #           --env IP_ADDR=${DOCKER_QEMU_IP_ADDR} \
14707b4d5b2SAlanny Lopez  #           --env SSH_PORT=${DOCKER_SSH_PORT} \
14807b4d5b2SAlanny Lopez  #           --env HTTPS_PORT=${DOCKER_HTTPS_PORT} \
14907b4d5b2SAlanny Lopez  #           --workdir ${HOME} \
15007b4d5b2SAlanny Lopez  #           --volume ${WORKSPACE}:${HOME} \
15107b4d5b2SAlanny Lopez  #           --tty \
15207b4d5b2SAlanny Lopez  #           ${DOCKER_IMG_NAME} ${HOME}/run-robot.sh
1530205e8daSAndrew Geissler
15474d2aba9SAlanny Lopez  # Now stop the QEMU Docker image
1550205e8daSAndrew Geissler  docker stop $obmc_qemu_docker
15607b4d5b2SAlanny Lopez
15707b4d5b2SAlanny Lopezelif [[ ${LAUNCH} == "k8s" ]]; then
15807b4d5b2SAlanny Lopez  # Package the Upstream into an image based off the one created by the build-qemu-robot.sh
15907b4d5b2SAlanny Lopez  # Dockerfile = $( cat << EOF
16007b4d5b2SAlanny Lopez  imgname=$DOCKER_IMG_NAME
16107b4d5b2SAlanny Lopez  cd $DIR
16207b4d5b2SAlanny Lopez  source ./kubernetes/kubernetes-launch.sh QEMU-launch false false deployment
16307b4d5b2SAlanny Lopez
16407b4d5b2SAlanny Lopez  # Xcat Launch (NYI)
16507b4d5b2SAlanny Lopez
16607b4d5b2SAlanny Lopez  # source ./kubernetes/kubernetes-launch.sh XCAT-launch true true
16707b4d5b2SAlanny Lopez
16807b4d5b2SAlanny Lopezelse
16907b4d5b2SAlanny Lopez  echo "LAUNCH variable invalid, Exiting"
17007b4d5b2SAlanny Lopez  exit 1
17107b4d5b2SAlanny Lopezfi
172