xenapi: Extract plugin installation functions

This change extracts the plugin installation functions, and covers the
extracted functions with tests. Use:

    ./test_funtions.sh run_tests

to run the tests.

Change-Id: I1d78d9e8cc4d52ee2df83d07e4c74dda4805f21a
This commit is contained in:
Mate Lakat 2013-03-22 16:34:05 +00:00
parent 5e482c9ea2
commit 57e3da9b76
4 changed files with 259 additions and 30 deletions

55
tools/xen/functions Normal file
View File

@ -0,0 +1,55 @@
#!/bin/bash
function xapi_plugin_location {
for PLUGIN_DIR in "/etc/xapi.d/plugins/" "/usr/lib/xcp/plugins/"
do
if [ -d $PLUGIN_DIR ]
then
echo $PLUGIN_DIR
return 0
fi
done
return 1
}
function zip_snapshot_location {
echo $1 | sed "s:\.git$::;s:$:/zipball/$2:g"
}
function create_directory_for_kernels {
mkdir -p "/boot/guest"
}
function extract_remote_zipball {
local ZIPBALL_URL=$1
local LOCAL_ZIPBALL=$(mktemp)
local EXTRACTED_FILES=$(mktemp -d)
(
wget -nv $ZIPBALL_URL -O $LOCAL_ZIPBALL --no-check-certificate
unzip -q -o $LOCAL_ZIPBALL -d $EXTRACTED_FILES
rm -f $LOCAL_ZIPBALL
) >&2
echo "$EXTRACTED_FILES"
}
function find_xapi_plugins_dir {
find $1 -path '*/xapi.d/plugins' -type d -print
}
function install_xapi_plugins_from_zipball {
local XAPI_PLUGIN_DIR
local EXTRACTED_FILES
local EXTRACTED_PLUGINS_DIR
XAPI_PLUGIN_DIR=$(xapi_plugin_location)
EXTRACTED_FILES=$(extract_remote_zipball $1)
EXTRACTED_PLUGINS_DIR=$(find_xapi_plugins_dir $EXTRACTED_FILES)
cp -pr $EXTRACTED_PLUGINS_DIR/* $XAPI_PLUGIN_DIR
rm -rf $EXTRACTED_FILES
chmod a+x ${XAPI_PLUGIN_DIR}*
}

View File

@ -28,6 +28,9 @@ THIS_DIR=$(cd $(dirname "$0") && pwd)
# Include onexit commands # Include onexit commands
. $THIS_DIR/scripts/on_exit.sh . $THIS_DIR/scripts/on_exit.sh
# xapi functions
. $THIS_DIR/functions
# #
# Get Settings # Get Settings
@ -43,48 +46,26 @@ xe_min()
xe "$cmd" --minimal "$@" xe "$cmd" --minimal "$@"
} }
# #
# Prepare Dom0 # Prepare Dom0
# including installing XenAPI plugins # including installing XenAPI plugins
# #
cd $THIS_DIR cd $THIS_DIR
if [ -f ./master ]
then
rm -rf ./master
rm -rf ./nova
fi
# get nova # Install plugins
NOVA_ZIPBALL_URL=${NOVA_ZIPBALL_URL:-$(echo $NOVA_REPO | sed "s:\.git$::;s:$:/zipball/$NOVA_BRANCH:g")}
wget -nv $NOVA_ZIPBALL_URL -O nova-zipball --no-check-certificate
unzip -q -o nova-zipball -d ./nova
# install xapi plugins ## Nova plugins
XAPI_PLUGIN_DIR=/etc/xapi.d/plugins/ NOVA_ZIPBALL_URL=${NOVA_ZIPBALL_URL:-$(zip_snapshot_location $NOVA_REPO $NOVA_BRANCH)}
if [ ! -d $XAPI_PLUGIN_DIR ]; then install_xapi_plugins_from_zipball $NOVA_ZIPBALL_URL
# the following is needed when using xcp-xapi
XAPI_PLUGIN_DIR=/usr/lib/xcp/plugins/
fi
cp -pr ./nova/*/plugins/xenserver/xenapi/etc/xapi.d/plugins/* $XAPI_PLUGIN_DIR
# Install the netwrap xapi plugin to support agent control of dom0 networking ## Install the netwrap xapi plugin to support agent control of dom0 networking
if [[ "$ENABLED_SERVICES" =~ "q-agt" && "$Q_PLUGIN" = "openvswitch" ]]; then if [[ "$ENABLED_SERVICES" =~ "q-agt" && "$Q_PLUGIN" = "openvswitch" ]]; then
if [ -f ./quantum ]; then QUANTUM_ZIPBALL_URL=${QUANTUM_ZIPBALL_URL:-$(zip_snapshot_location $QUANTUM_REPO $QUANTUM_BRANCH)}
rm -rf ./quantum install_xapi_plugins_from_zipball $QUANTUM_ZIPBALL_URL
fi
# get quantum
QUANTUM_ZIPBALL_URL=${QUANTUM_ZIPBALL_URL:-$(echo $QUANTUM_REPO | sed "s:\.git$::;s:$:/zipball/$QUANTUM_BRANCH:g")}
wget -nv $QUANTUM_ZIPBALL_URL -O quantum-zipball --no-check-certificate
unzip -q -o quantum-zipball -d ./quantum
cp -pr ./quantum/*/quantum/plugins/openvswitch/agent/xenapi/etc/xapi.d/plugins/* $XAPI_PLUGIN_DIR
fi fi
chmod a+x ${XAPI_PLUGIN_DIR}* create_directory_for_kernels
mkdir -p /boot/guest
# #
# Configure Networking # Configure Networking

59
tools/xen/mocks Normal file
View File

@ -0,0 +1,59 @@
#!/bin/bash
test ! -e "$LIST_OF_ACTIONS" && {
echo "Mocking is not set up properly."
echo "LIST_OF_ACTIONS should point to an existing file."
exit 1
}
test ! -e "$LIST_OF_DIRECTORIES" && {
echo "Mocking is not set up properly."
echo "LIST_OF_DIRECTORIES should point to an existing file."
exit 1
}
function mktemp {
if test "${1:-}" = "-d";
then
echo "tempdir"
else
echo "tempfile"
fi
}
function wget {
echo "wget $@" >> $LIST_OF_ACTIONS
}
function mkdir {
if test "${1:-}" = "-p";
then
echo "$2" >> $LIST_OF_DIRECTORIES
fi
}
function unzip {
echo "Random rubbish from unzip"
echo "unzip $@" >> $LIST_OF_ACTIONS
}
function rm {
echo "rm $@" >> $LIST_OF_ACTIONS
}
function [ {
if test "${1:-}" = "-d";
then
echo "[ $@" >> $LIST_OF_ACTIONS
for directory in $(cat $LIST_OF_DIRECTORIES)
do
if test "$directory" = "$2"
then
return 0
fi
done
return 1
fi
echo "Mock test does not implement the requested function"
exit 1
}

134
tools/xen/test_functions.sh Executable file
View File

@ -0,0 +1,134 @@
#!/bin/bash
# Tests for functions.
#
# The tests are sourcing the mocks file to mock out various functions. The
# mocking-out always happens in a sub-shell, thus it does not have impact on
# the functions defined here.
# To run the tests, please run:
#
# ./test_functions.sh run_tests
#
# To only print out the discovered test functions, run:
#
# ./test_functions.sh
. functions
# Setup
function before_each_test {
LIST_OF_DIRECTORIES=$(mktemp)
truncate -s 0 $LIST_OF_DIRECTORIES
LIST_OF_ACTIONS=$(mktemp)
truncate -s 0 $LIST_OF_ACTIONS
}
# Teardown
function after_each_test {
rm -f $LIST_OF_DIRECTORIES
rm -f $LIST_OF_ACTIONS
}
# Helpers
function given_directory_exists {
echo "$1" >> $LIST_OF_DIRECTORIES
}
function assert_directory_exists {
grep "$1" $LIST_OF_DIRECTORIES
}
function assert_previous_command_failed {
[ "$?" != "0" ] || exit 1
}
# Tests
function test_plugin_directory_on_xenserver {
given_directory_exists "/etc/xapi.d/plugins/"
PLUGDIR=$(. mocks && xapi_plugin_location)
[ "/etc/xapi.d/plugins/" = "$PLUGDIR" ]
}
function test_plugin_directory_on_xcp {
given_directory_exists "/usr/lib/xcp/plugins/"
PLUGDIR=$(. mocks && xapi_plugin_location)
[ "/usr/lib/xcp/plugins/" = "$PLUGDIR" ]
}
function test_no_plugin_directory_found {
set +e
local IGNORE
IGNORE=$(. mocks && xapi_plugin_location)
assert_previous_command_failed
grep "[ -d /etc/xapi.d/plugins/ ]" $LIST_OF_ACTIONS
grep "[ -d /usr/lib/xcp/plugins/ ]" $LIST_OF_ACTIONS
}
function test_zip_snapshot_location {
diff \
<(zip_snapshot_location "https://github.com/openstack/nova.git" "master") \
<(echo "https://github.com/openstack/nova/zipball/master")
}
function test_create_directory_for_kernels {
(. mocks && create_directory_for_kernels)
assert_directory_exists "/boot/guest"
}
function test_extract_remote_zipball {
local RESULT=$(. mocks && extract_remote_zipball "someurl")
diff <(cat $LIST_OF_ACTIONS) - << EOF
wget -nv someurl -O tempfile --no-check-certificate
unzip -q -o tempfile -d tempdir
rm -f tempfile
EOF
[ "$RESULT" = "tempdir" ]
}
function test_find_nova_plugins {
local tmpdir=$(mktemp -d)
mkdir -p "$tmpdir/blah/blah/u/xapi.d/plugins"
[ "$tmpdir/blah/blah/u/xapi.d/plugins" = $(find_xapi_plugins_dir $tmpdir) ]
rm -rf $tmpdir
}
# Test runner
[ "$1" = "" ] && {
grep -e "^function *test_" $0 | cut -d" " -f2
}
[ "$1" = "run_tests" ] && {
for testname in $($0)
do
echo "$testname"
before_each_test
(
set -eux
$testname
)
if [ "$?" != "0" ]
then
echo "FAIL"
exit 1
else
echo "PASS"
fi
after_each_test
done
}