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.
33*07b4d5b2SAlanny Lopez#  LAUNCH             = Used to determine how to launch the qemu robot test
34*07b4d5b2SAlanny Lopez#                       containers. The options are "local", and "k8s". It will
35*07b4d5b2SAlanny Lopez#                       default to local which will launch a single container
36*07b4d5b2SAlanny Lopez#                       to do the runs. If specified k8s will launch a group of
37*07b4d5b2SAlanny Lopez#                       containers into a kubernetes cluster using the helper
38*07b4d5b2SAlanny Lopez#                       script.
3974d2aba9SAlanny Lopez#
4074d2aba9SAlanny Lopez###############################################################################
410205e8daSAndrew Geissler
420205e8daSAndrew Geisslerset -uo pipefail
430205e8daSAndrew Geissler
4474d2aba9SAlanny LopezQEMU_RUN_TIMER=${QEMU_RUN_TIMER:-300}
451df680a1SAndrew GeisslerWORKSPACE=${WORKSPACE:-${HOME}/${RANDOM}${RANDOM}}
4674d2aba9SAlanny LopezDOCKER_IMG_NAME=${DOCKER_IMG_NAME:-openbmc/ubuntu-robot-qemu}
4774d2aba9SAlanny LopezOBMC_BUILD_DIR=${OBMC_BUILD_DIR:-/tmp/openbmc/build}
4874d2aba9SAlanny LopezUPSTREAM_WORKSPACE=${UPSTREAM_WORKSPACE:-${1}}
49*07b4d5b2SAlanny LopezLAUNCH=${LAUNCH:-local}
500205e8daSAndrew Geissler
5174d2aba9SAlanny Lopez# Determine the architecture
5274d2aba9SAlanny LopezARCH=$(uname -m)
530205e8daSAndrew Geissler
5474d2aba9SAlanny Lopez# Determine the prefix of the Dockerfile's base image and the QEMU_ARCH variable
5574d2aba9SAlanny Lopezcase ${ARCH} in
5674d2aba9SAlanny Lopez  "ppc64le")
570205e8daSAndrew Geissler    DOCKER_BASE="ppc64le/"
580205e8daSAndrew Geissler    QEMU_ARCH="ppc64le-linux"
5974d2aba9SAlanny Lopez    ;;
6074d2aba9SAlanny Lopez  "x86_64")
610205e8daSAndrew Geissler    DOCKER_BASE=""
620205e8daSAndrew Geissler    QEMU_ARCH="x86_64-linux"
6374d2aba9SAlanny Lopez    ;;
6474d2aba9SAlanny Lopez  *)
6574d2aba9SAlanny Lopez    echo "Unsupported system architecture(${ARCH}) found for docker image"
6674d2aba9SAlanny Lopez    exit 1
6774d2aba9SAlanny Lopezesac
680205e8daSAndrew Geissler
6974d2aba9SAlanny Lopez# Get the base directory of the openbmc-build-scripts repo so we can return
7074d2aba9SAlanny LopezDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7174d2aba9SAlanny Lopez
7274d2aba9SAlanny Lopez# Create the base Docker image for QEMU and Robot
730205e8daSAndrew Geissler. "$DIR/scripts/build-qemu-robot-docker.sh" "$DOCKER_IMG_NAME"
740205e8daSAndrew Geissler
750205e8daSAndrew Geissler# Copy the scripts to start and verify QEMU in the workspace
760205e8daSAndrew Geisslercp $DIR/scripts/boot-qemu* ${UPSTREAM_WORKSPACE}
770205e8daSAndrew Geissler
78*07b4d5b2SAlanny Lopez################################################################################
79*07b4d5b2SAlanny Lopez
80*07b4d5b2SAlanny Lopezif [[ ${LAUNCH} == "local" ]]; then
8174d2aba9SAlanny Lopez
820205e8daSAndrew Geissler  # Start QEMU docker instance
830205e8daSAndrew Geissler  # root in docker required to open up the https/ssh ports
8410a193c9SLeonel Gonzalez  obmc_qemu_docker=$(docker run --detach \
850205e8daSAndrew Geissler                                --user root \
8674d2aba9SAlanny Lopez                                --env HOME=${OBMC_BUILD_DIR} \
870205e8daSAndrew Geissler                                --env QEMU_RUN_TIMER=${QEMU_RUN_TIMER} \
880205e8daSAndrew Geissler                                --env QEMU_ARCH=${QEMU_ARCH} \
8974d2aba9SAlanny Lopez                                --workdir "${OBMC_BUILD_DIR}"           \
9074d2aba9SAlanny Lopez                                --volume "${UPSTREAM_WORKSPACE}":"${OBMC_BUILD_DIR}" \
910205e8daSAndrew Geissler                                --tty \
9274d2aba9SAlanny Lopez                                ${DOCKER_IMG_NAME} ${OBMC_BUILD_DIR}/boot-qemu-test.exp)
9374d2aba9SAlanny Lopez
940205e8daSAndrew Geissler  # We can use default ports because we're going to have the 2
950205e8daSAndrew Geissler  # docker instances talk over their private network
960205e8daSAndrew Geissler  DOCKER_SSH_PORT=22
970205e8daSAndrew Geissler  DOCKER_HTTPS_PORT=443
980205e8daSAndrew Geissler  DOCKER_QEMU_IP_ADDR="$(docker inspect $obmc_qemu_docker |  \
990205e8daSAndrew Geissler                       grep -m 1 "IPAddress\":" | cut -d '"' -f 4)"
1000205e8daSAndrew Geissler
10174d2aba9SAlanny Lopez  #Now wait for the OpenBMC QEMU Docker instance to get to standby
1020205e8daSAndrew Geissler  attempt=60
1030205e8daSAndrew Geissler  while [ $attempt -gt 0 ]; do
1040205e8daSAndrew Geissler    attempt=$(( $attempt - 1 ))
1050205e8daSAndrew Geissler    echo "Waiting for qemu to get to standby (attempt: $attempt)..."
1060205e8daSAndrew Geissler    result=$(docker logs $obmc_qemu_docker)
1070205e8daSAndrew Geissler    if grep -q 'OPENBMC-READY' <<< $result ; then
1080205e8daSAndrew Geissler      echo "QEMU is ready!"
1090205e8daSAndrew Geissler      # Give QEMU a few secs to stablize
1100205e8daSAndrew Geissler      sleep 5
1110205e8daSAndrew Geissler      break
1120205e8daSAndrew Geissler    fi
1130205e8daSAndrew Geissler      sleep 2
1140205e8daSAndrew Geissler  done
1150205e8daSAndrew Geissler
1160205e8daSAndrew Geissler  if [ "$attempt" -eq 0 ]; then
1170205e8daSAndrew Geissler    echo "Timed out waiting for QEMU, exiting"
1180205e8daSAndrew Geissler    exit 1
1190205e8daSAndrew Geissler  fi
1200205e8daSAndrew Geissler
121*07b4d5b2SAlanny Lopez  # Now run the Robot test (Tests commented out until they are working again)
1220205e8daSAndrew Geissler
1230205e8daSAndrew Geissler  # Timestamp for job
124*07b4d5b2SAlanny Lopez  #echo "Robot Test started, $(date)"
1250205e8daSAndrew Geissler
126*07b4d5b2SAlanny Lopez  #mkdir -p ${WORKSPACE}
127*07b4d5b2SAlanny Lopez  #cd ${WORKSPACE}
1280205e8daSAndrew Geissler
12974d2aba9SAlanny Lopez  # Copy in the script which will execute the Robot tests
130*07b4d5b2SAlanny Lopez  #cp $DIR/scripts/run-robot.sh ${WORKSPACE}
1310205e8daSAndrew Geissler
13274d2aba9SAlanny Lopez  # Run the Docker container to execute the Robot test cases
1330205e8daSAndrew Geissler  # The test results will be put in ${WORKSPACE}
134*07b4d5b2SAlanny Lopez  #docker run --rm \
135*07b4d5b2SAlanny Lopez  #           --user root \
136*07b4d5b2SAlanny Lopez  #           --env HOME=${HOME} \
137*07b4d5b2SAlanny Lopez  #           --env IP_ADDR=${DOCKER_QEMU_IP_ADDR} \
138*07b4d5b2SAlanny Lopez  #           --env SSH_PORT=${DOCKER_SSH_PORT} \
139*07b4d5b2SAlanny Lopez  #           --env HTTPS_PORT=${DOCKER_HTTPS_PORT} \
140*07b4d5b2SAlanny Lopez  #           --workdir ${HOME} \
141*07b4d5b2SAlanny Lopez  #           --volume ${WORKSPACE}:${HOME} \
142*07b4d5b2SAlanny Lopez  #           --tty \
143*07b4d5b2SAlanny Lopez  #           ${DOCKER_IMG_NAME} ${HOME}/run-robot.sh
1440205e8daSAndrew Geissler
14574d2aba9SAlanny Lopez  # Now stop the QEMU Docker image
1460205e8daSAndrew Geissler  docker stop $obmc_qemu_docker
147*07b4d5b2SAlanny Lopez
148*07b4d5b2SAlanny Lopezelif [[ ${LAUNCH} == "k8s" ]]; then
149*07b4d5b2SAlanny Lopez  # Package the Upstream into an image based off the one created by the build-qemu-robot.sh
150*07b4d5b2SAlanny Lopez  # Dockerfile = $( cat << EOF
151*07b4d5b2SAlanny Lopez  imgname=$DOCKER_IMG_NAME
152*07b4d5b2SAlanny Lopez  cd $DIR
153*07b4d5b2SAlanny Lopez  source ./kubernetes/kubernetes-launch.sh QEMU-launch false false deployment
154*07b4d5b2SAlanny Lopez
155*07b4d5b2SAlanny Lopez  # Xcat Launch (NYI)
156*07b4d5b2SAlanny Lopez
157*07b4d5b2SAlanny Lopez  # source ./kubernetes/kubernetes-launch.sh XCAT-launch true true
158*07b4d5b2SAlanny Lopez
159*07b4d5b2SAlanny Lopezelse
160*07b4d5b2SAlanny Lopez  echo "LAUNCH variable invalid, Exiting"
161*07b4d5b2SAlanny Lopez  exit 1
162*07b4d5b2SAlanny Lopezfi
163