]> xenbits.xensource.com Git - raisin.git/commitdiff
raisin: introduce tests
authorStefano Stabellini <stefano.stabellini@eu.citrix.com>
Tue, 12 May 2015 13:27:09 +0000 (13:27 +0000)
committerStefano Stabellini <stefano.stabellini@eu.citrix.com>
Tue, 12 May 2015 13:27:09 +0000 (13:27 +0000)
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

README
defconfig
lib/commands.sh
lib/common-functions.sh
lib/common-tests.sh [new file with mode: 0644]
raise
tests/busybox-pv [new file with mode: 0755]
tests/series [new file with mode: 0644]

diff --git a/README b/README
index b7832dae3949454fc706f2ba4e4447804b7e48c4..42c0f4d544c5680552fb218d9176d9182fdb72c2 100644 (file)
--- a/README
+++ b/README
@@ -102,3 +102,17 @@ check-package
 
 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
index 324e89185a7dc949b5e33b20edd7d600af4177fc..da81952210078f51fa52cb2869911c96ce76ab17 100644 (file)
--- a/defconfig
+++ b/defconfig
@@ -43,3 +43,8 @@ GRUB_REVISION="master"
 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"
index 801341bff42f6193b90aaa37d0ab70c179c87f30..ffbadb4a6fd68862e661d8e37bdcbfaa391d5fc9 100755 (executable)
@@ -103,3 +103,7 @@ function configure() {
     for_each_component configure
 }
 
+function test() {
+    init_tests
+    run_tests
+}
index d38788bafed7b5a6a6b6798c9c9f65fc299e3f28..d88bc0d104ad5d35ad6c0f28428cac1bbac63f41 100644 (file)
@@ -39,6 +39,7 @@ function common_init() {
     get_distro
     get_arch
     get_components
+    get_tests
 
     verbose_echo "Distro: $DISTRO"
     verbose_echo "Arch: $RAISIN_ARCH"
@@ -73,6 +74,24 @@ function get_components() {
     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
@@ -278,6 +297,64 @@ function for_each_component () {
     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"
 }
diff --git a/lib/common-tests.sh b/lib/common-tests.sh
new file mode 100644 (file)
index 0000000..8d2ee6b
--- /dev/null
@@ -0,0 +1,112 @@
+#!/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
+}
diff --git a/raise b/raise
index 68dbfd8b4a937da07733e1de6a5ab9de2c80ab08..dd275ad111fb2b16c9aa1476b667297e379dcc07 100755 (executable)
--- a/raise
+++ b/raise
@@ -3,7 +3,7 @@
 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"
@@ -14,6 +14,7 @@ _help() {
     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
@@ -25,10 +26,11 @@ fi
 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
 
@@ -59,7 +61,7 @@ do
 done
 
 case "$1" in
-    "install-builddep" | "build" | "install" | "configure" | "unraise" )
+    "install-builddep" | "build" | "install" | "configure" | "unraise" | "test" )
         COMMAND=$1
         ;;
     *)
diff --git a/tests/busybox-pv b/tests/busybox-pv
new file mode 100755 (executable)
index 0000000..ec3ba5c
--- /dev/null
@@ -0,0 +1,37 @@
+#!/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
+}
diff --git a/tests/series b/tests/series
new file mode 100644 (file)
index 0000000..a5ec626
--- /dev/null
@@ -0,0 +1 @@
+busybox-pv