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