blob: 874d02230d766993e77b8581b465e098c5e0bb2a [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
Ian Wienandaee18c72014-02-21 15:35:08 +110045function test_enable_service {
Doug Hellmannf04178f2012-07-05 17:10:03 -040046 local start="$1"
47 local add="$2"
48 local finish="$3"
49
50 ENABLED_SERVICES="$start"
51 enable_service $add
Sean Dague16dd8b32014-02-03 09:10:54 +090052 if [ "$ENABLED_SERVICES" = "$finish" ]; then
Doug Hellmannf04178f2012-07-05 17:10:03 -040053 echo "OK: $start + $add -> $ENABLED_SERVICES"
54 else
55 echo "changing $start to $finish with $add failed: $ENABLED_SERVICES"
56 fi
57}
58
59test_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'
67
68test_enable_service 'a,b,-c' c 'a,b'
69test_enable_service 'a,b,c' -c 'a,b'
70
Ian Wienandaee18c72014-02-21 15:35:08 +110071function test_disable_service {
Doug Hellmannf04178f2012-07-05 17:10:03 -040072 local start="$1"
73 local del="$2"
74 local finish="$3"
75
76 ENABLED_SERVICES="$start"
77 disable_service "$del"
Sean Dague16dd8b32014-02-03 09:10:54 +090078 if [ "$ENABLED_SERVICES" = "$finish" ]; then
Doug Hellmannf04178f2012-07-05 17:10:03 -040079 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
Sean Dague16dd8b32014-02-03 09:10:54 +0900103if [[ -z "$ENABLED_SERVICES" ]]; then
Doug Hellmannf04178f2012-07-05 17:10:03 -0400104 echo "OK"
105else
106 echo "disabling all services FAILED: $ENABLED_SERVICES"
107fi
108
109echo "Testing disable_negated_services()"
110
111
Ian Wienandaee18c72014-02-21 15:35:08 +1100112function test_disable_negated_services {
Doug Hellmannf04178f2012-07-05 17:10:03 -0400113 local start="$1"
114 local finish="$2"
115
116 ENABLED_SERVICES="$start"
117 disable_negated_services
Sean Dague16dd8b32014-02-03 09:10:54 +0900118 if [ "$ENABLED_SERVICES" = "$finish" ]; then
Doug Hellmannf04178f2012-07-05 17:10:03 -0400119 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'
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200132
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=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100143elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200144 is_package_installed rpm
145 VAL=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100146else
147 VAL=1
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200148fi
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=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100158elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200159 is_package_installed rpm bash
160 VAL=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100161else
162 VAL=1
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200163fi
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
Dean Troyer04762cd2013-08-27 17:06:14 -0500177
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