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 """Enable --fs-type option.
23 See pytest_configure() about how it works.
26 parser: Pytest command-line parser.
31 parser.addoption('--fs-type', action='append', default=None,
32 help='Targeting Filesystem Types')
34 def pytest_configure(config):
35 """Restrict a file system(s) to be tested.
37 A file system explicitly named with --fs-type option is selected
38 if it belongs to a default supported_fs_xxx list.
39 Multiple options can be specified.
42 config: Pytest configuration.
47 global supported_fs_basic
48 global supported_fs_ext
49 global supported_fs_mkdir
50 global supported_fs_unlink
52 def intersect(listA, listB):
53 return [x for x in listA if x in listB]
55 supported_fs = config.getoption('fs_type')
57 print('*** FS TYPE modified: %s' % supported_fs)
58 supported_fs_basic = intersect(supported_fs, supported_fs_basic)
59 supported_fs_ext = intersect(supported_fs, supported_fs_ext)
60 supported_fs_mkdir = intersect(supported_fs, supported_fs_mkdir)
61 supported_fs_unlink = intersect(supported_fs, supported_fs_unlink)
63 def pytest_generate_tests(metafunc):
64 """Parametrize fixtures, fs_obj_xxx
66 Each fixture will be parametrized with a corresponding support_fs_xxx
70 metafunc: Pytest test function.
75 if 'fs_obj_basic' in metafunc.fixturenames:
76 metafunc.parametrize('fs_obj_basic', supported_fs_basic,
77 indirect=True, scope='module')
78 if 'fs_obj_ext' in metafunc.fixturenames:
79 metafunc.parametrize('fs_obj_ext', supported_fs_ext,
80 indirect=True, scope='module')
81 if 'fs_obj_mkdir' in metafunc.fixturenames:
82 metafunc.parametrize('fs_obj_mkdir', supported_fs_mkdir,
83 indirect=True, scope='module')
84 if 'fs_obj_unlink' in metafunc.fixturenames:
85 metafunc.parametrize('fs_obj_unlink', supported_fs_unlink,
86 indirect=True, scope='module')
91 def fstype_to_ubname(fs_type):
92 """Convert a file system type to an U-boot specific string
94 A generated string can be used as part of file system related commands
95 or a config name in u-boot. Currently fat16 and fat32 are handled
99 fs_type: File system type.
102 A corresponding string for file system type.
104 if re.match('fat', fs_type):
109 def check_ubconfig(config, fs_type):
110 """Check whether a file system is enabled in u-boot configuration.
112 This function is assumed to be called in a fixture function so that
113 the whole test cases will be skipped if a given file system is not
117 fs_type: File system type.
122 if not config.buildconfig.get('config_cmd_%s' % fs_type, None):
123 pytest.skip('.config feature "CMD_%s" not enabled' % fs_type.upper())
124 if not config.buildconfig.get('config_%s_write' % fs_type, None):
125 pytest.skip('.config feature "%s_WRITE" not enabled'
128 def mk_fs(config, fs_type, size, id):
129 """Create a file system volume.
132 fs_type: File system type.
133 size: Size of file system in MiB.
134 id: Prefix string of volume's file name.
139 fs_img = '%s.%s.img' % (id, fs_type)
140 fs_img = config.persistent_data_dir + '/' + fs_img
142 if fs_type == 'fat16':
144 elif fs_type == 'fat32':
149 if re.match('fat', fs_type):
154 count = (size + 1048576 - 1) / 1048576
157 check_call('rm -f %s' % fs_img, shell=True)
158 check_call('dd if=/dev/zero of=%s bs=1M count=%d'
159 % (fs_img, count), shell=True)
160 check_call('mkfs.%s %s %s'
161 % (fs_lnxtype, mkfs_opt, fs_img), shell=True)
163 except CalledProcessError:
164 call('rm -f %s' % fs_img, shell=True)
167 # from test/py/conftest.py
168 def tool_is_in_path(tool):
169 """Check whether a given command is available on host.
175 True if available, False if not.
177 for path in os.environ['PATH'].split(os.pathsep):
178 fn = os.path.join(path, tool)
179 if os.path.isfile(fn) and os.access(fn, os.X_OK):
185 def mount_fs(fs_type, device, mount_point):
189 fs_type: File system type.
190 device: Volume's file name.
191 mount_point: Mount point.
200 if tool_is_in_path('guestmount'):
202 check_call('guestmount -a %s -m /dev/sda %s'
203 % (device, mount_point), shell=True)
205 mount_opt = 'loop,rw'
206 if re.match('fat', fs_type):
207 mount_opt += ',umask=0000'
209 check_call('sudo mount -o %s %s %s'
210 % (mount_opt, device, mount_point), shell=True)
212 # may not be effective for some file systems
213 check_call('sudo chmod a+rw %s' % mount_point, shell=True)
214 except CalledProcessError:
217 def umount_fs(mount_point):
221 mount_point: Mount point.
228 call('guestunmount %s' % mount_point, shell=True)
230 call('sudo umount %s' % mount_point, shell=True)
233 # Fixture for basic fs test
234 # derived from test/fs/fs-test.sh
236 # NOTE: yield_fixture was deprecated since pytest-3.0
237 @pytest.yield_fixture()
238 def fs_obj_basic(request, u_boot_config):
239 """Set up a file system to be used in basic fs test.
242 request: Pytest request object.
243 u_boot_config: U-boot configuration.
246 A fixture for basic fs test, i.e. a triplet of file system type,
247 volume file name and a list of MD5 hashes.
249 fs_type = request.param
252 fs_ubtype = fstype_to_ubname(fs_type)
253 check_ubconfig(u_boot_config, fs_ubtype)
255 mount_dir = u_boot_config.persistent_data_dir + '/mnt'
257 small_file = mount_dir + '/' + SMALL_FILE
258 big_file = mount_dir + '/' + BIG_FILE
263 fs_img = mk_fs(u_boot_config, fs_type, 0xc0000000, '3GB')
265 # Mount the image so we can populate it.
266 check_call('mkdir -p %s' % mount_dir, shell=True)
267 mount_fs(fs_type, fs_img, mount_dir)
269 # Create a subdirectory.
270 check_call('mkdir %s/SUBDIR' % mount_dir, shell=True)
272 # Create big file in this image.
273 # Note that we work only on the start 1MB, couple MBs in the 2GB range
274 # and the last 1 MB of the huge 2.5GB file.
275 # So, just put random values only in those areas.
276 check_call('dd if=/dev/urandom of=%s bs=1M count=1'
277 % big_file, shell=True)
278 check_call('dd if=/dev/urandom of=%s bs=1M count=2 seek=2047'
279 % big_file, shell=True)
280 check_call('dd if=/dev/urandom of=%s bs=1M count=1 seek=2499'
281 % big_file, shell=True)
283 # Create a small file in this image.
284 check_call('dd if=/dev/urandom of=%s bs=1M count=1'
285 % small_file, shell=True)
287 # Delete the small file copies which possibly are written as part of a
289 # check_call('rm -f "%s.w"' % MB1, shell=True)
290 # check_call('rm -f "%s.w2"' % MB1, shell=True)
292 # Generate the md5sums of reads that we will test against small file
294 'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum'
295 % small_file, shell=True)
296 md5val = [ out.split()[0] ]
298 # Generate the md5sums of reads that we will test against big file
299 # One from beginning of file.
301 'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum'
302 % big_file, shell=True)
303 md5val.append(out.split()[0])
305 # One from end of file.
307 'dd if=%s bs=1M skip=2499 count=1 2> /dev/null | md5sum'
308 % big_file, shell=True)
309 md5val.append(out.split()[0])
311 # One from the last 1MB chunk of 2GB
313 'dd if=%s bs=1M skip=2047 count=1 2> /dev/null | md5sum'
314 % big_file, shell=True)
315 md5val.append(out.split()[0])
317 # One from the start 1MB chunk from 2GB
319 'dd if=%s bs=1M skip=2048 count=1 2> /dev/null | md5sum'
320 % big_file, shell=True)
321 md5val.append(out.split()[0])
323 # One 1MB chunk crossing the 2GB boundary
325 'dd if=%s bs=512K skip=4095 count=2 2> /dev/null | md5sum'
326 % big_file, shell=True)
327 md5val.append(out.split()[0])
330 except CalledProcessError:
331 pytest.skip('Setup failed for filesystem: ' + fs_type)
334 yield [fs_ubtype, fs_img, md5val]
337 call('rmdir %s' % mount_dir, shell=True)
339 call('rm -f %s' % fs_img, shell=True)
342 # Fixture for extended fs test
344 # NOTE: yield_fixture was deprecated since pytest-3.0
345 @pytest.yield_fixture()
346 def fs_obj_ext(request, u_boot_config):
347 """Set up a file system to be used in extended fs test.
350 request: Pytest request object.
351 u_boot_config: U-boot configuration.
354 A fixture for extended fs test, i.e. a triplet of file system type,
355 volume file name and a list of MD5 hashes.
357 fs_type = request.param
360 fs_ubtype = fstype_to_ubname(fs_type)
361 check_ubconfig(u_boot_config, fs_ubtype)
363 mount_dir = u_boot_config.persistent_data_dir + '/mnt'
365 min_file = mount_dir + '/' + MIN_FILE
366 tmp_file = mount_dir + '/tmpfile'
371 fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
373 # Mount the image so we can populate it.
374 check_call('mkdir -p %s' % mount_dir, shell=True)
375 mount_fs(fs_type, fs_img, mount_dir)
377 # Create a test directory
378 check_call('mkdir %s/dir1' % mount_dir, shell=True)
380 # Create a small file and calculate md5
381 check_call('dd if=/dev/urandom of=%s bs=1K count=20'
382 % min_file, shell=True)
384 'dd if=%s bs=1K 2> /dev/null | md5sum'
385 % min_file, shell=True)
386 md5val = [ out.split()[0] ]
388 # Calculate md5sum of Test Case 4
389 check_call('dd if=%s of=%s bs=1K count=20'
390 % (min_file, tmp_file), shell=True)
391 check_call('dd if=%s of=%s bs=1K seek=5 count=20'
392 % (min_file, tmp_file), shell=True)
393 out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
394 % tmp_file, shell=True)
395 md5val.append(out.split()[0])
397 # Calculate md5sum of Test Case 5
398 check_call('dd if=%s of=%s bs=1K count=20'
399 % (min_file, tmp_file), shell=True)
400 check_call('dd if=%s of=%s bs=1K seek=5 count=5'
401 % (min_file, tmp_file), shell=True)
402 out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
403 % tmp_file, shell=True)
404 md5val.append(out.split()[0])
406 # Calculate md5sum of Test Case 7
407 check_call('dd if=%s of=%s bs=1K count=20'
408 % (min_file, tmp_file), shell=True)
409 check_call('dd if=%s of=%s bs=1K seek=20 count=20'
410 % (min_file, tmp_file), shell=True)
411 out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
412 % tmp_file, shell=True)
413 md5val.append(out.split()[0])
415 check_call('rm %s' % tmp_file, shell=True)
417 except CalledProcessError:
418 pytest.skip('Setup failed for filesystem: ' + fs_type)
421 yield [fs_ubtype, fs_img, md5val]
424 call('rmdir %s' % mount_dir, shell=True)
426 call('rm -f %s' % fs_img, shell=True)
429 # Fixture for mkdir test
431 # NOTE: yield_fixture was deprecated since pytest-3.0
432 @pytest.yield_fixture()
433 def fs_obj_mkdir(request, u_boot_config):
434 """Set up a file system to be used in mkdir test.
437 request: Pytest request object.
438 u_boot_config: U-boot configuration.
441 A fixture for mkdir test, i.e. a duplet of file system type and
444 fs_type = request.param
447 fs_ubtype = fstype_to_ubname(fs_type)
448 check_ubconfig(u_boot_config, fs_ubtype)
452 fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
454 pytest.skip('Setup failed for filesystem: ' + fs_type)
456 yield [fs_ubtype, fs_img]
459 call('rm -f %s' % fs_img, shell=True)
462 # Fixture for unlink test
464 # NOTE: yield_fixture was deprecated since pytest-3.0
465 @pytest.yield_fixture()
466 def fs_obj_unlink(request, u_boot_config):
467 """Set up a file system to be used in unlink test.
470 request: Pytest request object.
471 u_boot_config: U-boot configuration.
474 A fixture for unlink test, i.e. a duplet of file system type and
477 fs_type = request.param
480 fs_ubtype = fstype_to_ubname(fs_type)
481 check_ubconfig(u_boot_config, fs_ubtype)
483 mount_dir = u_boot_config.persistent_data_dir + '/mnt'
488 fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
490 # Mount the image so we can populate it.
491 check_call('mkdir -p %s' % mount_dir, shell=True)
492 mount_fs(fs_type, fs_img, mount_dir)
495 check_call('mkdir %s/dir1' % mount_dir, shell=True)
496 check_call('dd if=/dev/urandom of=%s/dir1/file1 bs=1K count=1'
497 % mount_dir, shell=True)
498 check_call('dd if=/dev/urandom of=%s/dir1/file2 bs=1K count=1'
499 % mount_dir, shell=True)
502 check_call('mkdir %s/dir2' % mount_dir, shell=True)
503 for i in range(0, 20):
504 check_call('mkdir %s/dir2/0123456789abcdef%02x'
505 % (mount_dir, i), shell=True)
508 check_call('mkdir %s/dir4' % mount_dir, shell=True)
511 check_call('mkdir %s/dir5' % mount_dir, shell=True)
512 check_call('dd if=/dev/urandom of=%s/dir5/file1 bs=1K count=1'
513 % mount_dir, shell=True)
516 except CalledProcessError:
517 pytest.skip('Setup failed for filesystem: ' + fs_type)
520 yield [fs_ubtype, fs_img]
523 call('rmdir %s' % mount_dir, shell=True)
525 call('rm -f %s' % fs_img, shell=True)