1de8106c1SAKASHI Takahiro# SPDX-License-Identifier: GPL-2.0+ 2de8106c1SAKASHI Takahiro# Copyright (c) 2018, Linaro Limited 3de8106c1SAKASHI Takahiro# Author: Takahiro Akashi <takahiro.akashi@linaro.org> 4de8106c1SAKASHI Takahiro 5de8106c1SAKASHI Takahiroimport os 6de8106c1SAKASHI Takahiroimport os.path 7de8106c1SAKASHI Takahiroimport pytest 8de8106c1SAKASHI Takahiroimport re 9de8106c1SAKASHI Takahirofrom subprocess import call, check_call, check_output, CalledProcessError 10de8106c1SAKASHI Takahirofrom fstest_defs import * 11de8106c1SAKASHI Takahiro 12de8106c1SAKASHI Takahirosupported_fs_basic = ['fat16', 'fat32', 'ext4'] 1371f27af5SAKASHI Takahirosupported_fs_ext = ['fat16', 'fat32'] 1450ca19ccSAKASHI Takahirosupported_fs_mkdir = ['fat16', 'fat32'] 15cce289a9SAkashi, Takahirosupported_fs_unlink = ['fat16', 'fat32'] 16de8106c1SAKASHI Takahiro 17de8106c1SAKASHI Takahiro# 18de8106c1SAKASHI Takahiro# Filesystem test specific setup 19de8106c1SAKASHI Takahiro# 20de8106c1SAKASHI Takahirodef pytest_addoption(parser): 21c906f372SAkashi Takahiro """Enable --fs-type option. 22c906f372SAkashi Takahiro 23c906f372SAkashi Takahiro See pytest_configure() about how it works. 24c906f372SAkashi Takahiro 25c906f372SAkashi Takahiro Args: 26c906f372SAkashi Takahiro parser: Pytest command-line parser. 27c906f372SAkashi Takahiro 28c906f372SAkashi Takahiro Returns: 29c906f372SAkashi Takahiro Nothing. 30c906f372SAkashi Takahiro """ 31de8106c1SAKASHI Takahiro parser.addoption('--fs-type', action='append', default=None, 32de8106c1SAKASHI Takahiro help='Targeting Filesystem Types') 33de8106c1SAKASHI Takahiro 34de8106c1SAKASHI Takahirodef pytest_configure(config): 35c906f372SAkashi Takahiro """Restrict a file system(s) to be tested. 36c906f372SAkashi Takahiro 37c906f372SAkashi Takahiro A file system explicitly named with --fs-type option is selected 38c906f372SAkashi Takahiro if it belongs to a default supported_fs_xxx list. 39c906f372SAkashi Takahiro Multiple options can be specified. 40c906f372SAkashi Takahiro 41c906f372SAkashi Takahiro Args: 42c906f372SAkashi Takahiro config: Pytest configuration. 43c906f372SAkashi Takahiro 44c906f372SAkashi Takahiro Returns: 45c906f372SAkashi Takahiro Nothing. 46c906f372SAkashi Takahiro """ 47de8106c1SAKASHI Takahiro global supported_fs_basic 4871f27af5SAKASHI Takahiro global supported_fs_ext 4950ca19ccSAKASHI Takahiro global supported_fs_mkdir 50cce289a9SAkashi, Takahiro global supported_fs_unlink 51de8106c1SAKASHI Takahiro 52de8106c1SAKASHI Takahiro def intersect(listA, listB): 53de8106c1SAKASHI Takahiro return [x for x in listA if x in listB] 54de8106c1SAKASHI Takahiro 55de8106c1SAKASHI Takahiro supported_fs = config.getoption('fs_type') 56de8106c1SAKASHI Takahiro if supported_fs: 57*871bf7d9SSimon Glass print('*** FS TYPE modified: %s' % supported_fs) 58de8106c1SAKASHI Takahiro supported_fs_basic = intersect(supported_fs, supported_fs_basic) 5971f27af5SAKASHI Takahiro supported_fs_ext = intersect(supported_fs, supported_fs_ext) 6050ca19ccSAKASHI Takahiro supported_fs_mkdir = intersect(supported_fs, supported_fs_mkdir) 61cce289a9SAkashi, Takahiro supported_fs_unlink = intersect(supported_fs, supported_fs_unlink) 62de8106c1SAKASHI Takahiro 63de8106c1SAKASHI Takahirodef pytest_generate_tests(metafunc): 64c906f372SAkashi Takahiro """Parametrize fixtures, fs_obj_xxx 65c906f372SAkashi Takahiro 66c906f372SAkashi Takahiro Each fixture will be parametrized with a corresponding support_fs_xxx 67c906f372SAkashi Takahiro list. 68c906f372SAkashi Takahiro 69c906f372SAkashi Takahiro Args: 70c906f372SAkashi Takahiro metafunc: Pytest test function. 71c906f372SAkashi Takahiro 72c906f372SAkashi Takahiro Returns: 73c906f372SAkashi Takahiro Nothing. 74c906f372SAkashi Takahiro """ 75de8106c1SAKASHI Takahiro if 'fs_obj_basic' in metafunc.fixturenames: 76de8106c1SAKASHI Takahiro metafunc.parametrize('fs_obj_basic', supported_fs_basic, 77de8106c1SAKASHI Takahiro indirect=True, scope='module') 7871f27af5SAKASHI Takahiro if 'fs_obj_ext' in metafunc.fixturenames: 7971f27af5SAKASHI Takahiro metafunc.parametrize('fs_obj_ext', supported_fs_ext, 8071f27af5SAKASHI Takahiro indirect=True, scope='module') 8150ca19ccSAKASHI Takahiro if 'fs_obj_mkdir' in metafunc.fixturenames: 8250ca19ccSAKASHI Takahiro metafunc.parametrize('fs_obj_mkdir', supported_fs_mkdir, 8350ca19ccSAKASHI Takahiro indirect=True, scope='module') 84cce289a9SAkashi, Takahiro if 'fs_obj_unlink' in metafunc.fixturenames: 85cce289a9SAkashi, Takahiro metafunc.parametrize('fs_obj_unlink', supported_fs_unlink, 86cce289a9SAkashi, Takahiro indirect=True, scope='module') 87de8106c1SAKASHI Takahiro 88de8106c1SAKASHI Takahiro# 89de8106c1SAKASHI Takahiro# Helper functions 90de8106c1SAKASHI Takahiro# 91de8106c1SAKASHI Takahirodef fstype_to_ubname(fs_type): 92c906f372SAkashi Takahiro """Convert a file system type to an U-boot specific string 93c906f372SAkashi Takahiro 94c906f372SAkashi Takahiro A generated string can be used as part of file system related commands 95c906f372SAkashi Takahiro or a config name in u-boot. Currently fat16 and fat32 are handled 96c906f372SAkashi Takahiro specifically. 97c906f372SAkashi Takahiro 98c906f372SAkashi Takahiro Args: 99c906f372SAkashi Takahiro fs_type: File system type. 100c906f372SAkashi Takahiro 101c906f372SAkashi Takahiro Return: 102c906f372SAkashi Takahiro A corresponding string for file system type. 103c906f372SAkashi Takahiro """ 104de8106c1SAKASHI Takahiro if re.match('fat', fs_type): 105de8106c1SAKASHI Takahiro return 'fat' 106de8106c1SAKASHI Takahiro else: 107de8106c1SAKASHI Takahiro return fs_type 108de8106c1SAKASHI Takahiro 109de8106c1SAKASHI Takahirodef check_ubconfig(config, fs_type): 110c906f372SAkashi Takahiro """Check whether a file system is enabled in u-boot configuration. 111c906f372SAkashi Takahiro 112c906f372SAkashi Takahiro This function is assumed to be called in a fixture function so that 113c906f372SAkashi Takahiro the whole test cases will be skipped if a given file system is not 114c906f372SAkashi Takahiro enabled. 115c906f372SAkashi Takahiro 116c906f372SAkashi Takahiro Args: 117c906f372SAkashi Takahiro fs_type: File system type. 118c906f372SAkashi Takahiro 119c906f372SAkashi Takahiro Return: 120c906f372SAkashi Takahiro Nothing. 121c906f372SAkashi Takahiro """ 122de8106c1SAKASHI Takahiro if not config.buildconfig.get('config_cmd_%s' % fs_type, None): 123de8106c1SAKASHI Takahiro pytest.skip('.config feature "CMD_%s" not enabled' % fs_type.upper()) 124de8106c1SAKASHI Takahiro if not config.buildconfig.get('config_%s_write' % fs_type, None): 125de8106c1SAKASHI Takahiro pytest.skip('.config feature "%s_WRITE" not enabled' 126de8106c1SAKASHI Takahiro % fs_type.upper()) 127de8106c1SAKASHI Takahiro 128de8106c1SAKASHI Takahirodef mk_fs(config, fs_type, size, id): 129c906f372SAkashi Takahiro """Create a file system volume. 130c906f372SAkashi Takahiro 131c906f372SAkashi Takahiro Args: 132c906f372SAkashi Takahiro fs_type: File system type. 133c906f372SAkashi Takahiro size: Size of file system in MiB. 134c906f372SAkashi Takahiro id: Prefix string of volume's file name. 135c906f372SAkashi Takahiro 136c906f372SAkashi Takahiro Return: 137c906f372SAkashi Takahiro Nothing. 138c906f372SAkashi Takahiro """ 139de8106c1SAKASHI Takahiro fs_img = '%s.%s.img' % (id, fs_type) 140de8106c1SAKASHI Takahiro fs_img = config.persistent_data_dir + '/' + fs_img 141de8106c1SAKASHI Takahiro 142de8106c1SAKASHI Takahiro if fs_type == 'fat16': 143de8106c1SAKASHI Takahiro mkfs_opt = '-F 16' 144de8106c1SAKASHI Takahiro elif fs_type == 'fat32': 145de8106c1SAKASHI Takahiro mkfs_opt = '-F 32' 146de8106c1SAKASHI Takahiro else: 147de8106c1SAKASHI Takahiro mkfs_opt = '' 148de8106c1SAKASHI Takahiro 149de8106c1SAKASHI Takahiro if re.match('fat', fs_type): 150de8106c1SAKASHI Takahiro fs_lnxtype = 'vfat' 151de8106c1SAKASHI Takahiro else: 152de8106c1SAKASHI Takahiro fs_lnxtype = fs_type 153de8106c1SAKASHI Takahiro 154de8106c1SAKASHI Takahiro count = (size + 1048576 - 1) / 1048576 155de8106c1SAKASHI Takahiro 156de8106c1SAKASHI Takahiro try: 157de8106c1SAKASHI Takahiro check_call('rm -f %s' % fs_img, shell=True) 158de8106c1SAKASHI Takahiro check_call('dd if=/dev/zero of=%s bs=1M count=%d' 159de8106c1SAKASHI Takahiro % (fs_img, count), shell=True) 160de8106c1SAKASHI Takahiro check_call('mkfs.%s %s %s' 161de8106c1SAKASHI Takahiro % (fs_lnxtype, mkfs_opt, fs_img), shell=True) 162de8106c1SAKASHI Takahiro return fs_img 163de8106c1SAKASHI Takahiro except CalledProcessError: 164de8106c1SAKASHI Takahiro call('rm -f %s' % fs_img, shell=True) 165de8106c1SAKASHI Takahiro raise 166de8106c1SAKASHI Takahiro 167de8106c1SAKASHI Takahiro# from test/py/conftest.py 168de8106c1SAKASHI Takahirodef tool_is_in_path(tool): 169c906f372SAkashi Takahiro """Check whether a given command is available on host. 170c906f372SAkashi Takahiro 171c906f372SAkashi Takahiro Args: 172c906f372SAkashi Takahiro tool: Command name. 173c906f372SAkashi Takahiro 174c906f372SAkashi Takahiro Return: 175c906f372SAkashi Takahiro True if available, False if not. 176c906f372SAkashi Takahiro """ 177*871bf7d9SSimon Glass for path in os.environ['PATH'].split(os.pathsep): 178de8106c1SAKASHI Takahiro fn = os.path.join(path, tool) 179de8106c1SAKASHI Takahiro if os.path.isfile(fn) and os.access(fn, os.X_OK): 180de8106c1SAKASHI Takahiro return True 181de8106c1SAKASHI Takahiro return False 182de8106c1SAKASHI Takahiro 183de8106c1SAKASHI Takahirofuse_mounted = False 184de8106c1SAKASHI Takahiro 185de8106c1SAKASHI Takahirodef mount_fs(fs_type, device, mount_point): 186c906f372SAkashi Takahiro """Mount a volume. 187c906f372SAkashi Takahiro 188c906f372SAkashi Takahiro Args: 189c906f372SAkashi Takahiro fs_type: File system type. 190c906f372SAkashi Takahiro device: Volume's file name. 191c906f372SAkashi Takahiro mount_point: Mount point. 192c906f372SAkashi Takahiro 193c906f372SAkashi Takahiro Return: 194c906f372SAkashi Takahiro Nothing. 195c906f372SAkashi Takahiro """ 196de8106c1SAKASHI Takahiro global fuse_mounted 197de8106c1SAKASHI Takahiro 198de8106c1SAKASHI Takahiro fuse_mounted = False 199de8106c1SAKASHI Takahiro try: 200de8106c1SAKASHI Takahiro if tool_is_in_path('guestmount'): 201de8106c1SAKASHI Takahiro fuse_mounted = True 202de8106c1SAKASHI Takahiro check_call('guestmount -a %s -m /dev/sda %s' 203de8106c1SAKASHI Takahiro % (device, mount_point), shell=True) 204de8106c1SAKASHI Takahiro else: 205*871bf7d9SSimon Glass mount_opt = 'loop,rw' 206de8106c1SAKASHI Takahiro if re.match('fat', fs_type): 207*871bf7d9SSimon Glass mount_opt += ',umask=0000' 208de8106c1SAKASHI Takahiro 209de8106c1SAKASHI Takahiro check_call('sudo mount -o %s %s %s' 210de8106c1SAKASHI Takahiro % (mount_opt, device, mount_point), shell=True) 211de8106c1SAKASHI Takahiro 212de8106c1SAKASHI Takahiro # may not be effective for some file systems 213de8106c1SAKASHI Takahiro check_call('sudo chmod a+rw %s' % mount_point, shell=True) 214de8106c1SAKASHI Takahiro except CalledProcessError: 215de8106c1SAKASHI Takahiro raise 216de8106c1SAKASHI Takahiro 217e4040df5SAkashi Takahirodef umount_fs(mount_point): 218c906f372SAkashi Takahiro """Unmount a volume. 219c906f372SAkashi Takahiro 220c906f372SAkashi Takahiro Args: 221c906f372SAkashi Takahiro mount_point: Mount point. 222c906f372SAkashi Takahiro 223c906f372SAkashi Takahiro Return: 224c906f372SAkashi Takahiro Nothing. 225c906f372SAkashi Takahiro """ 226de8106c1SAKASHI Takahiro if fuse_mounted: 227de8106c1SAKASHI Takahiro call('sync') 228de8106c1SAKASHI Takahiro call('guestunmount %s' % mount_point, shell=True) 229de8106c1SAKASHI Takahiro else: 230de8106c1SAKASHI Takahiro call('sudo umount %s' % mount_point, shell=True) 231de8106c1SAKASHI Takahiro 232de8106c1SAKASHI Takahiro# 233de8106c1SAKASHI Takahiro# Fixture for basic fs test 234de8106c1SAKASHI Takahiro# derived from test/fs/fs-test.sh 235de8106c1SAKASHI Takahiro# 236de8106c1SAKASHI Takahiro# NOTE: yield_fixture was deprecated since pytest-3.0 237de8106c1SAKASHI Takahiro@pytest.yield_fixture() 238de8106c1SAKASHI Takahirodef fs_obj_basic(request, u_boot_config): 239c906f372SAkashi Takahiro """Set up a file system to be used in basic fs test. 240c906f372SAkashi Takahiro 241c906f372SAkashi Takahiro Args: 242c906f372SAkashi Takahiro request: Pytest request object. 243c906f372SAkashi Takahiro u_boot_config: U-boot configuration. 244c906f372SAkashi Takahiro 245c906f372SAkashi Takahiro Return: 246c906f372SAkashi Takahiro A fixture for basic fs test, i.e. a triplet of file system type, 247c906f372SAkashi Takahiro volume file name and a list of MD5 hashes. 248c906f372SAkashi Takahiro """ 249de8106c1SAKASHI Takahiro fs_type = request.param 250de8106c1SAKASHI Takahiro fs_img = '' 251de8106c1SAKASHI Takahiro 252de8106c1SAKASHI Takahiro fs_ubtype = fstype_to_ubname(fs_type) 253de8106c1SAKASHI Takahiro check_ubconfig(u_boot_config, fs_ubtype) 254de8106c1SAKASHI Takahiro 255de8106c1SAKASHI Takahiro mount_dir = u_boot_config.persistent_data_dir + '/mnt' 256de8106c1SAKASHI Takahiro 257de8106c1SAKASHI Takahiro small_file = mount_dir + '/' + SMALL_FILE 258de8106c1SAKASHI Takahiro big_file = mount_dir + '/' + BIG_FILE 259de8106c1SAKASHI Takahiro 260de8106c1SAKASHI Takahiro try: 261de8106c1SAKASHI Takahiro 262de8106c1SAKASHI Takahiro # 3GiB volume 263de8106c1SAKASHI Takahiro fs_img = mk_fs(u_boot_config, fs_type, 0xc0000000, '3GB') 264de8106c1SAKASHI Takahiro 265de8106c1SAKASHI Takahiro # Mount the image so we can populate it. 266de8106c1SAKASHI Takahiro check_call('mkdir -p %s' % mount_dir, shell=True) 267de8106c1SAKASHI Takahiro mount_fs(fs_type, fs_img, mount_dir) 268de8106c1SAKASHI Takahiro 269de8106c1SAKASHI Takahiro # Create a subdirectory. 270de8106c1SAKASHI Takahiro check_call('mkdir %s/SUBDIR' % mount_dir, shell=True) 271de8106c1SAKASHI Takahiro 272de8106c1SAKASHI Takahiro # Create big file in this image. 273de8106c1SAKASHI Takahiro # Note that we work only on the start 1MB, couple MBs in the 2GB range 274de8106c1SAKASHI Takahiro # and the last 1 MB of the huge 2.5GB file. 275de8106c1SAKASHI Takahiro # So, just put random values only in those areas. 276de8106c1SAKASHI Takahiro check_call('dd if=/dev/urandom of=%s bs=1M count=1' 277de8106c1SAKASHI Takahiro % big_file, shell=True) 278de8106c1SAKASHI Takahiro check_call('dd if=/dev/urandom of=%s bs=1M count=2 seek=2047' 279de8106c1SAKASHI Takahiro % big_file, shell=True) 280de8106c1SAKASHI Takahiro check_call('dd if=/dev/urandom of=%s bs=1M count=1 seek=2499' 281de8106c1SAKASHI Takahiro % big_file, shell=True) 282de8106c1SAKASHI Takahiro 283de8106c1SAKASHI Takahiro # Create a small file in this image. 284de8106c1SAKASHI Takahiro check_call('dd if=/dev/urandom of=%s bs=1M count=1' 285de8106c1SAKASHI Takahiro % small_file, shell=True) 286de8106c1SAKASHI Takahiro 287de8106c1SAKASHI Takahiro # Delete the small file copies which possibly are written as part of a 288de8106c1SAKASHI Takahiro # previous test. 289de8106c1SAKASHI Takahiro # check_call('rm -f "%s.w"' % MB1, shell=True) 290de8106c1SAKASHI Takahiro # check_call('rm -f "%s.w2"' % MB1, shell=True) 291de8106c1SAKASHI Takahiro 292de8106c1SAKASHI Takahiro # Generate the md5sums of reads that we will test against small file 293de8106c1SAKASHI Takahiro out = check_output( 294de8106c1SAKASHI Takahiro 'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum' 295de8106c1SAKASHI Takahiro % small_file, shell=True) 296de8106c1SAKASHI Takahiro md5val = [ out.split()[0] ] 297de8106c1SAKASHI Takahiro 298de8106c1SAKASHI Takahiro # Generate the md5sums of reads that we will test against big file 299de8106c1SAKASHI Takahiro # One from beginning of file. 300de8106c1SAKASHI Takahiro out = check_output( 301de8106c1SAKASHI Takahiro 'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum' 302de8106c1SAKASHI Takahiro % big_file, shell=True) 303de8106c1SAKASHI Takahiro md5val.append(out.split()[0]) 304de8106c1SAKASHI Takahiro 305de8106c1SAKASHI Takahiro # One from end of file. 306de8106c1SAKASHI Takahiro out = check_output( 307de8106c1SAKASHI Takahiro 'dd if=%s bs=1M skip=2499 count=1 2> /dev/null | md5sum' 308de8106c1SAKASHI Takahiro % big_file, shell=True) 309de8106c1SAKASHI Takahiro md5val.append(out.split()[0]) 310de8106c1SAKASHI Takahiro 311de8106c1SAKASHI Takahiro # One from the last 1MB chunk of 2GB 312de8106c1SAKASHI Takahiro out = check_output( 313de8106c1SAKASHI Takahiro 'dd if=%s bs=1M skip=2047 count=1 2> /dev/null | md5sum' 314de8106c1SAKASHI Takahiro % big_file, shell=True) 315de8106c1SAKASHI Takahiro md5val.append(out.split()[0]) 316de8106c1SAKASHI Takahiro 317de8106c1SAKASHI Takahiro # One from the start 1MB chunk from 2GB 318de8106c1SAKASHI Takahiro out = check_output( 319de8106c1SAKASHI Takahiro 'dd if=%s bs=1M skip=2048 count=1 2> /dev/null | md5sum' 320de8106c1SAKASHI Takahiro % big_file, shell=True) 321de8106c1SAKASHI Takahiro md5val.append(out.split()[0]) 322de8106c1SAKASHI Takahiro 323de8106c1SAKASHI Takahiro # One 1MB chunk crossing the 2GB boundary 324de8106c1SAKASHI Takahiro out = check_output( 325de8106c1SAKASHI Takahiro 'dd if=%s bs=512K skip=4095 count=2 2> /dev/null | md5sum' 326de8106c1SAKASHI Takahiro % big_file, shell=True) 327de8106c1SAKASHI Takahiro md5val.append(out.split()[0]) 328de8106c1SAKASHI Takahiro 329e4040df5SAkashi Takahiro umount_fs(mount_dir) 330de8106c1SAKASHI Takahiro except CalledProcessError: 331de8106c1SAKASHI Takahiro pytest.skip('Setup failed for filesystem: ' + fs_type) 332de8106c1SAKASHI Takahiro return 333de8106c1SAKASHI Takahiro else: 334de8106c1SAKASHI Takahiro yield [fs_ubtype, fs_img, md5val] 335de8106c1SAKASHI Takahiro finally: 336e4040df5SAkashi Takahiro umount_fs(mount_dir) 337de8106c1SAKASHI Takahiro call('rmdir %s' % mount_dir, shell=True) 338de8106c1SAKASHI Takahiro if fs_img: 339de8106c1SAKASHI Takahiro call('rm -f %s' % fs_img, shell=True) 34071f27af5SAKASHI Takahiro 34171f27af5SAKASHI Takahiro# 34271f27af5SAKASHI Takahiro# Fixture for extended fs test 34371f27af5SAKASHI Takahiro# 34471f27af5SAKASHI Takahiro# NOTE: yield_fixture was deprecated since pytest-3.0 34571f27af5SAKASHI Takahiro@pytest.yield_fixture() 34671f27af5SAKASHI Takahirodef fs_obj_ext(request, u_boot_config): 347c906f372SAkashi Takahiro """Set up a file system to be used in extended fs test. 348c906f372SAkashi Takahiro 349c906f372SAkashi Takahiro Args: 350c906f372SAkashi Takahiro request: Pytest request object. 351c906f372SAkashi Takahiro u_boot_config: U-boot configuration. 352c906f372SAkashi Takahiro 353c906f372SAkashi Takahiro Return: 354c906f372SAkashi Takahiro A fixture for extended fs test, i.e. a triplet of file system type, 355c906f372SAkashi Takahiro volume file name and a list of MD5 hashes. 356c906f372SAkashi Takahiro """ 35771f27af5SAKASHI Takahiro fs_type = request.param 35871f27af5SAKASHI Takahiro fs_img = '' 35971f27af5SAKASHI Takahiro 36071f27af5SAKASHI Takahiro fs_ubtype = fstype_to_ubname(fs_type) 36171f27af5SAKASHI Takahiro check_ubconfig(u_boot_config, fs_ubtype) 36271f27af5SAKASHI Takahiro 36371f27af5SAKASHI Takahiro mount_dir = u_boot_config.persistent_data_dir + '/mnt' 36471f27af5SAKASHI Takahiro 36571f27af5SAKASHI Takahiro min_file = mount_dir + '/' + MIN_FILE 36671f27af5SAKASHI Takahiro tmp_file = mount_dir + '/tmpfile' 36771f27af5SAKASHI Takahiro 36871f27af5SAKASHI Takahiro try: 36971f27af5SAKASHI Takahiro 37071f27af5SAKASHI Takahiro # 128MiB volume 37171f27af5SAKASHI Takahiro fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB') 37271f27af5SAKASHI Takahiro 37371f27af5SAKASHI Takahiro # Mount the image so we can populate it. 37471f27af5SAKASHI Takahiro check_call('mkdir -p %s' % mount_dir, shell=True) 37571f27af5SAKASHI Takahiro mount_fs(fs_type, fs_img, mount_dir) 37671f27af5SAKASHI Takahiro 37771f27af5SAKASHI Takahiro # Create a test directory 37871f27af5SAKASHI Takahiro check_call('mkdir %s/dir1' % mount_dir, shell=True) 37971f27af5SAKASHI Takahiro 38071f27af5SAKASHI Takahiro # Create a small file and calculate md5 38171f27af5SAKASHI Takahiro check_call('dd if=/dev/urandom of=%s bs=1K count=20' 38271f27af5SAKASHI Takahiro % min_file, shell=True) 38371f27af5SAKASHI Takahiro out = check_output( 38471f27af5SAKASHI Takahiro 'dd if=%s bs=1K 2> /dev/null | md5sum' 38571f27af5SAKASHI Takahiro % min_file, shell=True) 38671f27af5SAKASHI Takahiro md5val = [ out.split()[0] ] 38771f27af5SAKASHI Takahiro 38871f27af5SAKASHI Takahiro # Calculate md5sum of Test Case 4 38971f27af5SAKASHI Takahiro check_call('dd if=%s of=%s bs=1K count=20' 39071f27af5SAKASHI Takahiro % (min_file, tmp_file), shell=True) 39171f27af5SAKASHI Takahiro check_call('dd if=%s of=%s bs=1K seek=5 count=20' 39271f27af5SAKASHI Takahiro % (min_file, tmp_file), shell=True) 39371f27af5SAKASHI Takahiro out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum' 39471f27af5SAKASHI Takahiro % tmp_file, shell=True) 39571f27af5SAKASHI Takahiro md5val.append(out.split()[0]) 39671f27af5SAKASHI Takahiro 39771f27af5SAKASHI Takahiro # Calculate md5sum of Test Case 5 39871f27af5SAKASHI Takahiro check_call('dd if=%s of=%s bs=1K count=20' 39971f27af5SAKASHI Takahiro % (min_file, tmp_file), shell=True) 40071f27af5SAKASHI Takahiro check_call('dd if=%s of=%s bs=1K seek=5 count=5' 40171f27af5SAKASHI Takahiro % (min_file, tmp_file), shell=True) 40271f27af5SAKASHI Takahiro out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum' 40371f27af5SAKASHI Takahiro % tmp_file, shell=True) 40471f27af5SAKASHI Takahiro md5val.append(out.split()[0]) 40571f27af5SAKASHI Takahiro 40671f27af5SAKASHI Takahiro # Calculate md5sum of Test Case 7 40771f27af5SAKASHI Takahiro check_call('dd if=%s of=%s bs=1K count=20' 40871f27af5SAKASHI Takahiro % (min_file, tmp_file), shell=True) 40971f27af5SAKASHI Takahiro check_call('dd if=%s of=%s bs=1K seek=20 count=20' 41071f27af5SAKASHI Takahiro % (min_file, tmp_file), shell=True) 41171f27af5SAKASHI Takahiro out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum' 41271f27af5SAKASHI Takahiro % tmp_file, shell=True) 41371f27af5SAKASHI Takahiro md5val.append(out.split()[0]) 41471f27af5SAKASHI Takahiro 41571f27af5SAKASHI Takahiro check_call('rm %s' % tmp_file, shell=True) 416e4040df5SAkashi Takahiro umount_fs(mount_dir) 41771f27af5SAKASHI Takahiro except CalledProcessError: 41871f27af5SAKASHI Takahiro pytest.skip('Setup failed for filesystem: ' + fs_type) 41971f27af5SAKASHI Takahiro return 42071f27af5SAKASHI Takahiro else: 42171f27af5SAKASHI Takahiro yield [fs_ubtype, fs_img, md5val] 42271f27af5SAKASHI Takahiro finally: 423e4040df5SAkashi Takahiro umount_fs(mount_dir) 42471f27af5SAKASHI Takahiro call('rmdir %s' % mount_dir, shell=True) 42571f27af5SAKASHI Takahiro if fs_img: 42671f27af5SAKASHI Takahiro call('rm -f %s' % fs_img, shell=True) 42750ca19ccSAKASHI Takahiro 42850ca19ccSAKASHI Takahiro# 42950ca19ccSAKASHI Takahiro# Fixture for mkdir test 43050ca19ccSAKASHI Takahiro# 43150ca19ccSAKASHI Takahiro# NOTE: yield_fixture was deprecated since pytest-3.0 43250ca19ccSAKASHI Takahiro@pytest.yield_fixture() 43350ca19ccSAKASHI Takahirodef fs_obj_mkdir(request, u_boot_config): 434c906f372SAkashi Takahiro """Set up a file system to be used in mkdir test. 435c906f372SAkashi Takahiro 436c906f372SAkashi Takahiro Args: 437c906f372SAkashi Takahiro request: Pytest request object. 438c906f372SAkashi Takahiro u_boot_config: U-boot configuration. 439c906f372SAkashi Takahiro 440c906f372SAkashi Takahiro Return: 441c906f372SAkashi Takahiro A fixture for mkdir test, i.e. a duplet of file system type and 442c906f372SAkashi Takahiro volume file name. 443c906f372SAkashi Takahiro """ 44450ca19ccSAKASHI Takahiro fs_type = request.param 44550ca19ccSAKASHI Takahiro fs_img = '' 44650ca19ccSAKASHI Takahiro 44750ca19ccSAKASHI Takahiro fs_ubtype = fstype_to_ubname(fs_type) 44850ca19ccSAKASHI Takahiro check_ubconfig(u_boot_config, fs_ubtype) 44950ca19ccSAKASHI Takahiro 45050ca19ccSAKASHI Takahiro try: 45150ca19ccSAKASHI Takahiro # 128MiB volume 45250ca19ccSAKASHI Takahiro fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB') 45350ca19ccSAKASHI Takahiro except: 45450ca19ccSAKASHI Takahiro pytest.skip('Setup failed for filesystem: ' + fs_type) 45550ca19ccSAKASHI Takahiro else: 45650ca19ccSAKASHI Takahiro yield [fs_ubtype, fs_img] 45750ca19ccSAKASHI Takahiro finally: 45850ca19ccSAKASHI Takahiro if fs_img: 45950ca19ccSAKASHI Takahiro call('rm -f %s' % fs_img, shell=True) 460cce289a9SAkashi, Takahiro 461cce289a9SAkashi, Takahiro# 462cce289a9SAkashi, Takahiro# Fixture for unlink test 463cce289a9SAkashi, Takahiro# 464cce289a9SAkashi, Takahiro# NOTE: yield_fixture was deprecated since pytest-3.0 465cce289a9SAkashi, Takahiro@pytest.yield_fixture() 466cce289a9SAkashi, Takahirodef fs_obj_unlink(request, u_boot_config): 467c906f372SAkashi Takahiro """Set up a file system to be used in unlink test. 468c906f372SAkashi Takahiro 469c906f372SAkashi Takahiro Args: 470c906f372SAkashi Takahiro request: Pytest request object. 471c906f372SAkashi Takahiro u_boot_config: U-boot configuration. 472c906f372SAkashi Takahiro 473c906f372SAkashi Takahiro Return: 474c906f372SAkashi Takahiro A fixture for unlink test, i.e. a duplet of file system type and 475c906f372SAkashi Takahiro volume file name. 476c906f372SAkashi Takahiro """ 477cce289a9SAkashi, Takahiro fs_type = request.param 478cce289a9SAkashi, Takahiro fs_img = '' 479cce289a9SAkashi, Takahiro 480cce289a9SAkashi, Takahiro fs_ubtype = fstype_to_ubname(fs_type) 481cce289a9SAkashi, Takahiro check_ubconfig(u_boot_config, fs_ubtype) 482cce289a9SAkashi, Takahiro 483cce289a9SAkashi, Takahiro mount_dir = u_boot_config.persistent_data_dir + '/mnt' 484cce289a9SAkashi, Takahiro 485cce289a9SAkashi, Takahiro try: 486cce289a9SAkashi, Takahiro 487cce289a9SAkashi, Takahiro # 128MiB volume 488cce289a9SAkashi, Takahiro fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB') 489cce289a9SAkashi, Takahiro 490cce289a9SAkashi, Takahiro # Mount the image so we can populate it. 491cce289a9SAkashi, Takahiro check_call('mkdir -p %s' % mount_dir, shell=True) 492cce289a9SAkashi, Takahiro mount_fs(fs_type, fs_img, mount_dir) 493cce289a9SAkashi, Takahiro 494cce289a9SAkashi, Takahiro # Test Case 1 & 3 495cce289a9SAkashi, Takahiro check_call('mkdir %s/dir1' % mount_dir, shell=True) 496cce289a9SAkashi, Takahiro check_call('dd if=/dev/urandom of=%s/dir1/file1 bs=1K count=1' 497cce289a9SAkashi, Takahiro % mount_dir, shell=True) 498cce289a9SAkashi, Takahiro check_call('dd if=/dev/urandom of=%s/dir1/file2 bs=1K count=1' 499cce289a9SAkashi, Takahiro % mount_dir, shell=True) 500cce289a9SAkashi, Takahiro 501cce289a9SAkashi, Takahiro # Test Case 2 502cce289a9SAkashi, Takahiro check_call('mkdir %s/dir2' % mount_dir, shell=True) 503cce289a9SAkashi, Takahiro for i in range(0, 20): 504cce289a9SAkashi, Takahiro check_call('mkdir %s/dir2/0123456789abcdef%02x' 505cce289a9SAkashi, Takahiro % (mount_dir, i), shell=True) 506cce289a9SAkashi, Takahiro 507cce289a9SAkashi, Takahiro # Test Case 4 508cce289a9SAkashi, Takahiro check_call('mkdir %s/dir4' % mount_dir, shell=True) 509cce289a9SAkashi, Takahiro 510cce289a9SAkashi, Takahiro # Test Case 5, 6 & 7 511cce289a9SAkashi, Takahiro check_call('mkdir %s/dir5' % mount_dir, shell=True) 512cce289a9SAkashi, Takahiro check_call('dd if=/dev/urandom of=%s/dir5/file1 bs=1K count=1' 513cce289a9SAkashi, Takahiro % mount_dir, shell=True) 514cce289a9SAkashi, Takahiro 515e4040df5SAkashi Takahiro umount_fs(mount_dir) 516cce289a9SAkashi, Takahiro except CalledProcessError: 517cce289a9SAkashi, Takahiro pytest.skip('Setup failed for filesystem: ' + fs_type) 518cce289a9SAkashi, Takahiro return 519cce289a9SAkashi, Takahiro else: 520cce289a9SAkashi, Takahiro yield [fs_ubtype, fs_img] 521cce289a9SAkashi, Takahiro finally: 522e4040df5SAkashi Takahiro umount_fs(mount_dir) 523cce289a9SAkashi, Takahiro call('rmdir %s' % mount_dir, shell=True) 524cce289a9SAkashi, Takahiro if fs_img: 525cce289a9SAkashi, Takahiro call('rm -f %s' % fs_img, shell=True) 526