blob: 4ebb00085e4a778a0434453ea365155428751dfc [file] [log] [blame]
Sean Dague53753292014-12-04 19:38:15 -05001#!/usr/bin/env bash
2
Ian Wienand9b0ebc42015-04-17 13:06:47 +10003# Tests for DevStack functions
Sean Dague53753292014-12-04 19:38:15 -05004
5TOP=$(cd $(dirname "$0")/.. && pwd)
6
7# Import common functions
8source $TOP/functions
Sean Dague53753292014-12-04 19:38:15 -05009
Ian Wienand9b0ebc42015-04-17 13:06:47 +100010# Import configuration
11source $TOP/openrc
12
Ian Wienand09f4ad22015-04-17 13:13:04 +100013source $TOP/tests/unittest.sh
Ian Wienand9b0ebc42015-04-17 13:06:47 +100014
15echo "Testing die_if_not_set()"
16
Ian Wienand9b845da2015-04-17 13:10:33 +100017bash -c "source $TOP/functions; X=`echo Y && true`; die_if_not_set $LINENO X 'not OK'"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100018if [[ $? != 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100019 failed "die_if_not_set [X='Y' true] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100020else
Ian Wienand09f4ad22015-04-17 13:13:04 +100021 passed 'OK'
Ian Wienand9b0ebc42015-04-17 13:06:47 +100022fi
23
Ian Wienand9b845da2015-04-17 13:10:33 +100024bash -c "source $TOP/functions; X=`true`; die_if_not_set $LINENO X 'OK'" > /dev/null 2>&1
Ian Wienand9b0ebc42015-04-17 13:06:47 +100025if [[ $? = 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100026 failed "die_if_not_set [X='' true] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100027fi
28
Ian Wienand9b845da2015-04-17 13:10:33 +100029bash -c "source $TOP/functions; X=`echo Y && false`; die_if_not_set $LINENO X 'not OK'"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100030if [[ $? != 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100031 failed "die_if_not_set [X='Y' false] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100032else
Ian Wienand09f4ad22015-04-17 13:13:04 +100033 passed 'OK'
Ian Wienand9b0ebc42015-04-17 13:06:47 +100034fi
35
Ian Wienand9b845da2015-04-17 13:10:33 +100036bash -c "source $TOP/functions; X=`false`; die_if_not_set $LINENO X 'OK'" > /dev/null 2>&1
Ian Wienand9b0ebc42015-04-17 13:06:47 +100037if [[ $? = 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100038 failed "die_if_not_set [X='' false] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100039fi
40
41
42# Enabling/disabling services
43
44echo "Testing enable_service()"
45
46function test_enable_service {
47 local start="$1"
48 local add="$2"
49 local finish="$3"
50
51 ENABLED_SERVICES="$start"
52 enable_service $add
53 if [ "$ENABLED_SERVICES" = "$finish" ]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100054 passed "OK: $start + $add -> $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100055 else
Ian Wienand09f4ad22015-04-17 13:13:04 +100056 failed "changing $start to $finish with $add failed: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100057 fi
Sean Dague53753292014-12-04 19:38:15 -050058}
59
Ian Wienand9b0ebc42015-04-17 13:06:47 +100060test_enable_service '' a 'a'
61test_enable_service 'a' b 'a,b'
62test_enable_service 'a,b' c 'a,b,c'
63test_enable_service 'a,b' c 'a,b,c'
64test_enable_service 'a,b,' c 'a,b,c'
65test_enable_service 'a,b' c,d 'a,b,c,d'
66test_enable_service 'a,b' "c d" 'a,b,c,d'
67test_enable_service 'a,b,c' c 'a,b,c'
Sean Dague53753292014-12-04 19:38:15 -050068
Ian Wienand9b0ebc42015-04-17 13:06:47 +100069test_enable_service 'a,b,-c' c 'a,b'
70test_enable_service 'a,b,c' -c 'a,b'
71
72function test_disable_service {
73 local start="$1"
74 local del="$2"
75 local finish="$3"
76
77 ENABLED_SERVICES="$start"
78 disable_service "$del"
79 if [ "$ENABLED_SERVICES" = "$finish" ]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100080 passed "OK: $start - $del -> $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100081 else
Ian Wienand09f4ad22015-04-17 13:13:04 +100082 failed "changing $start to $finish with $del failed: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100083 fi
84}
85
86echo "Testing disable_service()"
87test_disable_service 'a,b,c' a 'b,c'
88test_disable_service 'a,b,c' b 'a,c'
89test_disable_service 'a,b,c' c 'a,b'
90
91test_disable_service 'a,b,c' a 'b,c'
92test_disable_service 'b,c' b 'c'
93test_disable_service 'c' c ''
94test_disable_service '' d ''
95
96test_disable_service 'a,b,c,' c 'a,b'
97test_disable_service 'a,b' c 'a,b'
98
99
100echo "Testing disable_all_services()"
101ENABLED_SERVICES=a,b,c
102disable_all_services
103
104if [[ -z "$ENABLED_SERVICES" ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000105 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000106else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000107 failed "disabling all services FAILED: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000108fi
109
110echo "Testing disable_negated_services()"
111
112
113function test_disable_negated_services {
114 local start="$1"
115 local finish="$2"
116
117 ENABLED_SERVICES="$start"
118 disable_negated_services
119 if [ "$ENABLED_SERVICES" = "$finish" ]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000120 passed "OK: $start + $add -> $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000121 else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000122 failed "changing $start to $finish failed: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000123 fi
124}
125
126test_disable_negated_services '-a' ''
127test_disable_negated_services '-a,a' ''
128test_disable_negated_services '-a,-a' ''
129test_disable_negated_services 'a,-a' ''
130test_disable_negated_services 'b,a,-a' 'b'
131test_disable_negated_services 'a,b,-a' 'b'
132test_disable_negated_services 'a,-a,b' 'b'
Ian Wienand2796a822015-04-15 08:59:04 +1000133test_disable_negated_services 'a,aa,-a' 'aa'
134test_disable_negated_services 'aa,-a' 'aa'
135test_disable_negated_services 'a_a, -a_a' ''
136test_disable_negated_services 'a-b, -a-b' ''
137test_disable_negated_services 'a-b, b, -a-b' 'b'
138test_disable_negated_services 'a,-a,av2,b' 'av2,b'
139test_disable_negated_services 'a,aa,-a' 'aa'
140test_disable_negated_services 'a,av2,-a,a' 'av2'
141test_disable_negated_services 'a,-a,av2' 'av2'
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000142
143echo "Testing is_package_installed()"
144
145if [[ -z "$os_PACKAGE" ]]; then
146 GetOSVersion
147fi
148
149if [[ "$os_PACKAGE" = "deb" ]]; then
150 is_package_installed dpkg
151 VAL=$?
152elif [[ "$os_PACKAGE" = "rpm" ]]; then
153 is_package_installed rpm
154 VAL=$?
155else
156 VAL=1
157fi
158if [[ "$VAL" -eq 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000159 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000160else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000161 failed "is_package_installed() on existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000162fi
163
164if [[ "$os_PACKAGE" = "deb" ]]; then
165 is_package_installed dpkg bash
166 VAL=$?
167elif [[ "$os_PACKAGE" = "rpm" ]]; then
168 is_package_installed rpm bash
169 VAL=$?
170else
171 VAL=1
172fi
173if [[ "$VAL" -eq 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000174 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000175else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000176 failed "is_package_installed() on more than one existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000177fi
178
179is_package_installed zzzZZZzzz
180VAL=$?
181if [[ "$VAL" -ne 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000182 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000183else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000184 failed "is_package_installed() on non-existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000185fi
186
187# test against removed package...was a bug on Ubuntu
188if is_ubuntu; then
189 PKG=cowsay
190 if ! (dpkg -s $PKG >/dev/null 2>&1); then
191 # it was never installed...set up the condition
192 sudo apt-get install -y cowsay >/dev/null 2>&1
193 fi
194 if (dpkg -s $PKG >/dev/null 2>&1); then
195 # remove it to create the 'un' status
196 sudo dpkg -P $PKG >/dev/null 2>&1
197 fi
198
199 # now test the installed check on a deleted package
200 is_package_installed $PKG
201 VAL=$?
202 if [[ "$VAL" -ne 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000203 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000204 else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000205 failed "is_package_installed() on deleted package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000206 fi
207fi
208
209# test isset function
210echo "Testing isset()"
211you_should_not_have_this_variable=42
212
213if isset "you_should_not_have_this_variable"; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000214 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000215else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000216 failed "\"you_should_not_have_this_variable\" not declared. failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000217fi
218
219unset you_should_not_have_this_variable
220if isset "you_should_not_have_this_variable"; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000221 failed "\"you_should_not_have_this_variable\" looks like declared variable."
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000222else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000223 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000224fi
Ian Wienand09f4ad22015-04-17 13:13:04 +1000225
226report_results