blob: 126080f1e3f8c02131760fa2ae15bd3381739820 [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
13
14echo "Testing die_if_not_set()"
15
16bash -cx "source $TOP/functions; X=`echo Y && true`; die_if_not_set X 'not OK'"
17if [[ $? != 0 ]]; then
18 echo "die_if_not_set [X='Y' true] Failed"
19else
20 echo 'OK'
21fi
22
23bash -cx "source $TOP/functions; X=`true`; die_if_not_set X 'OK'"
24if [[ $? = 0 ]]; then
25 echo "die_if_not_set [X='' true] Failed"
26fi
27
28bash -cx "source $TOP/functions; X=`echo Y && false`; die_if_not_set X 'not OK'"
29if [[ $? != 0 ]]; then
30 echo "die_if_not_set [X='Y' false] Failed"
31else
32 echo 'OK'
33fi
34
35bash -cx "source $TOP/functions; X=`false`; die_if_not_set X 'OK'"
36if [[ $? = 0 ]]; then
37 echo "die_if_not_set [X='' false] Failed"
38fi
39
40
41# Enabling/disabling services
42
43echo "Testing enable_service()"
44
45function test_enable_service {
46 local start="$1"
47 local add="$2"
48 local finish="$3"
49
50 ENABLED_SERVICES="$start"
51 enable_service $add
52 if [ "$ENABLED_SERVICES" = "$finish" ]; then
53 echo "OK: $start + $add -> $ENABLED_SERVICES"
54 else
55 echo "changing $start to $finish with $add failed: $ENABLED_SERVICES"
56 fi
Sean Dague53753292014-12-04 19:38:15 -050057}
58
Ian Wienand9b0ebc42015-04-17 13:06:47 +100059test_enable_service '' a 'a'
60test_enable_service 'a' b 'a,b'
61test_enable_service 'a,b' c 'a,b,c'
62test_enable_service 'a,b' c 'a,b,c'
63test_enable_service 'a,b,' c 'a,b,c'
64test_enable_service 'a,b' c,d 'a,b,c,d'
65test_enable_service 'a,b' "c d" 'a,b,c,d'
66test_enable_service 'a,b,c' c 'a,b,c'
Sean Dague53753292014-12-04 19:38:15 -050067
Ian Wienand9b0ebc42015-04-17 13:06:47 +100068test_enable_service 'a,b,-c' c 'a,b'
69test_enable_service 'a,b,c' -c 'a,b'
70
71function test_disable_service {
72 local start="$1"
73 local del="$2"
74 local finish="$3"
75
76 ENABLED_SERVICES="$start"
77 disable_service "$del"
78 if [ "$ENABLED_SERVICES" = "$finish" ]; then
79 echo "OK: $start - $del -> $ENABLED_SERVICES"
80 else
81 echo "changing $start to $finish with $del failed: $ENABLED_SERVICES"
82 fi
83}
84
85echo "Testing disable_service()"
86test_disable_service 'a,b,c' a 'b,c'
87test_disable_service 'a,b,c' b 'a,c'
88test_disable_service 'a,b,c' c 'a,b'
89
90test_disable_service 'a,b,c' a 'b,c'
91test_disable_service 'b,c' b 'c'
92test_disable_service 'c' c ''
93test_disable_service '' d ''
94
95test_disable_service 'a,b,c,' c 'a,b'
96test_disable_service 'a,b' c 'a,b'
97
98
99echo "Testing disable_all_services()"
100ENABLED_SERVICES=a,b,c
101disable_all_services
102
103if [[ -z "$ENABLED_SERVICES" ]]; then
104 echo "OK"
105else
106 echo "disabling all services FAILED: $ENABLED_SERVICES"
107fi
108
109echo "Testing disable_negated_services()"
110
111
112function test_disable_negated_services {
113 local start="$1"
114 local finish="$2"
115
116 ENABLED_SERVICES="$start"
117 disable_negated_services
118 if [ "$ENABLED_SERVICES" = "$finish" ]; then
119 echo "OK: $start + $add -> $ENABLED_SERVICES"
120 else
121 echo "changing $start to $finish failed: $ENABLED_SERVICES"
122 fi
123}
124
125test_disable_negated_services '-a' ''
126test_disable_negated_services '-a,a' ''
127test_disable_negated_services '-a,-a' ''
128test_disable_negated_services 'a,-a' ''
129test_disable_negated_services 'b,a,-a' 'b'
130test_disable_negated_services 'a,b,-a' 'b'
131test_disable_negated_services 'a,-a,b' 'b'
132
133
134echo "Testing is_package_installed()"
135
136if [[ -z "$os_PACKAGE" ]]; then
137 GetOSVersion
138fi
139
140if [[ "$os_PACKAGE" = "deb" ]]; then
141 is_package_installed dpkg
142 VAL=$?
143elif [[ "$os_PACKAGE" = "rpm" ]]; then
144 is_package_installed rpm
145 VAL=$?
146else
147 VAL=1
148fi
149if [[ "$VAL" -eq 0 ]]; then
150 echo "OK"
151else
152 echo "is_package_installed() on existing package failed"
153fi
154
155if [[ "$os_PACKAGE" = "deb" ]]; then
156 is_package_installed dpkg bash
157 VAL=$?
158elif [[ "$os_PACKAGE" = "rpm" ]]; then
159 is_package_installed rpm bash
160 VAL=$?
161else
162 VAL=1
163fi
164if [[ "$VAL" -eq 0 ]]; then
165 echo "OK"
166else
167 echo "is_package_installed() on more than one existing package failed"
168fi
169
170is_package_installed zzzZZZzzz
171VAL=$?
172if [[ "$VAL" -ne 0 ]]; then
173 echo "OK"
174else
175 echo "is_package_installed() on non-existing package failed"
176fi
177
178# test against removed package...was a bug on Ubuntu
179if is_ubuntu; then
180 PKG=cowsay
181 if ! (dpkg -s $PKG >/dev/null 2>&1); then
182 # it was never installed...set up the condition
183 sudo apt-get install -y cowsay >/dev/null 2>&1
184 fi
185 if (dpkg -s $PKG >/dev/null 2>&1); then
186 # remove it to create the 'un' status
187 sudo dpkg -P $PKG >/dev/null 2>&1
188 fi
189
190 # now test the installed check on a deleted package
191 is_package_installed $PKG
192 VAL=$?
193 if [[ "$VAL" -ne 0 ]]; then
194 echo "OK"
195 else
196 echo "is_package_installed() on deleted package failed"
197 fi
198fi
199
200# test isset function
201echo "Testing isset()"
202you_should_not_have_this_variable=42
203
204if isset "you_should_not_have_this_variable"; then
205 echo "OK"
206else
207 echo "\"you_should_not_have_this_variable\" not declared. failed"
208fi
209
210unset you_should_not_have_this_variable
211if isset "you_should_not_have_this_variable"; then
212 echo "\"you_should_not_have_this_variable\" looks like declared variable. failed"
213else
214 echo "OK"
215fi