More notes
[rsync/rsync.git] / testsuite / rsync.fns
... / ...
CommitLineData
1#! /bin/sh
2
3# Copyright (C) 2001 by Martin Pool <mbp@samba.org>
4
5# General-purpose test functions for rsync.
6
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License version
9# 2 as published by the Free Software Foundation.
10#
11# This program is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this program; if not, write to the Free Software
18# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20
21TMP="$scratchdir"
22FROM=${TMP}/from
23TO=${TMP}/to
24LOG=${TMP}/log
25RSYNC="$rsync_bin"
26
27runtest() {
28 echo $ECHO_N "Test $1: $ECHO_C"
29 if eval "$2"
30 then
31 echo "${ECHO_T} done."
32 return 0
33 else
34 echo "${ECHO_T} failed!"
35 return 1
36 fi
37}
38
39printmsg() {
40 echo "$1"
41}
42
43
44rsync_ls_lR() {
45 find "$@" -print | sort | xargs $TLS
46}
47
48
49####################
50# Build test directories TO and FROM, with FROM full of files.
51
52hands_setup() {
53 # Clean before creation
54 rm -rf $FROM
55 rm -rf $TO
56
57 [ -d $TMP ] || mkdir $TMP
58 [ -d $FROM ] || mkdir $FROM
59 [ -d $TO ] || mkdir $TO
60
61 # On some BSD systems, the umask affects the mode of created
62 # symlinks, even though the mode apparently has no effect on how
63 # the links behave in the future, and it cannot be changed using
64 # chmod! rsync always sets its umask to 000 so that it can
65 # accurately recreate permissions, but this script is probably run
66 # with a different umask.
67
68 # This causes a little problem that "ls -l" of the two will not be
69 # the same. So, we need to set our umask before doing any creations.
70
71 # set up test data
72 touch ${FROM}/empty
73 mkdir ${FROM}/emptydir
74
75 # a hundred lines of text or so
76 rsync_ls_lR "${srcdir}" > ${FROM}/filelist
77
78 # This might fail on systems that don't have -n
79 echo $ECHO_N "This file has no trailing lf$ECHO_C" > ${FROM}/nolf
80 umask 0
81 ln -s nolf ${FROM}/nolf-symlink
82 umask 022
83
84 cat $srcdir/*.c > ${FROM}/text
85 mkdir ${FROM}/dir
86 cp ${FROM}/text ${FROM}/dir
87 mkdir ${FROM}/dir/subdir
88 mkdir ${FROM}/dir/subdir/subsubdir
89 ls -ltr /etc > ${FROM}/dir/subdir/subsubdir/etc-ltr-list
90 mkdir ${FROM}/dir/subdir/subsubdir2
91 ls -lt /bin > ${FROM}/dir/subdir/subsubdir2/bin-lt-list
92
93# echo testing head:
94# ls -lR ${srcdir} | head -10 || echo failed
95}
96
97
98hands_cleanup() {
99 rm -r "$TMP"
100}
101
102
103
104
105####################
106# Many machines do not have "mkdir -p", so we have to build up long paths.
107# How boring.
108makepath () {
109 echo " makepath $1"
110 p="$1"
111 (
112 # Absolut Unix.
113 if echo $p | grep '^/' >/dev/null
114 then
115 cd /
116 fi
117
118 # This will break if $1 contains a space.
119 for c in `echo $p | tr '/' ' '`
120 do
121 if [ -d "$c" ] || mkdir "$c"
122 then
123 cd "$c" || return $?
124 else
125 echo "failed to create $c" >&2; return $?
126 fi
127 done
128 )
129}
130
131
132
133###########################
134# Run a test (in '$1') then compare directories $2 and $3 to see if
135# there are any difference. If there are, explain them.
136
137checkit() {
138 failed=
139
140 # We can just write everything to stdout/stderr, because the
141 # wrapper hides it unless there is a problem.
142
143 echo "Running: \"$1\""
144 eval "$1"
145 status=$?
146 if [ $status != 0 ]; then
147 failed="YES";
148 fi
149
150 echo "-------------"
151 echo "check how the files compare with diff:"
152 echo ""
153 for f in `cd "$2"; find . -type f -print `
154 do
155 diff -c "$2"/"$f" "$3"/"$f" || failed=YES
156 done
157
158 echo "-------------"
159 echo "check how the directory listings compare with diff:"
160 echo ""
161 ( cd "$2" && rsync_ls_lR . ) > ${TMP}/ls-from
162 ( cd "$3" && rsync_ls_lR . ) > ${TMP}/ls-to
163 diff -c ${TMP}/ls-from ${TMP}/ls-to || failed=YES
164 if [ -z "${failed}" ] ; then
165 return 0
166 else
167 return 1
168 fi
169}
170
171
172# In fact, we need a more general feature of capturing all stderr/log files,
173# and dumping them if something goes wrong.
174
175checkforlogs() {
176 # skip it if we're under debian-test
177 if test -n "${Debian}" ; then return 0 ; fi
178
179 if [ -f $1 -a -s $1 ] ; then
180 echo "Failures have occurred. $1 follows:" >&2
181 cat $1 >&2
182 exit 1
183 fi
184}
185
186
187build_rsyncd_conf() {
188 # Build an appropriate configuration file
189 conf="$scratchdir/test-rsyncd.conf"
190 echo "building configuration $conf"
191
192 port=2612
193 pidfile="$scratchdir/rsyncd.pid"
194 logfile="$scratchdir/rsyncd.log"
195
196 cat >$conf <<EOF
197# rsyncd configuration file autogenerated by $0
198
199pid file = $pidfile
200use chroot = no
201hosts allow = localhost, 127.0.0.1
202log file = $logfile
203
204[test-from]
205 path = $FROM
206 read only = yes
207
208[test-to]
209 path = $TO
210 read only = no
211EOF
212}
213
214
215build_symlinks() {
216 fromdir="$scratchdir/from"
217 todir="$scratchdir/to"
218 mkdir "$fromdir"
219 date >"$fromdir/referent"
220 ln -s referent "$fromdir/relative"
221 ln -s "$fromdir/referent" "$fromdir/absolute"
222 ln -s nonexistent "$fromdir/dangling"
223 ln -s "$srcdir/rsync.c" "$fromdir/unsafe"
224}
225
226test_fail() {
227 echo "$@" >&2
228 exit 1
229}
230
231test_skipped() {
232 echo "$@" >&2
233 exit 77
234}
235
236# It failed, but we expected that. don't dump out error logs,
237# because most users won't want to see them. But do leave
238# the working directory around.
239test_xfail() {
240 echo "$@" >&2
241 exit 78
242}
243
244# be reproducible
245umask 077