1#!/bin/sh -x
2
3# configuration
4
5TIMEOUT_SYNC=15
6
7case $(uname -s) in
8	Linux)
9		MAKE='make'
10		;;
11	*)
12		MAKE='gmake'
13		;;
14esac
15
16# test aggressive optimizations
17export CFLAGS="$CFLAGS -O3 -march=native"
18autoreconf -if
19
20# Build unit test
21build_test() {
22	$MAKE clean
23	echo ">>> Testing with \"$@\""
24	# make sure we test paralled build as they tend to fail when single works
25	./configure $@ && $MAKE -j5 || {
26		echo "!!! test with \"$@\" configure options failed"
27		exit 1
28	}
29}
30
31# Cleanup functions for run_example()
32run_example_cleanup_success() {
33	rm -rf "examples/testdir"/{to,from}/*
34	pkill -F "$CLSYNC_PIDFILE"
35}
36run_example_cleanup_failure() {
37	pkill -F "$CLSYNC_PIDFILE" 2>/dev/null
38	echo "$@" >&2
39	exit 1
40}
41
42# Run example script
43run_example() {
44	MODE="$1"
45
46	export CLSYNC_PIDFILE="/tmp/clsync-example-$MODE.pid"
47
48	rm -rf "examples/testdir"/*/*
49	mkdir -p "examples/testdir/to" "examples/testdir/from"
50
51	trap run_example_cleanup_failure INT TERM
52	cd examples
53	bash -x clsync-start-"$MODE".sh --background --pid-file "$CLSYNC_PIDFILE" --config-file '/NULL/' -w1 -t1 -d1
54	cd -
55
56	sleep 1
57	mkdir -p examples/testdir/from/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/DIR
58	touch examples/testdir/from/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/a
59	touch examples/testdir/from/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/b
60	touch examples/testdir/from/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/c
61	touch examples/testdir/from/a/b/c/d/e/f/g/h/7
62	touch examples/testdir/from/test
63	mkdir examples/testdir/from/dontSync
64	i=0
65	while [ "$i" -le "$TIMEOUT_SYNC" ]; do
66		if [ -f "examples/testdir/to/test" -a -f "examples/testdir/to/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/a" -a -d "examples/testdir/to/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/DIR" ]; then
67			sleep 1
68			break
69		fi
70		sleep 1
71		i=$(( $i + 1 ))
72	done
73	if [ "$i" -gt "$TIMEOUT_SYNC" ]; then
74		run_example_cleanup_failure "$MODE" "timed out on initial syncing"
75	fi
76	if ! [ -f "$CLSYNC_PIDFILE" ]; then
77		run_example_cleanup_failure "$MODE" "no pidfile"
78	fi
79	touch "examples/testdir/from/file" "examples/testdir/from/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/DIR/file"
80	rm -rf "examples/testdir/from/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/DIR"
81	i=0
82	while [ "$i" -le "$TIMEOUT_SYNC" ]; do
83		if ! [ -f "$CLSYNC_PIDFILE" ]; then
84			run_example_cleanup_failure "$MODE" "premature exit"
85		fi
86		if [ -f "examples/testdir/to/file" -a ! -d "examples/testdir/to/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/DIR" ]; then
87			sleep 1
88			run_example_cleanup_success
89			return
90		fi
91		sleep 1
92		i=$(( $i + 1 ))
93	done
94	run_example_cleanup_failure "$MODE" "no successful sync"
95}
96
97if true; then
98
99	# Test all possible package-specific configure options.
100	# Do not test empty cases save as no options at all.
101
102	build_test
103
104	# clsync enabled
105	a0="--enable-clsync"
106	for a1 in "--enable-cluster --with-mhash" "--enable-cluster --without-mhash" "--disable-cluster"; do
107	for a2 in "--enable-debug" "--disable-debug"; do
108	for a3 in "--enable-paranoid=0" "--enable-paranoid=1" "--enable-paranoid=2" ; do
109	for a4 in "--with-capabilities" "--without-capabilities"; do
110	for a5 in "--enable-socket" "--disable-socket"; do
111	for a6 in "--enable-libclsync" "--disable-libclsync"; do
112		arg="$a0 $a1 $a2 $a3 $a4 $a5 $a6"
113		build_test "$arg"
114	done
115	done
116	done
117	done
118	done
119	done
120
121	# clsync disabled, libclsync enabled
122	a0="--disable-clsync --enable-libclsync"
123	for a2 in "--enable-debug" "--disable-debug"; do
124	for a3 in "--enable-paranoid=0" "--enable-paranoid=1" "--enable-paranoid=2" ; do
125		arg="$a0 $a1 $a2"
126		build_test "$arg"
127	done
128	done
129
130	# clsync disabled, libclsync disabled
131	build_test "--disable-clsync --disable-libclsync"
132
133fi
134
135if true; then
136
137	# Test coverage
138
139	export CFLAGS="$CFLAGS --coverage -O0"
140	export PATH="$(pwd):$PATH"
141	build_test --enable-cluster --enable-debug --enable-paranoid=2 --with-capabilities --without-mhash
142	run_example rsyncdirect
143	run_example rsyncshell
144#	run_example rsyncso
145	#run_example so
146	#run_example cluster
147
148fi
149
150exit 0
151