|
| 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 | + |
0 commit comments