t_mtree.sh revision 272345
1# $NetBSD: t_mtree.sh,v 1.6 2013/02/05 16:49:42 christos Exp $
2#
3# Copyright (c) 2009, 2012 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28# Postprocess mtree output, canonicalising portions that
29# are expected to differ from one run to another.
30#
31
32h_postprocess()
33{
34	sed -e '
35		/^#	   user: /s/:.*/: x/
36		/^#	machine: /s/:.*/: x/
37		/^#	   tree: /s/:.*/: x/
38		/^#	   date: /s/:.*/: x/
39		' \
40	    -e '/type=dir/s/ size=[0-9]*//'
41}
42
43h_check()
44{
45        diff -Nru "$1" "$2" || atf_fail "files $1 and $2 differ"
46}
47
48
49atf_test_case mtree_create
50atf_test_case netbsd6_create
51create_head()
52{
53	atf_set "descr" "Create a specfile describing a directory tree"
54}
55
56create_setup()
57{
58	# create some directories
59	rm -fr create
60	mkdir -p create/a/1 create/a/2 create/b
61	# create some files
62	for file in create/top.file.1 \
63		    create/a/a.file.1 \
64		    create/a/a.file.2 \
65		    create/a/1/a1.file.1 \
66		    create/b/b.file.1 \
67		    create/b/b.file.2
68	do
69		echo "$file" >$file
70	done
71	# hard link to file in same dir
72	ln create/b/b.file.1 create/b/b.hardlink.1
73	# hard link to file in another dir
74	ln create/b/b.file.2 create/a/a.hardlink.b2
75	# symlink to file
76	ln -s a.file.1 create/a.symlink.1
77	# symlink to dir
78	ln -s b create/top.symlink.b
79	# dangling symlink
80	ln -s nonexistent create/top.dangling
81}
82
83create_body()
84{
85	create_setup
86
87	# run mtree and check output
88	( cd create && mtree -F ${FLAVOR} -c -k type,nlink,link,size,sha256 ) >output.raw \
89	|| atf_fail "mtree exit status $?"
90	h_postprocess <output.raw >output
91	h_check "$(atf_get_srcdir)/${FLAVOR}_d_create.out" output
92}
93
94mtree_create_head() 
95{
96	FLAVOR=mtree create_head
97}
98netbsd6_create_head() 
99{
100	FLAVOR=netbsd6 create_head
101}
102
103mtree_create_body() 
104{
105	FLAVOR=mtree create_body
106}
107netbsd6_create_body() 
108{
109	FLAVOR=netbsd6 create_body
110}
111
112
113atf_test_case mtree_check
114atf_test_case netbsd6_check
115check_head()
116{
117	atf_set "descr" "Check a directory tree against a specfile"
118}
119
120check_body()
121{
122	# we use the same directory tree and specfile as in the "create" test
123	create_setup
124
125	# run mtree and check output
126	( cd create && mtree -F ${FLAVOR}  ) <"$(atf_get_srcdir)/${FLAVOR}_d_create.out" >output \
127	|| atf_fail "mtree exit status $?"
128	h_check /dev/null output
129}
130
131mtree_check_head() 
132{
133	FLAVOR=mtree check_head
134}
135netbsd6_check_head() 
136{
137	FLAVOR=netbsd6 check_head
138}
139
140mtree_check_body() 
141{
142	FLAVOR=mtree check_body
143}
144netbsd6_check_body() 
145{
146	FLAVOR=netbsd6 check_body
147}
148
149
150atf_test_case mtree_convert_C
151atf_test_case netbsd6_convert_C
152convert_C_head()
153{
154	atf_set "descr" "Convert a specfile to mtree -C format, unsorted"
155}
156
157convert_C_body()
158{
159	mtree -F ${FLAVOR} -C -K all <"$(atf_get_srcdir)/d_convert.in" >output
160	h_check "$(atf_get_srcdir)/d_convert_C.out" output
161}
162
163mtree_convert_C_head() 
164{
165	FLAVOR=mtree convert_C_head
166}
167netbsd6_convert_C_head() 
168{
169	FLAVOR=netbsd6 convert_C_head
170}
171
172mtree_convert_C_body() 
173{
174	FLAVOR=mtree convert_C_body
175}
176netbsd6_convert_C_body() 
177{
178	FLAVOR=netbsd6 convert_C_body
179}
180
181
182atf_test_case mtree_convert_C_S
183atf_test_case netbsd6_convert_C_S
184convert_C_S_head()
185{
186	atf_set "descr" "Convert a specfile to mtree -C format, sorted"
187}
188
189convert_C_S_body()
190{
191	mtree -F ${FLAVOR} -C -S -K all <"$(atf_get_srcdir)/d_convert.in" >output
192	h_check "$(atf_get_srcdir)/d_convert_C_S.out" output
193}
194
195mtree_convert_C_S_head() 
196{
197	FLAVOR=mtree convert_C_S_head
198}
199netbsd6_convert_C_S_head() 
200{
201	FLAVOR=netbsd6 convert_C_S_head
202}
203
204mtree_convert_C_S_body() 
205{
206	FLAVOR=mtree convert_C_S_body
207}
208netbsd6_convert_C_S_body() 
209{
210	FLAVOR=netbsd6 convert_C_S_body
211}
212
213
214atf_test_case mtree_convert_D
215atf_test_case netbsd6_convert_D
216convert_D_head()
217{
218	atf_set "descr" "Convert a specfile to mtree -D format, unsorted"
219}
220
221convert_D_body()
222{
223	mtree -F ${FLAVOR} -D -K all <"$(atf_get_srcdir)/d_convert.in" >output
224	h_check "$(atf_get_srcdir)/d_convert_D.out" output
225}
226
227mtree_convert_D_head() 
228{
229	FLAVOR=mtree convert_D_head
230}
231netbsd6_convert_D_head() 
232{
233	FLAVOR=netbsd6 convert_D_head
234}
235
236mtree_convert_D_body() 
237{
238	FLAVOR=mtree convert_D_body
239}
240netbsd6_convert_D_body() 
241{
242	FLAVOR=netbsd6 convert_D_body
243}
244
245
246atf_test_case mtree_convert_D_S
247atf_test_case netbsd6_convert_D_S
248convert_D_S_head()
249{
250	atf_set "descr" "Convert a specfile to mtree -D format, sorted"
251}
252
253convert_D_S_body()
254{
255	mtree -F ${FLAVOR} -D -S -K all <"$(atf_get_srcdir)/d_convert.in" >output
256	h_check "$(atf_get_srcdir)/d_convert_D_S.out" output
257}
258
259mtree_convert_D_S_head() 
260{
261	FLAVOR=mtree convert_D_S_head
262}
263netbsd6_convert_D_S_head() 
264{
265	FLAVOR=netbsd6 convert_D_S_head
266}
267
268mtree_convert_D_S_body() 
269{
270	FLAVOR=mtree convert_D_S_body
271}
272netbsd6_convert_D_S_body() 
273{
274	FLAVOR=netbsd6 convert_D_S_body
275}
276
277
278atf_test_case mtree_ignore
279atf_test_case netbs6_ignore
280ignore_head()
281{
282	atf_set "descr" "Test that -d ignores symlinks (PR bin/41061)"
283}
284
285ignore_body()
286{
287	mkdir newdir
288	mtree -F ${FLAVOR} -c | mtree -F ${FLAVOR} -Ck uid,gid,mode > mtree.spec
289	ln -s newdir otherdir
290
291	# This yields "extra: otherdir" even with -d.
292	# (PR bin/41061)
293	atf_check -s ignore -o empty -e empty -x "mtree -F ${FLAVOR} -d < mtree.spec"
294
295	# Delete the symlink and re-verify.
296	#
297	rm otherdir
298	atf_check -s ignore -o empty -e empty -x "mtree -F ${FLAVOR} -d < mtree.spec"
299}
300
301mtree_ignore_head() 
302{
303	FLAVOR=mtree ignore_head
304}
305netbsd6_ignore_head() 
306{
307	FLAVOR=netbsd6 ignore_head
308}
309
310mtree_ignore_body() 
311{
312	FLAVOR=mtree ignore_body
313}
314netbsd6_ignore_body() 
315{
316	FLAVOR=netbsd6 ignore_body
317}
318
319
320atf_test_case mtree_merge
321atf_test_case netbsd6_merge
322merge_head()
323{
324	atf_set "descr" "Merge records of different type"
325}
326
327merge_body()
328{
329	mtree -F ${FLAVOR} -C -M -K all <"$(atf_get_srcdir)/d_merge.in" >output
330	h_check "$(atf_get_srcdir)/d_merge_C_M.out" output
331	# same again, with sorting
332	mtree -F ${FLAVOR} -C -M -S -K all <"$(atf_get_srcdir)/d_merge.in" >output
333	h_check "$(atf_get_srcdir)/d_merge_C_M_S.out" output
334}
335
336mtree_merge_head() 
337{
338	FLAVOR=mtree merge_head
339}
340netbsd6_merge_head() 
341{
342	FLAVOR=netbsd6 merge_head
343}
344
345mtree_merge_body() 
346{
347	FLAVOR=mtree merge_body
348}
349netbsd6_merge_body() 
350{
351	FLAVOR=netbsd6 merge_body
352}
353
354
355atf_test_case mtree_nonemptydir
356atf_test_case netbsd6_nonemptydir
357nonemptydir_head()
358{
359	atf_set "descr" "Test that new non-empty " \
360			"directories are recorded (PR bin/25693)"
361}
362
363nonemptydir_body()
364{
365	mkdir testdir
366	cd testdir
367
368	mtree -F ${FLAVOR} -c > mtree.spec
369
370	if [ ! -f mtree.spec ]; then
371		atf_fail "mtree failed"
372	fi
373
374	touch bar
375	atf_check -s ignore -o save:output -x "mtree -F ${FLAVOR} -f mtree.spec"
376
377	if [ ! -n "$(egrep "extra: bar" output)" ]; then
378		atf_fail "mtree did not record changes (PR bin/25693)"
379	fi
380}
381
382mtree_nonemptydir_head() 
383{
384	FLAVOR=mtree nonemptydir_head
385}
386netbsd6_nonemptydir_head() 
387{
388	FLAVOR=netbsd6 nonemptydir_head
389}
390
391mtree_nonemptydir_body() 
392{
393	FLAVOR=mtree nonemptydir_body
394}
395netbsd6_nonemptydir_body() 
396{
397	FLAVOR=netbsd6 nonemptydir_body
398}
399
400
401atf_init_test_cases()
402{
403	atf_add_test_case mtree_create
404	atf_add_test_case mtree_check
405	atf_add_test_case mtree_convert_C
406	atf_add_test_case mtree_convert_C_S
407	atf_add_test_case mtree_convert_D
408	atf_add_test_case mtree_convert_D_S
409	atf_add_test_case mtree_ignore
410	atf_add_test_case mtree_merge
411	atf_add_test_case mtree_nonemptydir
412
413	atf_add_test_case netbsd6_create
414	atf_add_test_case netbsd6_check
415	atf_add_test_case netbsd6_convert_C
416	atf_add_test_case netbsd6_convert_C_S
417	atf_add_test_case netbsd6_convert_D
418	atf_add_test_case netbsd6_convert_D_S
419	atf_add_test_case netbsd6_ignore
420	atf_add_test_case netbsd6_merge
421	atf_add_test_case netbsd6_nonemptydir
422}
423