134229Speter#!/bin/sh
295349Sobrien#
334229Speter# Copyright (c) 2010 Hudson River Trading LLC
434229Speter# Written by: John H. Baldwin <jhb@FreeBSD.org>
551408Sobrien# All rights reserved.
634229Speter#
752112Sobrien# Redistribution and use in source and binary forms, with or without
818334Speter# modification, are permitted provided that the following conditions
918334Speter# are met:
1018334Speter# 1. Redistributions of source code must retain the above copyright
1118334Speter#    notice, this list of conditions and the following disclaimer.
1218334Speter# 2. Redistributions in binary form must reproduce the above copyright
1318334Speter#    notice, this list of conditions and the following disclaimer in the
1418334Speter#    documentation and/or other materials provided with the distribution.
1518334Speter#
1618334Speter# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1718334Speter# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1818334Speter# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1918334Speter# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2018334Speter# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2118334Speter# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2218334Speter# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2318334Speter# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2418334Speter# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2518334Speter# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2651408Sobrien# SUCH DAMAGE.
2718334Speter#
2895810Sobrien
2995810Sobrien# Various regression tests to test the -I flag to the 'update' command.
3051408Sobrien
3158478SobrienFAILED=no
3295810SobrienWORKDIR=work
3358478Sobrien
3458478Sobrienusage()
3551408Sobrien{
3658478Sobrien	echo "Usage: ignore.sh [-s script] [-w workdir]"
3758478Sobrien	exit 1
3858478Sobrien}
3995349Sobrien
4058478Sobrien# Allow the user to specify an alternate work directory or script.
4158478SobrienCOMMAND=etcupdate
4258478Sobrienwhile getopts "s:w:" option; do
4358478Sobrien	case $option in
4458478Sobrien		s)
4558478Sobrien			COMMAND="sh $OPTARG"
4658478Sobrien			;;
4758478Sobrien		w)
4858478Sobrien			WORKDIR=$OPTARG
4958478Sobrien			;;
5058478Sobrien		*)
5158478Sobrien			echo
5258478Sobrien			usage
5358478Sobrien			;;
5458478Sobrien	esac
5558478Sobriendone
5658478Sobrienshift $((OPTIND - 1))
5758478Sobrienif [ $# -ne 0 ]; then
5858478Sobrien	usage
5958478Sobrienfi
6058478Sobrien
6158478SobrienCONFLICTS=$WORKDIR/conflicts
6295349SobrienOLD=$WORKDIR/old
6358478SobrienNEW=$WORKDIR/current
6458478SobrienTEST=$WORKDIR/test
6558478Sobrien
6658478Sobrien# These tests deal with ignoring certain patterns of files.  We run the
6758478Sobrien# test multiple times ignoring different patterns.
6858478Sobrienbuild_trees()
6995349Sobrien{
7095349Sobrien	local i
7158478Sobrien
7295349Sobrien	rm -rf $OLD $NEW $TEST $CONFLICTS
7395349Sobrien	mkdir -p $OLD $NEW $TEST
7495349Sobrien
7595349Sobrien	for i in $OLD $NEW $TEST; do
7658478Sobrien		mkdir -p $i/tree
7758478Sobrien	done
7858478Sobrien
7958478Sobrien	# tree: Test three different cases (add, modify, remove) that all
8058478Sobrien	# match the tree/* glob.
8158478Sobrien	echo "foo" > $NEW/tree/add
8258478Sobrien	for i in $OLD $TEST; do
8358478Sobrien		echo "old" > $i/tree/modify
8458478Sobrien	done
8558478Sobrien	echo "new" > $NEW/tree/modify
8658478Sobrien	for i in $OLD $TEST; do
8758478Sobrien		echo "old" > $i/tree/remove
8858478Sobrien	done
8995349Sobrien
9095349Sobrien	# rmdir: Remove a whole tree.
9195349Sobrien	for i in $OLD $TEST; do
9258478Sobrien		mkdir $i/rmdir
9358478Sobrien		echo "foo" > $i/rmdir/file
9495349Sobrien	done
9558478Sobrien}
9658478Sobrien
9758478Sobrien# $1 - relative path to file that should be missing from TEST
9858478Sobrienmissing()
9958478Sobrien{
10058478Sobrien	if [ -e $TEST/$1 -o -L $TEST/$1 ]; then
10158478Sobrien		echo "File $1 should be missing"
10258478Sobrien		FAILED=yes
10358478Sobrien	fi
10496447Sobrien}
10596447Sobrien
10696447Sobrien# $1 - relative path to file that should be present in TEST
10796447Sobrienpresent()
10858478Sobrien{
10996447Sobrien	if ! [ -e $TEST/$1 -o -L $TEST/$1 ]; then
11096447Sobrien		echo "File $1 should be present"
11196447Sobrien		FAILED=yes
11296447Sobrien	fi
11396447Sobrien}
11458478Sobrien
11558478Sobrien# $1 - relative path to file that should be a directory in TEST
11695349Sobriendir()
11758478Sobrien{
11858478Sobrien	if ! [ -d $TEST/$1 ]; then
11958478Sobrien		echo "File $1 should be a directory"
12095349Sobrien		FAILED=yes
12158478Sobrien	fi
12295349Sobrien}
12395349Sobrien
12458478Sobrien# $1 - relative path to regular file that should be present in TEST
12558478Sobrien# $2 - optional string that should match file contents
12695349Sobrien# $3 - optional MD5 of the flie contents, overrides $2 if present
12795349Sobrienfile()
12895349Sobrien{
12958478Sobrien	local contents sum
13058478Sobrien
13158478Sobrien	if ! [ -f $TEST/$1 ]; then
13234229Speter		echo "File $1 should be a regular file"
13334229Speter		FAILED=yes
13434229Speter	elif [ $# -eq 2 ]; then
13518349Speter		contents=`cat $TEST/$1`
13634229Speter		if [ "$contents" != "$2" ]; then
13734229Speter			echo "File $1 has wrong contents"
13834229Speter			FAILED=yes
13934229Speter		fi
14018349Speter	elif [ $# -eq 3 ]; then
14134229Speter		sum=`md5 -q $TEST/$1`
14252112Sobrien		if [ "$sum" != "$3" ]; then
14368601Sobrien			echo "File $1 has wrong contents"
14468601Sobrien			FAILED=yes
14568601Sobrien		fi
14668601Sobrien	fi
14768601Sobrien}
14868601Sobrien
14918349Speter# $1 - relative path to a regular file that should have a conflict
15058478Sobrien# $2 - optional MD5 of the conflict file contents
15195810Sobrienconflict()
15295810Sobrien{
15358478Sobrien	local sum
15458478Sobrien
15595349Sobrien	if ! [ -f $CONFLICTS/$1 ]; then
15695349Sobrien		echo "File $1 missing conflict"
15795349Sobrien		FAILED=yes
15895349Sobrien	elif [ $# -gt 1 ]; then
15918349Speter		sum=`md5 -q $CONFLICTS/$1`
16095349Sobrien		if [ "$sum" != "$2" ]; then
16195349Sobrien			echo "Conflict $1 has wrong contents"
16295349Sobrien			FAILED=yes
16358478Sobrien		fi
16458478Sobrien	fi
16558478Sobrien}
16618349Speter
16795349Sobrien# $1 - relative path to a regular file that should not have a conflict
16895349Sobriennoconflict()
16995349Sobrien{
17095349Sobrien	if [ -f $CONFLICTS/$1 ]; then
17195349Sobrien		echo "File $1 should not have a conflict"
17218349Speter		FAILED=yes
17358478Sobrien	fi
17458478Sobrien}
17558478Sobrien
17658478Sobrienif [ `id -u` -ne 0 ]; then
17758478Sobrien	echo "must be root"
17858478Sobrien	exit 0
17958478Sobrienfi
18058478Sobrien
18158478Sobrienif [ -r /etc/etcupdate.conf ]; then
18258478Sobrien	echo "WARNING: /etc/etcupdate.conf settings may break some tests."
18358478Sobrienfi
18458478Sobrien
18595810Sobrien# First run the test ignoring no patterns.
18658478Sobrien
18758478Sobrienbuild_trees
18858478Sobrien
18958478Sobrien$COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
19058478Sobrien
19158478Sobriencat > $WORKDIR/correct.out <<EOF
19258478Sobrien  D /rmdir/file
19358478Sobrien  D /tree/remove
19458478Sobrien  D /rmdir
19558478Sobrien  U /tree/modify
19658478Sobrien  A /tree/add
19758478SobrienEOF
19858478Sobrien
19958478Sobrienecho "Differences for regular:"
20058478Sobriendiff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
20158478Sobrien    || FAILED=yes
20295810Sobrien
20358478Sobrienmissing /tree/remove
20458478Sobrienfile /tree/modify "new"
20558478Sobrienfile /tree/add "foo"
20658478Sobrienmissing /rmdir/file
20795348Sobrienmissing /rmdir
20895348Sobrien
20995348Sobrien# Now test with -I '/tree/*'.  This should preserve the /tree files.
21095348Sobrien
21196144Sobrienbuild_trees
21258478Sobrien
21358478Sobrien$COMMAND -r -I '/tree/*' -d $WORKDIR -D $TEST > $WORKDIR/test1.out
21458478Sobrien
21595349Sobriencat > $WORKDIR/correct1.out <<EOF
21658478Sobrien  D /rmdir/file
21734229Speter  D /rmdir
21895349SobrienEOF
21995349Sobrien
22034229Speterecho "Differences for -I '/tree/*':"
22134229Speterdiff -u -L "correct" $WORKDIR/correct1.out -L "test" $WORKDIR/test1.out \
22295810Sobrien    || FAILED=yes
22395810Sobrien
22495810Sobrienfile /tree/remove "old"
22518349Speterfile /tree/modify "old"
22634229Spetermissing /tree/add
22758478Sobrienmissing /rmdir/file
22858478Sobrienmissing /rmdir
22958478Sobrien
23034229Speter# Now test with two patterns.  This should preserve everything.
23195810Sobrien
23295810Sobrienbuild_trees
23395810Sobrien
23418334Speter$COMMAND -r -I '/tree/*' -I '/rmdir*' -d $WORKDIR -D $TEST > \
23558478Sobrien    $WORKDIR/test2.out
23658478Sobrien
23758478Sobriencat > $WORKDIR/correct2.out <<EOF
23858478SobrienEOF
23958478Sobrien
24058478Sobrienecho "Differences for -I '/tree/*' -I '/rmdir*':"
24158478Sobrien
24258478Sobriendiff -u -L "correct" $WORKDIR/correct2.out -L "test" $WORKDIR/test2.out \
24358478Sobrien    || FAILED=yes
24458478Sobrien
24558478Sobrienfile /tree/remove "old"
24658478Sobrienfile /tree/modify "old"
24758478Sobrienmissing /tree/add
24858478Sobrienfile /rmdir/file "foo"
24958478Sobrien
25058478Sobrien# Now test with a pattern that should cause a warning on /rmdir by
25158478Sobrien# only ignoring the files under that directory.  Note that this also
25258478Sobrien# tests putting two patterns into a single -I argument.
25358478Sobrien
25458478Sobrienbuild_trees
25556810Sobrien
25656810Sobrien$COMMAND -r -I '/tree/* /rmdir/*' -d $WORKDIR -D $TEST > \
25756810Sobrien    $WORKDIR/test3.out
25895810Sobrien
25956810Sobriencat > $WORKDIR/correct3.out <<EOF
26058478SobrienWarnings:
26152112Sobrien  Non-empty directory remains: /rmdir
26297907SobrienEOF
26397907Sobrien
26497907Sobrienecho "Differences for -I '/tree/* /rmdir/*':"
26597907Sobrien
26697907Sobriendiff -u -L "correct" $WORKDIR/correct3.out -L "test" $WORKDIR/test3.out \
26797907Sobrien    || FAILED=yes
26897907Sobrien
26996466Sobrienfile /tree/remove "old"
27018349Speterfile /tree/modify "old"
27173305Sobrienmissing /tree/add
27273305Sobrienfile /rmdir/file "foo"
27373305Sobriendir /rmdir
27473305Sobrien
27573305Sobrien[ "${FAILED}" = no ]
27673305Sobrien