Browse code

Improve exercise robustness

* Test returns and exit codes on most command invocations
* Add start and end banners to make output easier to find in
long log files
* Adds die_if_error(), die_if_not_set() and is_set() to functions
* Add some function tests

Fixes bug 944593

Change-Id: I55e2962c5fec9aad237b674732b1e922ad37a62e

Dean Troyer authored on 2012/03/03 01:44:29
Showing 8 changed files
... ...
@@ -2,7 +2,10 @@
2 2
 
3 3
 # we will use the ``euca2ools`` cli tool that wraps the python boto
4 4
 # library to test ec2 compatibility
5
-#
5
+
6
+echo "**************************************************"
7
+echo "Begin DevStack Exercise: $0"
8
+echo "**************************************************"
6 9
 
7 10
 # This script exits on an error so that errors don't compound and you see
8 11
 # only the first error that occured.
... ...
@@ -16,7 +19,12 @@ set -o xtrace
16 16
 # ========
17 17
 
18 18
 # Use openrc + stackrc + localrc for settings
19
-pushd $(cd $(dirname "$0")/.. && pwd)
19
+pushd $(cd $(dirname "$0")/.. && pwd) >/dev/null
20
+
21
+# Import common functions
22
+source ./functions
23
+
24
+# Import configuration
20 25
 source ./openrc
21 26
 
22 27
 # Remove old certificates
... ...
@@ -27,7 +35,7 @@ rm -f pk.pem
27 27
 # Get Certificates
28 28
 nova x509-get-root-cert
29 29
 nova x509-create-cert
30
-popd
30
+popd >/dev/null
31 31
 
32 32
 # Max time to wait for image to be registered
33 33
 REGISTER_TIMEOUT=${REGISTER_TIMEOUT:-15}
... ...
@@ -36,10 +44,14 @@ BUCKET=testbucket
36 36
 IMAGE=bundle.img
37 37
 truncate -s 5M /tmp/$IMAGE
38 38
 euca-bundle-image -i /tmp/$IMAGE
39
+die_if_error "Failure bundling image $IMAGE"
39 40
 
40 41
 
41 42
 euca-upload-bundle -b $BUCKET -m /tmp/$IMAGE.manifest.xml
43
+die_if_error "Failure uploading bundle $IMAGE to $BUCKET"
44
+
42 45
 AMI=`euca-register $BUCKET/$IMAGE.manifest.xml | cut -f2`
46
+die_if_not_set AMI "Failure registering $BUCKET/$IMAGE"
43 47
 
44 48
 # Wait for the image to become available
45 49
 if ! timeout $REGISTER_TIMEOUT sh -c "while euca-describe-images | grep '$AMI' | grep 'available'; do sleep 1; done"; then
... ...
@@ -49,3 +61,9 @@ fi
49 49
 
50 50
 # Clean up
51 51
 euca-deregister $AMI
52
+die_if_error "Failure deregistering $AMI"
53
+
54
+set +o xtrace
55
+echo "**************************************************"
56
+echo "End DevStack Exercise: $0"
57
+echo "**************************************************"
... ...
@@ -2,6 +2,10 @@
2 2
 
3 3
 # Test OpenStack client enviroment variable handling
4 4
 
5
+echo "**************************************************"
6
+echo "Begin DevStack Exercise: $0"
7
+echo "**************************************************"
8
+
5 9
 # Verify client workage
6 10
 VERIFY=${1:-""}
7 11
 
... ...
@@ -10,6 +14,11 @@ VERIFY=${1:-""}
10 10
 
11 11
 # Use openrc + stackrc + localrc for settings
12 12
 pushd $(cd $(dirname "$0")/.. && pwd) >/dev/null
13
+
14
+# Import common functions
15
+source ./functions
16
+
17
+# Import configuration
13 18
 source ./openrc
14 19
 popd >/dev/null
15 20
 
... ...
@@ -23,19 +32,10 @@ unset NOVA_URL
23 23
 unset NOVA_USERNAME
24 24
 unset NOVA_VERSION
25 25
 
26
-# Make sure we have the vars we are expecting
27
-function is_set() {
28
-    local var=\$"$1"
29
-    eval echo $1=$var
30
-    if eval "[ -z $var ]"; then
31
-        return 1
32
-    fi
33
-    return 0
34
-}
35
-
36 26
 for i in OS_TENANT_NAME OS_USERNAME OS_PASSWORD OS_AUTH_URL; do
37 27
     is_set $i
38 28
     if [[ $? -ne 0 ]]; then
29
+        echo "$i expected to be set"
39 30
         ABORT=1
40 31
     fi
41 32
 done
... ...
@@ -52,14 +52,6 @@ if [[ "$ENABLED_SERVICES" =~ "key" ]]; then
52 52
     if [[ "$SKIP_EXERCISES" =~ "key" ]] ; then
53 53
         STATUS_KEYSTONE="Skipped"
54 54
     else
55
-        # We need to run the keystone test as admin since there doesn't
56
-        # seem to be anything to test the cli vars that runs as a user
57
-        # tenant-list should do that, it isn't implemented (yet)
58
-        xOS_TENANT_NAME=$OS_TENANT_NAME
59
-        xOS_USERNAME=$OS_USERNAME
60
-        export OS_USERNAME=admin
61
-        export OS_TENANT_NAME=admin
62
-
63 55
         echo -e "\nTest Keystone"
64 56
         if keystone service-list; then
65 57
             STATUS_KEYSTONE="Succeeded"
... ...
@@ -67,9 +59,6 @@ if [[ "$ENABLED_SERVICES" =~ "key" ]]; then
67 67
             STATUS_KEYSTONE="Failed"
68 68
             RETURN=1
69 69
         fi
70
-
71
-        OS_TENANT_NAME=$xOS_TENANT_NAME
72
-        OS_USERNAME=$xOS_USERNAME
73 70
     fi
74 71
 fi
75 72
 
... ...
@@ -139,4 +128,8 @@ report "Nova" $STATUS_NOVA
139 139
 report "Glance" $STATUS_GLANCE
140 140
 report "Swift" $STATUS_SWIFT
141 141
 
142
+echo "**************************************************"
143
+echo "End DevStack Exercise: $0"
144
+echo "**************************************************"
145
+
142 146
 exit $RETURN
... ...
@@ -2,7 +2,10 @@
2 2
 
3 3
 # we will use the ``euca2ools`` cli tool that wraps the python boto
4 4
 # library to test ec2 compatibility
5
-#
5
+
6
+echo "**************************************************"
7
+echo "Begin DevStack Exercise: $0"
8
+echo "**************************************************"
6 9
 
7 10
 # This script exits on an error so that errors don't compound and you see
8 11
 # only the first error that occured.
... ...
@@ -16,9 +19,14 @@ set -o xtrace
16 16
 # ========
17 17
 
18 18
 # Use openrc + stackrc + localrc for settings
19
-pushd $(cd $(dirname "$0")/.. && pwd)
19
+pushd $(cd $(dirname "$0")/.. && pwd) >/dev/null
20
+
21
+# Import common functions
22
+source ./functions
23
+
24
+# Import configuration
20 25
 source ./openrc
21
-popd
26
+popd >/dev/null
22 27
 
23 28
 # Max time to wait while vm goes from build to active state
24 29
 ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30}
... ...
@@ -49,6 +57,7 @@ fi
49 49
 
50 50
 # Launch it
51 51
 INSTANCE=`euca-run-instances -g $SECGROUP -t $DEFAULT_INSTANCE_TYPE $IMAGE | grep INSTANCE | cut -f2`
52
+die_if_not_set INSTANCE "Failure launching instance"
52 53
 
53 54
 # Assure it has booted within a reasonable time
54 55
 if ! timeout $RUNNING_TIMEOUT sh -c "while ! euca-describe-instances $INSTANCE | grep -q running; do sleep 1; done"; then
... ...
@@ -58,12 +67,15 @@ fi
58 58
 
59 59
 # Allocate floating address
60 60
 FLOATING_IP=`euca-allocate-address | cut -f2`
61
+die_if_not_set FLOATING_IP "Failure allocating floating IP"
61 62
 
62 63
 # Associate floating address
63 64
 euca-associate-address -i $INSTANCE $FLOATING_IP
65
+die_if_error "Failure associating address $FLOATING_IP to $INSTANCE"
64 66
 
65 67
 # Authorize pinging
66 68
 euca-authorize -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP
69
+die_if_error "Failure authorizing rule in $SECGROUP"
67 70
 
68 71
 # Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
69 72
 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
... ...
@@ -73,9 +85,11 @@ fi
73 73
 
74 74
 # Revoke pinging
75 75
 euca-revoke -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP
76
+die_if_error "Failure revoking rule in $SECGROUP"
76 77
 
77 78
 # Release floating address
78 79
 euca-disassociate-address $FLOATING_IP
80
+die_if_error "Failure disassociating address $FLOATING_IP"
79 81
 
80 82
 # Wait just a tick for everything above to complete so release doesn't fail
81 83
 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep $INSTANCE | grep -q $FLOATING_IP; do sleep 1; done"; then
... ...
@@ -85,6 +99,7 @@ fi
85 85
 
86 86
 # Release floating address
87 87
 euca-release-address $FLOATING_IP
88
+die_if_error "Failure releasing address $FLOATING_IP"
88 89
 
89 90
 # Wait just a tick for everything above to complete so terminate doesn't fail
90 91
 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep -q $FLOATING_IP; do sleep 1; done"; then
... ...
@@ -94,6 +109,7 @@ fi
94 94
 
95 95
 # Terminate instance
96 96
 euca-terminate-instances $INSTANCE
97
+die_if_error "Failure terminating instance $INSTANCE"
97 98
 
98 99
 # Assure it has terminated within a reasonable time
99 100
 if ! timeout $TERMINATE_TIMEOUT sh -c "while euca-describe-instances $INSTANCE | grep -q running; do sleep 1; done"; then
... ...
@@ -103,3 +119,9 @@ fi
103 103
 
104 104
 # Delete group
105 105
 euca-delete-group $SECGROUP
106
+die_if_error "Failure deleting security group $SECGROUP"
107
+
108
+set +o xtrace
109
+echo "**************************************************"
110
+echo "End DevStack Exercise: $0"
111
+echo "**************************************************"
... ...
@@ -7,6 +7,10 @@
7 7
 #
8 8
 
9 9
 
10
+echo "**************************************************"
11
+echo "Begin DevStack Exercise: $0"
12
+echo "**************************************************"
13
+
10 14
 # This script exits on an error so that errors don't compound and you see
11 15
 # only the first error that occured.
12 16
 set -o errexit
... ...
@@ -20,9 +24,14 @@ set -o xtrace
20 20
 # ========
21 21
 
22 22
 # Use openrc + stackrc + localrc for settings
23
-pushd $(cd $(dirname "$0")/.. && pwd)
23
+pushd $(cd $(dirname "$0")/.. && pwd) >/dev/null
24
+
25
+# Import common functions
26
+source ./functions
27
+
28
+# Import configuration
24 29
 source ./openrc
25
-popd
30
+popd >/dev/null
26 31
 
27 32
 # Max time to wait while vm goes from build to active state
28 33
 ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30}
... ...
@@ -87,15 +96,16 @@ fi
87 87
 # List of instance types:
88 88
 nova flavor-list
89 89
 
90
-INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
90
+INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
91 91
 if [[ -z "$INSTANCE_TYPE" ]]; then
92 92
     # grab the first flavor in the list to launch if default doesn't exist
93
-   INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2`
93
+   INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
94 94
 fi
95 95
 
96
-NAME="myserver"
96
+NAME="ex-float"
97 97
 
98
-VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'`
98
+VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
99
+die_if_not_set VM_UUID "Failure launching $NAME"
99 100
 
100 101
 # Testing
101 102
 # =======
... ...
@@ -114,7 +124,8 @@ if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | g
114 114
 fi
115 115
 
116 116
 # get the IP of the server
117
-IP=`nova show $VM_UUID | grep "private network" | cut -d"|" -f3`
117
+IP=`nova show $VM_UUID | grep "private network" | get_field 2`
118
+die_if_not_set IP "Failure retrieving IP address"
118 119
 
119 120
 # for single node deployments, we can ping private ips
120 121
 MULTI_HOST=${MULTI_HOST:-0}
... ...
@@ -147,7 +158,8 @@ fi
147 147
 nova secgroup-list-rules $SECGROUP
148 148
 
149 149
 # allocate a floating ip from default pool
150
-FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | cut -d '|' -f2`
150
+FLOATING_IP=`nova floating-ip-create | grep $DEFAULT_FLOATING_POOL | get_field 1`
151
+die_if_not_set FLOATING_IP "Failure creating floating IP"
151 152
 
152 153
 # list floating addresses
153 154
 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep -q $FLOATING_IP; do sleep 1; done"; then
... ...
@@ -157,6 +169,7 @@ fi
157 157
 
158 158
 # add floating ip to our server
159 159
 nova add-floating-ip $VM_UUID $FLOATING_IP
160
+die_if_error "Failure adding floating IP $FLOATING_IP to $NAME"
160 161
 
161 162
 # test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
162 163
 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
... ...
@@ -165,7 +178,8 @@ if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sle
165 165
 fi
166 166
 
167 167
 # Allocate an IP from second floating pool
168
-TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | cut -d '|' -f2`
168
+TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | get_field 1`
169
+die_if_not_set TEST_FLOATING_IP "Failure creating floating IP in $TEST_FLOATING_POOL"
169 170
 
170 171
 # list floating addresses
171 172
 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep $TEST_FLOATING_POOL | grep -q $TEST_FLOATING_IP; do sleep 1; done"; then
... ...
@@ -175,6 +189,7 @@ fi
175 175
 
176 176
 # dis-allow icmp traffic (ping)
177 177
 nova secgroup-delete-rule $SECGROUP icmp -1 -1 0.0.0.0/0
178
+die_if_error "Failure deleting security group rule from $SECGROUP"
178 179
 
179 180
 # FIXME (anthony): make xs support security groups
180 181
 if [ "$VIRT_DRIVER" != "xenserver" ]; then
... ...
@@ -188,12 +203,15 @@ fi
188 188
 
189 189
 # de-allocate the floating ip
190 190
 nova floating-ip-delete $FLOATING_IP
191
+die_if_error "Failure deleting floating IP $FLOATING_IP"
191 192
 
192 193
 # Delete second floating IP
193 194
 nova floating-ip-delete $TEST_FLOATING_IP
195
+die_if_error "Failure deleting floating IP $TEST_FLOATING_IP"
194 196
 
195 197
 # shutdown the server
196 198
 nova delete $VM_UUID
199
+die_if_error "Failure deleting instance $NAME"
197 200
 
198 201
 # make sure the VM shuts down within a reasonable time
199 202
 if ! timeout $TERMINATE_TIMEOUT sh -c "while nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
... ...
@@ -203,3 +221,9 @@ fi
203 203
 
204 204
 # Delete a secgroup
205 205
 nova secgroup-delete $SECGROUP
206
+die_if_error "Failure deleting security group $SECGROUP"
207
+
208
+set +o xtrace
209
+echo "**************************************************"
210
+echo "End DevStack Exercise: $0"
211
+echo "**************************************************"
... ...
@@ -2,6 +2,10 @@
2 2
 
3 3
 # Test swift via the command line tools that ship with it.
4 4
 
5
+echo "**************************************************"
6
+echo "Begin DevStack Exercise: $0"
7
+echo "**************************************************"
8
+
5 9
 # This script exits on an error so that errors don't compound and you see
6 10
 # only the first error that occured.
7 11
 set -o errexit
... ...
@@ -15,9 +19,17 @@ set -o xtrace
15 15
 # ========
16 16
 
17 17
 # Use openrc + stackrc + localrc for settings
18
-pushd $(cd $(dirname "$0")/.. && pwd)
18
+pushd $(cd $(dirname "$0")/.. && pwd) >/dev/null
19
+
20
+# Import common functions
21
+source ./functions
22
+
23
+# Import configuration
19 24
 source ./openrc
20
-popd
25
+popd >/dev/null
26
+
27
+# Container name
28
+CONTAINER=ex-swift
21 29
 
22 30
 
23 31
 # Testing Swift
... ...
@@ -25,16 +37,26 @@ popd
25 25
 
26 26
 # Check if we have to swift via keystone
27 27
 swift stat
28
+die_if_error "Failure geting status"
28 29
 
29 30
 # We start by creating a test container
30
-swift post testcontainer
31
+swift post $CONTAINER
32
+die_if_error "Failure creating container $CONTAINER"
31 33
 
32 34
 # add some files into it.
33
-swift upload testcontainer /etc/issue
35
+swift upload $CONTAINER /etc/issue
36
+die_if_error "Failure uploading file to container $CONTAINER"
34 37
 
35 38
 # list them
36
-swift list testcontainer
39
+swift list $CONTAINER
40
+die_if_error "Failure listing contents of container $CONTAINER"
37 41
 
38 42
 # And we may want to delete them now that we have tested that
39 43
 # everything works.
40
-swift delete testcontainer
44
+swift delete $CONTAINER
45
+die_if_error "Failure deleting container $CONTAINER"
46
+
47
+set +o xtrace
48
+echo "**************************************************"
49
+echo "End DevStack Exercise: $0"
50
+echo "**************************************************"
... ...
@@ -2,6 +2,10 @@
2 2
 
3 3
 # Test nova volumes with the nova command from python-novaclient
4 4
 
5
+echo "**************************************************"
6
+echo "Begin DevStack Exercise: $0"
7
+echo "**************************************************"
8
+
5 9
 # This script exits on an error so that errors don't compound and you see
6 10
 # only the first error that occured.
7 11
 set -o errexit
... ...
@@ -15,9 +19,14 @@ set -o xtrace
15 15
 # ========
16 16
 
17 17
 # Use openrc + stackrc + localrc for settings
18
-pushd $(cd $(dirname "$0")/.. && pwd)
18
+pushd $(cd $(dirname "$0")/.. && pwd) >/dev/null
19
+
20
+# Import common functions
21
+source ./functions
22
+
23
+# Import configuration
19 24
 source ./openrc
20
-popd
25
+popd >/dev/null
21 26
 
22 27
 # Max time to wait while vm goes from build to active state
23 28
 ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30}
... ...
@@ -55,21 +64,6 @@ IMAGE=`glance -f index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1`
55 55
 # determinine instance type
56 56
 # -------------------------
57 57
 
58
-# Helper function to grab a numbered field from python novaclient cli result
59
-# Fields are numbered starting with 1
60
-# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
61
-function get_field () {
62
-    while read data
63
-    do
64
-        if [ "$1" -lt 0 ]; then
65
-            field="(\$(NF$1))"
66
-        else
67
-            field="\$$(($1 + 1))"
68
-        fi
69
-        echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
70
-    done
71
-}
72
-
73 58
 # List of instance types:
74 59
 nova flavor-list
75 60
 
... ...
@@ -79,9 +73,11 @@ if [[ -z "$INSTANCE_TYPE" ]]; then
79 79
    INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
80 80
 fi
81 81
 
82
-NAME="myserver"
82
+NAME="ex-vol"
83 83
 
84 84
 VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
85
+die_if_not_set VM_UUID "Failure launching $NAME"
86
+
85 87
 
86 88
 # Testing
87 89
 # =======
... ...
@@ -101,6 +97,7 @@ fi
101 101
 
102 102
 # get the IP of the server
103 103
 IP=`nova show $VM_UUID | grep "private network" | get_field 2`
104
+die_if_not_set IP "Failure retrieving IP address"
104 105
 
105 106
 # for single node deployments, we can ping private ips
106 107
 MULTI_HOST=${MULTI_HOST:-0}
... ...
@@ -130,6 +127,10 @@ fi
130 130
 
131 131
 # Create a new volume
132 132
 nova volume-create --display_name $VOL_NAME --display_description "test volume: $VOL_NAME" 1
133
+if [[ $? != 0 ]]; then
134
+    echo "Failure creating volume $VOL_NAME"
135
+    exit 1
136
+fi
133 137
 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
134 138
     echo "Volume $VOL_NAME not created"
135 139
     exit 1
... ...
@@ -137,16 +138,19 @@ fi
137 137
 
138 138
 # Get volume ID
139 139
 VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | get_field 1`
140
+die_if_not_set VOL_ID "Failure retrieving volume ID for $VOL_NAME"
140 141
 
141 142
 # Attach to server
142 143
 DEVICE=/dev/vdb
143 144
 nova volume-attach $VM_UUID $VOL_ID $DEVICE
145
+die_if_error "Failure attaching volume $VOL_NAME to $NAME"
144 146
 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep in-use; do sleep 1; done"; then
145 147
     echo "Volume $VOL_NAME not attached to $NAME"
146 148
     exit 1
147 149
 fi
148 150
 
149 151
 VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | get_field -1`
152
+die_if_not_set VOL_ATTACH "Failure retrieving $VOL_NAME status"
150 153
 if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
151 154
     echo "Volume not attached to correct instance"
152 155
     exit 1
... ...
@@ -154,6 +158,7 @@ fi
154 154
 
155 155
 # Detach volume
156 156
 nova volume-detach $VM_UUID $VOL_ID
157
+die_if_error "Failure detaching volume $VOL_NAME from $NAME"
157 158
 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME | grep available; do sleep 1; done"; then
158 159
     echo "Volume $VOL_NAME not detached from $NAME"
159 160
     exit 1
... ...
@@ -161,6 +166,7 @@ fi
161 161
 
162 162
 # Delete volume
163 163
 nova volume-delete $VOL_ID
164
+die_if_error "Failure deleting volume $VOL_NAME"
164 165
 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME; do sleep 1; done"; then
165 166
     echo "Volume $VOL_NAME not deleted"
166 167
     exit 1
... ...
@@ -168,3 +174,9 @@ fi
168 168
 
169 169
 # shutdown the server
170 170
 nova delete $NAME
171
+die_if_error "Failure deleting instance $NAME"
172
+
173
+set +o xtrace
174
+echo "**************************************************"
175
+echo "End DevStack Exercise: $0"
176
+echo "**************************************************"
... ...
@@ -22,6 +22,48 @@ function cp_it {
22 22
 }
23 23
 
24 24
 
25
+# Checks the exit code of the last command and prints "message"
26
+# if it is non-zero and exits
27
+# die_if_error "message"
28
+function die_if_error() {
29
+    local exitcode=$?
30
+    if [ $exitcode != 0 ]; then
31
+        echo $@
32
+        exit $exitcode
33
+    fi
34
+}
35
+
36
+
37
+# Checks an environment variable is not set or has length 0 OR if the
38
+# exit code is non-zero and prints "message" and exits
39
+# NOTE: env-var is the variable name without a '$'
40
+# die_if_not_set env-var "message"
41
+function die_if_not_set() {
42
+    local exitcode=$?
43
+    local evar=$1; shift
44
+    if ! is_set $evar || [ $exitcode != 0 ]; then
45
+        echo $@
46
+        exit 99
47
+    fi
48
+}
49
+
50
+
51
+# Grab a numbered field from python prettytable output
52
+# Fields are numbered starting with 1
53
+# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
54
+# get_field field-number
55
+function get_field() {
56
+    while read data; do
57
+        if [ "$1" -lt 0 ]; then
58
+            field="(\$(NF$1))"
59
+        else
60
+            field="\$$(($1 + 1))"
61
+        fi
62
+        echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
63
+    done
64
+}
65
+
66
+
25 67
 # git clone only if directory doesn't exist already.  Since ``DEST`` might not
26 68
 # be owned by the installation user, we create the directory and change the
27 69
 # ownership to the proper user.
... ...
@@ -67,6 +109,18 @@ function git_clone {
67 67
 }
68 68
 
69 69
 
70
+
71
+# Test if the named environment variable is set and not zero length
72
+# is_set env-var
73
+function is_set() {
74
+    local var=\$"$1"
75
+    if eval "[ -z $var ]"; then
76
+        return 1
77
+    fi
78
+    return 0
79
+}
80
+
81
+
70 82
 # pip install wrapper to set cache and proxy environment variables
71 83
 # pip_install package [package ...]
72 84
 function pip_install {
73 85
new file mode 100755
... ...
@@ -0,0 +1,54 @@
0
+#!/usr/bin/env bash
1
+
2
+# Tests for DevStack functions
3
+
4
+TOP=$(cd $(dirname "$0")/.. && pwd)
5
+
6
+# Import common functions
7
+source $TOP/functions
8
+
9
+# Import configuration
10
+source $TOP/openrc
11
+
12
+
13
+echo "Testing die_if_error()"
14
+
15
+bash -c "source $TOP/functions; true; die_if_error 'not OK'"
16
+if [[ $? != 0 ]]; then
17
+    echo "die_if_error [true] Failed"
18
+fi
19
+
20
+bash -c "source $TOP/functions; false; die_if_error 'OK'"
21
+if [[ $? = 0 ]]; then
22
+    echo "die_if_error [false] Failed"
23
+else
24
+    echo 'OK'
25
+fi
26
+
27
+
28
+echo "Testing die_if_not_set()"
29
+
30
+bash -c "source $TOP/functions; X=`echo Y && true`; die_if_not_set X 'not OK'"
31
+if [[ $? != 0 ]]; then
32
+    echo "die_if_not_set [X='Y' true] Failed"
33
+else
34
+    echo 'OK'
35
+fi
36
+
37
+bash -c "source $TOP/functions; X=`true`; die_if_not_set X 'OK'"
38
+if [[ $? = 0 ]]; then
39
+    echo "die_if_not_set [X='' true] Failed"
40
+fi
41
+
42
+bash -c "source $TOP/functions; X=`echo Y && false`; die_if_not_set X 'not OK'"
43
+if [[ $? != 0 ]]; then
44
+    echo "die_if_not_set [X='Y' false] Failed"
45
+else
46
+    echo 'OK'
47
+fi
48
+
49
+bash -c "source $TOP/functions; X=`false`; die_if_not_set X 'OK'"
50
+if [[ $? = 0 ]]; then
51
+    echo "die_if_not_set [X='' false] Failed"
52
+fi
53
+