Skip to content

Commit 743a2c0

Browse files
authored
Merge pull request #127 from polycube-network/pr/fix_tests_wait_service
tests: check for services to be loaded before starting test
2 parents ae16e79 + 46d164a commit 743a2c0

4 files changed

Lines changed: 212 additions & 291 deletions

File tree

tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ test_log*
22
test_results*
33
tmp
44
result.json
5+
load_services

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: 6 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
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

8+
SERVICES_LOAD_TIMEOUT=10
9+
610
# launch polycubed in DEBUG mode
711
DEBUG=false
812

913
# result.json path
1014
RESULT_JSON="result.json"
1115

12-
# cleanup environment before exit
13-
function cleanup {
14-
set +e
15-
echo "killing polycubed ..."
16-
sudo pkill polycubed >> $test_tmp
17-
echo "{ \"passed\":\"$test_passed\", \"total\":\"$test_total\", \"test_log\":\"$test_log\" }" > $RESULT_JSON
18-
19-
cat $test_results
20-
21-
echo ""
22-
echo "FAILED TESTS:"
23-
echo ""
24-
cat $test_results | grep FAILED -A 1
16+
declare -A services
17+
services[iptables]=pcn-iptables
2518

26-
if $failed ; then
27-
exit 1
28-
fi
29-
}
3019
trap cleanup EXIT
3120

3221
# $1 setup RELAUNCH_POLYCUBED
@@ -64,134 +53,6 @@ echo "Tests Scripts - LOG FILE" > $test_log
6453
date >> $test_log
6554
echo >> $test_log
6655

67-
# execute and log test ($1:path)
68-
function log_test {
69-
"$@" >> $test_tmp 2>&1
70-
local status=$?
71-
last_test_result=$status
72-
73-
# check if polycubed is still alive (no crash in the meanwhile)
74-
polycubed_alive=$(ps -el | grep polycubed)
75-
if [ -z "$polycubed_alive" ]; then
76-
echo "polycubed not running ..."
77-
polycubed_crash=true
78-
status=1
79-
fi
80-
test_total=$(($test_total+1))
81-
if [ $status -ne 0 ]; then
82-
echo "++++TEST $1 FAILED++++"
83-
echo "++++TEST $1 FAILED++++" >> $test_results
84-
echo "++++TEST $1 FAILED++++" >> $test_tmp
85-
failed=true
86-
cat $test_tmp >> $test_log
87-
else
88-
test_passed=$(($test_passed+1))
89-
echo "++++TEST $1 PASSED++++"
90-
echo "++++TEST $1 PASSED++++" >> $test_results
91-
echo "++++TEST $1 PASSED++++" >> $test_tmp
92-
fi
93-
return $status
94-
}
95-
96-
# Check if polycubed rest server is responding
97-
function polycubed_is_responding {
98-
ret=$(polycubectl ? > /dev/null)
99-
ret=$(echo $?)
100-
echo $ret
101-
}
102-
103-
# Kill polycubed, and wait all services to be unloaded and process to be completely killed
104-
function polycubed_kill_and_wait {
105-
echo "killing polycubed ..."
106-
sudo pkill polycubed >> $test_tmp
107-
108-
done=0
109-
i=0
110-
while : ; do
111-
sleep 1
112-
alive=$(ps -el | grep polycubed)
113-
if [ -z "$alive" ]; then
114-
done=1
115-
fi
116-
117-
i=$((i+1))
118-
119-
if [ "$done" -ne 0 ]; then
120-
echo "killing polycubed in $i seconds"
121-
break
122-
fi
123-
done
124-
}
125-
126-
# Relaunch polycubed, if deamon is not running
127-
function polycubed_relaunch_if_not_running {
128-
alive=$(ps -el | grep polycubed)
129-
if [ -z "$alive" ]; then
130-
echo "polycubed not running ..."
131-
echo "relaunching polycubed ..."
132-
$polycubed >> $test_tmp 2>&1 &
133-
fi
134-
}
135-
136-
# Launch polycubed, and wait until it becomes responsive
137-
function launch_and_wait_polycubed_is_responding {
138-
if $RELAUNCH_POLYCUBED; then
139-
echo "starting polycubed ..."
140-
$polycubed >> $test_tmp 2>&1 &
141-
else
142-
polycubed_alive=$(ps -el | grep polycubed)
143-
if [ -z "$polycubed_alive" ]; then
144-
echo "polycubed not running ..."
145-
echo "relaunching polycubed ..."
146-
$polycubed >> $test_tmp 2>&1 &
147-
fi
148-
fi
149-
150-
done=0
151-
i=0
152-
while : ; do
153-
sleep 1
154-
responding=$(polycubed_is_responding)
155-
if [[ $responding -eq 0 ]]; then
156-
done=1
157-
else
158-
polycubed_relaunch_if_not_running
159-
fi
160-
i=$((i+1))
161-
if [ "$done" -ne 0 ]; then
162-
if $RELAUNCH_POLYCUBED; then
163-
echo "starting polycubed in $i seconds"
164-
else
165-
if [ -z "$polycubed_alive" ]; then
166-
echo "relaunching polycubed in $i seconds"
167-
fi
168-
fi
169-
break
170-
fi
171-
done
172-
}
173-
174-
# run test ($1:path)
175-
function run_test {
176-
polycubed_crash=false
177-
STARTTIME=$(date +%s)
178-
echo
179-
echo "Starting test $1 ..."
180-
echo "++++Starting test $1++++" > $test_tmp
181-
launch_and_wait_polycubed_is_responding
182-
echo "executing test ..."
183-
log_test $1
184-
if $RELAUNCH_POLYCUBED; then
185-
polycubed_kill_and_wait
186-
fi
187-
echo "Finished test $1"
188-
ENDTIME=$(date +%s)
189-
echo "Time elapsed: $(($ENDTIME - $STARTTIME))s"
190-
echo "Time elapsed: $(($ENDTIME - $STARTTIME))s" >> $test_results
191-
echo "Time elapsed: $(($ENDTIME - $STARTTIME))s" >> $test_tmp
192-
echo "++++Finished test $1++++" >> $test_tmp
193-
}
194-
19556
function run_tests_from_dir {
19657
for test in $1local_test*.sh; do
19758
if [[ $test == *"local_test*.sh"* ]]; then

0 commit comments

Comments
 (0)