xref: /freebsd/usr.sbin/etcupdate/tests/always_test.sh (revision e8e8c939350bdf3c228a411caa9660c607c27a11)
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 -A flag to the 'update' command.
31
32FAILED=no
33WORKDIR=work
34
35usage()
36{
37	echo "Usage: always.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# The various states of the comparison of a file between two trees.
68states="equal first second difftype difflinks difffiles"
69
70# These tests deal with ignoring certain patterns of files.  We run
71# the test multiple times forcing the install of different patterns.
72build_trees()
73{
74	local i
75
76	rm -rf $OLD $NEW $TEST $CONFLICTS
77
78	for i in $states; do
79		for j in $states; do
80			for k in $states; do
81				mkdir -p $OLD/$i/$j/$k $NEW/$i/$j/$k \
82				    $TEST/$i/$j/$k
83			done
84		done
85	done
86
87	# What follows are the various warning/conflict cases from the
88	# larger regression tests.  These results of many of these
89	# tests should be changed when installation is forced.  The
90	# cases when these updates should still fail even when forced
91	# are: 1) it should not force the removal of a modified file
92	# and 2) it should not remove a subdirectory that contains a
93	# modified or added file.
94
95	# /first/difftype/second: File with different local type
96	# removed.  Should generate a warning.
97	mkfifo $OLD/first/difftype/second/fifo
98	mkdir $TEST/first/difftype/second/fifo
99
100	# /first/difflinks/second: Modified link removed.  Should
101	# generate a warning.
102	ln -s "old link" $OLD/first/difflinks/second/link
103	ln -s "test link" $TEST/first/difflinks/second/link
104
105	# /first/difffiles/second: Modified file removed.  Should
106	# generate a warning.
107	echo "foo" > $OLD/first/difffiles/second/file
108	echo "bar" > $TEST/first/difffiles/second/file
109
110	# /second/second/difftype: Newly added file conflicts with
111	# existing file in test tree of a different type.  Should
112	# generate a warning.
113	mkdir $NEW/second/second/difftype/dir
114	mkfifo $TEST/second/second/difftype/dir
115
116	# /second/second/difflinks: Newly added link conflicts with
117	# existing link in test tree.  Should generate a warning.
118	ln -s "new link" $NEW/second/second/difflinks/link
119	ln -s "test link" $TEST/second/second/difflinks/link
120
121	# /second/second/difffiles: Newly added file conflicts with
122	# existing file in test tree.  Should generate a warning.
123	echo "new" > $NEW/second/second/difffiles/file
124	echo "test" > $TEST/second/second/difffiles/file
125
126	# /difftype/first/first: A removed file has changed type.
127	# This should generate a warning.
128	mkfifo $OLD/difftype/first/first/fifo
129	mkdir $NEW/difftype/first/first/fifo
130
131	# /difftype/difftype/difftype: All three files (old, new, and
132	# test) are different types from each other.  This should
133	# generate a warning.
134	mkfifo $OLD/difftype/difftype/difftype/one
135	mkdir $NEW/difftype/difftype/difftype/one
136	echo "foo" > $TEST/difftype/difftype/difftype/one
137	mkdir $OLD/difftype/difftype/difftype/two
138	echo "baz" > $NEW/difftype/difftype/difftype/two
139	ln -s "bar" $TEST/difftype/difftype/difftype/two
140
141	# /difftype/difftype/difflinks: A file has changed from a
142	# non-link to a link in both the new and test trees, but the
143	# target of the new and test links differ.  This should
144	# generate a new link conflict.
145	mkfifo $OLD/difftype/difftype/difflinks/link
146	ln -s "new" $NEW/difftype/difftype/difflinks/link
147	ln -s "test" $TEST/difftype/difftype/difflinks/link
148
149	# /difftype/difftype/difffile: A file has changed from a
150	# non-regular file to a regular file in both the new and test
151	# trees, but the contents in the new and test files differ.
152	# This should generate a new file conflict.
153	ln -s "old" $OLD/difftype/difftype/difffiles/file
154	echo "foo" > $NEW/difftype/difftype/difffiles/file
155	echo "bar" > $TEST/difftype/difftype/difffiles/file
156
157	# /difflinks/first/first: A modified link is missing in the
158	# test tree.  This should generate a warning.
159	ln -s "old" $OLD/difflinks/first/first/link
160	ln -s "new" $NEW/difflinks/first/first/link
161
162	# /difflinks/difftype/difftype: An updated link has been
163	# changed to a different file type in the test tree.  This
164	# should generate a warning.
165	ln -s "old" $OLD/difflinks/difftype/difftype/link
166	ln -s "new" $NEW/difflinks/difftype/difftype/link
167	echo "test" > $TEST/difflinks/difftype/difftype/link
168
169	# /difflinks/difflinks/difflinks: An updated link has been
170	# modified in the test tree and doesn't match either the old
171	# or new links.  This should generate a warning.
172	ln -s "old" $OLD/difflinks/difflinks/difflinks/link
173	ln -s "new" $NEW/difflinks/difflinks/difflinks/link
174	ln -s "test" $TEST/difflinks/difflinks/difflinks/link
175
176	# /difffiles/first/first: A removed file has been changed in
177	# the new tree.  This should generate a warning.
178	echo "foo" > $OLD/difffiles/first/first/file
179	echo "bar" > $NEW/difffiles/first/first/file
180
181	# /difffiles/difftype/difftype: An updated regular file has
182	# been changed to a different file type in the test tree.
183	# This should generate a warning.
184	echo "old" > $OLD/difffiles/difftype/difftype/file
185	echo "new" > $NEW/difffiles/difftype/difftype/file
186	mkfifo $TEST/difffiles/difftype/difftype/file
187
188	# /difffiles/difffiles/difffiles: A modified regular file was
189	# updated in the new tree.  The changes should be merged into
190	# to the new file if possible.  If the merge fails, a conflict
191	# should be generated.  For this test we just include the
192	# conflict case.
193	cat > $OLD/difffiles/difffiles/difffiles/conflict <<EOF
194this is an old file
195EOF
196	cat > $NEW/difffiles/difffiles/difffiles/conflict <<EOF
197this is a new file
198EOF
199	cat > $TEST/difffiles/difffiles/difffiles/conflict <<EOF
200this is a test file
201EOF
202
203	## Tests for adding directories
204	mkdir -p $OLD/adddir $NEW/adddir $TEST/adddir
205
206	# /adddir/conflict: Add a new file in a directory that already
207	# exists as a file.  This should generate two warnings.
208	mkdir $NEW/adddir/conflict
209	touch $NEW/adddir/conflict/newfile
210	touch $TEST/adddir/conflict
211
212	## Tests for removing directories
213	mkdir -p $OLD/rmdir $NEW/rmdir $TEST/rmdir
214
215	# /rmdir/extra: Do not remove a directory with an extra local file.
216	# This should generate a warning.
217	for i in $OLD $TEST; do
218		mkdir $i/rmdir/extra
219	done
220	echo "foo" > $TEST/rmdir/extra/localfile.txt
221
222	# /rmdir/conflict: Do not remove a directory with a conflicted
223	# remove file.  This should generate a warning.
224	for i in $OLD $TEST; do
225		mkdir $i/rmdir/conflict
226	done
227	mkfifo $OLD/rmdir/conflict/difftype
228	mkdir $TEST/rmdir/conflict/difftype
229
230	## Tests for converting files to directories and vice versa
231	for i in $OLD $NEW $TEST; do
232		for j in already old fromdir todir; do
233			mkdir -p $i/dirchange/$j
234		done
235	done
236
237	# /dirchange/fromdir/extradir: Convert a directory tree to a
238	# file.  The test tree includes an extra file in the directory
239	# that is not present in the old tree.  This should generate a
240	# warning.
241	for i in $OLD $TEST; do
242		mkdir $i/dirchange/fromdir/extradir
243		echo "foo" > $i/dirchange/fromdir/extradir/file
244	done
245	mkfifo $TEST/dirchange/fromdir/extradir/fifo
246	ln -s "bar" $NEW/dirchange/fromdir/extradir
247
248	# /dirchange/fromdir/conflict: Convert a directory tree to a
249	# file.  The test tree includes a local change that generates
250	# a warning and prevents the removal of the directory.
251	for i in $OLD $TEST; do
252		mkdir $i/dirchange/fromdir/conflict
253	done
254	echo "foo" > $OLD/dirchange/fromdir/conflict/somefile
255	echo "bar" > $TEST/dirchange/fromdir/conflict/somefile
256	mkfifo $NEW/dirchange/fromdir/conflict
257
258	# /dirchange/todir/difffile: Convert a file to a directory
259	# tree.  The test tree has a locally modified version of the
260	# file so that the conversion fails with a warning.
261	echo "foo" > $OLD/dirchange/todir/difffile
262	mkdir $NEW/dirchange/todir/difffile
263	echo "baz" > $NEW/dirchange/todir/difffile/file
264	echo "bar" > $TEST/dirchange/todir/difffile
265
266	# /dirchange/todir/difftype: Similar to the previous test, but
267	# the conflict is due to a change in the file type.
268	echo "foo" > $OLD/dirchange/todir/difftype
269	mkdir $NEW/dirchange/todir/difftype
270	echo "baz" > $NEW/dirchange/todir/difftype/file
271	mkfifo $TEST/dirchange/todir/difftype
272}
273
274# $1 - relative path to file that should be missing from TEST
275missing()
276{
277	if [ -e $TEST/$1 -o -L $TEST/$1 ]; then
278		echo "File $1 should be missing"
279		FAILED=yes
280	fi
281}
282
283# $1 - relative path to file that should be present in TEST
284present()
285{
286	if ! [ -e $TEST/$1 -o -L $TEST/$1 ]; then
287		echo "File $1 should be present"
288		FAILED=yes
289	fi
290}
291
292# $1 - relative path to file that should be a fifo in TEST
293fifo()
294{
295	if ! [ -p $TEST/$1 ]; then
296		echo "File $1 should be a FIFO"
297		FAILED=yes
298	fi
299}
300
301# $1 - relative path to file that should be a directory in TEST
302dir()
303{
304	if ! [ -d $TEST/$1 ]; then
305		echo "File $1 should be a directory"
306		FAILED=yes
307	fi
308}
309
310# $1 - relative path to file that should be a symlink in TEST
311# $2 - optional value of the link
312link()
313{
314	local val
315
316	if ! [ -L $TEST/$1 ]; then
317		echo "File $1 should be a link"
318		FAILED=yes
319	elif [ $# -gt 1 ]; then
320		val=`readlink $TEST/$1`
321		if [ "$val" != "$2" ]; then
322			echo "Link $1 should link to \"$2\""
323			FAILED=yes
324		fi
325	fi
326}
327
328# $1 - relative path to regular file that should be present in TEST
329# $2 - optional string that should match file contents
330# $3 - optional MD5 of the flie contents, overrides $2 if present
331file()
332{
333	local contents sum
334
335	if ! [ -f $TEST/$1 ]; then
336		echo "File $1 should be a regular file"
337		FAILED=yes
338	elif [ $# -eq 2 ]; then
339		contents=`cat $TEST/$1`
340		if [ "$contents" != "$2" ]; then
341			echo "File $1 has wrong contents"
342			FAILED=yes
343		fi
344	elif [ $# -eq 3 ]; then
345		sum=`md5 -q $TEST/$1`
346		if [ "$sum" != "$3" ]; then
347			echo "File $1 has wrong contents"
348			FAILED=yes
349		fi
350	fi
351}
352
353# $1 - relative path to a regular file that should have a conflict
354# $2 - optional MD5 of the conflict file contents
355conflict()
356{
357	local sum
358
359	if ! [ -f $CONFLICTS/$1 ]; then
360		echo "File $1 missing conflict"
361		FAILED=yes
362	elif [ $# -gt 1 ]; then
363		sum=`md5 -q $CONFLICTS/$1`
364		if [ "$sum" != "$2" ]; then
365			echo "Conflict $1 has wrong contents"
366			FAILED=yes
367		fi
368	fi
369}
370
371# $1 - relative path to a regular file that should not have a conflict
372noconflict()
373{
374	if [ -f $CONFLICTS/$1 ]; then
375		echo "File $1 should not have a conflict"
376		FAILED=yes
377	fi
378}
379
380if [ `id -u` -ne 0 ]; then
381	echo "must be root"
382	exit 0
383fi
384
385if [ -r /etc/etcupdate.conf ]; then
386	echo "WARNING: /etc/etcupdate.conf settings may break some tests."
387fi
388
389# First run the test ignoring no patterns.
390
391build_trees
392
393$COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
394
395cat > $WORKDIR/correct.out <<EOF
396  D /dirchange/fromdir/extradir/file
397  C /difffiles/difffiles/difffiles/conflict
398  C /difftype/difftype/difffiles/file
399  C /second/second/difffiles/file
400Warnings:
401  Modified regular file remains: /dirchange/fromdir/conflict/somefile
402  Modified regular file remains: /first/difffiles/second/file
403  Modified symbolic link remains: /first/difflinks/second/link
404  Modified directory remains: /first/difftype/second/fifo
405  Modified directory remains: /rmdir/conflict/difftype
406  Non-empty directory remains: /rmdir/extra
407  Non-empty directory remains: /rmdir/conflict
408  Modified mismatch: /difffiles/difftype/difftype/file (regular file vs fifo file)
409  Removed file changed: /difffiles/first/first/file
410  Modified link changed: /difflinks/difflinks/difflinks/link ("old" became "new")
411  Modified mismatch: /difflinks/difftype/difftype/link (symbolic link vs regular file)
412  Removed link changed: /difflinks/first/first/link ("old" became "new")
413  New link conflict: /difftype/difftype/difflinks/link ("new" vs "test")
414  Modified regular file changed: /difftype/difftype/difftype/one (fifo file became directory)
415  Modified symbolic link changed: /difftype/difftype/difftype/two (directory became regular file)
416  Remove mismatch: /difftype/first/first/fifo (fifo file became directory)
417  Modified directory changed: /dirchange/fromdir/conflict (directory became fifo file)
418  Modified directory changed: /dirchange/fromdir/extradir (directory became symbolic link)
419  Modified regular file changed: /dirchange/todir/difffile (regular file became directory)
420  Modified fifo file changed: /dirchange/todir/difftype (regular file became directory)
421  New file mismatch: /adddir/conflict (directory vs regular file)
422  Directory mismatch: $TEST/adddir/conflict (regular file)
423  Directory mismatch: $TEST/dirchange/todir/difffile (regular file)
424  Directory mismatch: $TEST/dirchange/todir/difftype (fifo file)
425  New link conflict: /second/second/difflinks/link ("new link" vs "test link")
426  New file mismatch: /second/second/difftype/dir (directory vs fifo file)
427EOF
428
429echo "Differences for regular:"
430diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
431    || FAILED=yes
432
433## /first/difftype/second:
434present /first/difftype/second/fifo
435
436## /first/difflinks/second:
437link /first/difflinks/second/link "test link"
438
439## /first/difffiles/second:
440file /first/difffiles/second/file "bar"
441
442## /second/second/difftype:
443fifo /second/second/difftype/dir
444
445## /second/second/difflinks:
446link /second/second/difflinks/link "test link"
447
448## /second/second/difffiles:
449file /second/second/difffiles/file "test"
450conflict /second/second/difffiles/file 4f2ee8620a251fd53f06bb6112eb6ffa
451
452## /difftype/first/first:
453missing /difftype/first/first/fifo
454
455## /difftype/difftype/difftype:
456file /difftype/difftype/difftype/one "foo"
457link /difftype/difftype/difftype/two "bar"
458
459## /difftype/difftype/difflinks:
460link /difftype/difftype/difflinks/link "test"
461
462## /difftype/difftype/difffile:
463conflict /difftype/difftype/difffiles/file 117f2bcd1f6491f6044e79e5a57a9229
464
465## /difflinks/first/first:
466missing /difflinks/first/first/link
467
468## /difflinks/difftype/difftype:
469file /difflinks/difftype/difftype/link "test"
470
471## /difflinks/difflinks/difflinks:
472link /difflinks/difflinks/difflinks/link "test"
473
474## /difffiles/first/first:
475missing /difffiles/first/first/file
476
477## /difffiles/difftype/difftype:
478fifo /difffiles/difftype/difftype/file
479
480## /difffiles/difffiles/difffiles:
481file /difffiles/difffiles/difffiles/conflict "this is a test file"
482conflict /difffiles/difffiles/difffiles/conflict \
483    8261cfdd89280c4a6c26e4ac86541fe9
484
485## /adddir/conflict:
486file /adddir/conflict
487
488## /rmdir/extra:
489dir /rmdir/extra
490file /rmdir/extra/localfile.txt "foo"
491
492## /rmdir/conflict:
493dir /rmdir/conflict/difftype
494present /rmdir/conflict
495
496## /dirchange/fromdir/extradir:
497missing /dirchange/fromdir/extradir/file
498fifo /dirchange/fromdir/extradir/fifo
499
500## /dirchange/fromdir/conflict:
501file /dirchange/fromdir/conflict/somefile "bar"
502
503## /dirchange/todir/difffile:
504file /dirchange/todir/difffile "bar"
505
506## /dirchange/todir/difftype:
507fifo /dirchange/todir/difftype
508
509# Now test with -A '/first*' -A '/second* /*di*'.  This should remove
510# most of the warnings and conflicts.
511
512build_trees
513
514$COMMAND -r -A '/first*' -A '/second* /*di*' -d $WORKDIR -D $TEST > \
515    $WORKDIR/test1.out
516
517cat > $WORKDIR/correct1.out <<EOF
518  D /dirchange/fromdir/extradir/file
519  U /difffiles/difffiles/difffiles/conflict
520  U /difffiles/difftype/difftype/file
521  A /difffiles/first/first/file
522  U /difflinks/difflinks/difflinks/link
523  U /difflinks/difftype/difftype/link
524  A /difflinks/first/first/link
525  U /difftype/difftype/difffiles/file
526  U /difftype/difftype/difflinks/link
527  D /difftype/difftype/difftype/one
528  U /difftype/difftype/difftype/two
529  U /dirchange/todir/difffile
530  U /dirchange/todir/difftype
531  U /adddir/conflict
532  A /adddir/conflict/newfile
533  A /dirchange/todir/difffile/file
534  A /dirchange/todir/difftype/file
535  U /second/second/difffiles/file
536  U /second/second/difflinks/link
537  D /second/second/difftype/dir
538Warnings:
539  Modified regular file remains: /dirchange/fromdir/conflict/somefile
540  Modified regular file remains: /first/difffiles/second/file
541  Modified symbolic link remains: /first/difflinks/second/link
542  Modified directory remains: /first/difftype/second/fifo
543  Modified directory remains: /rmdir/conflict/difftype
544  Non-empty directory remains: /rmdir/extra
545  Non-empty directory remains: /rmdir/conflict
546  Modified directory changed: /dirchange/fromdir/conflict (directory became fifo file)
547  Modified directory changed: /dirchange/fromdir/extradir (directory became symbolic link)
548EOF
549
550echo "Differences for -A '/first*' -A '/second* /*di*':"
551diff -u -L "correct" $WORKDIR/correct1.out -L "test" $WORKDIR/test1.out \
552    || FAILED=yes
553
554## /first/difftype/second:
555present /first/difftype/second/fifo
556
557## /first/difflinks/second:
558link /first/difflinks/second/link "test link"
559
560## /first/difffiles/second:
561file /first/difffiles/second/file "bar"
562
563## /second/second/difftype:
564missing /second/second/difftype/dir
565
566## /second/second/difflinks:
567link /second/second/difflinks/link "new link"
568
569## /second/second/difffiles:
570file /second/second/difffiles/file "new"
571noconflict /second/second/difffiles/file
572
573## /difftype/first/first:
574missing /difftype/first/first/fifo
575
576## /difftype/difftype/difftype:
577missing /difftype/difftype/difftype/one
578file /difftype/difftype/difftype/two "baz"
579
580## /difftype/difftype/difflinks:
581link /difftype/difftype/difflinks/link "new"
582
583## /difftype/difftype/difffile:
584noconflict /difftype/difftype/difffiles/file
585file /difftype/difftype/difffiles/file "foo"
586
587## /difflinks/first/first:
588link /difflinks/first/first/link "new"
589
590## /difflinks/difftype/difftype:
591link /difflinks/difftype/difftype/link "new"
592
593## /difflinks/difflinks/difflinks:
594link /difflinks/difflinks/difflinks/link "new"
595
596## /difffiles/first/first:
597file /difffiles/first/first/file "bar"
598
599## /difffiles/difftype/difftype:
600file /difffiles/difftype/difftype/file "new"
601
602## /difffiles/difffiles/difffiles:
603noconflict /difffiles/difffiles/difffiles/conflict
604file /difffiles/difffiles/difffiles/conflict "this is a new file"
605
606## /adddir/conflict:
607file /adddir/conflict/newfile
608
609## /rmdir/extra:
610dir /rmdir/extra
611file /rmdir/extra/localfile.txt "foo"
612
613## /rmdir/conflict:
614dir /rmdir/conflict/difftype
615present /rmdir/conflict
616
617## /dirchange/fromdir/extradir:
618missing /dirchange/fromdir/extradir/file
619fifo /dirchange/fromdir/extradir/fifo
620
621## /dirchange/fromdir/conflict:
622file /dirchange/fromdir/conflict/somefile "bar"
623
624## /dirchange/todir/difffile:
625file /dirchange/todir/difffile/file "baz"
626
627## /dirchange/todir/difftype:
628file /dirchange/todir/difftype/file "baz"
629
630[ "${FAILED}" = no ]
631