]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.sbin/etcupdate/tests/ignore_test.sh
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / usr.sbin / etcupdate / tests / ignore_test.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2010 Hudson River Trading LLC
4 # Written by: John H. Baldwin <jhb@FreeBSD.org>
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in the
14 #    documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 # SUCH DAMAGE.
27 #
28 # $FreeBSD$
29
30 # Various regression tests to test the -I flag to the 'update' command.
31
32 FAILED=no
33 WORKDIR=work
34
35 usage()
36 {
37         echo "Usage: ignore.sh [-s script] [-w workdir]"
38         exit 1
39 }
40
41 # Allow the user to specify an alternate work directory or script.
42 COMMAND=etcupdate
43 while getopts "s:w:" option; do
44         case $option in
45                 s)
46                         COMMAND="sh $OPTARG"
47                         ;;
48                 w)
49                         WORKDIR=$OPTARG
50                         ;;
51                 *)
52                         echo
53                         usage
54                         ;;
55         esac
56 done
57 shift $((OPTIND - 1))
58 if [ $# -ne 0 ]; then
59         usage
60 fi
61
62 CONFLICTS=$WORKDIR/conflicts
63 OLD=$WORKDIR/old
64 NEW=$WORKDIR/current
65 TEST=$WORKDIR/test
66
67 # These tests deal with ignoring certain patterns of files.  We run the
68 # test multiple times ignoring different patterns.
69 build_trees()
70 {
71         local i
72
73         rm -rf $OLD $NEW $TEST $CONFLICTS
74         mkdir -p $OLD $NEW $TEST
75
76         for i in $OLD $NEW $TEST; do
77                 mkdir -p $i/tree
78         done
79
80         # tree: Test three different cases (add, modify, remove) that all
81         # match the tree/* glob.
82         echo "foo" > $NEW/tree/add
83         for i in $OLD $TEST; do
84                 echo "old" > $i/tree/modify
85         done
86         echo "new" > $NEW/tree/modify
87         for i in $OLD $TEST; do
88                 echo "old" > $i/tree/remove
89         done
90
91         # rmdir: Remove a whole tree.
92         for i in $OLD $TEST; do
93                 mkdir $i/rmdir
94                 echo "foo" > $i/rmdir/file
95         done
96 }
97
98 # $1 - relative path to file that should be missing from TEST
99 missing()
100 {
101         if [ -e $TEST/$1 -o -L $TEST/$1 ]; then
102                 echo "File $1 should be missing"
103                 FAILED=yes
104         fi
105 }
106
107 # $1 - relative path to file that should be present in TEST
108 present()
109 {
110         if ! [ -e $TEST/$1 -o -L $TEST/$1 ]; then
111                 echo "File $1 should be present"
112                 FAILED=yes
113         fi
114 }
115
116 # $1 - relative path to file that should be a directory in TEST
117 dir()
118 {
119         if ! [ -d $TEST/$1 ]; then
120                 echo "File $1 should be a directory"
121                 FAILED=yes
122         fi
123 }
124
125 # $1 - relative path to regular file that should be present in TEST
126 # $2 - optional string that should match file contents
127 # $3 - optional MD5 of the flie contents, overrides $2 if present
128 file()
129 {
130         local contents sum
131
132         if ! [ -f $TEST/$1 ]; then
133                 echo "File $1 should be a regular file"
134                 FAILED=yes
135         elif [ $# -eq 2 ]; then
136                 contents=`cat $TEST/$1`
137                 if [ "$contents" != "$2" ]; then
138                         echo "File $1 has wrong contents"
139                         FAILED=yes
140                 fi
141         elif [ $# -eq 3 ]; then
142                 sum=`md5 -q $TEST/$1`
143                 if [ "$sum" != "$3" ]; then
144                         echo "File $1 has wrong contents"
145                         FAILED=yes
146                 fi
147         fi
148 }
149
150 # $1 - relative path to a regular file that should have a conflict
151 # $2 - optional MD5 of the conflict file contents
152 conflict()
153 {
154         local sum
155
156         if ! [ -f $CONFLICTS/$1 ]; then
157                 echo "File $1 missing conflict"
158                 FAILED=yes
159         elif [ $# -gt 1 ]; then
160                 sum=`md5 -q $CONFLICTS/$1`
161                 if [ "$sum" != "$2" ]; then
162                         echo "Conflict $1 has wrong contents"
163                         FAILED=yes
164                 fi
165         fi
166 }
167
168 # $1 - relative path to a regular file that should not have a conflict
169 noconflict()
170 {
171         if [ -f $CONFLICTS/$1 ]; then
172                 echo "File $1 should not have a conflict"
173                 FAILED=yes
174         fi
175 }
176
177 if [ `id -u` -ne 0 ]; then
178         echo "must be root"
179         exit 0
180 fi
181
182 if [ -r /etc/etcupdate.conf ]; then
183         echo "WARNING: /etc/etcupdate.conf settings may break some tests."
184 fi
185
186 # First run the test ignoring no patterns.
187
188 build_trees
189
190 $COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
191
192 cat > $WORKDIR/correct.out <<EOF
193   D /rmdir/file
194   D /tree/remove
195   D /rmdir
196   U /tree/modify
197   A /tree/add
198 EOF
199
200 echo "Differences for regular:"
201 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
202     || FAILED=yes
203
204 missing /tree/remove
205 file /tree/modify "new"
206 file /tree/add "foo"
207 missing /rmdir/file
208 missing /rmdir
209
210 # Now test with -I '/tree/*'.  This should preserve the /tree files.
211
212 build_trees
213
214 $COMMAND -r -I '/tree/*' -d $WORKDIR -D $TEST > $WORKDIR/test1.out
215
216 cat > $WORKDIR/correct1.out <<EOF
217   D /rmdir/file
218   D /rmdir
219 EOF
220
221 echo "Differences for -I '/tree/*':"
222 diff -u -L "correct" $WORKDIR/correct1.out -L "test" $WORKDIR/test1.out \
223     || FAILED=yes
224
225 file /tree/remove "old"
226 file /tree/modify "old"
227 missing /tree/add
228 missing /rmdir/file
229 missing /rmdir
230
231 # Now test with two patterns.  This should preserve everything.
232
233 build_trees
234
235 $COMMAND -r -I '/tree/*' -I '/rmdir*' -d $WORKDIR -D $TEST > \
236     $WORKDIR/test2.out
237
238 cat > $WORKDIR/correct2.out <<EOF
239 EOF
240
241 echo "Differences for -I '/tree/*' -I '/rmdir*':"
242
243 diff -u -L "correct" $WORKDIR/correct2.out -L "test" $WORKDIR/test2.out \
244     || FAILED=yes
245
246 file /tree/remove "old"
247 file /tree/modify "old"
248 missing /tree/add
249 file /rmdir/file "foo"
250
251 # Now test with a pattern that should cause a warning on /rmdir by
252 # only ignoring the files under that directory.  Note that this also
253 # tests putting two patterns into a single -I argument.
254
255 build_trees
256
257 $COMMAND -r -I '/tree/* /rmdir/*' -d $WORKDIR -D $TEST > \
258     $WORKDIR/test3.out
259
260 cat > $WORKDIR/correct3.out <<EOF
261 Warnings:
262   Non-empty directory remains: /rmdir
263 EOF
264
265 echo "Differences for -I '/tree/* /rmdir/*':"
266
267 diff -u -L "correct" $WORKDIR/correct3.out -L "test" $WORKDIR/test3.out \
268     || FAILED=yes
269
270 file /tree/remove "old"
271 file /tree/modify "old"
272 missing /tree/add
273 file /rmdir/file "foo"
274 dir /rmdir
275
276 [ "${FAILED}" = no ]