1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
#!/bin/bash
################################################################
# This is a small collection of helper functions to produce
# TAP, as in http://en.wikipedia.org/wiki/Test_Anything_Protocol
# excercised by prove(1)
#
#
# Minimal Introduction
# --------------------
#
# Each test script (*/*.t) will include this file,
# which will generate a "testing plan
# http://search.cpan.org/~petdance/Test-Harness-2.64/lib/Test/Harness/TAP.pod#THE_TAP_FORMAT
#
# TAP's general format is:
#
# 1..N
# ok 1 Description # Directive
# # Diagnostic
# ....
# not ok 46 Description
# ok 47 Description
# ok 48 Description
# not ok 49 # TODO not yet implemented
# not ok 50 # SKIP needs 77bit architecture
# more tests....
#
# Skipping whole test files:
#
# 1..0 # Skipped: WWW::Mechanize not installed
#
# Bail out!
#
# As an emergency measure a test script can decide that further tests are useless
# (e.g. missing dependencies) and testing should stop immediately. In that case
# the test script prints the magic words
#
# Bail out! MySQL is not running.
#
################################################################
# You better not use fd 44 for anything else...
exec 44>&1
bail_out()
{
>&44 echo "Bail out! $*"
# no exit trap on bailout.
trap - EXIT
exit 1
}
dbg()
{
local lvl=$1
shift
(( $DEBUG_LEVEL < $lvl )) && return
>&44 echo "##<$lvl># $*"
}
# Sometimes (e.g. when doing "-TT"), single shot ("-iii") is not good enough.
# But we should be able to kill that daemon without using "killall".
# So several layers of indirection are bad,
# the caller would not know whom to kill really.
csync2_daemon()
{
dbg 1 "CSYNC2_SYSTEM_DIR=$CSYNC2_SYSTEM_DIR csync2 -D $CSYNC2_DATABASE $*"
exec "$SOURCE_DIR/csync2" -D "$CSYNC2_DATABASE" "$@"
}
csync2()
{
local ex
dbg 1 "CSYNC2_SYSTEM_DIR=$CSYNC2_SYSTEM_DIR csync2 -D $CSYNC2_DATABASE $*"
command "$SOURCE_DIR/csync2" -D "$CSYNC2_DATABASE" "$@"
ex=$?
dbg 1 "exit code: $ex"
return $ex
}
## Does NOT remove the config file
cleanup()
{
local host
local i
case $CSYNC2_DATABASE in
sqlite://*|sqlite2://*|sqlite3://*|/*)
mkdir -p "$CSYNC2_DATABASE/"
rm -f "$CSYNC2_DATABASE/"*.csync2.test.db*
;;
pgsql://csync2:csync2@*/)
host=${CSYNC2_DATABASE#pgsql://csync2:csync2@}
host=${host%/}
[[ $host = *[/\ ]* ]] && bail_out "cannot use $CSYNC2_DATABASE"
for i in {1..9}; do
psql "host=$host user=csync2 password=csync2 dbname=postgres" \
-c "DROP DATABASE IF EXISTS csync2_${i}_csync2_test;" \
|| bail_out "could not cleanup postgres database $i"
done
;;
mysql://csync2:csync2@*/)
host=${CSYNC2_DATABASE#mysql://csync2:csync2@}
host=${host%/}
[[ $host = *[/\ ]* ]] && bail_out "cannot use $CSYNC2_DATABASE"
for i in {1..9}; do
mysql --host=$host --user=csync2 --password=csync2 mysql \
-e "DROP DATABASE IF EXISTS csync2_${i}_csync2_test;" \
|| bail_out "could not cleanup mysql database $i"
done
;;
*)
bail_out "unsupported CSYNC2_DATABASE setting $CSYNC2_DATABASE"
;;
esac
rm -rf "$TESTS_DIR/"{1..9}
mkdir -p "$TESTS_DIR/"{1..9}
}
prepare_etc_hosts_bring_up_ips()
{
for i in {1..9}; do
eval N$i=$i.csync2.test
eval IP$i=127.2.1.$i
eval D$i=$TESTS_DIR/$i
grep -qFxe "127.2.1.$i $i.csync2.test" /etc/hosts \
|| echo "127.2.1.$i $i.csync2.test" >> /etc/hosts
ip -o -f inet a s dev lo | grep -qFe " 127.2.1.$i/" ||
ip a add dev lo 127.2.1.$i/24
done
}
# generates a new config file with proper %demodir% prefixes
prepare_cfg_file()
{
local CFG="$CSYNC2_SYSTEM_DIR/csync2.cfg";
if test -e "$CFG" ; then
dbg 1 "$CFG already in place, using it as is"
return
fi
dbg 0 "generating $CFG"
cat > "$CFG" <<-___
group demo
{
host 1.csync2.test;
host 2.csync2.test;
key csync2.key_demo;
include %demodir%;
exclude %demodir%/e;
}
prefix demodir
{
on 1.csync2.test: $TESTS_DIR/1;
on 2.csync2.test: $TESTS_DIR/2;
on 3.csync2.test: $TESTS_DIR/3;
on 4.csync2.test: $TESTS_DIR/4;
on 5.csync2.test: $TESTS_DIR/5;
on 6.csync2.test: $TESTS_DIR/6;
on 7.csync2.test: $TESTS_DIR/7;
on 8.csync2.test: $TESTS_DIR/8;
on 9.csync2.test: $TESTS_DIR/9;
}
nossl * *;
___
}
__test_result_header()
{
printf "# %s %s %s\n" $TEST_CALL_STACK
echo "# =========== $description = trace = {{{3"
sed -e 's/^/# -x : /' < "$TESTS_TMP_DIR/xtrace"
echo "# =========== $description = err = {{{3"
sed -e 's/^/# err: /' < "$TESTS_TMP_DIR/err"
echo "# =========== $description = out = {{{2"
sed -e 's/^/# out: /' < "$TESTS_TMP_DIR/out"
echo "# =========== $description = exit:$result = expected:$expected_result_code }}}1"
}
__test_not_ok()
{
__test_result_header
echo "not ok $__test_cur - $description"
let __test_not_ok_cnt++
return 1
}
__test_ok()
{
(( $DEBUG_LEVEL > 1 )) && __test_result_header
echo "ok $__test_cur - $description"
let __test_ok_cnt++
return 0
}
_TEST()
{
let __test_cur++
local expected_result_code=$1; shift
local description=$1; shift
local result="N/A"
export TEST_DESCRIPTION="$0 $description"
export TEST_CALL_STACK="$(i=0; while caller $i; do let i++; done)"
# truncate stdout/stderr/xtrace capture files
: >"$TESTS_TMP_DIR/out" >"$TESTS_TMP_DIR/err" >"$TESTS_TMP_DIR/xtrace"
echo "# =========== "$test_line" {{{1"
if [[ $# = 0 ]] ; then
__test_not_ok "Bad test line"
bail_out "You need to fix the test scripts first"
return 1
fi
(
set -e
cd "$TESTS_TMP_DIR"
exec >>out 2>>err 45>>xtrace
BASH_XTRACEFD=45
: $BASHPID
set +e -x
"$@"
)
result=$?
if [[ ${GIVE_ME_A_SHELL_AFTER_EACH_TEST-} ]] && test -t 0 && test -t 1 ; then
(
cd "$TESTS_TMP_DIR"
echo "-----------"
echo "CSYNC2 TEST debug shell ..."
echo "$0 -- "$test_line
echo "To abort further tests: touch bailout"
echo " -------------"
export -f csync2 dbg
bash
)
test -e "$TESTS_TMP_DIR/bailout" && bail_out "$TESTS_TMP_DIR/bailout"
fi
if [[ $result = $expected_result_code ]] ; then
__test_ok
else
__test_not_ok
[[ ${BAIL_OUT_EARLY:-} ]] && bail_out "$BAIL_OUT_EARLY"
fi
}
TEST()
{
local test_line="TEST '$1' ${*:2}"
_TEST 0 "$@"
}
TEST_EXPECT_EXIT_CODE()
{
local test_line="TEST_EXPECT_EXIT_CODE [expected:$1] '$2' ${*:3}"
_TEST "$@"
}
TEST_BREAK()
{
GIVE_ME_A_SHELL_AFTER_EACH_TEST=1 TEST "$@"
}
require()
{
if ! "$@"; then
echo "# $*"
echo "1..0 # Skipped: test requirements failed"
exit 1
fi
}
#############################################################################
## some helper functions that are likely to be reused by many test scripts ##
#############################################################################
csync2_u()
{
local tmp kid now client_exit nc_exit server_exit
if csync2 -N $1 -M > /dev/null; then
csync2 -N $2 -iii -vvv &
kid=$!
# try to avoid the connect-before-listen race,
# wait for the expected listening socket to show up,
# with timeout using bash magic $SECONDS.
now=$SECONDS
while ! ss -tnl src $2:csync2 | grep -q ^LISTEN; do
kill -0 $kid
(( SECONDS - now < 2 ))
sleep 0.1
done
csync2 -N $1 -uvvv
client_exit=$?
if ss -tnl src $2:csync2 | grep -q ^LISTEN; then
# server still alive?
# then no connection was made...
# attempt to do one now.
tmp=$(nc $2 $CSYNC2_PORT <<<"BYE" )
nc_exit=$?
[[ $nc_exit = 0 ]] || kill $kid
else
nc_exit=1
fi
wait $kid
server_exit=$?
[[ $client_exit = 0 && $server_exit = 0 && $nc_exit != 0 ]]
else
echo "Apparently nothing dirty on $1, not starting server on $2"
# but still do the csync2 -u ...
csync2 -N $1 -uvvv
fi
}
################################
## Basic setup code follows ##
################################
set -u
if [[ $BASH_SOURCE == include.sh ]] ; then
TESTS_DIR=$PWD
elif [[ $BASH_SOURCE == */include.sh ]]; then
TESTS_DIR=${BASH_SOURCE%/include.sh}
else
bail_out "Sorry, I'm confused..."
fi
TESTS_DIR=$(cd "$TESTS_DIR" && pwd )
SOURCE_DIR=$(cd "$TESTS_DIR/.." && pwd )
: CSYNC2_PORT ${CSYNC2_PORT:=30865}
: CSYNC2_SYSTEM_DIR ${CSYNC2_SYSTEM_DIR:=$TESTS_DIR/etc}
: CSYNC2_DATABASE ${CSYNC2_DATABASE:=$TESTS_DIR/db}
: DEBUG_LEVEL=${DEBUG_LEVEL:=0}
__test_cur=0
__test_ok_cnt=0
__test_not_ok_cnt=0
case "$0" in
-bash|bash)
echo >&2 "Interactive mode??"
__test_total_cnt=0
export -f csync2
;;
*)
TESTS_TMP_DIR=$(mktemp -d "$TESTS_DIR/tmp.XXX")
trap 'rm -rf "$TESTS_TMP_DIR"' EXIT
__test_total_cnt=$(grep -Ece '^[[:space:]]*\<(TEST|TEST_EXPECT_EXIT_CODE|TEST_BREAK)\>' $0)
;;
esac
if [[ $# -gt 1 ]] && [[ $1 = require ]]; then
"$@"
fi
export CSYNC2_SYSTEM_DIR CSYNC2_DATABASE
export TESTS_DIR TESTS_TMP_DIR SOURCE_DIR
prepare_etc_hosts_bring_up_ips
prepare_cfg_file
# The Plan
echo 1..$__test_total_cnt
|