Skip to content

Commit 46d164a

Browse files
committed
test: move logic into helpers_tests.bash
this commit wants to avoid code duplication between testing scripts Signed-off-by: Matteo Bertrone <m.bertrone@gmail.com>
1 parent d1dffa8 commit 46d164a

3 files changed

Lines changed: 195 additions & 376 deletions

File tree

tests/helpers_tests.bash

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#!/usr/bin/env bash
2+
3+
# cleanup environment before exit
4+
function cleanup {
5+
set +e
6+
echo "killing polycubed ..."
7+
sudo pkill polycubed >> $test_tmp
8+
echo "{ \"passed\":\"$test_passed\", \"total\":\"$test_total\", \"test_log\":\"$test_log\" }" > $RESULT_JSON
9+
10+
cat $test_results
11+
12+
echo ""
13+
echo "FAILED TESTS:"
14+
echo ""
15+
cat $test_results | grep FAILED -A 1
16+
17+
if $failed ; then
18+
exit 1
19+
fi
20+
}
21+
22+
# execute and log test ($1:path)
23+
function log_test {
24+
"$@" >> $test_tmp 2>&1
25+
local status=$?
26+
last_test_result=$status
27+
28+
# check if polycubed is still alive (no crash in the meanwhile)
29+
polycubed_alive=$(ps -el | grep polycubed)
30+
if [ -z "$polycubed_alive" ]; then
31+
echo "polycubed not running ..."
32+
polycubed_crash=true
33+
status=1
34+
fi
35+
test_total=$(($test_total+1))
36+
if [ $status -ne 0 ]; then
37+
echo "++++TEST $1 FAILED++++"
38+
echo "++++TEST $1 FAILED++++" >> $test_results
39+
echo "++++TEST $1 FAILED++++" >> $test_tmp
40+
failed=true
41+
cat $test_tmp >> $test_log
42+
else
43+
test_passed=$(($test_passed+1))
44+
echo "++++TEST $1 PASSED++++"
45+
echo "++++TEST $1 PASSED++++" >> $test_results
46+
echo "++++TEST $1 PASSED++++" >> $test_tmp
47+
fi
48+
return $status
49+
}
50+
51+
# Check if services are loaded
52+
function services_are_loaded {
53+
echo "load_services:" > load_services
54+
count=0
55+
services_show=$(polycubectl services show)
56+
for i in "${!services[@]}"
57+
do
58+
lines=$(echo $services_show | grep $i | wc -l)
59+
if [ $lines -ne 0 ]
60+
then
61+
echo "$i YES" >> load_services
62+
else
63+
count=$((count + 1))
64+
echo "$i NO" >> load_services
65+
fi
66+
done
67+
echo $count
68+
}
69+
70+
# Check if polycubed rest server is responding
71+
function polycubed_is_responding {
72+
ret=$(polycubectl ? > /dev/null)
73+
ret=$(echo $?)
74+
echo $ret
75+
}
76+
77+
# Kill polycubed, and wait all services to be unloaded and process to be completely killed
78+
function polycubed_kill_and_wait {
79+
echo "killing polycubed ..."
80+
sudo pkill polycubed >> $test_tmp
81+
82+
done=0
83+
i=0
84+
while : ; do
85+
sleep 1
86+
alive=$(ps -el | grep polycubed)
87+
if [ -z "$alive" ]; then
88+
done=1
89+
fi
90+
91+
i=$((i+1))
92+
93+
if [ "$done" -ne 0 ]; then
94+
echo "killing polycubed in $i seconds"
95+
break
96+
fi
97+
done
98+
}
99+
100+
# Relaunch polycubed, if deamon is not running
101+
function polycubed_relaunch_if_not_running {
102+
alive=$(ps -el | grep polycubed)
103+
if [ -z "$alive" ]; then
104+
echo "polycubed not running ..."
105+
echo "relaunching polycubed ..."
106+
$polycubed >> $test_tmp 2>&1 &
107+
fi
108+
}
109+
110+
# Launch polycubed, and wait until it becomes responsive
111+
function launch_and_wait_polycubed_is_responding {
112+
if $RELAUNCH_POLYCUBED; then
113+
echo "starting polycubed ..."
114+
$polycubed >> $test_tmp 2>&1 &
115+
else
116+
polycubed_alive=$(ps -el | grep polycubed)
117+
if [ -z "$polycubed_alive" ]; then
118+
echo "polycubed not running ..."
119+
echo "relaunching polycubed ..."
120+
$polycubed >> $test_tmp 2>&1 &
121+
fi
122+
fi
123+
124+
done=0
125+
i=0
126+
while : ; do
127+
sleep 1
128+
responding=$(polycubed_is_responding)
129+
if [[ $responding -eq 0 ]]; then
130+
done=1
131+
else
132+
polycubed_relaunch_if_not_running
133+
fi
134+
i=$((i+1))
135+
if [ "$done" -ne 0 ]; then
136+
if $RELAUNCH_POLYCUBED; then
137+
echo "starting polycubed in $i seconds"
138+
else
139+
if [ -z "$polycubed_alive" ]; then
140+
echo "relaunching polycubed in $i seconds"
141+
fi
142+
fi
143+
break
144+
fi
145+
done
146+
147+
done=0
148+
i=0
149+
while : ; do
150+
sleep 1
151+
loaded=$(services_are_loaded)
152+
if [[ $loaded -eq 0 ]]; then
153+
done=1
154+
fi
155+
156+
i=$((i+1))
157+
if [ "$i" -eq $SERVICES_LOAD_TIMEOUT ]
158+
then
159+
echo "+ERROR+ timeout in checking services loaded $i seconds. try to run test anyway"
160+
cat load_services | grep NO
161+
break
162+
fi
163+
if [ "$done" -ne 0 ]; then
164+
echo "checking services loaded in $i seconds"
165+
break
166+
fi
167+
done
168+
}
169+
170+
# run test ($1:path)
171+
function run_test {
172+
polycubed_crash=false
173+
STARTTIME=$(date +%s)
174+
echo
175+
echo "Starting test $1 ..."
176+
echo "++++Starting test $1++++" > $test_tmp
177+
launch_and_wait_polycubed_is_responding
178+
echo "executing test ..."
179+
log_test $1
180+
if $RELAUNCH_POLYCUBED; then
181+
polycubed_kill_and_wait
182+
fi
183+
echo "Finished test $1"
184+
ENDTIME=$(date +%s)
185+
echo "Time elapsed: $(($ENDTIME - $STARTTIME))s"
186+
echo "Time elapsed: $(($ENDTIME - $STARTTIME))s" >> $test_results
187+
echo "Time elapsed: $(($ENDTIME - $STARTTIME))s" >> $test_tmp
188+
echo "++++Finished test $1++++" >> $test_tmp
189+
}
190+

tests/run-tests-iptables.sh

Lines changed: 2 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
source "${BASH_SOURCE%/*}/helpers_tests.bash"
4+
35
# use a clean instance of polycubed to run each test
46
RELAUNCH_POLYCUBED=true
57

@@ -14,24 +16,6 @@ RESULT_JSON="result.json"
1416
declare -A services
1517
services[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-
}
3519
trap cleanup EXIT
3620

3721
# $1 setup RELAUNCH_POLYCUBED
@@ -69,175 +53,6 @@ echo "Tests Scripts - LOG FILE" > $test_log
6953
date >> $test_log
7054
echo >> $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-
24156
function run_tests_from_dir {
24257
for test in $1local_test*.sh; do
24358
if [[ $test == *"local_test*.sh"* ]]; then

0 commit comments

Comments
 (0)