Browse code

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

Mate Lakat authored on 2013/03/23 01:34:05
Showing 4 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,55 @@
0
+#!/bin/bash
1
+
2
+function xapi_plugin_location {
3
+    for PLUGIN_DIR in "/etc/xapi.d/plugins/" "/usr/lib/xcp/plugins/"
4
+    do
5
+        if [ -d $PLUGIN_DIR ]
6
+        then
7
+            echo $PLUGIN_DIR
8
+            return 0
9
+        fi
10
+    done
11
+    return 1
12
+}
13
+
14
+function zip_snapshot_location {
15
+    echo $1 | sed "s:\.git$::;s:$:/zipball/$2:g"
16
+}
17
+
18
+function create_directory_for_kernels {
19
+    mkdir -p "/boot/guest"
20
+}
21
+
22
+function extract_remote_zipball {
23
+    local ZIPBALL_URL=$1
24
+
25
+    local LOCAL_ZIPBALL=$(mktemp)
26
+    local EXTRACTED_FILES=$(mktemp -d)
27
+
28
+    (
29
+        wget -nv $ZIPBALL_URL -O $LOCAL_ZIPBALL --no-check-certificate
30
+        unzip -q -o $LOCAL_ZIPBALL -d $EXTRACTED_FILES
31
+        rm -f $LOCAL_ZIPBALL
32
+    ) >&2
33
+
34
+    echo "$EXTRACTED_FILES"
35
+}
36
+
37
+function find_xapi_plugins_dir {
38
+    find $1 -path '*/xapi.d/plugins' -type d -print
39
+}
40
+
41
+function install_xapi_plugins_from_zipball {
42
+    local XAPI_PLUGIN_DIR
43
+    local EXTRACTED_FILES
44
+    local EXTRACTED_PLUGINS_DIR
45
+
46
+    XAPI_PLUGIN_DIR=$(xapi_plugin_location)
47
+
48
+    EXTRACTED_FILES=$(extract_remote_zipball $1)
49
+    EXTRACTED_PLUGINS_DIR=$(find_xapi_plugins_dir $EXTRACTED_FILES)
50
+
51
+    cp -pr $EXTRACTED_PLUGINS_DIR/* $XAPI_PLUGIN_DIR
52
+    rm -rf $EXTRACTED_FILES
53
+    chmod a+x ${XAPI_PLUGIN_DIR}*
54
+}
... ...
@@ -28,6 +28,9 @@ THIS_DIR=$(cd $(dirname "$0") && pwd)
28 28
 # Include onexit commands
29 29
 . $THIS_DIR/scripts/on_exit.sh
30 30
 
31
+# xapi functions
32
+. $THIS_DIR/functions
33
+
31 34
 
32 35
 #
33 36
 # Get Settings
... ...
@@ -43,48 +46,26 @@ xe_min()
43 43
   xe "$cmd" --minimal "$@"
44 44
 }
45 45
 
46
-
47 46
 #
48 47
 # Prepare Dom0
49 48
 # including installing XenAPI plugins
50 49
 #
51 50
 
52 51
 cd $THIS_DIR
53
-if [ -f ./master ]
54
-then
55
-    rm -rf ./master
56
-    rm -rf ./nova
57
-fi
58 52
 
59
-# get nova
60
-NOVA_ZIPBALL_URL=${NOVA_ZIPBALL_URL:-$(echo $NOVA_REPO | sed "s:\.git$::;s:$:/zipball/$NOVA_BRANCH:g")}
61
-wget -nv $NOVA_ZIPBALL_URL -O nova-zipball --no-check-certificate
62
-unzip -q -o nova-zipball  -d ./nova
53
+# Install plugins
63 54
 
64
-# install xapi plugins
65
-XAPI_PLUGIN_DIR=/etc/xapi.d/plugins/
66
-if [ ! -d $XAPI_PLUGIN_DIR ]; then
67
-    # the following is needed when using xcp-xapi
68
-    XAPI_PLUGIN_DIR=/usr/lib/xcp/plugins/
69
-fi
70
-cp -pr ./nova/*/plugins/xenserver/xenapi/etc/xapi.d/plugins/* $XAPI_PLUGIN_DIR
55
+## Nova plugins
56
+NOVA_ZIPBALL_URL=${NOVA_ZIPBALL_URL:-$(zip_snapshot_location $NOVA_REPO $NOVA_BRANCH)}
57
+install_xapi_plugins_from_zipball $NOVA_ZIPBALL_URL
71 58
 
72
-# Install the netwrap xapi plugin to support agent control of dom0 networking
59
+## Install the netwrap xapi plugin to support agent control of dom0 networking
73 60
 if [[ "$ENABLED_SERVICES" =~ "q-agt" && "$Q_PLUGIN" = "openvswitch" ]]; then
74
-    if [ -f ./quantum ]; then
75
-        rm -rf ./quantum
76
-    fi
77
-    # get quantum
78
-    QUANTUM_ZIPBALL_URL=${QUANTUM_ZIPBALL_URL:-$(echo $QUANTUM_REPO | sed "s:\.git$::;s:$:/zipball/$QUANTUM_BRANCH:g")}
79
-    wget -nv $QUANTUM_ZIPBALL_URL -O quantum-zipball --no-check-certificate
80
-    unzip -q -o quantum-zipball  -d ./quantum
81
-    cp -pr ./quantum/*/quantum/plugins/openvswitch/agent/xenapi/etc/xapi.d/plugins/* $XAPI_PLUGIN_DIR
61
+    QUANTUM_ZIPBALL_URL=${QUANTUM_ZIPBALL_URL:-$(zip_snapshot_location $QUANTUM_REPO $QUANTUM_BRANCH)}
62
+    install_xapi_plugins_from_zipball $QUANTUM_ZIPBALL_URL
82 63
 fi
83 64
 
84
-chmod a+x ${XAPI_PLUGIN_DIR}*
85
-
86
-mkdir -p /boot/guest
87
-
65
+create_directory_for_kernels
88 66
 
89 67
 #
90 68
 # Configure Networking
91 69
new file mode 100644
... ...
@@ -0,0 +1,59 @@
0
+#!/bin/bash
1
+
2
+test ! -e "$LIST_OF_ACTIONS" && {
3
+    echo "Mocking is not set up properly."
4
+    echo "LIST_OF_ACTIONS should point to an existing file."
5
+    exit 1
6
+}
7
+
8
+test ! -e "$LIST_OF_DIRECTORIES" && {
9
+    echo "Mocking is not set up properly."
10
+    echo "LIST_OF_DIRECTORIES should point to an existing file."
11
+    exit 1
12
+}
13
+
14
+function mktemp {
15
+    if test "${1:-}" = "-d";
16
+    then
17
+        echo "tempdir"
18
+    else
19
+        echo "tempfile"
20
+    fi
21
+}
22
+
23
+function wget {
24
+    echo "wget $@" >> $LIST_OF_ACTIONS
25
+}
26
+
27
+function mkdir {
28
+    if test "${1:-}" = "-p";
29
+    then
30
+        echo "$2" >> $LIST_OF_DIRECTORIES
31
+    fi
32
+}
33
+
34
+function unzip {
35
+    echo "Random rubbish from unzip"
36
+    echo "unzip $@" >> $LIST_OF_ACTIONS
37
+}
38
+
39
+function rm {
40
+    echo "rm $@" >> $LIST_OF_ACTIONS
41
+}
42
+
43
+function [ {
44
+    if test "${1:-}" = "-d";
45
+    then
46
+        echo "[ $@" >> $LIST_OF_ACTIONS
47
+        for directory in $(cat $LIST_OF_DIRECTORIES)
48
+        do
49
+            if test "$directory" = "$2"
50
+            then
51
+                return 0
52
+            fi
53
+        done
54
+        return 1
55
+    fi
56
+    echo "Mock test does not implement the requested function"
57
+    exit 1
58
+}
0 59
new file mode 100755
... ...
@@ -0,0 +1,134 @@
0
+#!/bin/bash
1
+
2
+# Tests for functions.
3
+#
4
+# The tests are sourcing the mocks file to mock out various functions. The
5
+# mocking-out always happens in a sub-shell, thus it does not have impact on
6
+# the functions defined here.
7
+
8
+# To run the tests, please run:
9
+#
10
+# ./test_functions.sh run_tests
11
+#
12
+# To only print out the discovered test functions, run:
13
+#
14
+# ./test_functions.sh
15
+
16
+. functions
17
+
18
+# Setup
19
+function before_each_test {
20
+    LIST_OF_DIRECTORIES=$(mktemp)
21
+    truncate -s 0 $LIST_OF_DIRECTORIES
22
+
23
+    LIST_OF_ACTIONS=$(mktemp)
24
+    truncate -s 0 $LIST_OF_ACTIONS
25
+}
26
+
27
+# Teardown
28
+function after_each_test {
29
+    rm -f $LIST_OF_DIRECTORIES
30
+    rm -f $LIST_OF_ACTIONS
31
+}
32
+
33
+# Helpers
34
+function given_directory_exists {
35
+    echo "$1" >> $LIST_OF_DIRECTORIES
36
+}
37
+
38
+function assert_directory_exists {
39
+    grep "$1" $LIST_OF_DIRECTORIES
40
+}
41
+
42
+function assert_previous_command_failed {
43
+    [ "$?" != "0" ] || exit 1
44
+}
45
+
46
+# Tests
47
+function test_plugin_directory_on_xenserver {
48
+    given_directory_exists "/etc/xapi.d/plugins/"
49
+
50
+    PLUGDIR=$(. mocks && xapi_plugin_location)
51
+
52
+    [ "/etc/xapi.d/plugins/" = "$PLUGDIR" ]
53
+}
54
+
55
+function test_plugin_directory_on_xcp {
56
+    given_directory_exists "/usr/lib/xcp/plugins/"
57
+
58
+    PLUGDIR=$(. mocks && xapi_plugin_location)
59
+
60
+    [ "/usr/lib/xcp/plugins/" = "$PLUGDIR" ]
61
+}
62
+
63
+function test_no_plugin_directory_found {
64
+    set +e
65
+
66
+    local IGNORE
67
+    IGNORE=$(. mocks && xapi_plugin_location)
68
+
69
+    assert_previous_command_failed
70
+
71
+    grep "[ -d /etc/xapi.d/plugins/ ]" $LIST_OF_ACTIONS
72
+    grep "[ -d /usr/lib/xcp/plugins/ ]" $LIST_OF_ACTIONS
73
+}
74
+
75
+function test_zip_snapshot_location {
76
+    diff \
77
+    <(zip_snapshot_location "https://github.com/openstack/nova.git" "master") \
78
+    <(echo "https://github.com/openstack/nova/zipball/master")
79
+}
80
+
81
+function test_create_directory_for_kernels {
82
+    (. mocks && create_directory_for_kernels)
83
+
84
+    assert_directory_exists "/boot/guest"
85
+}
86
+
87
+function test_extract_remote_zipball {
88
+    local RESULT=$(. mocks && extract_remote_zipball "someurl")
89
+
90
+    diff <(cat $LIST_OF_ACTIONS) - << EOF
91
+wget -nv someurl -O tempfile --no-check-certificate
92
+unzip -q -o tempfile -d tempdir
93
+rm -f tempfile
94
+EOF
95
+
96
+    [ "$RESULT" = "tempdir" ]
97
+}
98
+
99
+function test_find_nova_plugins {
100
+    local tmpdir=$(mktemp -d)
101
+
102
+    mkdir -p "$tmpdir/blah/blah/u/xapi.d/plugins"
103
+
104
+    [ "$tmpdir/blah/blah/u/xapi.d/plugins" = $(find_xapi_plugins_dir $tmpdir) ]
105
+
106
+    rm -rf $tmpdir
107
+}
108
+
109
+# Test runner
110
+[ "$1" = "" ] && {
111
+    grep -e "^function *test_" $0 | cut -d" " -f2
112
+}
113
+
114
+[ "$1" = "run_tests" ] && {
115
+    for testname in $($0)
116
+    do
117
+        echo "$testname"
118
+        before_each_test
119
+        (
120
+            set -eux
121
+            $testname
122
+        )
123
+        if [ "$?" != "0" ]
124
+        then
125
+            echo "FAIL"
126
+            exit 1
127
+        else
128
+            echo "PASS"
129
+        fi
130
+
131
+        after_each_test
132
+    done
133
+}