Script which launches QEMU and runs robot CI test against it

This script will be run within automation to do a basic QEMU
CI test against the input openbmc build.

Change-Id: I6ffccd7f5767bec9bc673b1a41bd7bd1d960924c
Signed-off-by: Andrew Geissler <andrewg@us.ibm.com>
diff --git a/run-qemu-robot-test.sh b/run-qemu-robot-test.sh
new file mode 100755
index 0000000..1682500
--- /dev/null
+++ b/run-qemu-robot-test.sh
@@ -0,0 +1,100 @@
+#!/bin/bash -xe
+
+# This script is for starting QEMU against the input build and running
+#  the robot CI test suite against it.
+#
+#  Parameters:
+#   UPSTREAM_WORKSPACE = <required, base dir of QEMU image>
+#   WORKSPACE =          <optional, temp dir for robot script>
+
+set -uo pipefail
+
+QEMU_RUN_TIMER=300
+WORKSPACE=${WORKSPACE:-${HOME}}/${RANDOM}${RANDOM}
+DOCKER_IMG_NAME="openbmc/ubuntu-robot-qemu"
+
+# Get base directory of our repo so we can find the scripts later
+DIR="${BASH_SOURCE%/*}"
+if [[ ! -d "$DIR" || "$DIR" == "." ]]; then DIR="$PWD"; fi
+
+cd ${UPSTREAM_WORKSPACE}
+
+# Determine our architecture, ppc64le or the other one
+if [ $(uname -m) == "ppc64le" ]; then
+    DOCKER_BASE="ppc64le/"
+    QEMU_ARCH="ppc64le-linux"
+else
+    DOCKER_BASE=""
+    QEMU_ARCH="x86_64-linux"
+fi
+
+# Create the docker image that QEMU and Robot will run in
+. "$DIR/scripts/build-qemu-robot-docker.sh" "$DOCKER_IMG_NAME"
+
+# Copy the scripts to start and verify QEMU in the workspace
+cp $DIR/scripts/boot-qemu* ${UPSTREAM_WORKSPACE}
+
+# Start QEMU docker instance
+# root in docker required to open up the https/ssh ports
+obmc_qemu_docker=$(docker run --detach \
+                              --user root \
+                              --env HOME=${HOME} \
+                              --env QEMU_RUN_TIMER=${QEMU_RUN_TIMER} \
+                              --env QEMU_ARCH=${QEMU_ARCH} \
+                              --workdir "${HOME}"           \
+                              --volume "${UPSTREAM_WORKSPACE}":"${HOME}" \
+                              --tty \
+                              ${DOCKER_IMG_NAME} ${HOME}/boot-qemu-test.exp)
+
+# We can use default ports because we're going to have the 2
+# docker instances talk over their private network
+DOCKER_SSH_PORT=22
+DOCKER_HTTPS_PORT=443
+DOCKER_QEMU_IP_ADDR="$(docker inspect $obmc_qemu_docker |  \
+                      grep -m 1 "IPAddress\":" | cut -d '"' -f 4)"
+
+# Now wait for the openbmc qemu docker instance to get to standby
+attempt=60
+while [ $attempt -gt 0 ]; do
+    attempt=$(( $attempt - 1 ))
+    echo "Waiting for qemu to get to standby (attempt: $attempt)..."
+    result=$(docker logs $obmc_qemu_docker)
+    if grep -q 'OPENBMC-READY' <<< $result ; then
+        echo "QEMU is ready!"
+        # Give QEMU a few secs to stablize
+        sleep 5
+        break
+    fi
+    sleep 2
+done
+
+if [ "$attempt" -eq 0 ]; then
+    echo "Timed out waiting for QEMU, exiting"
+    exit 1
+fi
+
+# Now run the robot test
+
+# Timestamp for job
+echo "Robot Test started, $(date)"
+
+mkdir -p ${WORKSPACE}
+cd ${WORKSPACE}
+
+# Copy in the script which will execute the robot tests
+cp $DIR/scripts/run-robot.sh ${WORKSPACE}
+
+# Run the docker container to execute the robot test cases
+# The test results will be put in ${WORKSPACE}
+docker run --user root \
+           --env HOME=${HOME} \
+           --env IP_ADDR=${DOCKER_QEMU_IP_ADDR} \
+           --env SSH_PORT=${DOCKER_SSH_PORT} \
+           --env HTTPS_PORT=${DOCKER_HTTPS_PORT} \
+           --workdir ${HOME} \
+           --volume ${WORKSPACE}:${HOME} \
+           --tty \
+           ${DOCKER_IMG_NAME} ${HOME}/run-robot.sh
+
+# Now stop the QEMU docker image
+docker stop $obmc_qemu_docker
diff --git a/scripts/boot-qemu-test.exp b/scripts/boot-qemu-test.exp
new file mode 100755
index 0000000..f4c3d38
--- /dev/null
+++ b/scripts/boot-qemu-test.exp
@@ -0,0 +1,39 @@
+#!/usr/bin/expect
+#
+# Launch QEMU and verify we can login then sleep for defined amount of time
+# before exiting
+#
+#  Requires following env variables be set:
+#   QEMU_RUN_TIMER  Amount of time to run the QEMU instance
+#   HOME            Location of scripts
+
+set timeout "$env(QEMU_RUN_TIMER)*2"
+set command "$env(HOME)/boot-qemu.sh"
+
+spawn $command
+
+expect {
+  timeout { send_user "\nFailed to boot\n"; exit 1 }
+  eof { send_user "\nFailure, got EOF"; exit 1 }
+  "qemuarm login:"
+}
+
+send "root\r"
+
+expect {
+  timeout { send_user "\nFailed, no login prompt\n"; exit 1 }
+  eof { send_user "\nFailure, got EOF"; exit 1 }
+  "Password:"
+}
+
+send "0penBmc\r"
+
+expect {
+  timeout { send_user "\nFailed, could not login\n"; exit 1 }
+  eof { send_user "\nFailure, got EOF"; exit 1 }
+  "root@qemuarm:~#"
+}
+
+send_user "OPENBMC-READY\n"
+sleep "$env(QEMU_RUN_TIMER)"
+send_user "OPENBMC-EXITING\n"
diff --git a/scripts/boot-qemu.sh b/scripts/boot-qemu.sh
new file mode 100755
index 0000000..e0cd4de
--- /dev/null
+++ b/scripts/boot-qemu.sh
@@ -0,0 +1,43 @@
+#!/bin/bash -xe
+#
+# Launch QEMU using the raw commands
+#
+#  Parameters:
+#   parm1:  <optional, QEMU architecture to use >
+#            default is ${QEMU_ARCH} - ppc64le-linux or x86_64-linux
+#   parm2:  <optional, full path to base directory of qemu binary and images >
+#            default is ${HOME}
+
+set -uo pipefail
+
+QEMU_ARCH=${1:-$QEMU_ARCH}
+echo "QEMU_ARCH = $QEMU_ARCH"
+if [[ -z $QEMU_ARCH ]]; then
+    echo "Did not pass in required QEMU arch parameter"
+    exit -1
+fi
+
+BASE_DIR=${2:-$HOME}
+echo "BASE_DIR = $BASE_DIR"
+if [[ ! -d $BASE_DIR ]]; then
+    echo "No input directory and HOME not set!"
+    exit -1
+fi
+
+cd ${BASE_DIR}
+
+./tmp/sysroots/${QEMU_ARCH}/usr/bin/qemu-system-arm \
+    -nographic \
+    -kernel ./tmp/deploy/images/qemuarm/zImage-qemuarm.bin \
+    -machine versatilepb \
+    -drive file=./tmp/deploy/images/qemuarm/obmc-phosphor-image-qemuarm.ext4,format=raw \
+    -no-reboot \
+    -show-cursor \
+    -usb \
+    -usbdevice wacom-tablet \
+    -no-reboot -m 128 \
+    -redir tcp:22::22 \
+    -redir tcp:443::443 \
+    --append \
+    "root=/dev/sda rw console=ttyAMA0,115200 console=tty mem=128M highres=off \
+    rootfstype=ext4 console=ttyS0"
diff --git a/scripts/build-qemu-robot-docker.sh b/scripts/build-qemu-robot-docker.sh
new file mode 100755
index 0000000..d1339ac
--- /dev/null
+++ b/scripts/build-qemu-robot-docker.sh
@@ -0,0 +1,75 @@
+#!/bin/bash -xe
+#
+# Build the required docker image to run QEMU and Robot test cases
+#
+#  Parameters:
+#   parm1:  <optional, the name of the docker image to generate>
+#            default is openbmc/ubuntu-robot-qemu
+
+set -uo pipefail
+
+DOCKER_IMG_NAME=${1:-"openbmc/ubuntu-robot-qemu"}
+
+# Determine our architecture, ppc64le or the other one
+if [ $(uname -m) == "ppc64le" ]; then
+    DOCKER_BASE="ppc64le/"
+else
+    DOCKER_BASE=""
+fi
+
+################################# docker img # #################################
+# Create docker image that can run QEMU and Robot Tests
+Dockerfile=$(cat << EOF
+FROM ${DOCKER_BASE}ubuntu:latest
+
+ENV DEBIAN_FRONTEND noninteractive
+
+RUN apt-get update && apt-get install -yy \
+    debianutils \
+    gawk \
+    git \
+    python \
+    python-dev \
+    python-setuptools \
+    socat \
+    texinfo \
+    wget \
+    gcc \
+    libffi-dev \
+    libssl-dev \
+    xterm \
+    mwm \
+    ssh \
+    vim \
+    iputils-ping \
+    sudo \
+    cpio \
+    unzip \
+    diffstat \
+    expect \
+    curl
+
+RUN easy_install \
+    tox \
+    pip \
+    requests
+
+RUN pip install \
+    robotframework \
+    robotframework-requests \
+    robotframework-sshlibrary \
+    robotframework-scplibrary
+
+RUN grep -q ${GROUPS} /etc/group || groupadd -g ${GROUPS} ${USER}
+RUN grep -q ${UID} /etc/passwd || useradd -d ${HOME} -m -u ${UID} -g ${GROUPS} \
+                    ${USER}
+USER ${USER}
+ENV HOME ${HOME}
+RUN /bin/bash
+EOF
+)
+
+################################# docker img # #################################
+
+# Build above image
+docker build -t ${DOCKER_IMG_NAME} - <<< "${Dockerfile}"
diff --git a/scripts/run-robot.sh b/scripts/run-robot.sh
new file mode 100755
index 0000000..4c311c2
--- /dev/null
+++ b/scripts/run-robot.sh
@@ -0,0 +1,36 @@
+#!/bin/bash -x
+# Extract and run the OpenBMC robot test suite
+#
+# The robot test results will be copied to ${HOME}
+#
+#  Requires following env variables be set:
+#   IP_ADDR     IP Address of openbmc
+#   SSH_PORT    SSH port of openbmc
+#   HTTPS_PORT  HTTPS port of openbmc
+#
+#  Optional env variable
+#   ROBOT_CODE_HOME  Location to extract the code
+#                    Default will be a temp location in /tmp/
+
+# we don't want to fail on bad rc since robot tests may fail
+
+ROBOT_CODE_HOME=${ROBOT_CODE_HOME:-/tmp/$(whoami)/${RANDOM}/obmc-robot/}
+
+git clone https://github.com/openbmc/openbmc-test-automation.git \
+        ${ROBOT_CODE_HOME}
+
+cd ${ROBOT_CODE_HOME}
+
+chmod ugo+rw -R ${ROBOT_CODE_HOME}/*
+
+# Execute the CI tests
+export OPENBMC_HOST=${IP_ADDR}
+export SSH_PORT=${SSH_PORT}
+export HTTPS_PORT=${HTTPS_PORT}
+
+tox -e qemu -- --include CI tests
+
+cp ${ROBOT_CODE_HOME}/*.xml ${HOME}/
+cp ${ROBOT_CODE_HOME}/*.html ${HOME}/
+
+#rm -rf ${ROBOT_CODE_HOME}