AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # Copyright (c) 2018, Linaro Limited |
| 3 | # Author: Takahiro Akashi <takahiro.akashi@linaro.org> |
| 4 | |
| 5 | import os |
| 6 | import os.path |
| 7 | import pytest |
| 8 | import re |
| 9 | from subprocess import call, check_call, check_output, CalledProcessError |
| 10 | from fstest_defs import * |
| 11 | |
| 12 | supported_fs_basic = ['fat16', 'fat32', 'ext4'] |
AKASHI Takahiro | dde5d3f | 2018-09-11 15:59:20 +0900 | [diff] [blame] | 13 | supported_fs_ext = ['fat16', 'fat32'] |
AKASHI Takahiro | 1e90c2c | 2018-09-11 15:59:21 +0900 | [diff] [blame] | 14 | supported_fs_mkdir = ['fat16', 'fat32'] |
Akashi, Takahiro | d49e799 | 2018-09-11 16:06:03 +0900 | [diff] [blame] | 15 | supported_fs_unlink = ['fat16', 'fat32'] |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 16 | |
| 17 | # |
| 18 | # Filesystem test specific setup |
| 19 | # |
| 20 | def pytest_addoption(parser): |
Akashi Takahiro | bcdd1f2 | 2018-09-27 16:07:23 +0900 | [diff] [blame] | 21 | """Enable --fs-type option. |
| 22 | |
| 23 | See pytest_configure() about how it works. |
| 24 | |
| 25 | Args: |
| 26 | parser: Pytest command-line parser. |
| 27 | |
| 28 | Returns: |
| 29 | Nothing. |
| 30 | """ |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 31 | parser.addoption('--fs-type', action='append', default=None, |
| 32 | help='Targeting Filesystem Types') |
| 33 | |
| 34 | def pytest_configure(config): |
Akashi Takahiro | bcdd1f2 | 2018-09-27 16:07:23 +0900 | [diff] [blame] | 35 | """Restrict a file system(s) to be tested. |
| 36 | |
| 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. |
| 40 | |
| 41 | Args: |
| 42 | config: Pytest configuration. |
| 43 | |
| 44 | Returns: |
| 45 | Nothing. |
| 46 | """ |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 47 | global supported_fs_basic |
AKASHI Takahiro | dde5d3f | 2018-09-11 15:59:20 +0900 | [diff] [blame] | 48 | global supported_fs_ext |
AKASHI Takahiro | 1e90c2c | 2018-09-11 15:59:21 +0900 | [diff] [blame] | 49 | global supported_fs_mkdir |
Akashi, Takahiro | d49e799 | 2018-09-11 16:06:03 +0900 | [diff] [blame] | 50 | global supported_fs_unlink |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 51 | |
| 52 | def intersect(listA, listB): |
| 53 | return [x for x in listA if x in listB] |
| 54 | |
| 55 | supported_fs = config.getoption('fs_type') |
| 56 | if supported_fs: |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 57 | print('*** FS TYPE modified: %s' % supported_fs) |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 58 | supported_fs_basic = intersect(supported_fs, supported_fs_basic) |
AKASHI Takahiro | dde5d3f | 2018-09-11 15:59:20 +0900 | [diff] [blame] | 59 | supported_fs_ext = intersect(supported_fs, supported_fs_ext) |
AKASHI Takahiro | 1e90c2c | 2018-09-11 15:59:21 +0900 | [diff] [blame] | 60 | supported_fs_mkdir = intersect(supported_fs, supported_fs_mkdir) |
Akashi, Takahiro | d49e799 | 2018-09-11 16:06:03 +0900 | [diff] [blame] | 61 | supported_fs_unlink = intersect(supported_fs, supported_fs_unlink) |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 62 | |
| 63 | def pytest_generate_tests(metafunc): |
Akashi Takahiro | bcdd1f2 | 2018-09-27 16:07:23 +0900 | [diff] [blame] | 64 | """Parametrize fixtures, fs_obj_xxx |
| 65 | |
| 66 | Each fixture will be parametrized with a corresponding support_fs_xxx |
| 67 | list. |
| 68 | |
| 69 | Args: |
| 70 | metafunc: Pytest test function. |
| 71 | |
| 72 | Returns: |
| 73 | Nothing. |
| 74 | """ |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 75 | if 'fs_obj_basic' in metafunc.fixturenames: |
| 76 | metafunc.parametrize('fs_obj_basic', supported_fs_basic, |
| 77 | indirect=True, scope='module') |
AKASHI Takahiro | dde5d3f | 2018-09-11 15:59:20 +0900 | [diff] [blame] | 78 | if 'fs_obj_ext' in metafunc.fixturenames: |
| 79 | metafunc.parametrize('fs_obj_ext', supported_fs_ext, |
| 80 | indirect=True, scope='module') |
AKASHI Takahiro | 1e90c2c | 2018-09-11 15:59:21 +0900 | [diff] [blame] | 81 | if 'fs_obj_mkdir' in metafunc.fixturenames: |
| 82 | metafunc.parametrize('fs_obj_mkdir', supported_fs_mkdir, |
| 83 | indirect=True, scope='module') |
Akashi, Takahiro | d49e799 | 2018-09-11 16:06:03 +0900 | [diff] [blame] | 84 | if 'fs_obj_unlink' in metafunc.fixturenames: |
| 85 | metafunc.parametrize('fs_obj_unlink', supported_fs_unlink, |
| 86 | indirect=True, scope='module') |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 87 | |
| 88 | # |
| 89 | # Helper functions |
| 90 | # |
| 91 | def fstype_to_ubname(fs_type): |
Akashi Takahiro | bcdd1f2 | 2018-09-27 16:07:23 +0900 | [diff] [blame] | 92 | """Convert a file system type to an U-boot specific string |
| 93 | |
| 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 |
| 96 | specifically. |
| 97 | |
| 98 | Args: |
| 99 | fs_type: File system type. |
| 100 | |
| 101 | Return: |
| 102 | A corresponding string for file system type. |
| 103 | """ |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 104 | if re.match('fat', fs_type): |
| 105 | return 'fat' |
| 106 | else: |
| 107 | return fs_type |
| 108 | |
| 109 | def check_ubconfig(config, fs_type): |
Akashi Takahiro | bcdd1f2 | 2018-09-27 16:07:23 +0900 | [diff] [blame] | 110 | """Check whether a file system is enabled in u-boot configuration. |
| 111 | |
| 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 |
| 114 | enabled. |
| 115 | |
| 116 | Args: |
| 117 | fs_type: File system type. |
| 118 | |
| 119 | Return: |
| 120 | Nothing. |
| 121 | """ |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 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' |
| 126 | % fs_type.upper()) |
| 127 | |
| 128 | def mk_fs(config, fs_type, size, id): |
Akashi Takahiro | bcdd1f2 | 2018-09-27 16:07:23 +0900 | [diff] [blame] | 129 | """Create a file system volume. |
| 130 | |
| 131 | Args: |
| 132 | fs_type: File system type. |
| 133 | size: Size of file system in MiB. |
| 134 | id: Prefix string of volume's file name. |
| 135 | |
| 136 | Return: |
| 137 | Nothing. |
| 138 | """ |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 139 | fs_img = '%s.%s.img' % (id, fs_type) |
| 140 | fs_img = config.persistent_data_dir + '/' + fs_img |
| 141 | |
| 142 | if fs_type == 'fat16': |
| 143 | mkfs_opt = '-F 16' |
| 144 | elif fs_type == 'fat32': |
| 145 | mkfs_opt = '-F 32' |
Jean-Jacques Hiblot | 5390ced | 2019-02-13 12:15:22 +0100 | [diff] [blame^] | 146 | elif fs_type == 'ext4': |
| 147 | mkfs_opt = '-O ^metadata_csum' |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 148 | else: |
| 149 | mkfs_opt = '' |
| 150 | |
| 151 | if re.match('fat', fs_type): |
| 152 | fs_lnxtype = 'vfat' |
| 153 | else: |
| 154 | fs_lnxtype = fs_type |
| 155 | |
| 156 | count = (size + 1048576 - 1) / 1048576 |
| 157 | |
| 158 | try: |
| 159 | check_call('rm -f %s' % fs_img, shell=True) |
| 160 | check_call('dd if=/dev/zero of=%s bs=1M count=%d' |
| 161 | % (fs_img, count), shell=True) |
| 162 | check_call('mkfs.%s %s %s' |
| 163 | % (fs_lnxtype, mkfs_opt, fs_img), shell=True) |
| 164 | return fs_img |
| 165 | except CalledProcessError: |
| 166 | call('rm -f %s' % fs_img, shell=True) |
| 167 | raise |
| 168 | |
| 169 | # from test/py/conftest.py |
| 170 | def tool_is_in_path(tool): |
Akashi Takahiro | bcdd1f2 | 2018-09-27 16:07:23 +0900 | [diff] [blame] | 171 | """Check whether a given command is available on host. |
| 172 | |
| 173 | Args: |
| 174 | tool: Command name. |
| 175 | |
| 176 | Return: |
| 177 | True if available, False if not. |
| 178 | """ |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 179 | for path in os.environ['PATH'].split(os.pathsep): |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 180 | fn = os.path.join(path, tool) |
| 181 | if os.path.isfile(fn) and os.access(fn, os.X_OK): |
| 182 | return True |
| 183 | return False |
| 184 | |
| 185 | fuse_mounted = False |
| 186 | |
| 187 | def mount_fs(fs_type, device, mount_point): |
Akashi Takahiro | bcdd1f2 | 2018-09-27 16:07:23 +0900 | [diff] [blame] | 188 | """Mount a volume. |
| 189 | |
| 190 | Args: |
| 191 | fs_type: File system type. |
| 192 | device: Volume's file name. |
| 193 | mount_point: Mount point. |
| 194 | |
| 195 | Return: |
| 196 | Nothing. |
| 197 | """ |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 198 | global fuse_mounted |
| 199 | |
| 200 | fuse_mounted = False |
| 201 | try: |
| 202 | if tool_is_in_path('guestmount'): |
| 203 | fuse_mounted = True |
| 204 | check_call('guestmount -a %s -m /dev/sda %s' |
| 205 | % (device, mount_point), shell=True) |
| 206 | else: |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 207 | mount_opt = 'loop,rw' |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 208 | if re.match('fat', fs_type): |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 209 | mount_opt += ',umask=0000' |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 210 | |
| 211 | check_call('sudo mount -o %s %s %s' |
| 212 | % (mount_opt, device, mount_point), shell=True) |
| 213 | |
| 214 | # may not be effective for some file systems |
| 215 | check_call('sudo chmod a+rw %s' % mount_point, shell=True) |
| 216 | except CalledProcessError: |
| 217 | raise |
| 218 | |
Akashi Takahiro | 89101f8 | 2018-09-27 16:07:22 +0900 | [diff] [blame] | 219 | def umount_fs(mount_point): |
Akashi Takahiro | bcdd1f2 | 2018-09-27 16:07:23 +0900 | [diff] [blame] | 220 | """Unmount a volume. |
| 221 | |
| 222 | Args: |
| 223 | mount_point: Mount point. |
| 224 | |
| 225 | Return: |
| 226 | Nothing. |
| 227 | """ |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 228 | if fuse_mounted: |
| 229 | call('sync') |
| 230 | call('guestunmount %s' % mount_point, shell=True) |
| 231 | else: |
| 232 | call('sudo umount %s' % mount_point, shell=True) |
| 233 | |
| 234 | # |
| 235 | # Fixture for basic fs test |
| 236 | # derived from test/fs/fs-test.sh |
| 237 | # |
| 238 | # NOTE: yield_fixture was deprecated since pytest-3.0 |
| 239 | @pytest.yield_fixture() |
| 240 | def fs_obj_basic(request, u_boot_config): |
Akashi Takahiro | bcdd1f2 | 2018-09-27 16:07:23 +0900 | [diff] [blame] | 241 | """Set up a file system to be used in basic fs test. |
| 242 | |
| 243 | Args: |
| 244 | request: Pytest request object. |
| 245 | u_boot_config: U-boot configuration. |
| 246 | |
| 247 | Return: |
| 248 | A fixture for basic fs test, i.e. a triplet of file system type, |
| 249 | volume file name and a list of MD5 hashes. |
| 250 | """ |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 251 | fs_type = request.param |
| 252 | fs_img = '' |
| 253 | |
| 254 | fs_ubtype = fstype_to_ubname(fs_type) |
| 255 | check_ubconfig(u_boot_config, fs_ubtype) |
| 256 | |
| 257 | mount_dir = u_boot_config.persistent_data_dir + '/mnt' |
| 258 | |
| 259 | small_file = mount_dir + '/' + SMALL_FILE |
| 260 | big_file = mount_dir + '/' + BIG_FILE |
| 261 | |
| 262 | try: |
| 263 | |
| 264 | # 3GiB volume |
| 265 | fs_img = mk_fs(u_boot_config, fs_type, 0xc0000000, '3GB') |
| 266 | |
| 267 | # Mount the image so we can populate it. |
| 268 | check_call('mkdir -p %s' % mount_dir, shell=True) |
| 269 | mount_fs(fs_type, fs_img, mount_dir) |
| 270 | |
| 271 | # Create a subdirectory. |
| 272 | check_call('mkdir %s/SUBDIR' % mount_dir, shell=True) |
| 273 | |
| 274 | # Create big file in this image. |
| 275 | # Note that we work only on the start 1MB, couple MBs in the 2GB range |
| 276 | # and the last 1 MB of the huge 2.5GB file. |
| 277 | # So, just put random values only in those areas. |
| 278 | check_call('dd if=/dev/urandom of=%s bs=1M count=1' |
| 279 | % big_file, shell=True) |
| 280 | check_call('dd if=/dev/urandom of=%s bs=1M count=2 seek=2047' |
| 281 | % big_file, shell=True) |
| 282 | check_call('dd if=/dev/urandom of=%s bs=1M count=1 seek=2499' |
| 283 | % big_file, shell=True) |
| 284 | |
| 285 | # Create a small file in this image. |
| 286 | check_call('dd if=/dev/urandom of=%s bs=1M count=1' |
| 287 | % small_file, shell=True) |
| 288 | |
| 289 | # Delete the small file copies which possibly are written as part of a |
| 290 | # previous test. |
| 291 | # check_call('rm -f "%s.w"' % MB1, shell=True) |
| 292 | # check_call('rm -f "%s.w2"' % MB1, shell=True) |
| 293 | |
| 294 | # Generate the md5sums of reads that we will test against small file |
| 295 | out = check_output( |
| 296 | 'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum' |
| 297 | % small_file, shell=True) |
| 298 | md5val = [ out.split()[0] ] |
| 299 | |
| 300 | # Generate the md5sums of reads that we will test against big file |
| 301 | # One from beginning of file. |
| 302 | out = check_output( |
| 303 | 'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum' |
| 304 | % big_file, shell=True) |
| 305 | md5val.append(out.split()[0]) |
| 306 | |
| 307 | # One from end of file. |
| 308 | out = check_output( |
| 309 | 'dd if=%s bs=1M skip=2499 count=1 2> /dev/null | md5sum' |
| 310 | % big_file, shell=True) |
| 311 | md5val.append(out.split()[0]) |
| 312 | |
| 313 | # One from the last 1MB chunk of 2GB |
| 314 | out = check_output( |
| 315 | 'dd if=%s bs=1M skip=2047 count=1 2> /dev/null | md5sum' |
| 316 | % big_file, shell=True) |
| 317 | md5val.append(out.split()[0]) |
| 318 | |
| 319 | # One from the start 1MB chunk from 2GB |
| 320 | out = check_output( |
| 321 | 'dd if=%s bs=1M skip=2048 count=1 2> /dev/null | md5sum' |
| 322 | % big_file, shell=True) |
| 323 | md5val.append(out.split()[0]) |
| 324 | |
| 325 | # One 1MB chunk crossing the 2GB boundary |
| 326 | out = check_output( |
| 327 | 'dd if=%s bs=512K skip=4095 count=2 2> /dev/null | md5sum' |
| 328 | % big_file, shell=True) |
| 329 | md5val.append(out.split()[0]) |
| 330 | |
Akashi Takahiro | 89101f8 | 2018-09-27 16:07:22 +0900 | [diff] [blame] | 331 | umount_fs(mount_dir) |
AKASHI Takahiro | dde5d3f | 2018-09-11 15:59:20 +0900 | [diff] [blame] | 332 | except CalledProcessError: |
| 333 | pytest.skip('Setup failed for filesystem: ' + fs_type) |
| 334 | return |
| 335 | else: |
| 336 | yield [fs_ubtype, fs_img, md5val] |
| 337 | finally: |
Akashi Takahiro | 89101f8 | 2018-09-27 16:07:22 +0900 | [diff] [blame] | 338 | umount_fs(mount_dir) |
AKASHI Takahiro | dde5d3f | 2018-09-11 15:59:20 +0900 | [diff] [blame] | 339 | call('rmdir %s' % mount_dir, shell=True) |
| 340 | if fs_img: |
| 341 | call('rm -f %s' % fs_img, shell=True) |
| 342 | |
| 343 | # |
| 344 | # Fixture for extended fs test |
| 345 | # |
| 346 | # NOTE: yield_fixture was deprecated since pytest-3.0 |
| 347 | @pytest.yield_fixture() |
| 348 | def fs_obj_ext(request, u_boot_config): |
Akashi Takahiro | bcdd1f2 | 2018-09-27 16:07:23 +0900 | [diff] [blame] | 349 | """Set up a file system to be used in extended fs test. |
| 350 | |
| 351 | Args: |
| 352 | request: Pytest request object. |
| 353 | u_boot_config: U-boot configuration. |
| 354 | |
| 355 | Return: |
| 356 | A fixture for extended fs test, i.e. a triplet of file system type, |
| 357 | volume file name and a list of MD5 hashes. |
| 358 | """ |
AKASHI Takahiro | dde5d3f | 2018-09-11 15:59:20 +0900 | [diff] [blame] | 359 | fs_type = request.param |
| 360 | fs_img = '' |
| 361 | |
| 362 | fs_ubtype = fstype_to_ubname(fs_type) |
| 363 | check_ubconfig(u_boot_config, fs_ubtype) |
| 364 | |
| 365 | mount_dir = u_boot_config.persistent_data_dir + '/mnt' |
| 366 | |
| 367 | min_file = mount_dir + '/' + MIN_FILE |
| 368 | tmp_file = mount_dir + '/tmpfile' |
| 369 | |
| 370 | try: |
| 371 | |
| 372 | # 128MiB volume |
| 373 | fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB') |
| 374 | |
| 375 | # Mount the image so we can populate it. |
| 376 | check_call('mkdir -p %s' % mount_dir, shell=True) |
| 377 | mount_fs(fs_type, fs_img, mount_dir) |
| 378 | |
| 379 | # Create a test directory |
| 380 | check_call('mkdir %s/dir1' % mount_dir, shell=True) |
| 381 | |
| 382 | # Create a small file and calculate md5 |
| 383 | check_call('dd if=/dev/urandom of=%s bs=1K count=20' |
| 384 | % min_file, shell=True) |
| 385 | out = check_output( |
| 386 | 'dd if=%s bs=1K 2> /dev/null | md5sum' |
| 387 | % min_file, shell=True) |
| 388 | md5val = [ out.split()[0] ] |
| 389 | |
| 390 | # Calculate md5sum of Test Case 4 |
| 391 | check_call('dd if=%s of=%s bs=1K count=20' |
| 392 | % (min_file, tmp_file), shell=True) |
| 393 | check_call('dd if=%s of=%s bs=1K seek=5 count=20' |
| 394 | % (min_file, tmp_file), shell=True) |
| 395 | out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum' |
| 396 | % tmp_file, shell=True) |
| 397 | md5val.append(out.split()[0]) |
| 398 | |
| 399 | # Calculate md5sum of Test Case 5 |
| 400 | check_call('dd if=%s of=%s bs=1K count=20' |
| 401 | % (min_file, tmp_file), shell=True) |
| 402 | check_call('dd if=%s of=%s bs=1K seek=5 count=5' |
| 403 | % (min_file, tmp_file), shell=True) |
| 404 | out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum' |
| 405 | % tmp_file, shell=True) |
| 406 | md5val.append(out.split()[0]) |
| 407 | |
| 408 | # Calculate md5sum of Test Case 7 |
| 409 | check_call('dd if=%s of=%s bs=1K count=20' |
| 410 | % (min_file, tmp_file), shell=True) |
| 411 | check_call('dd if=%s of=%s bs=1K seek=20 count=20' |
| 412 | % (min_file, tmp_file), shell=True) |
| 413 | out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum' |
| 414 | % tmp_file, shell=True) |
| 415 | md5val.append(out.split()[0]) |
| 416 | |
| 417 | check_call('rm %s' % tmp_file, shell=True) |
Akashi Takahiro | 89101f8 | 2018-09-27 16:07:22 +0900 | [diff] [blame] | 418 | umount_fs(mount_dir) |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 419 | except CalledProcessError: |
| 420 | pytest.skip('Setup failed for filesystem: ' + fs_type) |
| 421 | return |
| 422 | else: |
| 423 | yield [fs_ubtype, fs_img, md5val] |
| 424 | finally: |
Akashi Takahiro | 89101f8 | 2018-09-27 16:07:22 +0900 | [diff] [blame] | 425 | umount_fs(mount_dir) |
AKASHI Takahiro | 615af9a | 2018-09-11 15:59:19 +0900 | [diff] [blame] | 426 | call('rmdir %s' % mount_dir, shell=True) |
| 427 | if fs_img: |
| 428 | call('rm -f %s' % fs_img, shell=True) |
AKASHI Takahiro | 1e90c2c | 2018-09-11 15:59:21 +0900 | [diff] [blame] | 429 | |
| 430 | # |
| 431 | # Fixture for mkdir test |
| 432 | # |
| 433 | # NOTE: yield_fixture was deprecated since pytest-3.0 |
| 434 | @pytest.yield_fixture() |
| 435 | def fs_obj_mkdir(request, u_boot_config): |
Akashi Takahiro | bcdd1f2 | 2018-09-27 16:07:23 +0900 | [diff] [blame] | 436 | """Set up a file system to be used in mkdir test. |
| 437 | |
| 438 | Args: |
| 439 | request: Pytest request object. |
| 440 | u_boot_config: U-boot configuration. |
| 441 | |
| 442 | Return: |
| 443 | A fixture for mkdir test, i.e. a duplet of file system type and |
| 444 | volume file name. |
| 445 | """ |
AKASHI Takahiro | 1e90c2c | 2018-09-11 15:59:21 +0900 | [diff] [blame] | 446 | fs_type = request.param |
| 447 | fs_img = '' |
| 448 | |
| 449 | fs_ubtype = fstype_to_ubname(fs_type) |
| 450 | check_ubconfig(u_boot_config, fs_ubtype) |
| 451 | |
| 452 | try: |
| 453 | # 128MiB volume |
| 454 | fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB') |
| 455 | except: |
| 456 | pytest.skip('Setup failed for filesystem: ' + fs_type) |
| 457 | else: |
| 458 | yield [fs_ubtype, fs_img] |
| 459 | finally: |
| 460 | if fs_img: |
| 461 | call('rm -f %s' % fs_img, shell=True) |
Akashi, Takahiro | d49e799 | 2018-09-11 16:06:03 +0900 | [diff] [blame] | 462 | |
| 463 | # |
| 464 | # Fixture for unlink test |
| 465 | # |
| 466 | # NOTE: yield_fixture was deprecated since pytest-3.0 |
| 467 | @pytest.yield_fixture() |
| 468 | def fs_obj_unlink(request, u_boot_config): |
Akashi Takahiro | bcdd1f2 | 2018-09-27 16:07:23 +0900 | [diff] [blame] | 469 | """Set up a file system to be used in unlink test. |
| 470 | |
| 471 | Args: |
| 472 | request: Pytest request object. |
| 473 | u_boot_config: U-boot configuration. |
| 474 | |
| 475 | Return: |
| 476 | A fixture for unlink test, i.e. a duplet of file system type and |
| 477 | volume file name. |
| 478 | """ |
Akashi, Takahiro | d49e799 | 2018-09-11 16:06:03 +0900 | [diff] [blame] | 479 | fs_type = request.param |
| 480 | fs_img = '' |
| 481 | |
| 482 | fs_ubtype = fstype_to_ubname(fs_type) |
| 483 | check_ubconfig(u_boot_config, fs_ubtype) |
| 484 | |
| 485 | mount_dir = u_boot_config.persistent_data_dir + '/mnt' |
| 486 | |
| 487 | try: |
| 488 | |
| 489 | # 128MiB volume |
| 490 | fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB') |
| 491 | |
| 492 | # Mount the image so we can populate it. |
| 493 | check_call('mkdir -p %s' % mount_dir, shell=True) |
| 494 | mount_fs(fs_type, fs_img, mount_dir) |
| 495 | |
| 496 | # Test Case 1 & 3 |
| 497 | check_call('mkdir %s/dir1' % mount_dir, shell=True) |
| 498 | check_call('dd if=/dev/urandom of=%s/dir1/file1 bs=1K count=1' |
| 499 | % mount_dir, shell=True) |
| 500 | check_call('dd if=/dev/urandom of=%s/dir1/file2 bs=1K count=1' |
| 501 | % mount_dir, shell=True) |
| 502 | |
| 503 | # Test Case 2 |
| 504 | check_call('mkdir %s/dir2' % mount_dir, shell=True) |
| 505 | for i in range(0, 20): |
| 506 | check_call('mkdir %s/dir2/0123456789abcdef%02x' |
| 507 | % (mount_dir, i), shell=True) |
| 508 | |
| 509 | # Test Case 4 |
| 510 | check_call('mkdir %s/dir4' % mount_dir, shell=True) |
| 511 | |
| 512 | # Test Case 5, 6 & 7 |
| 513 | check_call('mkdir %s/dir5' % mount_dir, shell=True) |
| 514 | check_call('dd if=/dev/urandom of=%s/dir5/file1 bs=1K count=1' |
| 515 | % mount_dir, shell=True) |
| 516 | |
Akashi Takahiro | 89101f8 | 2018-09-27 16:07:22 +0900 | [diff] [blame] | 517 | umount_fs(mount_dir) |
Akashi, Takahiro | d49e799 | 2018-09-11 16:06:03 +0900 | [diff] [blame] | 518 | except CalledProcessError: |
| 519 | pytest.skip('Setup failed for filesystem: ' + fs_type) |
| 520 | return |
| 521 | else: |
| 522 | yield [fs_ubtype, fs_img] |
| 523 | finally: |
Akashi Takahiro | 89101f8 | 2018-09-27 16:07:22 +0900 | [diff] [blame] | 524 | umount_fs(mount_dir) |
Akashi, Takahiro | d49e799 | 2018-09-11 16:06:03 +0900 | [diff] [blame] | 525 | call('rmdir %s' % mount_dir, shell=True) |
| 526 | if fs_img: |
| 527 | call('rm -f %s' % fs_img, shell=True) |