[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v2 1/4] raisin: introduce tests



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@xxxxxxxxxxxxx>

---

Changes in v2:

- use found as a boolean
- print error to stderr
---
 README                  |   14 ++++++
 defconfig               |    5 +++
 lib/commands.sh         |    4 ++
 lib/common-functions.sh |   74 +++++++++++++++++++++++++++++++
 lib/common-tests.sh     |  112 +++++++++++++++++++++++++++++++++++++++++++++++
 raise                   |    8 ++--
 tests/busybox-pv        |   38 ++++++++++++++++
 tests/series            |    1 +
 8 files changed, 253 insertions(+), 3 deletions(-)
 create mode 100644 lib/common-tests.sh
 create mode 100755 tests/busybox-pv
 create mode 100644 tests/series

diff --git a/README b/README
index b7832da..42c0f4d 100644
--- 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
diff --git a/defconfig b/defconfig
index b4ed94d..e88f3d3 100644
--- a/defconfig
+++ b/defconfig
@@ -39,3 +39,8 @@ GRUB_REVISION="master"
 LIBVIRT_REVISION="master"
 OVMF_REVISION="master"
 LINUX_REVISION="master"
+
+# Tests
+## All tests: busybox-pv
+## ENABLED_TESTS is the list of test run by raise test
+ENABLED_TESTS="busybox-pv"
diff --git a/lib/commands.sh b/lib/commands.sh
index 801341b..ffbadb4 100755
--- a/lib/commands.sh
+++ b/lib/commands.sh
@@ -103,3 +103,7 @@ function configure() {
     for_each_component configure
 }
 
+function test() {
+    init_tests
+    run_tests
+}
diff --git a/lib/common-functions.sh b/lib/common-functions.sh
index d38788b..5b82c4c 100644
--- a/lib/common-functions.sh
+++ b/lib/common-functions.sh
@@ -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,61 @@ 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
+
+        verbose_echo running test "$t"
+        "$BASEDIR"/tests/$t
+        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
index 0000000..8d2ee6b
--- /dev/null
+++ b/lib/common-tests.sh
@@ -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 68dbfd8..dd275ad 100755
--- 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
index 0000000..941c739
--- /dev/null
+++ b/tests/busybox-pv
@@ -0,0 +1,38 @@
+#!/usr/bin/env bash
+
+set -e
+
+source $BASEDIR/lib/common-tests.sh
+
+function cleanup() {
+    $SUDO xl destroy raisin-test || true
+    umount $LOOP || true
+    cd "$BASEDIR"
+    $SUDO losetup -d $LOOP
+    rm -rf $TMPDIR
+}
+
+trap cleanup EXIT
+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
index 0000000..a5ec626
--- /dev/null
+++ b/tests/series
@@ -0,0 +1 @@
+busybox-pv
-- 
1.7.10.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.