Fixes
[wimlib] / tests / test-imagex-mount
1 #!/bin/sh
2
3 # Test WIM mounting
4
5 set -e
6 srcdir=${srcdir:-.}
7 srcdir=`realpath $srcdir`
8 cd tests
9
10 imagex() {
11         echo "imagex $@"
12         ../imagex $@  > /dev/null
13 }
14
15 imagex_info() {
16         echo "imagex info $@"
17         ../imagex info "$@"
18 }
19
20 imagex_mountrw() {
21         echo "imagex mountrw $@"
22         ../imagex mountrw --debug "$@" &> mount.log &
23         while ! mountpoint $2 >& /dev/null; do
24                 sleep 0.01;
25         done
26         return 0
27 }
28
29 cleanup() {
30         fusermount -u tmp &> /dev/null || true
31         fusermount -u tmp.mnt &> /dev/null || true
32         rm -rf dir* tmp* *.wim *.swm empty.wim tmp.orig tmp.mnt \
33                         tmp.apply mount.log test.tar wimlib-staging*
34 }
35 init() {
36         mkdir dir
37         cp $srcdir/src/*.c $srcdir/src/*.h dir
38         mkdir dir/subdir
39         echo 'hello' > dir/subdir/hello
40         echo 'hello' > dir/subdir/hello2
41         ln dir/subdir/hello dir/subdir/hellolink
42         echo -n > dir/subdir/empty_file
43         ln -s hello dir/subdir/rel_symlink
44
45         mkdir dir2
46         echo 'testing' > dir2/file
47         dd if=/dev/zero of=dir2/zeroes bs=4096 count=5
48         mkdir tmp.empty tmp.mnt tmp.apply tmp.orig
49         imagex capture tmp.empty empty.wim
50 }
51
52 error() {
53         echo "**********************************************"
54         echo "                  Test failure                "
55         echo $*
56         echo "**********************************************"
57         exit 1
58 }
59
60 cleanup
61 init
62
63 # imagex mount
64
65 for flag in "--compress=none" "--compress=maximum" "--compress=fast"; do
66         echo "Using flag $flag"
67         echo "Testing mounting WIM read-only"
68         if ! imagex capture dir dir.wim $flag; then
69                 error "Failed to capture WIM"
70         fi
71         mkdir tmp
72         if ! imagex mount dir.wim dir tmp; then
73                 error "Failde to mount test WIM read-only"
74         fi
75         echo "Testing extracting file from mounted read-only WIM"
76         if ! cp tmp/lz.c lz.c; then
77                 error "Failed to extract file from read-only mounted WIM"
78         fi
79         if ! diff -q dir/lz.c lz.c; then
80                 error "Extracted file does not match copy in mounted WIM"
81         fi
82         if ! diff -q tmp/lz.c dir/lz.c; then
83                 error "Extractef file does not match original"
84         fi
85         rm -f lz.c
86         echo "Testing modifying mounted read-only WIM (should fail)"
87         if rm tmp/lz.c; then
88                 error "Removing file from read-only mounted WIM didn't fail"
89         fi
90         if touch tmp/newfile; then
91                 error "Creating file on read-only mounted WIM didn't fail"
92         fi
93         if echo 3 > tmp/lz.c; then
94                 error "Writing to file on read-only mounted WIM didn't fail"
95         fi
96         echo "Testing diff of mounted read-only WIM with original directory"
97         if ! diff -q -r tmp dir; then
98                 error "Recursive diff of read-only mounted WIM with original directory failed"
99         fi
100         echo "Testing unmount of read-only filesystem"
101         if ! imagex unmount tmp; then
102                 error "Unmounting read-only WIM failed"
103         fi
104         echo "Testing unmount of read-only filesystem with --commit given"
105         if ! imagex mount dir.wim dir tmp; then
106                 error "Failed to re-mount WIM read-only"
107         fi
108         if ! imagex unmount tmp --commit; then
109                 error "Failed to unmount read-only WIM with --commit flag (should be ignored)"
110         fi
111         rm -rf tmp dir.wim
112 done
113
114 # imagex mountrw
115 echo "Testing mounting WIM read-write"
116 if ! imagex capture dir dir.wim; then
117         error "Failed to capture WIM"
118 fi
119 mkdir tmp
120 if ! imagex mountrw dir.wim dir tmp; then
121         error "Failed to mount test WIM read-write"
122 fi
123 echo "Testing unmounting WIM unmodified"
124 if ! imagex unmount tmp; then
125         error "Failed to unmount test WIM unmodified"
126 fi
127 echo "Testing unmounting WIM unmodified with --commit and --check"
128 if ! imagex mountrw dir.wim dir tmp; then
129         error "Failed to re-mount test WIM read-write"
130 fi
131 if ! imagex unmount tmp --commit --check; then
132         error "Failed to unmount read-write mounted WIM with changes commited (no changes made)"
133 fi
134 echo "Testing removing file from mounted WIM"
135 if ! imagex mountrw dir.wim dir tmp; then
136         error "Failed to re-mount test WIM read-write"
137 fi
138 if ! rm tmp/lz.c; then
139         error "Failed to remove file from read-write mounted WIM"
140 fi
141 if test -f tmp/lz.c; then
142         error "Removing file from read-write mounted WIM failed"
143 fi
144 echo "Testing making directory in mounted WIM"
145 if ! mkdir tmp/newdir; then
146         error "Failed to make directory in read-write mounted WIM"
147 fi
148 if ! test -d tmp/newdir; then
149         error "Making directory in read-write mounted WIM failed"
150 fi
151 echo "Testing making new empty file in mounted WIM"
152 if ! touch tmp/newdir/empty_file; then
153         error "Could not create new empty file in read-write mounted WIM"
154 fi
155 if ! test -f tmp/newdir/empty_file; then
156         error "New empty file not created correctly in read-write mounted WIM"
157 fi
158 if ! test "`stat -c %s tmp/newdir/empty_file`" = 0; then
159         error "New empty file in read-write mounted WIM is not empty"
160 fi
161 echo "Testing making new non-empty file in mounted WIM"
162 if ! dd if=/dev/zero of=tmp/newdir/zeroes1 bs=1 count=4096; then
163         error "Failed to make new non-empty file in mounted WIM"
164 fi
165 if ! dd if=/dev/zero of=tmp/newdir/zeroes2 bs=4096 count=1; then
166         error "Failed to make new non-empty file in mounted WIM"
167 fi
168 if ! diff -q tmp/newdir/zeroes1 tmp/newdir/zeroes2; then
169         error "New files in mounted WIM not made correctly"
170 fi
171 echo "Unmounting WIM with changes committed and --check"
172 if ! imagex unmount tmp --commit --check; then
173         error "Failed to unmount read-write mounted WIM"
174 fi
175 if test "`imagex_info dir.wim | grep Integrity | awk '{print $3}'`" != "yes"; then
176         error "Integrity information was not included"
177 fi
178 rm -rf tmp
179 if ! imagex apply dir.wim tmp; then
180         error "Failed to apply WIM we had previously mounted read-write"
181 fi
182 if ! diff -q tmp/newdir/zeroes1 tmp/newdir/zeroes2; then
183         error "The new non-empty files we made in the read-write mounted WIM were not extracted correctly"
184 fi
185 if test `stat -c %s tmp/newdir/empty_file` != 0; then
186         error "The new empty file we made in the read-write mounted WIM was not extracted correctly"
187 fi
188 if test `stat -c %s tmp/newdir/zeroes1` != 4096; then
189         error "The new non-empty files we made in the read-write mounted WIM were not extracted correctly"
190 fi
191
192 # Now do some tests using tar.
193 do_tree_cmp() {
194         if ! ./tree-cmp $1 $2; then
195                 if [ -x /usr/bin/tree ]; then
196                         echo "Dumping tree of applied image"
197                         tree $2 --inodes -F -s --noreport
198                         error 'Information was lost or corrupted while capturing
199                                 and then applying a directory tree'
200                 fi
201         fi
202 }
203
204 msg() {
205         echo "--------------------------------------------------------------------"
206         echo "Testing making $1 on read-write mounted WIM"
207         echo "--------------------------------------------------------------------"
208 }
209
210 do_test() {
211
212         # Create tree, tar it up, and untar it on an empty WIM image mounted
213         # read-write
214
215         cp empty.wim test.wim
216
217         cd tmp.orig
218         eval "$1"
219         if [ -x /usr/bin/tree ]; then
220                 tree . --inodes -F -s --noreport
221         fi
222         tar cf ../test.tar .
223         cd ..
224
225         if ! imagex_mountrw test.wim tmp.mnt; then
226                 error "Failed to mount WIM read-write"
227         fi
228
229         cd tmp.mnt
230         if ! tar xf ../test.tar; then
231                 error "Failed to untar archive on read-write mounted WIM"
232         fi
233         cd ..
234
235         # Diff the original tree with the mounted WIM
236         do_tree_cmp tmp.orig tmp.mnt
237
238         # Clear the mounted WIM and do it again!  (We need to test deleting
239         # stuff as well as creating stuff.)
240         if ! rm -rf tmp.mnt/*; then
241                 error "Failed to clear mounted WIM"
242         fi
243
244         cd tmp.mnt
245         if ! tar xf ../test.tar; then
246                 error "Failed to untar archive on read-write mounted WIM"
247         fi
248         cd ..
249
250         # Diff the original tree with the mounted WIM
251         do_tree_cmp tmp.orig tmp.mnt
252
253         # Unmount the WIM, apply it, and diff the original tree with the applied
254         # tree
255         if ! imagex unmount tmp.mnt --commit; then
256                 error "Failed to unmount WIM mounted read-write"
257         fi
258         if ! imagex apply test.wim tmp.apply; then
259                 error "Failed to apply WIM we previously had mounted read-write"
260         fi
261         do_tree_cmp tmp.orig tmp.apply
262         rm -rf tmp.orig/* tmp.apply/*
263 }
264
265 . $srcdir/tests/common_tests.sh
266
267 cleanup
268
269 echo "**********************************************************"
270 echo "                 WIM mount tests passed                   "
271 echo "**********************************************************"