Browse code

Move function.sh to test_functions.sh

run_tests.sh runs tests starting with test_*

The existing test_functions.sh is really testing true/false. Move
that to test_truefalse.sh

Then move functions.sh to test_functions.sh. This will ensure it is
run during unit testing from run-tests.sh

Change-Id: I959ac38c946da1fb47458b8c4f09157f74f0e644

Ian Wienand authored on 2015/04/17 12:06:47
Showing 3 changed files
1 1
deleted file mode 100755
... ...
@@ -1,215 +0,0 @@
1
-#!/usr/bin/env bash
2
-
3
-# Tests for DevStack functions
4
-
5
-TOP=$(cd $(dirname "$0")/.. && pwd)
6
-
7
-# Import common functions
8
-source $TOP/functions
9
-
10
-# Import configuration
11
-source $TOP/openrc
12
-
13
-
14
-echo "Testing die_if_not_set()"
15
-
16
-bash -cx "source $TOP/functions; X=`echo Y && true`; die_if_not_set X 'not OK'"
17
-if [[ $? != 0 ]]; then
18
-    echo "die_if_not_set [X='Y' true] Failed"
19
-else
20
-    echo 'OK'
21
-fi
22
-
23
-bash -cx "source $TOP/functions; X=`true`; die_if_not_set X 'OK'"
24
-if [[ $? = 0 ]]; then
25
-    echo "die_if_not_set [X='' true] Failed"
26
-fi
27
-
28
-bash -cx "source $TOP/functions; X=`echo Y && false`; die_if_not_set X 'not OK'"
29
-if [[ $? != 0 ]]; then
30
-    echo "die_if_not_set [X='Y' false] Failed"
31
-else
32
-    echo 'OK'
33
-fi
34
-
35
-bash -cx "source $TOP/functions; X=`false`; die_if_not_set X 'OK'"
36
-if [[ $? = 0 ]]; then
37
-    echo "die_if_not_set [X='' false] Failed"
38
-fi
39
-
40
-
41
-# Enabling/disabling services
42
-
43
-echo "Testing enable_service()"
44
-
45
-function 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
57
-}
58
-
59
-test_enable_service '' a 'a'
60
-test_enable_service 'a' b 'a,b'
61
-test_enable_service 'a,b' c 'a,b,c'
62
-test_enable_service 'a,b' c 'a,b,c'
63
-test_enable_service 'a,b,' c 'a,b,c'
64
-test_enable_service 'a,b' c,d 'a,b,c,d'
65
-test_enable_service 'a,b' "c d" 'a,b,c,d'
66
-test_enable_service 'a,b,c' c 'a,b,c'
67
-
68
-test_enable_service 'a,b,-c' c 'a,b'
69
-test_enable_service 'a,b,c' -c 'a,b'
70
-
71
-function 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
-
85
-echo "Testing disable_service()"
86
-test_disable_service 'a,b,c' a 'b,c'
87
-test_disable_service 'a,b,c' b 'a,c'
88
-test_disable_service 'a,b,c' c 'a,b'
89
-
90
-test_disable_service 'a,b,c' a 'b,c'
91
-test_disable_service 'b,c' b 'c'
92
-test_disable_service 'c' c ''
93
-test_disable_service '' d ''
94
-
95
-test_disable_service 'a,b,c,' c 'a,b'
96
-test_disable_service 'a,b' c 'a,b'
97
-
98
-
99
-echo "Testing disable_all_services()"
100
-ENABLED_SERVICES=a,b,c
101
-disable_all_services
102
-
103
-if [[ -z "$ENABLED_SERVICES" ]]; then
104
-    echo "OK"
105
-else
106
-    echo "disabling all services FAILED: $ENABLED_SERVICES"
107
-fi
108
-
109
-echo "Testing disable_negated_services()"
110
-
111
-
112
-function 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
-
125
-test_disable_negated_services '-a' ''
126
-test_disable_negated_services '-a,a' ''
127
-test_disable_negated_services '-a,-a' ''
128
-test_disable_negated_services 'a,-a' ''
129
-test_disable_negated_services 'b,a,-a' 'b'
130
-test_disable_negated_services 'a,b,-a' 'b'
131
-test_disable_negated_services 'a,-a,b' 'b'
132
-
133
-
134
-echo "Testing is_package_installed()"
135
-
136
-if [[ -z "$os_PACKAGE" ]]; then
137
-    GetOSVersion
138
-fi
139
-
140
-if [[ "$os_PACKAGE" = "deb" ]]; then
141
-    is_package_installed dpkg
142
-    VAL=$?
143
-elif [[ "$os_PACKAGE" = "rpm" ]]; then
144
-    is_package_installed rpm
145
-    VAL=$?
146
-else
147
-    VAL=1
148
-fi
149
-if [[ "$VAL" -eq 0 ]]; then
150
-    echo "OK"
151
-else
152
-    echo "is_package_installed() on existing package failed"
153
-fi
154
-
155
-if [[ "$os_PACKAGE" = "deb" ]]; then
156
-    is_package_installed dpkg bash
157
-    VAL=$?
158
-elif [[ "$os_PACKAGE" = "rpm" ]]; then
159
-    is_package_installed rpm bash
160
-    VAL=$?
161
-else
162
-    VAL=1
163
-fi
164
-if [[ "$VAL" -eq 0 ]]; then
165
-    echo "OK"
166
-else
167
-    echo "is_package_installed() on more than one existing package failed"
168
-fi
169
-
170
-is_package_installed zzzZZZzzz
171
-VAL=$?
172
-if [[ "$VAL" -ne 0 ]]; then
173
-    echo "OK"
174
-else
175
-    echo "is_package_installed() on non-existing package failed"
176
-fi
177
-
178
-# test against removed package...was a bug on Ubuntu
179
-if 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
198
-fi
199
-
200
-# test isset function
201
-echo  "Testing isset()"
202
-you_should_not_have_this_variable=42
203
-
204
-if isset "you_should_not_have_this_variable"; then
205
-    echo "OK"
206
-else
207
-    echo "\"you_should_not_have_this_variable\" not declared. failed"
208
-fi
209
-
210
-unset you_should_not_have_this_variable
211
-if isset "you_should_not_have_this_variable"; then
212
-    echo "\"you_should_not_have_this_variable\" looks like declared variable. failed"
213
-else
214
-    echo "OK"
215
-fi
... ...
@@ -1,34 +1,215 @@
1 1
 #!/usr/bin/env bash
2 2
 
3
-# Tests for DevStack meta-config functions
3
+# Tests for DevStack functions
4 4
 
5 5
 TOP=$(cd $(dirname "$0")/.. && pwd)
6 6
 
7 7
 # Import common functions
8 8
 source $TOP/functions
9
-source $TOP/tests/unittest.sh
10
-
11
-function test_truefalse {
12
-    local one=1
13
-    local captrue=True
14
-    local lowtrue=true
15
-    local abrevtrue=t
16
-    local zero=0
17
-    local capfalse=False
18
-    local lowfalse=false
19
-    local abrevfalse=f
20
-    for against in True False; do
21
-        for name in one captrue lowtrue abrevtrue; do
22
-            assert_equal "True" $(trueorfalse $against $name) "\$(trueorfalse $against $name)"
23
-        done
24
-    done
25
-    for against in True False; do
26
-        for name in zero capfalse lowfalse abrevfalse; do
27
-            assert_equal "False" $(trueorfalse $against $name) "\$(trueorfalse $against $name)"
28
-        done
29
-    done
9
+
10
+# Import configuration
11
+source $TOP/openrc
12
+
13
+
14
+echo "Testing die_if_not_set()"
15
+
16
+bash -cx "source $TOP/functions; X=`echo Y && true`; die_if_not_set X 'not OK'"
17
+if [[ $? != 0 ]]; then
18
+    echo "die_if_not_set [X='Y' true] Failed"
19
+else
20
+    echo 'OK'
21
+fi
22
+
23
+bash -cx "source $TOP/functions; X=`true`; die_if_not_set X 'OK'"
24
+if [[ $? = 0 ]]; then
25
+    echo "die_if_not_set [X='' true] Failed"
26
+fi
27
+
28
+bash -cx "source $TOP/functions; X=`echo Y && false`; die_if_not_set X 'not OK'"
29
+if [[ $? != 0 ]]; then
30
+    echo "die_if_not_set [X='Y' false] Failed"
31
+else
32
+    echo 'OK'
33
+fi
34
+
35
+bash -cx "source $TOP/functions; X=`false`; die_if_not_set X 'OK'"
36
+if [[ $? = 0 ]]; then
37
+    echo "die_if_not_set [X='' false] Failed"
38
+fi
39
+
40
+
41
+# Enabling/disabling services
42
+
43
+echo "Testing enable_service()"
44
+
45
+function 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
57
+}
58
+
59
+test_enable_service '' a 'a'
60
+test_enable_service 'a' b 'a,b'
61
+test_enable_service 'a,b' c 'a,b,c'
62
+test_enable_service 'a,b' c 'a,b,c'
63
+test_enable_service 'a,b,' c 'a,b,c'
64
+test_enable_service 'a,b' c,d 'a,b,c,d'
65
+test_enable_service 'a,b' "c d" 'a,b,c,d'
66
+test_enable_service 'a,b,c' c 'a,b,c'
67
+
68
+test_enable_service 'a,b,-c' c 'a,b'
69
+test_enable_service 'a,b,c' -c 'a,b'
70
+
71
+function 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
+
85
+echo "Testing disable_service()"
86
+test_disable_service 'a,b,c' a 'b,c'
87
+test_disable_service 'a,b,c' b 'a,c'
88
+test_disable_service 'a,b,c' c 'a,b'
89
+
90
+test_disable_service 'a,b,c' a 'b,c'
91
+test_disable_service 'b,c' b 'c'
92
+test_disable_service 'c' c ''
93
+test_disable_service '' d ''
94
+
95
+test_disable_service 'a,b,c,' c 'a,b'
96
+test_disable_service 'a,b' c 'a,b'
97
+
98
+
99
+echo "Testing disable_all_services()"
100
+ENABLED_SERVICES=a,b,c
101
+disable_all_services
102
+
103
+if [[ -z "$ENABLED_SERVICES" ]]; then
104
+    echo "OK"
105
+else
106
+    echo "disabling all services FAILED: $ENABLED_SERVICES"
107
+fi
108
+
109
+echo "Testing disable_negated_services()"
110
+
111
+
112
+function 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
30 123
 }
31 124
 
32
-test_truefalse
125
+test_disable_negated_services '-a' ''
126
+test_disable_negated_services '-a,a' ''
127
+test_disable_negated_services '-a,-a' ''
128
+test_disable_negated_services 'a,-a' ''
129
+test_disable_negated_services 'b,a,-a' 'b'
130
+test_disable_negated_services 'a,b,-a' 'b'
131
+test_disable_negated_services 'a,-a,b' 'b'
132
+
133
+
134
+echo "Testing is_package_installed()"
135
+
136
+if [[ -z "$os_PACKAGE" ]]; then
137
+    GetOSVersion
138
+fi
139
+
140
+if [[ "$os_PACKAGE" = "deb" ]]; then
141
+    is_package_installed dpkg
142
+    VAL=$?
143
+elif [[ "$os_PACKAGE" = "rpm" ]]; then
144
+    is_package_installed rpm
145
+    VAL=$?
146
+else
147
+    VAL=1
148
+fi
149
+if [[ "$VAL" -eq 0 ]]; then
150
+    echo "OK"
151
+else
152
+    echo "is_package_installed() on existing package failed"
153
+fi
154
+
155
+if [[ "$os_PACKAGE" = "deb" ]]; then
156
+    is_package_installed dpkg bash
157
+    VAL=$?
158
+elif [[ "$os_PACKAGE" = "rpm" ]]; then
159
+    is_package_installed rpm bash
160
+    VAL=$?
161
+else
162
+    VAL=1
163
+fi
164
+if [[ "$VAL" -eq 0 ]]; then
165
+    echo "OK"
166
+else
167
+    echo "is_package_installed() on more than one existing package failed"
168
+fi
169
+
170
+is_package_installed zzzZZZzzz
171
+VAL=$?
172
+if [[ "$VAL" -ne 0 ]]; then
173
+    echo "OK"
174
+else
175
+    echo "is_package_installed() on non-existing package failed"
176
+fi
177
+
178
+# test against removed package...was a bug on Ubuntu
179
+if 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
198
+fi
199
+
200
+# test isset function
201
+echo  "Testing isset()"
202
+you_should_not_have_this_variable=42
203
+
204
+if isset "you_should_not_have_this_variable"; then
205
+    echo "OK"
206
+else
207
+    echo "\"you_should_not_have_this_variable\" not declared. failed"
208
+fi
33 209
 
34
-report_results
210
+unset you_should_not_have_this_variable
211
+if isset "you_should_not_have_this_variable"; then
212
+    echo "\"you_should_not_have_this_variable\" looks like declared variable. failed"
213
+else
214
+    echo "OK"
215
+fi
35 216
new file mode 100755
... ...
@@ -0,0 +1,34 @@
0
+#!/usr/bin/env bash
1
+
2
+# Tests for DevStack meta-config functions
3
+
4
+TOP=$(cd $(dirname "$0")/.. && pwd)
5
+
6
+# Import common functions
7
+source $TOP/functions
8
+source $TOP/tests/unittest.sh
9
+
10
+function test_truefalse {
11
+    local one=1
12
+    local captrue=True
13
+    local lowtrue=true
14
+    local abrevtrue=t
15
+    local zero=0
16
+    local capfalse=False
17
+    local lowfalse=false
18
+    local abrevfalse=f
19
+    for against in True False; do
20
+        for name in one captrue lowtrue abrevtrue; do
21
+            assert_equal "True" $(trueorfalse $against $name) "\$(trueorfalse $against $name)"
22
+        done
23
+    done
24
+    for against in True False; do
25
+        for name in zero capfalse lowfalse abrevfalse; do
26
+            assert_equal "False" $(trueorfalse $against $name) "\$(trueorfalse $against $name)"
27
+        done
28
+    done
29
+}
30
+
31
+test_truefalse
32
+
33
+report_results