11#! /bin/bash
22
3+ source " ${BASH_SOURCE%/* } /helpers_tests.bash"
4+
35# use a clean instance of polycubed to run each test
46RELAUNCH_POLYCUBED=true
57
@@ -14,24 +16,6 @@ RESULT_JSON="result.json"
1416declare -A services
1517services[iptables]=pcn-iptables
1618
17- # cleanup environment before exit
18- function cleanup {
19- set +e
20- echo " killing polycubed ..."
21- sudo pkill polycubed >> $test_tmp
22- echo " { \" passed\" :\" $test_passed \" , \" total\" :\" $test_total \" , \" test_log\" :\" $test_log \" }" > $RESULT_JSON
23-
24- cat $test_results
25-
26- echo " "
27- echo " FAILED TESTS:"
28- echo " "
29- cat $test_results | grep FAILED -A 1
30-
31- if $failed ; then
32- exit 1
33- fi
34- }
3519trap cleanup EXIT
3620
3721# $1 setup RELAUNCH_POLYCUBED
@@ -69,175 +53,6 @@ echo "Tests Scripts - LOG FILE" > $test_log
6953date >> $test_log
7054echo >> $test_log
7155
72- # execute and log test ($1:path)
73- function log_test {
74- " $@ " >> $test_tmp 2>&1
75- local status=$?
76- last_test_result=$status
77-
78- # check if polycubed is still alive (no crash in the meanwhile)
79- polycubed_alive=$( ps -el | grep polycubed)
80- if [ -z " $polycubed_alive " ]; then
81- echo " polycubed not running ..."
82- polycubed_crash=true
83- status=1
84- fi
85- test_total=$(( $test_total + 1 ))
86- if [ $status -ne 0 ]; then
87- echo " ++++TEST $1 FAILED++++"
88- echo " ++++TEST $1 FAILED++++" >> $test_results
89- echo " ++++TEST $1 FAILED++++" >> $test_tmp
90- failed=true
91- cat $test_tmp >> $test_log
92- else
93- test_passed=$(( $test_passed + 1 ))
94- echo " ++++TEST $1 PASSED++++"
95- echo " ++++TEST $1 PASSED++++" >> $test_results
96- echo " ++++TEST $1 PASSED++++" >> $test_tmp
97- fi
98- return $status
99- }
100-
101- # Check if services are loaded
102- function services_are_loaded {
103- echo " load_services:" > load_services
104- count=0
105- services_show=$( polycubectl services show)
106- for i in " ${! services[@]} "
107- do
108- lines=$( echo $services_show | grep $i | wc -l)
109- if [ $lines -ne 0 ]
110- then
111- echo " $i YES" >> load_services
112- else
113- count=$(( count + 1 ))
114- echo " $i NO" >> load_services
115- fi
116- done
117- echo $count
118- }
119-
120- # Check if polycubed rest server is responding
121- function polycubed_is_responding {
122- ret=$( polycubectl ? > /dev/null)
123- ret=$( echo $? )
124- echo $ret
125- }
126-
127- # Kill polycubed, and wait all services to be unloaded and process to be completely killed
128- function polycubed_kill_and_wait {
129- echo " killing polycubed ..."
130- sudo pkill polycubed >> $test_tmp
131-
132- done=0
133- i=0
134- while : ; do
135- sleep 1
136- alive=$( ps -el | grep polycubed)
137- if [ -z " $alive " ]; then
138- done=1
139- fi
140-
141- i=$(( i+ 1 ))
142-
143- if [ " $done " -ne 0 ]; then
144- echo " killing polycubed in $i seconds"
145- break
146- fi
147- done
148- }
149-
150- # Relaunch polycubed, if deamon is not running
151- function polycubed_relaunch_if_not_running {
152- alive=$( ps -el | grep polycubed)
153- if [ -z " $alive " ]; then
154- echo " polycubed not running ..."
155- echo " relaunching polycubed ..."
156- $polycubed >> $test_tmp 2>&1 &
157- fi
158- }
159-
160- # Launch polycubed, and wait until it becomes responsive
161- function launch_and_wait_polycubed_is_responding {
162- if $RELAUNCH_POLYCUBED ; then
163- echo " starting polycubed ..."
164- $polycubed >> $test_tmp 2>&1 &
165- else
166- polycubed_alive=$( ps -el | grep polycubed)
167- if [ -z " $polycubed_alive " ]; then
168- echo " polycubed not running ..."
169- echo " relaunching polycubed ..."
170- $polycubed >> $test_tmp 2>&1 &
171- fi
172- fi
173-
174- done=0
175- i=0
176- while : ; do
177- sleep 1
178- responding=$( polycubed_is_responding)
179- if [[ $responding -eq 0 ]]; then
180- done=1
181- else
182- polycubed_relaunch_if_not_running
183- fi
184- i=$(( i+ 1 ))
185- if [ " $done " -ne 0 ]; then
186- if $RELAUNCH_POLYCUBED ; then
187- echo " starting polycubed in $i seconds"
188- else
189- if [ -z " $polycubed_alive " ]; then
190- echo " relaunching polycubed in $i seconds"
191- fi
192- fi
193- break
194- fi
195- done
196-
197- done=0
198- i=0
199- while : ; do
200- sleep 1
201- loaded=$( services_are_loaded)
202- if [[ $loaded -eq 0 ]]; then
203- done=1
204- fi
205-
206- i=$(( i+ 1 ))
207- if [ " $i " -eq $SERVICES_LOAD_TIMEOUT ]
208- then
209- echo " +ERROR+ timeout in checking services loaded $i seconds. try to run test anyway"
210- cat load_services | grep NO
211- break
212- fi
213- if [ " $done " -ne 0 ]; then
214- echo " checking services loaded in $i seconds"
215- break
216- fi
217- done
218- }
219-
220- # run test ($1:path)
221- function run_test {
222- polycubed_crash=false
223- STARTTIME=$( date +%s)
224- echo
225- echo " Starting test $1 ..."
226- echo " ++++Starting test $1 ++++" > $test_tmp
227- launch_and_wait_polycubed_is_responding
228- echo " executing test ..."
229- log_test $1
230- if $RELAUNCH_POLYCUBED ; then
231- polycubed_kill_and_wait
232- fi
233- echo " Finished test $1 "
234- ENDTIME=$( date +%s)
235- echo " Time elapsed: $(( $ENDTIME - $STARTTIME )) s"
236- echo " Time elapsed: $(( $ENDTIME - $STARTTIME )) s" >> $test_results
237- echo " Time elapsed: $(( $ENDTIME - $STARTTIME )) s" >> $test_tmp
238- echo " ++++Finished test $1 ++++" >> $test_tmp
239- }
240-
24156function run_tests_from_dir {
24257 for test in $1 local_test* .sh; do
24358 if [[ $test == * " local_test*.sh" * ]]; then
0 commit comments