]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/regression/usr.sbin/etcupdate/ignore.sh
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / regression / usr.sbin / etcupdate / ignore.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2010 Advanced Computing Technologies 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 WORKDIR=work
33
34 usage()
35 {
36         echo "Usage: ignore.sh [-w workdir]"
37         exit 1
38 }
39
40 # Allow the user to specify an alternate work directory.
41 while getopts "w:" option; do
42         case $option in
43                 w)
44                         WORKDIR=$OPTARG
45                         ;;
46                 *)
47                         echo
48                         usage
49                         ;;
50         esac
51 done
52 shift $((OPTIND - 1))
53 if [ $# -ne 0 ]; then
54         usage
55 fi
56
57 CONFLICTS=$WORKDIR/conflicts
58 OLD=$WORKDIR/old
59 NEW=$WORKDIR/current
60 TEST=$WORKDIR/test
61
62 # These tests deal with ignoring certain patterns of files.  We run the
63 # test multiple times ignoring different patterns.
64 build_trees()
65 {
66         local i
67
68         rm -rf $OLD $NEW $TEST $CONFLICTS
69         mkdir -p $OLD $NEW $TEST
70
71         for i in $OLD $NEW $TEST; do
72                 mkdir -p $i/tree
73         done
74
75         # tree: Test three different cases (add, modify, remove) that all
76         # match the tree/* glob.
77         echo "foo" > $NEW/tree/add
78         for i in $OLD $TEST; do
79                 echo "old" > $i/tree/modify
80         done
81         echo "new" > $NEW/tree/modify
82         for i in $OLD $TEST; do
83                 echo "old" > $i/tree/remove
84         done
85
86         # rmdir: Remove a whole tree.
87         for i in $OLD $TEST; do
88                 mkdir $i/rmdir
89                 echo "foo" > $i/rmdir/file
90         done
91 }
92
93 # $1 - relative path to file that should be missing from TEST
94 missing()
95 {
96         if [ -e $TEST/$1 -o -L $TEST/$1 ]; then
97                 echo "File $1 should be missing"
98         fi
99 }
100
101 # $1 - relative path to file that should be present in TEST
102 present()
103 {
104         if ! [ -e $TEST/$1 -o -L $TEST/$1 ]; then
105                 echo "File $1 should be present"
106         fi
107 }
108
109 # $1 - relative path to file that should be a directory in TEST
110 dir()
111 {
112         if ! [ -d $TEST/$1 ]; then
113                 echo "File $1 should be a directory"
114         fi
115 }
116
117 # $1 - relative path to regular file that should be present in TEST
118 # $2 - optional string that should match file contents
119 # $3 - optional MD5 of the flie contents, overrides $2 if present
120 file()
121 {
122         local contents sum
123
124         if ! [ -f $TEST/$1 ]; then
125                 echo "File $1 should be a regular file"
126         elif [ $# -eq 2 ]; then
127                 contents=`cat $TEST/$1`
128                 if [ "$contents" != "$2" ]; then
129                         echo "File $1 has wrong contents"
130                 fi
131         elif [ $# -eq 3 ]; then
132                 sum=`md5 -q $TEST/$1`
133                 if [ "$sum" != "$3" ]; then
134                         echo "File $1 has wrong contents"
135                 fi
136         fi
137 }
138
139 # $1 - relative path to a regular file that should have a conflict
140 # $2 - optional MD5 of the conflict file contents
141 conflict()
142 {
143         local sum
144
145         if ! [ -f $CONFLICTS/$1 ]; then
146                 echo "File $1 missing conflict"
147         elif [ $# -gt 1 ]; then
148                 sum=`md5 -q $CONFLICTS/$1`
149                 if [ "$sum" != "$2" ]; then
150                         echo "Conflict $1 has wrong contents"
151                 fi
152         fi
153 }
154
155 # $1 - relative path to a regular file that should not have a conflict
156 noconflict()
157 {
158         if [ -f $CONFLICTS/$1 ]; then
159                 echo "File $1 should not have a conflict"
160         fi
161 }
162
163 if [ `id -u` -ne 0 ]; then
164         echo "must be root"
165 fi
166
167 if [ -r /etc/etcupdate.conf ]; then
168         echo "WARNING: /etc/etcupdate.conf settings may break some tests."
169 fi
170
171 # First run the test ignoring no patterns.
172
173 build_trees
174
175 etcupdate -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
176
177 cat > $WORKDIR/correct.out <<EOF
178   D /rmdir/file
179   D /tree/remove
180   D /rmdir
181   U /tree/modify
182   A /tree/add
183 EOF
184
185 echo "Differences for regular:"
186 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out
187
188 missing /tree/remove
189 file /tree/modify "new"
190 file /tree/add "foo"
191 missing /rmdir/file
192 missing /rmdir
193
194 # Now test with -I '/tree/*'.  This should preserve the /tree files.
195
196 build_trees
197
198 etcupdate -r -I '/tree/*' -d $WORKDIR -D $TEST > $WORKDIR/test1.out
199
200 cat > $WORKDIR/correct1.out <<EOF
201   D /rmdir/file
202   D /rmdir
203 EOF
204
205 echo "Differences for -I '/tree/*':"
206 diff -u -L "correct" $WORKDIR/correct1.out -L "test" $WORKDIR/test1.out
207
208 file /tree/remove "old"
209 file /tree/modify "old"
210 missing /tree/add
211 missing /rmdir/file
212 missing /rmdir
213
214 # Now test with two patterns.  This should preserve everything.
215
216 build_trees
217
218 etcupdate -r -I '/tree/*' -I '/rmdir*' -d $WORKDIR -D $TEST > \
219     $WORKDIR/test2.out
220
221 cat > $WORKDIR/correct2.out <<EOF
222 EOF
223
224 echo "Differences for -I '/tree/*' -I '/rmdir*':"
225
226 diff -u -L "correct" $WORKDIR/correct2.out -L "test" $WORKDIR/test2.out
227
228 file /tree/remove "old"
229 file /tree/modify "old"
230 missing /tree/add
231 file /rmdir/file "foo"
232
233 # Now test with a pattern that should cause a warning on /rmdir by
234 # only ignoring the files under that directory.  Note that this also
235 # tests putting two patterns into a single -I argument.
236
237 build_trees
238
239 etcupdate -r -I '/tree/* /rmdir/*' -d $WORKDIR -D $TEST > \
240     $WORKDIR/test3.out
241
242 cat > $WORKDIR/correct3.out <<EOF
243 Warnings:
244   Non-empty directory remains: /rmdir
245 EOF
246
247 echo "Differences for -I '/tree/* /rmdir/*':"
248
249 diff -u -L "correct" $WORKDIR/correct3.out -L "test" $WORKDIR/test3.out
250
251 file /tree/remove "old"
252 file /tree/modify "old"
253 missing /tree/add
254 file /rmdir/file "foo"
255 dir /rmdir