blob: f8e2c9e2e4a94af30627f1c4dc2c0e9622898ef2 [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'
130
131
132echo "Testing is_package_installed()"
133
134if [[ -z "$os_PACKAGE" ]]; then
135 GetOSVersion
136fi
137
138if [[ "$os_PACKAGE" = "deb" ]]; then
139 is_package_installed dpkg
140 VAL=$?
141elif [[ "$os_PACKAGE" = "rpm" ]]; then
142 is_package_installed rpm
143 VAL=$?
144else
145 VAL=1
146fi
147if [[ "$VAL" -eq 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000148 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000149else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000150 failed "is_package_installed() on existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000151fi
152
153if [[ "$os_PACKAGE" = "deb" ]]; then
154 is_package_installed dpkg bash
155 VAL=$?
156elif [[ "$os_PACKAGE" = "rpm" ]]; then
157 is_package_installed rpm bash
158 VAL=$?
159else
160 VAL=1
161fi
162if [[ "$VAL" -eq 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000163 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000164else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000165 failed "is_package_installed() on more than one existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000166fi
167
168is_package_installed zzzZZZzzz
169VAL=$?
170if [[ "$VAL" -ne 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000171 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000172else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000173 failed "is_package_installed() on non-existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000174fi
175
176# test against removed package...was a bug on Ubuntu
177if is_ubuntu; then
178 PKG=cowsay
179 if ! (dpkg -s $PKG >/dev/null 2>&1); then
180 # it was never installed...set up the condition
181 sudo apt-get install -y cowsay >/dev/null 2>&1
182 fi
183 if (dpkg -s $PKG >/dev/null 2>&1); then
184 # remove it to create the 'un' status
185 sudo dpkg -P $PKG >/dev/null 2>&1
186 fi
187
188 # now test the installed check on a deleted package
189 is_package_installed $PKG
190 VAL=$?
191 if [[ "$VAL" -ne 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000192 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000193 else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000194 failed "is_package_installed() on deleted package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000195 fi
196fi
197
198# test isset function
199echo "Testing isset()"
200you_should_not_have_this_variable=42
201
202if isset "you_should_not_have_this_variable"; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000203 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000204else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000205 failed "\"you_should_not_have_this_variable\" not declared. failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000206fi
207
208unset you_should_not_have_this_variable
209if isset "you_should_not_have_this_variable"; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000210 failed "\"you_should_not_have_this_variable\" looks like declared variable."
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000211else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000212 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000213fi
Ian Wienand09f4ad22015-04-17 13:13:04 +1000214
215report_results