blob: 95dafe1028889330e6e0a21dc38fd0b0581ce07c [file] [log] [blame]
Dean Troyer489bd2a2012-03-02 10:44:29 -06001#!/usr/bin/env bash
2
3# Tests for DevStack functions
4
5TOP=$(cd $(dirname "$0")/.. && pwd)
6
7# Import common functions
8source $TOP/functions
9
10# Import configuration
11source $TOP/openrc
12
13
Dean Troyer489bd2a2012-03-02 10:44:29 -060014echo "Testing die_if_not_set()"
15
Dean Troyer27e32692012-03-16 16:16:56 -050016bash -cx "source $TOP/functions; X=`echo Y && true`; die_if_not_set X 'not OK'"
Dean Troyer489bd2a2012-03-02 10:44:29 -060017if [[ $? != 0 ]]; then
18 echo "die_if_not_set [X='Y' true] Failed"
19else
20 echo 'OK'
21fi
22
Dean Troyer27e32692012-03-16 16:16:56 -050023bash -cx "source $TOP/functions; X=`true`; die_if_not_set X 'OK'"
Dean Troyer489bd2a2012-03-02 10:44:29 -060024if [[ $? = 0 ]]; then
25 echo "die_if_not_set [X='' true] Failed"
26fi
27
Dean Troyer27e32692012-03-16 16:16:56 -050028bash -cx "source $TOP/functions; X=`echo Y && false`; die_if_not_set X 'not OK'"
Dean Troyer489bd2a2012-03-02 10:44:29 -060029if [[ $? != 0 ]]; then
30 echo "die_if_not_set [X='Y' false] Failed"
31else
32 echo 'OK'
33fi
34
Dean Troyer27e32692012-03-16 16:16:56 -050035bash -cx "source $TOP/functions; X=`false`; die_if_not_set X 'OK'"
Dean Troyer489bd2a2012-03-02 10:44:29 -060036if [[ $? = 0 ]]; then
37 echo "die_if_not_set [X='' false] Failed"
38fi
39
Dean Troyer13dc5cc2012-03-27 14:50:45 -050040
Doug Hellmannf04178f2012-07-05 17:10:03 -040041# 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" ]
53 then
54 echo "OK: $start + $add -> $ENABLED_SERVICES"
55 else
56 echo "changing $start to $finish with $add failed: $ENABLED_SERVICES"
57 fi
58}
59
60test_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'
68
69test_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" ]
80 then
81 echo "OK: $start - $del -> $ENABLED_SERVICES"
82 else
83 echo "changing $start to $finish with $del failed: $ENABLED_SERVICES"
84 fi
85}
86
87echo "Testing disable_service()"
88test_disable_service 'a,b,c' a 'b,c'
89test_disable_service 'a,b,c' b 'a,c'
90test_disable_service 'a,b,c' c 'a,b'
91
92test_disable_service 'a,b,c' a 'b,c'
93test_disable_service 'b,c' b 'c'
94test_disable_service 'c' c ''
95test_disable_service '' d ''
96
97test_disable_service 'a,b,c,' c 'a,b'
98test_disable_service 'a,b' c 'a,b'
99
100
101echo "Testing disable_all_services()"
102ENABLED_SERVICES=a,b,c
103disable_all_services
104
105if [[ -z "$ENABLED_SERVICES" ]]
106then
107 echo "OK"
108else
109 echo "disabling all services FAILED: $ENABLED_SERVICES"
110fi
111
112echo "Testing disable_negated_services()"
113
114
115function test_disable_negated_services() {
116 local start="$1"
117 local finish="$2"
118
119 ENABLED_SERVICES="$start"
120 disable_negated_services
121 if [ "$ENABLED_SERVICES" = "$finish" ]
122 then
123 echo "OK: $start + $add -> $ENABLED_SERVICES"
124 else
125 echo "changing $start to $finish failed: $ENABLED_SERVICES"
126 fi
127}
128
129test_disable_negated_services '-a' ''
130test_disable_negated_services '-a,a' ''
131test_disable_negated_services '-a,-a' ''
132test_disable_negated_services 'a,-a' ''
133test_disable_negated_services 'b,a,-a' 'b'
134test_disable_negated_services 'a,b,-a' 'b'
135test_disable_negated_services 'a,-a,b' 'b'
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200136
137
138echo "Testing is_package_installed()"
139
140if [[ -z "$os_PACKAGE" ]]; then
141 GetOSVersion
142fi
143
144if [[ "$os_PACKAGE" = "deb" ]]; then
145 is_package_installed dpkg
146 VAL=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100147elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200148 is_package_installed rpm
149 VAL=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100150else
151 VAL=1
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200152fi
153if [[ "$VAL" -eq 0 ]]; then
154 echo "OK"
155else
156 echo "is_package_installed() on existing package failed"
157fi
158
159if [[ "$os_PACKAGE" = "deb" ]]; then
160 is_package_installed dpkg bash
161 VAL=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100162elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200163 is_package_installed rpm bash
164 VAL=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100165else
166 VAL=1
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200167fi
168if [[ "$VAL" -eq 0 ]]; then
169 echo "OK"
170else
171 echo "is_package_installed() on more than one existing package failed"
172fi
173
174is_package_installed zzzZZZzzz
175VAL=$?
176if [[ "$VAL" -ne 0 ]]; then
177 echo "OK"
178else
179 echo "is_package_installed() on non-existing package failed"
180fi
Dean Troyer04762cd2013-08-27 17:06:14 -0500181
182# test against removed package...was a bug on Ubuntu
183if is_ubuntu; then
184 PKG=cowsay
185 if ! (dpkg -s $PKG >/dev/null 2>&1); then
186 # it was never installed...set up the condition
187 sudo apt-get install -y cowsay >/dev/null 2>&1
188 fi
189 if (dpkg -s $PKG >/dev/null 2>&1); then
190 # remove it to create the 'un' status
191 sudo dpkg -P $PKG >/dev/null 2>&1
192 fi
193
194 # now test the installed check on a deleted package
195 is_package_installed $PKG
196 VAL=$?
197 if [[ "$VAL" -ne 0 ]]; then
198 echo "OK"
199 else
200 echo "is_package_installed() on deleted package failed"
201 fi
202fi