1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2018, Linaro Limited
3 # Author: Takahiro Akashi <takahiro.akashi@linaro.org>
9 from subprocess import call, check_call, check_output, CalledProcessError
10 from fstest_defs import *
12 supported_fs_basic = ['fat16', 'fat32', 'ext4']
13 supported_fs_ext = ['fat16', 'fat32']
14 supported_fs_mkdir = ['fat16', 'fat32']
15 supported_fs_unlink = ['fat16', 'fat32']
18 # Filesystem test specific setup
20 def pytest_addoption(parser):
21 parser.addoption('--fs-type', action='append', default=None,
22 help='Targeting Filesystem Types')
24 def pytest_configure(config):
25 global supported_fs_basic
26 global supported_fs_ext
27 global supported_fs_mkdir
28 global supported_fs_unlink
30 def intersect(listA, listB):
31 return [x for x in listA if x in listB]
33 supported_fs = config.getoption('fs_type')
35 print("*** FS TYPE modified: %s" % supported_fs)
36 supported_fs_basic = intersect(supported_fs, supported_fs_basic)
37 supported_fs_ext = intersect(supported_fs, supported_fs_ext)
38 supported_fs_mkdir = intersect(supported_fs, supported_fs_mkdir)
39 supported_fs_unlink = intersect(supported_fs, supported_fs_unlink)
41 def pytest_generate_tests(metafunc):
42 if 'fs_obj_basic' in metafunc.fixturenames:
43 metafunc.parametrize('fs_obj_basic', supported_fs_basic,
44 indirect=True, scope='module')
45 if 'fs_obj_ext' in metafunc.fixturenames:
46 metafunc.parametrize('fs_obj_ext', supported_fs_ext,
47 indirect=True, scope='module')
48 if 'fs_obj_mkdir' in metafunc.fixturenames:
49 metafunc.parametrize('fs_obj_mkdir', supported_fs_mkdir,
50 indirect=True, scope='module')
51 if 'fs_obj_unlink' in metafunc.fixturenames:
52 metafunc.parametrize('fs_obj_unlink', supported_fs_unlink,
53 indirect=True, scope='module')
58 def fstype_to_ubname(fs_type):
59 if re.match('fat', fs_type):
64 def check_ubconfig(config, fs_type):
65 if not config.buildconfig.get('config_cmd_%s' % fs_type, None):
66 pytest.skip('.config feature "CMD_%s" not enabled' % fs_type.upper())
67 if not config.buildconfig.get('config_%s_write' % fs_type, None):
68 pytest.skip('.config feature "%s_WRITE" not enabled'
71 def mk_fs(config, fs_type, size, id):
72 fs_img = '%s.%s.img' % (id, fs_type)
73 fs_img = config.persistent_data_dir + '/' + fs_img
75 if fs_type == 'fat16':
77 elif fs_type == 'fat32':
82 if re.match('fat', fs_type):
87 count = (size + 1048576 - 1) / 1048576
90 check_call('rm -f %s' % fs_img, shell=True)
91 check_call('dd if=/dev/zero of=%s bs=1M count=%d'
92 % (fs_img, count), shell=True)
93 check_call('mkfs.%s %s %s'
94 % (fs_lnxtype, mkfs_opt, fs_img), shell=True)
96 except CalledProcessError:
97 call('rm -f %s' % fs_img, shell=True)
100 # from test/py/conftest.py
101 def tool_is_in_path(tool):
102 for path in os.environ["PATH"].split(os.pathsep):
103 fn = os.path.join(path, tool)
104 if os.path.isfile(fn) and os.access(fn, os.X_OK):
110 def mount_fs(fs_type, device, mount_point):
115 if tool_is_in_path('guestmount'):
117 check_call('guestmount -a %s -m /dev/sda %s'
118 % (device, mount_point), shell=True)
120 mount_opt = "loop,rw"
121 if re.match('fat', fs_type):
122 mount_opt += ",umask=0000"
124 check_call('sudo mount -o %s %s %s'
125 % (mount_opt, device, mount_point), shell=True)
127 # may not be effective for some file systems
128 check_call('sudo chmod a+rw %s' % mount_point, shell=True)
129 except CalledProcessError:
132 def umount_fs(fs_type, mount_point):
135 call('guestunmount %s' % mount_point, shell=True)
137 call('sudo umount %s' % mount_point, shell=True)
140 # Fixture for basic fs test
141 # derived from test/fs/fs-test.sh
143 # NOTE: yield_fixture was deprecated since pytest-3.0
144 @pytest.yield_fixture()
145 def fs_obj_basic(request, u_boot_config):
146 fs_type = request.param
149 fs_ubtype = fstype_to_ubname(fs_type)
150 check_ubconfig(u_boot_config, fs_ubtype)
152 mount_dir = u_boot_config.persistent_data_dir + '/mnt'
154 small_file = mount_dir + '/' + SMALL_FILE
155 big_file = mount_dir + '/' + BIG_FILE
160 fs_img = mk_fs(u_boot_config, fs_type, 0xc0000000, '3GB')
162 # Mount the image so we can populate it.
163 check_call('mkdir -p %s' % mount_dir, shell=True)
164 mount_fs(fs_type, fs_img, mount_dir)
166 # Create a subdirectory.
167 check_call('mkdir %s/SUBDIR' % mount_dir, shell=True)
169 # Create big file in this image.
170 # Note that we work only on the start 1MB, couple MBs in the 2GB range
171 # and the last 1 MB of the huge 2.5GB file.
172 # So, just put random values only in those areas.
173 check_call('dd if=/dev/urandom of=%s bs=1M count=1'
174 % big_file, shell=True)
175 check_call('dd if=/dev/urandom of=%s bs=1M count=2 seek=2047'
176 % big_file, shell=True)
177 check_call('dd if=/dev/urandom of=%s bs=1M count=1 seek=2499'
178 % big_file, shell=True)
180 # Create a small file in this image.
181 check_call('dd if=/dev/urandom of=%s bs=1M count=1'
182 % small_file, shell=True)
184 # Delete the small file copies which possibly are written as part of a
186 # check_call('rm -f "%s.w"' % MB1, shell=True)
187 # check_call('rm -f "%s.w2"' % MB1, shell=True)
189 # Generate the md5sums of reads that we will test against small file
191 'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum'
192 % small_file, shell=True)
193 md5val = [ out.split()[0] ]
195 # Generate the md5sums of reads that we will test against big file
196 # One from beginning of file.
198 'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum'
199 % big_file, shell=True)
200 md5val.append(out.split()[0])
202 # One from end of file.
204 'dd if=%s bs=1M skip=2499 count=1 2> /dev/null | md5sum'
205 % big_file, shell=True)
206 md5val.append(out.split()[0])
208 # One from the last 1MB chunk of 2GB
210 'dd if=%s bs=1M skip=2047 count=1 2> /dev/null | md5sum'
211 % big_file, shell=True)
212 md5val.append(out.split()[0])
214 # One from the start 1MB chunk from 2GB
216 'dd if=%s bs=1M skip=2048 count=1 2> /dev/null | md5sum'
217 % big_file, shell=True)
218 md5val.append(out.split()[0])
220 # One 1MB chunk crossing the 2GB boundary
222 'dd if=%s bs=512K skip=4095 count=2 2> /dev/null | md5sum'
223 % big_file, shell=True)
224 md5val.append(out.split()[0])
226 umount_fs(fs_type, mount_dir)
227 except CalledProcessError:
228 pytest.skip('Setup failed for filesystem: ' + fs_type)
231 yield [fs_ubtype, fs_img, md5val]
233 umount_fs(fs_type, mount_dir)
234 call('rmdir %s' % mount_dir, shell=True)
236 call('rm -f %s' % fs_img, shell=True)
239 # Fixture for extended fs test
241 # NOTE: yield_fixture was deprecated since pytest-3.0
242 @pytest.yield_fixture()
243 def fs_obj_ext(request, u_boot_config):
244 fs_type = request.param
247 fs_ubtype = fstype_to_ubname(fs_type)
248 check_ubconfig(u_boot_config, fs_ubtype)
250 mount_dir = u_boot_config.persistent_data_dir + '/mnt'
252 min_file = mount_dir + '/' + MIN_FILE
253 tmp_file = mount_dir + '/tmpfile'
258 fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
260 # Mount the image so we can populate it.
261 check_call('mkdir -p %s' % mount_dir, shell=True)
262 mount_fs(fs_type, fs_img, mount_dir)
264 # Create a test directory
265 check_call('mkdir %s/dir1' % mount_dir, shell=True)
267 # Create a small file and calculate md5
268 check_call('dd if=/dev/urandom of=%s bs=1K count=20'
269 % min_file, shell=True)
271 'dd if=%s bs=1K 2> /dev/null | md5sum'
272 % min_file, shell=True)
273 md5val = [ out.split()[0] ]
275 # Calculate md5sum of Test Case 4
276 check_call('dd if=%s of=%s bs=1K count=20'
277 % (min_file, tmp_file), shell=True)
278 check_call('dd if=%s of=%s bs=1K seek=5 count=20'
279 % (min_file, tmp_file), shell=True)
280 out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
281 % tmp_file, shell=True)
282 md5val.append(out.split()[0])
284 # Calculate md5sum of Test Case 5
285 check_call('dd if=%s of=%s bs=1K count=20'
286 % (min_file, tmp_file), shell=True)
287 check_call('dd if=%s of=%s bs=1K seek=5 count=5'
288 % (min_file, tmp_file), shell=True)
289 out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
290 % tmp_file, shell=True)
291 md5val.append(out.split()[0])
293 # Calculate md5sum of Test Case 7
294 check_call('dd if=%s of=%s bs=1K count=20'
295 % (min_file, tmp_file), shell=True)
296 check_call('dd if=%s of=%s bs=1K seek=20 count=20'
297 % (min_file, tmp_file), shell=True)
298 out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
299 % tmp_file, shell=True)
300 md5val.append(out.split()[0])
302 check_call('rm %s' % tmp_file, shell=True)
303 umount_fs(fs_type, mount_dir)
304 except CalledProcessError:
305 pytest.skip('Setup failed for filesystem: ' + fs_type)
308 yield [fs_ubtype, fs_img, md5val]
310 umount_fs(fs_type, mount_dir)
311 call('rmdir %s' % mount_dir, shell=True)
313 call('rm -f %s' % fs_img, shell=True)
316 # Fixture for mkdir test
318 # NOTE: yield_fixture was deprecated since pytest-3.0
319 @pytest.yield_fixture()
320 def fs_obj_mkdir(request, u_boot_config):
321 fs_type = request.param
324 fs_ubtype = fstype_to_ubname(fs_type)
325 check_ubconfig(u_boot_config, fs_ubtype)
329 fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
331 pytest.skip('Setup failed for filesystem: ' + fs_type)
333 yield [fs_ubtype, fs_img]
336 call('rm -f %s' % fs_img, shell=True)
339 # Fixture for unlink test
341 # NOTE: yield_fixture was deprecated since pytest-3.0
342 @pytest.yield_fixture()
343 def fs_obj_unlink(request, u_boot_config):
344 fs_type = request.param
347 fs_ubtype = fstype_to_ubname(fs_type)
348 check_ubconfig(u_boot_config, fs_ubtype)
350 mount_dir = u_boot_config.persistent_data_dir + '/mnt'
355 fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
357 # Mount the image so we can populate it.
358 check_call('mkdir -p %s' % mount_dir, shell=True)
359 mount_fs(fs_type, fs_img, mount_dir)
362 check_call('mkdir %s/dir1' % mount_dir, shell=True)
363 check_call('dd if=/dev/urandom of=%s/dir1/file1 bs=1K count=1'
364 % mount_dir, shell=True)
365 check_call('dd if=/dev/urandom of=%s/dir1/file2 bs=1K count=1'
366 % mount_dir, shell=True)
369 check_call('mkdir %s/dir2' % mount_dir, shell=True)
370 for i in range(0, 20):
371 check_call('mkdir %s/dir2/0123456789abcdef%02x'
372 % (mount_dir, i), shell=True)
375 check_call('mkdir %s/dir4' % mount_dir, shell=True)
378 check_call('mkdir %s/dir5' % mount_dir, shell=True)
379 check_call('dd if=/dev/urandom of=%s/dir5/file1 bs=1K count=1'
380 % mount_dir, shell=True)
382 umount_fs(fs_type, mount_dir)
383 except CalledProcessError:
384 pytest.skip('Setup failed for filesystem: ' + fs_type)
387 yield [fs_ubtype, fs_img]
389 umount_fs(fs_type, mount_dir)
390 call('rmdir %s' % mount_dir, shell=True)
392 call('rm -f %s' % fs_img, shell=True)