1238423Sjhb#!/bin/sh
2238423Sjhb#
3238423Sjhb# Copyright (c) 2010 Advanced Computing Technologies LLC
4238423Sjhb# Written by: John H. Baldwin <jhb@FreeBSD.org>
5238423Sjhb# All rights reserved.
6238423Sjhb#
7238423Sjhb# Redistribution and use in source and binary forms, with or without
8238423Sjhb# modification, are permitted provided that the following conditions
9238423Sjhb# are met:
10238423Sjhb# 1. Redistributions of source code must retain the above copyright
11238423Sjhb#    notice, this list of conditions and the following disclaimer.
12238423Sjhb# 2. Redistributions in binary form must reproduce the above copyright
13238423Sjhb#    notice, this list of conditions and the following disclaimer in the
14238423Sjhb#    documentation and/or other materials provided with the distribution.
15238423Sjhb#
16238423Sjhb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17238423Sjhb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18238423Sjhb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19238423Sjhb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20238423Sjhb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21238423Sjhb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22238423Sjhb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23238423Sjhb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24238423Sjhb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25238423Sjhb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26238423Sjhb# SUCH DAMAGE.
27238423Sjhb#
28238423Sjhb# $FreeBSD$
29238423Sjhb
30238423Sjhb# Various regression tests to test the -I flag to the 'update' command.
31238423Sjhb
32263221SjmmvFAILED=no
33238423SjhbWORKDIR=work
34238423Sjhb
35238423Sjhbusage()
36238423Sjhb{
37258063Sjhb	echo "Usage: ignore.sh [-s script] [-w workdir]"
38238423Sjhb	exit 1
39238423Sjhb}
40238423Sjhb
41258063Sjhb# Allow the user to specify an alternate work directory or script.
42258063SjhbCOMMAND=etcupdate
43258063Sjhbwhile getopts "s:w:" option; do
44238423Sjhb	case $option in
45258063Sjhb		s)
46258063Sjhb			COMMAND="sh $OPTARG"
47258063Sjhb			;;
48238423Sjhb		w)
49238423Sjhb			WORKDIR=$OPTARG
50238423Sjhb			;;
51238423Sjhb		*)
52238423Sjhb			echo
53238423Sjhb			usage
54238423Sjhb			;;
55238423Sjhb	esac
56238423Sjhbdone
57238423Sjhbshift $((OPTIND - 1))
58238423Sjhbif [ $# -ne 0 ]; then
59238423Sjhb	usage
60238423Sjhbfi
61238423Sjhb
62238423SjhbCONFLICTS=$WORKDIR/conflicts
63238423SjhbOLD=$WORKDIR/old
64238423SjhbNEW=$WORKDIR/current
65238423SjhbTEST=$WORKDIR/test
66238423Sjhb
67238423Sjhb# These tests deal with ignoring certain patterns of files.  We run the
68238423Sjhb# test multiple times ignoring different patterns.
69238423Sjhbbuild_trees()
70238423Sjhb{
71238423Sjhb	local i
72238423Sjhb
73238423Sjhb	rm -rf $OLD $NEW $TEST $CONFLICTS
74238423Sjhb	mkdir -p $OLD $NEW $TEST
75238423Sjhb
76238423Sjhb	for i in $OLD $NEW $TEST; do
77238423Sjhb		mkdir -p $i/tree
78238423Sjhb	done
79238423Sjhb
80238423Sjhb	# tree: Test three different cases (add, modify, remove) that all
81238423Sjhb	# match the tree/* glob.
82238423Sjhb	echo "foo" > $NEW/tree/add
83238423Sjhb	for i in $OLD $TEST; do
84238423Sjhb		echo "old" > $i/tree/modify
85238423Sjhb	done
86238423Sjhb	echo "new" > $NEW/tree/modify
87238423Sjhb	for i in $OLD $TEST; do
88238423Sjhb		echo "old" > $i/tree/remove
89238423Sjhb	done
90238423Sjhb
91238423Sjhb	# rmdir: Remove a whole tree.
92238423Sjhb	for i in $OLD $TEST; do
93238423Sjhb		mkdir $i/rmdir
94238423Sjhb		echo "foo" > $i/rmdir/file
95238423Sjhb	done
96238423Sjhb}
97238423Sjhb
98238423Sjhb# $1 - relative path to file that should be missing from TEST
99238423Sjhbmissing()
100238423Sjhb{
101238423Sjhb	if [ -e $TEST/$1 -o -L $TEST/$1 ]; then
102238423Sjhb		echo "File $1 should be missing"
103263221Sjmmv		FAILED=yes
104238423Sjhb	fi
105238423Sjhb}
106238423Sjhb
107238423Sjhb# $1 - relative path to file that should be present in TEST
108238423Sjhbpresent()
109238423Sjhb{
110238423Sjhb	if ! [ -e $TEST/$1 -o -L $TEST/$1 ]; then
111238423Sjhb		echo "File $1 should be present"
112263221Sjmmv		FAILED=yes
113238423Sjhb	fi
114238423Sjhb}
115238423Sjhb
116238423Sjhb# $1 - relative path to file that should be a directory in TEST
117238423Sjhbdir()
118238423Sjhb{
119238423Sjhb	if ! [ -d $TEST/$1 ]; then
120238423Sjhb		echo "File $1 should be a directory"
121263221Sjmmv		FAILED=yes
122238423Sjhb	fi
123238423Sjhb}
124238423Sjhb
125238423Sjhb# $1 - relative path to regular file that should be present in TEST
126238423Sjhb# $2 - optional string that should match file contents
127238423Sjhb# $3 - optional MD5 of the flie contents, overrides $2 if present
128238423Sjhbfile()
129238423Sjhb{
130238423Sjhb	local contents sum
131238423Sjhb
132238423Sjhb	if ! [ -f $TEST/$1 ]; then
133238423Sjhb		echo "File $1 should be a regular file"
134263221Sjmmv		FAILED=yes
135238423Sjhb	elif [ $# -eq 2 ]; then
136238423Sjhb		contents=`cat $TEST/$1`
137238423Sjhb		if [ "$contents" != "$2" ]; then
138238423Sjhb			echo "File $1 has wrong contents"
139263221Sjmmv			FAILED=yes
140238423Sjhb		fi
141238423Sjhb	elif [ $# -eq 3 ]; then
142238423Sjhb		sum=`md5 -q $TEST/$1`
143238423Sjhb		if [ "$sum" != "$3" ]; then
144238423Sjhb			echo "File $1 has wrong contents"
145263221Sjmmv			FAILED=yes
146238423Sjhb		fi
147238423Sjhb	fi
148238423Sjhb}
149238423Sjhb
150238423Sjhb# $1 - relative path to a regular file that should have a conflict
151238423Sjhb# $2 - optional MD5 of the conflict file contents
152238423Sjhbconflict()
153238423Sjhb{
154238423Sjhb	local sum
155238423Sjhb
156238423Sjhb	if ! [ -f $CONFLICTS/$1 ]; then
157238423Sjhb		echo "File $1 missing conflict"
158263221Sjmmv		FAILED=yes
159238423Sjhb	elif [ $# -gt 1 ]; then
160238423Sjhb		sum=`md5 -q $CONFLICTS/$1`
161238423Sjhb		if [ "$sum" != "$2" ]; then
162238423Sjhb			echo "Conflict $1 has wrong contents"
163263221Sjmmv			FAILED=yes
164238423Sjhb		fi
165238423Sjhb	fi
166238423Sjhb}
167238423Sjhb
168238423Sjhb# $1 - relative path to a regular file that should not have a conflict
169238423Sjhbnoconflict()
170238423Sjhb{
171238423Sjhb	if [ -f $CONFLICTS/$1 ]; then
172238423Sjhb		echo "File $1 should not have a conflict"
173263221Sjmmv		FAILED=yes
174238423Sjhb	fi
175238423Sjhb}
176238423Sjhb
177238423Sjhbif [ `id -u` -ne 0 ]; then
178238423Sjhb	echo "must be root"
179263221Sjmmv	exit 0
180238423Sjhbfi
181238423Sjhb
182238423Sjhbif [ -r /etc/etcupdate.conf ]; then
183238423Sjhb	echo "WARNING: /etc/etcupdate.conf settings may break some tests."
184238423Sjhbfi
185238423Sjhb
186238423Sjhb# First run the test ignoring no patterns.
187238423Sjhb
188238423Sjhbbuild_trees
189238423Sjhb
190258063Sjhb$COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
191238423Sjhb
192238423Sjhbcat > $WORKDIR/correct.out <<EOF
193238423Sjhb  D /rmdir/file
194238423Sjhb  D /tree/remove
195238423Sjhb  D /rmdir
196238423Sjhb  U /tree/modify
197238423Sjhb  A /tree/add
198238423SjhbEOF
199238423Sjhb
200238423Sjhbecho "Differences for regular:"
201263221Sjmmvdiff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
202263221Sjmmv    || FAILED=yes
203238423Sjhb
204238423Sjhbmissing /tree/remove
205238423Sjhbfile /tree/modify "new"
206238423Sjhbfile /tree/add "foo"
207238423Sjhbmissing /rmdir/file
208238423Sjhbmissing /rmdir
209238423Sjhb
210238423Sjhb# Now test with -I '/tree/*'.  This should preserve the /tree files.
211238423Sjhb
212238423Sjhbbuild_trees
213238423Sjhb
214258063Sjhb$COMMAND -r -I '/tree/*' -d $WORKDIR -D $TEST > $WORKDIR/test1.out
215238423Sjhb
216238423Sjhbcat > $WORKDIR/correct1.out <<EOF
217238423Sjhb  D /rmdir/file
218238423Sjhb  D /rmdir
219238423SjhbEOF
220238423Sjhb
221238423Sjhbecho "Differences for -I '/tree/*':"
222263221Sjmmvdiff -u -L "correct" $WORKDIR/correct1.out -L "test" $WORKDIR/test1.out \
223263221Sjmmv    || FAILED=yes
224238423Sjhb
225238423Sjhbfile /tree/remove "old"
226238423Sjhbfile /tree/modify "old"
227238423Sjhbmissing /tree/add
228238423Sjhbmissing /rmdir/file
229238423Sjhbmissing /rmdir
230238423Sjhb
231238423Sjhb# Now test with two patterns.  This should preserve everything.
232238423Sjhb
233238423Sjhbbuild_trees
234238423Sjhb
235258063Sjhb$COMMAND -r -I '/tree/*' -I '/rmdir*' -d $WORKDIR -D $TEST > \
236238423Sjhb    $WORKDIR/test2.out
237238423Sjhb
238238423Sjhbcat > $WORKDIR/correct2.out <<EOF
239238423SjhbEOF
240238423Sjhb
241238423Sjhbecho "Differences for -I '/tree/*' -I '/rmdir*':"
242238423Sjhb
243263221Sjmmvdiff -u -L "correct" $WORKDIR/correct2.out -L "test" $WORKDIR/test2.out \
244263221Sjmmv    || FAILED=yes
245238423Sjhb
246238423Sjhbfile /tree/remove "old"
247238423Sjhbfile /tree/modify "old"
248238423Sjhbmissing /tree/add
249238423Sjhbfile /rmdir/file "foo"
250238423Sjhb
251238423Sjhb# Now test with a pattern that should cause a warning on /rmdir by
252238423Sjhb# only ignoring the files under that directory.  Note that this also
253238423Sjhb# tests putting two patterns into a single -I argument.
254238423Sjhb
255238423Sjhbbuild_trees
256238423Sjhb
257258063Sjhb$COMMAND -r -I '/tree/* /rmdir/*' -d $WORKDIR -D $TEST > \
258238423Sjhb    $WORKDIR/test3.out
259238423Sjhb
260238423Sjhbcat > $WORKDIR/correct3.out <<EOF
261238423SjhbWarnings:
262238423Sjhb  Non-empty directory remains: /rmdir
263238423SjhbEOF
264238423Sjhb
265238423Sjhbecho "Differences for -I '/tree/* /rmdir/*':"
266238423Sjhb
267263221Sjmmvdiff -u -L "correct" $WORKDIR/correct3.out -L "test" $WORKDIR/test3.out \
268263221Sjmmv    || FAILED=yes
269238423Sjhb
270238423Sjhbfile /tree/remove "old"
271238423Sjhbfile /tree/modify "old"
272238423Sjhbmissing /tree/add
273238423Sjhbfile /rmdir/file "foo"
274238423Sjhbdir /rmdir
275263221Sjmmv
276263221Sjmmv[ "${FAILED}" = no ]
277