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