blob: f555de8dff04cc988b0aad7c4f92abc8301aadbe [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 Wienand09f4ad22015-04-17 13:13:04 +100010source $TOP/tests/unittest.sh
Ian Wienand9b0ebc42015-04-17 13:06:47 +100011
12echo "Testing die_if_not_set()"
13
Ian Wienand9b845da2015-04-17 13:10:33 +100014bash -c "source $TOP/functions; X=`echo Y && true`; die_if_not_set $LINENO X 'not OK'"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100015if [[ $? != 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100016 failed "die_if_not_set [X='Y' true] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100017else
Ian Wienand09f4ad22015-04-17 13:13:04 +100018 passed 'OK'
Ian Wienand9b0ebc42015-04-17 13:06:47 +100019fi
20
Ian Wienand9b845da2015-04-17 13:10:33 +100021bash -c "source $TOP/functions; X=`true`; die_if_not_set $LINENO X 'OK'" > /dev/null 2>&1
Ian Wienand9b0ebc42015-04-17 13:06:47 +100022if [[ $? = 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100023 failed "die_if_not_set [X='' true] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100024fi
25
Ian Wienand9b845da2015-04-17 13:10:33 +100026bash -c "source $TOP/functions; X=`echo Y && false`; die_if_not_set $LINENO X 'not OK'"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100027if [[ $? != 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100028 failed "die_if_not_set [X='Y' false] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100029else
Ian Wienand09f4ad22015-04-17 13:13:04 +100030 passed 'OK'
Ian Wienand9b0ebc42015-04-17 13:06:47 +100031fi
32
Ian Wienand9b845da2015-04-17 13:10:33 +100033bash -c "source $TOP/functions; X=`false`; die_if_not_set $LINENO X 'OK'" > /dev/null 2>&1
Ian Wienand9b0ebc42015-04-17 13:06:47 +100034if [[ $? = 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100035 failed "die_if_not_set [X='' false] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100036fi
37
38
39# Enabling/disabling services
40
41echo "Testing enable_service()"
42
43function test_enable_service {
44 local start="$1"
45 local add="$2"
46 local finish="$3"
47
48 ENABLED_SERVICES="$start"
49 enable_service $add
50 if [ "$ENABLED_SERVICES" = "$finish" ]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100051 passed "OK: $start + $add -> $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100052 else
Ian Wienand09f4ad22015-04-17 13:13:04 +100053 failed "changing $start to $finish with $add failed: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100054 fi
Sean Dague53753292014-12-04 19:38:15 -050055}
56
Ian Wienand9b0ebc42015-04-17 13:06:47 +100057test_enable_service '' a 'a'
58test_enable_service 'a' b 'a,b'
59test_enable_service 'a,b' c 'a,b,c'
60test_enable_service 'a,b' c 'a,b,c'
61test_enable_service 'a,b,' c 'a,b,c'
62test_enable_service 'a,b' c,d 'a,b,c,d'
63test_enable_service 'a,b' "c d" 'a,b,c,d'
64test_enable_service 'a,b,c' c 'a,b,c'
Sean Dague53753292014-12-04 19:38:15 -050065
Ian Wienand9b0ebc42015-04-17 13:06:47 +100066test_enable_service 'a,b,-c' c 'a,b'
67test_enable_service 'a,b,c' -c 'a,b'
68
69function test_disable_service {
70 local start="$1"
71 local del="$2"
72 local finish="$3"
73
74 ENABLED_SERVICES="$start"
75 disable_service "$del"
76 if [ "$ENABLED_SERVICES" = "$finish" ]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100077 passed "OK: $start - $del -> $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100078 else
Ian Wienand09f4ad22015-04-17 13:13:04 +100079 failed "changing $start to $finish with $del failed: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100080 fi
81}
82
83echo "Testing disable_service()"
84test_disable_service 'a,b,c' a 'b,c'
85test_disable_service 'a,b,c' b 'a,c'
86test_disable_service 'a,b,c' c 'a,b'
87
88test_disable_service 'a,b,c' a 'b,c'
89test_disable_service 'b,c' b 'c'
90test_disable_service 'c' c ''
91test_disable_service '' d ''
92
93test_disable_service 'a,b,c,' c 'a,b'
94test_disable_service 'a,b' c 'a,b'
95
96
97echo "Testing disable_all_services()"
98ENABLED_SERVICES=a,b,c
99disable_all_services
100
101if [[ -z "$ENABLED_SERVICES" ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000102 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000103else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000104 failed "disabling all services FAILED: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000105fi
106
107echo "Testing disable_negated_services()"
108
109
110function test_disable_negated_services {
111 local start="$1"
112 local finish="$2"
113
114 ENABLED_SERVICES="$start"
115 disable_negated_services
116 if [ "$ENABLED_SERVICES" = "$finish" ]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000117 passed "OK: $start + $add -> $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000118 else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000119 failed "changing $start to $finish failed: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000120 fi
121}
122
123test_disable_negated_services '-a' ''
124test_disable_negated_services '-a,a' ''
125test_disable_negated_services '-a,-a' ''
126test_disable_negated_services 'a,-a' ''
127test_disable_negated_services 'b,a,-a' 'b'
128test_disable_negated_services 'a,b,-a' 'b'
129test_disable_negated_services 'a,-a,b' 'b'
Ian Wienand2796a822015-04-15 08:59:04 +1000130test_disable_negated_services 'a,aa,-a' 'aa'
131test_disable_negated_services 'aa,-a' 'aa'
132test_disable_negated_services 'a_a, -a_a' ''
133test_disable_negated_services 'a-b, -a-b' ''
134test_disable_negated_services 'a-b, b, -a-b' 'b'
135test_disable_negated_services 'a,-a,av2,b' 'av2,b'
136test_disable_negated_services 'a,aa,-a' 'aa'
137test_disable_negated_services 'a,av2,-a,a' 'av2'
138test_disable_negated_services 'a,-a,av2' 'av2'
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000139
fumihiko kakuma8606c982015-04-13 09:55:06 +0900140echo "Testing remove_disabled_services()"
141
142function test_remove_disabled_services {
143 local service_list="$1"
144 local remove_list="$2"
145 local expected="$3"
146
147 results=$(remove_disabled_services "$service_list" "$remove_list")
148 if [ "$results" = "$expected" ]; then
149 passed "OK: '$service_list' - '$remove_list' -> '$results'"
150 else
151 failed "getting '$expected' from '$service_list' - '$remove_list' failed: '$results'"
152 fi
153}
154
155test_remove_disabled_services 'a,b,c' 'a,c' 'b'
156test_remove_disabled_services 'a,b,c' 'b' 'a,c'
157test_remove_disabled_services 'a,b,c,d' 'a,c d' 'b'
158test_remove_disabled_services 'a,b c,d' 'a d' 'b,c'
159test_remove_disabled_services 'a,b,c' 'a,b,c' ''
160test_remove_disabled_services 'a,b,c' 'd' 'a,b,c'
161test_remove_disabled_services 'a,b,c' '' 'a,b,c'
162test_remove_disabled_services '' 'a,b,c' ''
163test_remove_disabled_services '' '' ''
164
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000165echo "Testing is_package_installed()"
166
167if [[ -z "$os_PACKAGE" ]]; then
168 GetOSVersion
169fi
170
171if [[ "$os_PACKAGE" = "deb" ]]; then
172 is_package_installed dpkg
173 VAL=$?
174elif [[ "$os_PACKAGE" = "rpm" ]]; then
175 is_package_installed rpm
176 VAL=$?
177else
178 VAL=1
179fi
180if [[ "$VAL" -eq 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000181 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000182else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000183 failed "is_package_installed() on existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000184fi
185
186if [[ "$os_PACKAGE" = "deb" ]]; then
187 is_package_installed dpkg bash
188 VAL=$?
189elif [[ "$os_PACKAGE" = "rpm" ]]; then
190 is_package_installed rpm bash
191 VAL=$?
192else
193 VAL=1
194fi
195if [[ "$VAL" -eq 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000196 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000197else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000198 failed "is_package_installed() on more than one existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000199fi
200
201is_package_installed zzzZZZzzz
202VAL=$?
203if [[ "$VAL" -ne 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000204 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000205else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000206 failed "is_package_installed() on non-existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000207fi
208
209# test against removed package...was a bug on Ubuntu
210if is_ubuntu; then
211 PKG=cowsay
212 if ! (dpkg -s $PKG >/dev/null 2>&1); then
213 # it was never installed...set up the condition
214 sudo apt-get install -y cowsay >/dev/null 2>&1
215 fi
216 if (dpkg -s $PKG >/dev/null 2>&1); then
217 # remove it to create the 'un' status
218 sudo dpkg -P $PKG >/dev/null 2>&1
219 fi
220
221 # now test the installed check on a deleted package
222 is_package_installed $PKG
223 VAL=$?
224 if [[ "$VAL" -ne 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000225 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000226 else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000227 failed "is_package_installed() on deleted package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000228 fi
229fi
230
231# test isset function
232echo "Testing isset()"
233you_should_not_have_this_variable=42
234
235if isset "you_should_not_have_this_variable"; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000236 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000237else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000238 failed "\"you_should_not_have_this_variable\" not declared. failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000239fi
240
241unset you_should_not_have_this_variable
242if isset "you_should_not_have_this_variable"; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000243 failed "\"you_should_not_have_this_variable\" looks like declared variable."
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000244else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000245 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000246fi
Ian Wienand09f4ad22015-04-17 13:13:04 +1000247
248report_results