Split out generic functions for starting rsyncd.
[rsync/rsync.git] / testsuite / rsync.fns
1 #! /bin/sh
2
3 # Copyright (C) 2001 by Martin Pool <mbp@samba.org>
4
5 # General-purpose test functions for rsync.
6
7 TMP="$scratchdir"
8 FROM=${TMP}/from
9 TO=${TMP}/to
10 F1=text1
11 LOG=${TMP}/log
12 RSYNC="$rsync_bin"
13
14 runtest() {
15     echo $ECHO_N "Test $1: $ECHO_C"
16     eval "$2"
17 }
18
19 printmsg() {
20     echo "$1"
21 }
22
23 hands_setup() {
24     [ -d $FROM ] || mkdir $FROM
25     [ -d $TO ] || mkdir $TO
26
27     # set up test data
28     touch ${FROM}/empty
29     mkdir ${FROM}/emptydir
30
31     # a few hundred lines of test
32     ls -lR / | head -200 > ${FROM}/filelist
33
34     # This might fail on systems that don't have -n
35     echo $ECHO_N "This file has no trailing lf$ECHO_C" > ${FROM}/nolf
36     ln -s nolf ${FROM}/nolf-symlink
37     cat $srcdir/*.c | head -2000 > ${FROM}/${F1}
38     mkdir ${FROM}/dir
39     cp ${FROM}/${F1} ${FROM}/dir
40     mkdir ${FROM}/dir/subdir
41     mkdir ${FROM}/dir/subdir/subsubdir
42     ls -ltr /etc > ${FROM}/dir/subdir/subsubdir/etc-ltr-list
43     mkdir ${FROM}/dir/subdir/subsubdir2
44     ls -lt /bin > ${FROM}/dir/subdir/subsubdir2/bin-lt-list
45 }
46
47
48 ###########################
49 # Run a test (in '$1') then compare directories $2 and $3 to see if
50 # there are any difference.  If there are, explain them.
51
52 checkit() {
53     log=${LOG}
54     failed=
55     # the log accumulates all output; we only display it if there 
56     # is a problem.
57
58     echo "Running: \"$1\""  >${log}
59     echo "">>${log}
60     eval "$1"  >>${log} 2>&1
61     status=$?
62     if [ $status != 0 ]; then
63         failed="YES";
64     fi
65
66     echo "-------------">>${log}
67     echo "check how the files compare with diff:">>${log}
68     echo "">>${log}
69     diff -cr $2 $3 >>${log} 2>&1 || failed=YES
70     echo "-------------">>${log}
71     echo "check how the directory listings compare with diff:">>${log}
72     echo "">>${log}
73     ( cd $2 ; ls -laR ) > ${TMP}/ls-from 2>>${log}
74     ( cd $3 ; ls -laR ) > ${TMP}/ls-to  2>>${log}
75     diff -c ${TMP}/ls-from ${TMP}/ls-to >>${log} 2>&1 || failed=YES
76     if [ -z "${failed}" ] ; then
77         echo "${ECHO_T} done."
78         rm $log
79         return 0
80     else
81         echo "${ECHO_T} failed!"
82         cat ${log}
83         rm ${log}
84         return 1
85     fi
86 }
87
88
89 # In fact, we need a more general feature of capturing all stderr/log files,
90 # and dumping them if something goes wrong.
91
92 checkforlogs() {
93   # skip it if we're under debian-test
94   if test -n "${Debian}" ; then return 0 ; fi
95
96   if [ -f $1 -a -s $1 ] ; then
97         echo "Failures have occurred.  $1 follows:" >&2
98         cat $1 >&2
99         exit 1
100   fi
101 }
102
103
104 function build_rsyncd_conf {
105     # Build an appropriate configuration file
106     conf="$scratchdir/test-rsyncd.conf"
107     echo "building configuration $conf"
108
109     port=2612
110     pidfile="$scratchdir/rsyncd.pid"
111     logfile="$scratchdir/rsyncd.log"
112
113     cat >$conf <<EOF
114     # rsyncd configuration file autogenerated by $0
115
116     pid file = $pidfile
117     use chroot = no
118     hosts allow = localhost, 127.0.0.1
119     log file = $logfile
120
121     [test-from] = $scratchdir/daemon-from/
122             read only = yes
123
124     [test-to] = $scratchdir/daemon-to/
125             read only = no
126
127 EOF
128 }
129
130
131
132 function start_rsyncd {
133     echo starting daemon
134     $rsync_bin --daemon --port $port --config $conf
135     sleep 2
136     pid=`cat "$pidfile"`
137     echo rsyncd running as process $pid
138
139
140     # We need to make sure that we always kill rsync, even if there's an
141     # error.  Otherwise it might hang around, and be insecure or at any
142     # rate keep the port bound and prevent the tests running in the
143     # future.
144
145     trap "echo killing off process $pid; kill $pid" EXIT
146 }