Introduce a new command to run functional tests and unit tests.
Introduce a generic infrastrucutre to run tests on the local machine.
Add a library of common functions that can be used by the test scripts
to setup guest VMs.
Add a simple test script that boots a single busybox based PV guest.
Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
Changes in v3:
- source test scripts
- expose a _test and a _cleanup function from the test script
Changes in v2:
- use found as a boolean
- print error to stderr
If your component comes with additional data, maybe a config script or
anything else, place it under "data".
+
+
+= Testing =
+
+Raisin can also be used for testing. Make sure to have Xen already up
+and running (raise build, raise install and host reboot).
+Ask Raisin to run tests like this:
+
+./raise test
+
+You can specify a subset of tests to run with ENABLED_TESTS in the
+config file, or the TESTS environmental variable:
+
+TESTS="busybox-pv" ./raise test
LIBVIRT_REVISION="xen-tested-master"
OVMF_REVISION="a065efc7c7ce8bb3e5cb3e463099d023d4a92927"
LINUX_REVISION="master"
+
+# Tests
+## All tests: busybox-pv
+## ENABLED_TESTS is the list of test run by raise test
+ENABLED_TESTS="busybox-pv"
for_each_component configure
}
+function test() {
+ init_tests
+ run_tests
+}
get_distro
get_arch
get_components
+ get_tests
verbose_echo "Distro: $DISTRO"
verbose_echo "Arch: $RAISIN_ARCH"
export COMPONENTS
}
+function get_tests() {
+ if [[ -z "$TESTS" ]]
+ then
+ TESTS="$ENABLED_TESTS"
+ fi
+
+ if [[ -z "$TESTS" ]]
+ then
+ local t
+ for t in `cat "$BASEDIR"/tests/series`
+ do
+ TESTS="$TESTS $t"
+ verbose_echo "Found test $t"
+ done
+ fi
+ export TESTS
+}
+
function get_distro() {
if [[ -x "`which lsb_release 2>/dev/null`" ]]
then
done
}
+function run_tests() {
+ local t
+ local enabled
+ local found
+
+ for t in `cat "$BASEDIR"/tests/series`
+ do
+ found=false
+ for enabled in $TESTS
+ do
+ if [[ $enabled = $t ]]
+ then
+ found=true
+ break
+ fi
+ done
+ if ! $found
+ then
+ verbose_echo "$t" is disabled
+ continue
+ fi
+
+ source "$BASEDIR"/tests/$t
+
+ verbose_echo running test "$t"
+ "$t"_test
+ "$t"_cleanup
+ verbose_echo "test "$t" done"
+ done
+}
+
+function init_tests() {
+ local -a missing
+
+ check-package bridge-utils
+ if [[ $DISTRO = "Debian" ]]
+ then
+ check-package busybox-static
+ elif [[ $DISTRO = "Fedora" ]]
+ then
+ check-package busybox grub2 which
+ else
+ echo "I don't know distro $DISTRO. It might be missing packages."
+ fi
+
+ if [[ -n "${missing[@]}" ]]
+ then
+ verbose_echo "Installing ${missing[@]}"
+ install-package "${missing[@]}"
+ fi
+
+ if ! ifconfig xenbr1 &>/dev/null
+ then
+ $SUDO brctl addbr xenbr1
+ $SUDO ifconfig xenbr1 169.254.0.1 up
+ fi
+}
+
function _build_package_deb() {
fakeroot bash ./scripts/mkdeb "$1"
}
--- /dev/null
+#!/usr/bin/env bash
+
+source ${RAISIN_PATH}/common-functions.sh
+
+# $1 disk name
+# $2 disk size
+function allocate_disk() {
+ local disk
+ local size
+
+ disk=$1
+ size=$2
+
+ size=$((size+511))
+ size=$((size/512))
+
+ dd if=/dev/zero of=$disk bs=512 count=$size
+ sync
+}
+
+# $1 disk name
+# print loop device name
+function create_loop() {
+ local disk
+ local loop
+
+ disk=`readlink -f $1`
+
+ $SUDO losetup -f $disk
+ loop=`$SUDO losetup -a | grep $disk | cut -d : -f 1`
+ echo $loop
+}
+
+# $1 dev name
+function busybox_rootfs() {
+ local dev
+ local tmpdir
+
+ dev=$1
+
+ $SUDO mkfs.ext3 $dev
+
+ tmpdir=`mktemp -d`
+ $SUDO mount $dev $tmpdir
+ mkdir -p $tmpdir/bin
+ mkdir -p $tmpdir/sbin
+ mkdir -p $tmpdir/dev
+ mkdir -p $tmpdir/proc
+ mkdir -p $tmpdir/sys
+ mkdir -p $tmpdir/lib
+ mkdir -p $tmpdir/var
+ cp `which busybox` $tmpdir/bin
+ $tmpdir/bin/busybox --install $tmpdir/bin
+
+ $SUDO umount $tmpdir
+ rmdir $tmpdir
+}
+
+function busybox_network_init() {
+ local dev
+ local tmpdir
+
+ dev=$1
+ tmpdir=`mktemp -d`
+
+ $SUDO mount $dev $tmpdir
+ rm -f $tmpdir/bin/init
+ cat >$tmpdir/bin/init <<EOF
+#!/bin/sh
+mount -t proc proc /proc
+mount -t sysfs sysfs /sys
+ifconfig eth0 169.254.0.2 up
+/bin/sh
+EOF
+ chmod +x $tmpdir/bin/init
+
+ $SUDO umount $tmpdir
+ rmdir $tmpdir
+}
+
+function check_guest_alive() {
+ local i
+ i=0
+ while ! ping -c 1 169.254.0.2 &> /dev/null
+ do
+ sleep 1
+ i=$((i+1))
+ if [[ $i -gt 60 ]]
+ then
+ echo Timeout connecting to guest
+ return 1
+ fi
+ done
+ return 0
+}
+
+function get_host_kernel() {
+ echo "/boot/vmlinuz-`uname -r`"
+}
+
+function get_host_initrd() {
+ if [[ $DISTRO = "Debian" ]]
+ then
+ echo "/boot/initrd.img-`uname -r`"
+ elif [[ $DISTRO = "Fedora" ]]
+ then
+ echo "/boot/initramfs-`uname -r`".img
+ else
+ echo "I don't know how to find the initrd" >&2
+ exit 1
+ fi
+}
set -e
_help() {
- echo "Usage: ./build.sh <options> <command>"
+ echo "Usage: ./raise <options> <command>"
echo "where options are:"
echo " -v | --verbose Verbose"
echo " -y | --yes Do not ask questions and continue"
echo " install Install binaries under / (requires sudo)"
echo " configure Configure the system (requires sudo)"
echo " unraise Uninstall and unconfigure the system (requires sudo)"
+ echo " test Runs tests on the system (requires sudo, Xen must be running)"
}
# Include your defaults
source ./config
# To use this as a library, set RAISIN_PATH appropriately
-[[ -z "$RAISIN_PATH" ]] && RAISIN_PATH="$PWD/lib"
+[[ -z "$RAISIN_PATH" ]] && export RAISIN_PATH="$PWD/lib"
# Then as many as the sub-libraries as you need
source ${RAISIN_PATH}/common-functions.sh
+source ${RAISIN_PATH}/common-tests.sh
source ${RAISIN_PATH}/git-checkout.sh
source ${RAISIN_PATH}/commands.sh
done
case "$1" in
- "install-builddep" | "build" | "install" | "configure" | "unraise" )
+ "install-builddep" | "build" | "install" | "configure" | "unraise" | "test" )
COMMAND=$1
;;
*)
--- /dev/null
+#!/usr/bin/env bash
+
+set -e
+
+function busybox-pv-cleanup() {
+ $SUDO xl destroy raisin-test || true
+ umount $LOOP || true
+ cd "$BASEDIR"
+ $SUDO losetup -d $LOOP
+ rm -rf $TMPDIR
+}
+
+function busybox-pv-test() {
+ TMPDIR=`mktemp -d`
+ cd $TMPDIR
+
+ allocate_disk busybox-vm-disk $((20*1024*1024))
+ LOOP=`create_loop busybox-vm-disk`
+ busybox_rootfs $LOOP
+ busybox_network_init $LOOP
+
+ cat >busybox-pv <<EOF
+kernel = "`get_host_kernel`"
+ramdisk = "`get_host_initrd`"
+extra = "root=/dev/xvda console=hvc0"
+memory = 512
+name = "raisin-test"
+vcpus = 2
+disk = [ '$LOOP,raw,xvda,w' ]
+serial="pty"
+boot="c"
+vif=['bridge=xenbr1']
+EOF
+
+ $SUDO xl create busybox-pv
+ check_guest_alive
+}
--- /dev/null
+busybox-pv