t_mtree.sh revision 1.2 1 # $NetBSD: t_mtree.sh,v 1.2 2012/03/18 11:50:55 jruoho Exp $
2 #
3 # Copyright (c) 2009 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 h_postprocess()
32 {
33 sed -e '
34 /^# user: /s/:.*/: x/
35 /^# machine: /s/:.*/: x/
36 /^# tree: /s/:.*/: x/
37 /^# date: /s/:.*/: x/
38 '
39 }
40
41 h_check()
42 {
43 diff -Nru "$1" "$2" || atf_fail "files $1 and $2 differ"
44 }
45
46 atf_test_case create
47 create_head()
48 {
49 atf_set "descr" "Create a specfile describing a directory tree"
50 }
51 create_setup()
52 {
53 # create some directories
54 mkdir -p create/a/1 create/a/2 create/b
55 # create some files
56 for file in create/top.file.1 \
57 create/a/a.file.1 \
58 create/a/a.file.2 \
59 create/a/1/a1.file.1 \
60 create/b/b.file.1 \
61 create/b/b.file.2
62 do
63 echo "$file" >$file
64 done
65 # hard link to file in same dir
66 ln create/b/b.file.1 create/b/b.hardlink.1
67 # hard link to file in another dir
68 ln create/b/b.file.2 create/a/a.hardlink.b2
69 # symlink to file
70 ln -s a.file.1 create/a.symlink.1
71 # symlink to dir
72 ln -s b create/top.symlink.b
73 # dangling symlink
74 ln -s nonexistent create/top.dangling
75 }
76 create_body()
77 {
78 create_setup
79
80 # run mtree and check output
81 ( cd create && mtree -c -k type,nlink,link,size,sha256 ) >output.raw \
82 || atf_fail "mtree exit status $?"
83 h_postprocess <output.raw >output
84 h_check "$(atf_get_srcdir)/d_create.out" output
85 }
86
87 atf_test_case check
88 check_head()
89 {
90 atf_set "descr" "Check a directory tree against a specfile"
91 }
92 check_body()
93 {
94 # we use the same directory tree and specfile as in the "create" test
95 create_setup
96
97 # run mtree and check output
98 ( cd create && mtree ) <"$(atf_get_srcdir)/d_create.out" >output \
99 || atf_fail "mtree exit status $?"
100 h_check /dev/null output
101 }
102
103 atf_test_case convert_C
104 convert_C_head()
105 {
106 atf_set "descr" "Convert a specfile to mtree -C format, unsorted"
107 }
108 convert_C_body()
109 {
110 mtree -C -K all <"$(atf_get_srcdir)/d_convert.in" >output
111 h_check "$(atf_get_srcdir)/d_convert_C.out" output
112 }
113
114 atf_test_case convert_C_S
115 convert_C_S_head()
116 {
117 atf_set "descr" "Convert a specfile to mtree -C format, sorted"
118 }
119 convert_C_S_body()
120 {
121 mtree -C -S -K all <"$(atf_get_srcdir)/d_convert.in" >output
122 h_check "$(atf_get_srcdir)/d_convert_C_S.out" output
123 }
124
125 atf_test_case convert_D
126 convert_D_head()
127 {
128 atf_set "descr" "Convert a specfile to mtree -D format, unsorted"
129 }
130 convert_D_body()
131 {
132 mtree -D -K all <"$(atf_get_srcdir)/d_convert.in" >output
133 h_check "$(atf_get_srcdir)/d_convert_D.out" output
134 }
135
136 atf_test_case convert_D_S
137 convert_D_S_head()
138 {
139 atf_set "descr" "Convert a specfile to mtree -D format, sorted"
140 }
141 convert_D_S_body()
142 {
143 mtree -D -S -K all <"$(atf_get_srcdir)/d_convert.in" >output
144 h_check "$(atf_get_srcdir)/d_convert_D_S.out" output
145 }
146
147 atf_test_case merge
148 merge_head()
149 {
150 atf_set "descr" "Merge records of different type"
151 }
152 merge_body()
153 {
154 mtree -C -M -K all <"$(atf_get_srcdir)/d_merge.in" >output
155 h_check "$(atf_get_srcdir)/d_merge_C_M.out" output
156 # same again, with sorting
157 mtree -C -M -S -K all <"$(atf_get_srcdir)/d_merge.in" >output
158 h_check "$(atf_get_srcdir)/d_merge_C_M_S.out" output
159 }
160
161 atf_test_case nonemptydir
162 nonemptydir_head() {
163 atf_set "descr" "Test that new non-empty " \
164 "directories are recorded (PR bin/25693)"
165 }
166
167 nonemptydir_body() {
168
169 mkdir testdir
170 cd testdir
171
172 mtree -c > mtree.spec
173
174 if [ ! -f mtree.spec ]; then
175 atf_fail "mtree failed"
176 fi
177
178 touch bar
179 atf_check -s ignore -o save:output -x "mtree -f mtree.spec"
180
181 if [ ! -n "$(egrep "extra: bar" output)" ]; then
182 atf_fail "mtree did not record changes (PR bin/25693)"
183 fi
184 }
185
186 atf_init_test_cases()
187 {
188 atf_add_test_case create
189 atf_add_test_case check
190 atf_add_test_case convert_C
191 atf_add_test_case convert_C_S
192 atf_add_test_case convert_D
193 atf_add_test_case convert_D_S
194 atf_add_test_case merge
195 atf_add_test_case nonemptydir
196 }
197