Commit | Line | Data |
---|---|---|
d820215b MP |
1 | #! /bin/sh |
2 | ||
3 | # Copyright (C) 2001 by Martin Pool <mbp@samba.org> | |
4 | ||
5 | # General-purpose test functions for rsync. | |
3a4c683f | 6 | |
0154b302 MP |
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 | ||
3a4c683f MP |
21 | TMP="$scratchdir" |
22 | FROM=${TMP}/from | |
23 | TO=${TMP}/to | |
3a4c683f | 24 | LOG=${TMP}/log |
3fedd74b | 25 | RSYNC="$rsync_bin" |
3a4c683f | 26 | |
2094283b | 27 | # Berkley's nice. |
96553aa7 | 28 | PATH="$PATH:/usr/ucb" |
2094283b | 29 | |
84229c7a WD |
30 | if diff -u $srcdir/testsuite/rsync.fns $srcdir/testsuite/rsync.fns >/dev/null 2>&1; then |
31 | diffopt="-u" | |
32 | else | |
33 | diffopt="-c" | |
34 | fi | |
35 | ||
3a4c683f MP |
36 | runtest() { |
37 | echo $ECHO_N "Test $1: $ECHO_C" | |
501972bf MP |
38 | if eval "$2" |
39 | then | |
40 | echo "${ECHO_T} done." | |
41 | return 0 | |
42 | else | |
43 | echo "${ECHO_T} failed!" | |
44 | return 1 | |
45 | fi | |
3a4c683f MP |
46 | } |
47 | ||
48 | printmsg() { | |
49 | echo "$1" | |
50 | } | |
51 | ||
e052b21f | 52 | |
6773a779 | 53 | rsync_ls_lR() { |
dfef3f10 MP |
54 | find "$@" -print | sort | xargs "$TOOLDIR/tls" |
55 | } | |
56 | ||
57 | rsync_getgroups() { | |
58 | "$TOOLDIR/getgroups" | |
57835c00 MP |
59 | } |
60 | ||
61 | ||
e052b21f MP |
62 | #################### |
63 | # Build test directories TO and FROM, with FROM full of files. | |
64 | ||
3a4c683f | 65 | hands_setup() { |
501972bf MP |
66 | # Clean before creation |
67 | rm -rf $FROM | |
68 | rm -rf $TO | |
3d807132 MP |
69 | |
70 | [ -d $TMP ] || mkdir $TMP | |
3a4c683f MP |
71 | [ -d $FROM ] || mkdir $FROM |
72 | [ -d $TO ] || mkdir $TO | |
73 | ||
e052b21f MP |
74 | # On some BSD systems, the umask affects the mode of created |
75 | # symlinks, even though the mode apparently has no effect on how | |
76 | # the links behave in the future, and it cannot be changed using | |
77 | # chmod! rsync always sets its umask to 000 so that it can | |
78 | # accurately recreate permissions, but this script is probably run | |
79 | # with a different umask. | |
80 | ||
81 | # This causes a little problem that "ls -l" of the two will not be | |
82 | # the same. So, we need to set our umask before doing any creations. | |
83 | ||
3a4c683f MP |
84 | # set up test data |
85 | touch ${FROM}/empty | |
86 | mkdir ${FROM}/emptydir | |
e8ca5901 | 87 | |
571a4b26 | 88 | # a hundred lines of text or so |
3d807132 | 89 | rsync_ls_lR "${srcdir}" > ${FROM}/filelist |
3a4c683f MP |
90 | |
91 | # This might fail on systems that don't have -n | |
92 | echo $ECHO_N "This file has no trailing lf$ECHO_C" > ${FROM}/nolf | |
0154b302 | 93 | umask 0 |
3a4c683f | 94 | ln -s nolf ${FROM}/nolf-symlink |
4c80c473 | 95 | umask 022 |
0154b302 | 96 | |
4c80c473 | 97 | cat $srcdir/*.c > ${FROM}/text |
3a4c683f | 98 | mkdir ${FROM}/dir |
4c80c473 | 99 | cp ${FROM}/text ${FROM}/dir |
3a4c683f MP |
100 | mkdir ${FROM}/dir/subdir |
101 | mkdir ${FROM}/dir/subdir/subsubdir | |
102 | ls -ltr /etc > ${FROM}/dir/subdir/subsubdir/etc-ltr-list | |
103 | mkdir ${FROM}/dir/subdir/subsubdir2 | |
104 | ls -lt /bin > ${FROM}/dir/subdir/subsubdir2/bin-lt-list | |
7d691654 | 105 | |
8f98c608 MP |
106 | # echo testing head: |
107 | # ls -lR ${srcdir} | head -10 || echo failed | |
3a4c683f | 108 | } |
3fedd74b MP |
109 | |
110 | ||
99cdaff7 MP |
111 | #################### |
112 | # Many machines do not have "mkdir -p", so we have to build up long paths. | |
113 | # How boring. | |
114 | makepath () { | |
7c1b7890 | 115 | echo " makepath $1" |
99cdaff7 MP |
116 | p="$1" |
117 | ( | |
118 | # Absolut Unix. | |
119 | if echo $p | grep '^/' >/dev/null | |
120 | then | |
121 | cd / | |
122 | fi | |
123 | ||
124 | # This will break if $1 contains a space. | |
125 | for c in `echo $p | tr '/' ' '` | |
126 | do | |
112e7311 MP |
127 | if [ -d "$c" ] || mkdir "$c" |
128 | then | |
129 | cd "$c" || return $? | |
130 | else | |
131 | echo "failed to create $c" >&2; return $? | |
132 | fi | |
99cdaff7 MP |
133 | done |
134 | ) | |
135 | } | |
136 | ||
137 | ||
138 | ||
3fedd74b MP |
139 | ########################### |
140 | # Run a test (in '$1') then compare directories $2 and $3 to see if | |
141 | # there are any difference. If there are, explain them. | |
142 | ||
f494f286 MP |
143 | # So normally basically $1 should be an rsync command, and $2 and $3 |
144 | # the source and destination directories. This is only good when you | |
145 | # expect to transfer the whole directory exactly as is. If some files | |
146 | # should be excluded, you might need to use something else. | |
147 | ||
3fedd74b | 148 | checkit() { |
3fedd74b | 149 | failed= |
3fedd74b | 150 | |
cf72f204 MP |
151 | # We can just write everything to stdout/stderr, because the |
152 | # wrapper hides it unless there is a problem. | |
153 | ||
154 | echo "Running: \"$1\"" | |
155 | eval "$1" | |
3fedd74b MP |
156 | status=$? |
157 | if [ $status != 0 ]; then | |
158 | failed="YES"; | |
159 | fi | |
160 | ||
cf72f204 MP |
161 | echo "-------------" |
162 | echo "check how the files compare with diff:" | |
163 | echo "" | |
d58e4c27 MP |
164 | for f in `cd "$2"; find . -type f -print ` |
165 | do | |
84229c7a | 166 | diff $diffopt "$2"/"$f" "$3"/"$f" || failed=YES |
d58e4c27 MP |
167 | done |
168 | ||
cf72f204 MP |
169 | echo "-------------" |
170 | echo "check how the directory listings compare with diff:" | |
171 | echo "" | |
172 | ( cd "$2" && rsync_ls_lR . ) > ${TMP}/ls-from | |
173 | ( cd "$3" && rsync_ls_lR . ) > ${TMP}/ls-to | |
84229c7a | 174 | diff $diffopt ${TMP}/ls-from ${TMP}/ls-to || failed=YES |
3fedd74b | 175 | if [ -z "${failed}" ] ; then |
3fedd74b MP |
176 | return 0 |
177 | else | |
3fedd74b MP |
178 | return 1 |
179 | fi | |
180 | } | |
181 | ||
863dff51 | 182 | |
4a7cb3e8 | 183 | build_rsyncd_conf() { |
d2094cc3 MP |
184 | # Build an appropriate configuration file |
185 | conf="$scratchdir/test-rsyncd.conf" | |
186 | echo "building configuration $conf" | |
187 | ||
188 | port=2612 | |
189 | pidfile="$scratchdir/rsyncd.pid" | |
190 | logfile="$scratchdir/rsyncd.log" | |
191 | ||
192 | cat >$conf <<EOF | |
3aae15ec | 193 | # rsyncd configuration file autogenerated by $0 |
d2094cc3 | 194 | |
3aae15ec MP |
195 | pid file = $pidfile |
196 | use chroot = no | |
197 | hosts allow = localhost, 127.0.0.1 | |
198 | log file = $logfile | |
eddeaf76 | 199 | max verbosity = 9 |
d2094cc3 | 200 | |
76ee1d18 WD |
201 | uid = 0 |
202 | gid = 0 | |
98c1b325 | 203 | |
2e6c7f45 MP |
204 | [test-from] |
205 | path = $FROM | |
3aae15ec | 206 | read only = yes |
d2094cc3 | 207 | |
2e6c7f45 MP |
208 | [test-to] |
209 | path = $TO | |
3aae15ec | 210 | read only = no |
d2094cc3 MP |
211 | EOF |
212 | } | |
213 | ||
214 | ||
3cd2af41 MP |
215 | build_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" | |
1db8b61d | 223 | ln -s "$srcdir/rsync.c" "$fromdir/unsafe" |
3cd2af41 MP |
224 | } |
225 | ||
226 | test_fail() { | |
227 | echo "$@" >&2 | |
228 | exit 1 | |
229 | } | |
230 | ||
a217ad30 MP |
231 | test_skipped() { |
232 | echo "$@" >&2 | |
7d821932 | 233 | echo "$@" > "$TMP/whyskipped" |
a217ad30 MP |
234 | exit 77 |
235 | } | |
236 | ||
be2f866b MP |
237 | # It failed, but we expected that. don't dump out error logs, |
238 | # because most users won't want to see them. But do leave | |
239 | # the working directory around. | |
240 | test_xfail() { | |
241 | echo "$@" >&2 | |
242 | exit 78 | |
243 | } | |
244 | ||
d1239eae MP |
245 | # Determine what shell command will appropriately test for links. |
246 | ln -s foo "$scratchdir/testlink" | |
247 | for cmd in test /bin/test /usr/bin/test /usr/ucb/bin/test /usr/ucb/test | |
248 | do | |
249 | for switch in -h -L | |
250 | do | |
251 | if $cmd $switch "$scratchdir/testlink" 2>/dev/null | |
252 | then | |
253 | # how nice | |
254 | TEST_SYMLINK_CMD="$cmd $switch" | |
255 | # i wonder if break 2 is portable? | |
256 | break 2 | |
257 | fi | |
258 | done | |
259 | done | |
cca4e067 MP |
260 | # ok, now get rid of it |
261 | rm "$scratchdir/testlink" | |
d1239eae MP |
262 | |
263 | ||
264 | if [ "x$TEST_SYMLINK_CMD" = 'x' ] | |
265 | then | |
266 | test_fail "Couldn't determine how to test for symlinks" | |
267 | else | |
268 | echo "Testing for symlinks using '$TEST_SYMLINK_CMD'" | |
269 | fi | |
270 | ||
271 | ||
d96d3893 MP |
272 | # Test whether something is a link, allowing for shell peculiarities |
273 | is_a_link() { | |
d1239eae MP |
274 | # note the variable contains the first option and therefore is not quoted |
275 | $TEST_SYMLINK_CMD "$1" | |
d96d3893 MP |
276 | } |
277 | ||
77867907 MP |
278 | |
279 | # We need to set the umask to be reproducible. Note also that when we | |
280 | # do some daemon tests as root, we will setuid() and therefore the | |
281 | # directory has to be writable by the nobody user in some cases. The | |
282 | # best thing is probably to explicitly chmod those directories after | |
283 | # creation. | |
284 | ||
ad301e48 | 285 | umask 022 |