xref: /freebsd/usr.sbin/etcupdate/tests/ignore_test.sh (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
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
32FAILED=no
33WORKDIR=work
34
35usage()
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.
42COMMAND=etcupdate
43while 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
56done
57shift $((OPTIND - 1))
58if [ $# -ne 0 ]; then
59	usage
60fi
61
62CONFLICTS=$WORKDIR/conflicts
63OLD=$WORKDIR/old
64NEW=$WORKDIR/current
65TEST=$WORKDIR/test
66
67# These tests deal with ignoring certain patterns of files.  We run the
68# test multiple times ignoring different patterns.
69build_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
99missing()
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
108present()
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
117dir()
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
128file()
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
152conflict()
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
169noconflict()
170{
171	if [ -f $CONFLICTS/$1 ]; then
172		echo "File $1 should not have a conflict"
173		FAILED=yes
174	fi
175}
176
177if [ `id -u` -ne 0 ]; then
178	echo "must be root"
179	exit 0
180fi
181
182if [ -r /etc/etcupdate.conf ]; then
183	echo "WARNING: /etc/etcupdate.conf settings may break some tests."
184fi
185
186# First run the test ignoring no patterns.
187
188build_trees
189
190$COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
191
192cat > $WORKDIR/correct.out <<EOF
193  D /rmdir/file
194  D /tree/remove
195  D /rmdir
196  U /tree/modify
197  A /tree/add
198EOF
199
200echo "Differences for regular:"
201diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
202    || FAILED=yes
203
204missing /tree/remove
205file /tree/modify "new"
206file /tree/add "foo"
207missing /rmdir/file
208missing /rmdir
209
210# Now test with -I '/tree/*'.  This should preserve the /tree files.
211
212build_trees
213
214$COMMAND -r -I '/tree/*' -d $WORKDIR -D $TEST > $WORKDIR/test1.out
215
216cat > $WORKDIR/correct1.out <<EOF
217  D /rmdir/file
218  D /rmdir
219EOF
220
221echo "Differences for -I '/tree/*':"
222diff -u -L "correct" $WORKDIR/correct1.out -L "test" $WORKDIR/test1.out \
223    || FAILED=yes
224
225file /tree/remove "old"
226file /tree/modify "old"
227missing /tree/add
228missing /rmdir/file
229missing /rmdir
230
231# Now test with two patterns.  This should preserve everything.
232
233build_trees
234
235$COMMAND -r -I '/tree/*' -I '/rmdir*' -d $WORKDIR -D $TEST > \
236    $WORKDIR/test2.out
237
238cat > $WORKDIR/correct2.out <<EOF
239EOF
240
241echo "Differences for -I '/tree/*' -I '/rmdir*':"
242
243diff -u -L "correct" $WORKDIR/correct2.out -L "test" $WORKDIR/test2.out \
244    || FAILED=yes
245
246file /tree/remove "old"
247file /tree/modify "old"
248missing /tree/add
249file /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
255build_trees
256
257$COMMAND -r -I '/tree/* /rmdir/*' -d $WORKDIR -D $TEST > \
258    $WORKDIR/test3.out
259
260cat > $WORKDIR/correct3.out <<EOF
261Warnings:
262  Non-empty directory remains: /rmdir
263EOF
264
265echo "Differences for -I '/tree/* /rmdir/*':"
266
267diff -u -L "correct" $WORKDIR/correct3.out -L "test" $WORKDIR/test3.out \
268    || FAILED=yes
269
270file /tree/remove "old"
271file /tree/modify "old"
272missing /tree/add
273file /rmdir/file "foo"
274dir /rmdir
275
276[ "${FAILED}" = no ]
277