t_mtree.sh revision 1.4.2.2 1 # $NetBSD: t_mtree.sh,v 1.4.2.2 2012/04/17 00:09:23 yamt 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 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
52 create_setup()
53 {
54 # create some directories
55 mkdir -p create/a/1 create/a/2 create/b
56 # create some files
57 for file in create/top.file.1 \
58 create/a/a.file.1 \
59 create/a/a.file.2 \
60 create/a/1/a1.file.1 \
61 create/b/b.file.1 \
62 create/b/b.file.2
63 do
64 echo "$file" >$file
65 done
66 # hard link to file in same dir
67 ln create/b/b.file.1 create/b/b.hardlink.1
68 # hard link to file in another dir
69 ln create/b/b.file.2 create/a/a.hardlink.b2
70 # symlink to file
71 ln -s a.file.1 create/a.symlink.1
72 # symlink to dir
73 ln -s b create/top.symlink.b
74 # dangling symlink
75 ln -s nonexistent create/top.dangling
76 }
77
78 create_body()
79 {
80 create_setup
81
82 # run mtree and check output
83 ( cd create && mtree -c -k type,nlink,link,size,sha256 ) >output.raw \
84 || atf_fail "mtree exit status $?"
85 h_postprocess <output.raw >output
86 h_check "$(atf_get_srcdir)/d_create.out" output
87 }
88
89 atf_test_case check
90 check_head()
91 {
92 atf_set "descr" "Check a directory tree against a specfile"
93 }
94
95 check_body()
96 {
97 # we use the same directory tree and specfile as in the "create" test
98 create_setup
99
100 # run mtree and check output
101 ( cd create && mtree ) <"$(atf_get_srcdir)/d_create.out" >output \
102 || atf_fail "mtree exit status $?"
103 h_check /dev/null output
104 }
105
106 atf_test_case convert_C
107 convert_C_head()
108 {
109 atf_set "descr" "Convert a specfile to mtree -C format, unsorted"
110 }
111
112 convert_C_body()
113 {
114 mtree -C -K all <"$(atf_get_srcdir)/d_convert.in" >output
115 h_check "$(atf_get_srcdir)/d_convert_C.out" output
116 }
117
118 atf_test_case convert_C_S
119 convert_C_S_head()
120 {
121 atf_set "descr" "Convert a specfile to mtree -C format, sorted"
122 }
123
124 convert_C_S_body()
125 {
126 mtree -C -S -K all <"$(atf_get_srcdir)/d_convert.in" >output
127 h_check "$(atf_get_srcdir)/d_convert_C_S.out" output
128 }
129
130 atf_test_case convert_D
131 convert_D_head()
132 {
133 atf_set "descr" "Convert a specfile to mtree -D format, unsorted"
134 }
135
136 convert_D_body()
137 {
138 mtree -D -K all <"$(atf_get_srcdir)/d_convert.in" >output
139 h_check "$(atf_get_srcdir)/d_convert_D.out" output
140 }
141
142 atf_test_case convert_D_S
143 convert_D_S_head()
144 {
145 atf_set "descr" "Convert a specfile to mtree -D format, sorted"
146 }
147
148 convert_D_S_body()
149 {
150 mtree -D -S -K all <"$(atf_get_srcdir)/d_convert.in" >output
151 h_check "$(atf_get_srcdir)/d_convert_D_S.out" output
152 }
153
154 atf_test_case ignore
155 ignore_head()
156 {
157 atf_set "descr" "Test that -d ignores symlinks (PR bin/41061)"
158 }
159
160 ignore_body()
161 {
162 mkdir newdir
163 mtree -c | mtree -Ck uid,gid,mode > mtree.spec
164 ln -s newdir otherdir
165
166 # This yields "extra: otherdir" even with -d.
167 # (PR bin/41061)
168 atf_check -s ignore -o empty -e empty -x "mtree -d < mtree.spec"
169
170 # Delete the symlink and re-verify.
171 #
172 rm otherdir
173 atf_check -s ignore -o empty -e empty -x "mtree -d < mtree.spec"
174 }
175
176 atf_test_case merge
177 merge_head()
178 {
179 atf_set "descr" "Merge records of different type"
180 }
181
182 merge_body()
183 {
184 mtree -C -M -K all <"$(atf_get_srcdir)/d_merge.in" >output
185 h_check "$(atf_get_srcdir)/d_merge_C_M.out" output
186 # same again, with sorting
187 mtree -C -M -S -K all <"$(atf_get_srcdir)/d_merge.in" >output
188 h_check "$(atf_get_srcdir)/d_merge_C_M_S.out" output
189 }
190
191 atf_test_case nonemptydir
192 nonemptydir_head()
193 {
194 atf_set "descr" "Test that new non-empty " \
195 "directories are recorded (PR bin/25693)"
196 }
197
198 nonemptydir_body()
199 {
200 mkdir testdir
201 cd testdir
202
203 mtree -c > mtree.spec
204
205 if [ ! -f mtree.spec ]; then
206 atf_fail "mtree failed"
207 fi
208
209 touch bar
210 atf_check -s ignore -o save:output -x "mtree -f mtree.spec"
211
212 if [ ! -n "$(egrep "extra: bar" output)" ]; then
213 atf_fail "mtree did not record changes (PR bin/25693)"
214 fi
215 }
216
217 atf_init_test_cases()
218 {
219 atf_add_test_case create
220 atf_add_test_case check
221 atf_add_test_case convert_C
222 atf_add_test_case convert_C_S
223 atf_add_test_case convert_D
224 atf_add_test_case convert_D_S
225 atf_add_test_case ignore
226 atf_add_test_case merge
227 atf_add_test_case nonemptydir
228 }
229