blob: 745ed0ed3817f890bacf8dccfc6831f45abc60c2 [file] [log] [blame]
AKASHI Takahiro615af9a2018-09-11 15:59:19 +09001# SPDX-License-Identifier: GPL-2.0+
2# Copyright (c) 2018, Linaro Limited
3# Author: Takahiro Akashi <takahiro.akashi@linaro.org>
4
5import os
6import os.path
7import pytest
8import re
9from subprocess import call, check_call, check_output, CalledProcessError
10from fstest_defs import *
11
12supported_fs_basic = ['fat16', 'fat32', 'ext4']
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +090013supported_fs_ext = ['fat16', 'fat32']
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +090014supported_fs_mkdir = ['fat16', 'fat32']
Akashi, Takahirod49e7992018-09-11 16:06:03 +090015supported_fs_unlink = ['fat16', 'fat32']
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090016
17#
18# Filesystem test specific setup
19#
20def pytest_addoption(parser):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +090021 """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 Takahiro615af9a2018-09-11 15:59:19 +090031 parser.addoption('--fs-type', action='append', default=None,
32 help='Targeting Filesystem Types')
33
34def pytest_configure(config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +090035 """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 Takahiro615af9a2018-09-11 15:59:19 +090047 global supported_fs_basic
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +090048 global supported_fs_ext
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +090049 global supported_fs_mkdir
Akashi, Takahirod49e7992018-09-11 16:06:03 +090050 global supported_fs_unlink
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090051
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 Glasse9f4d872018-12-27 08:11:13 -070057 print('*** FS TYPE modified: %s' % supported_fs)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090058 supported_fs_basic = intersect(supported_fs, supported_fs_basic)
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +090059 supported_fs_ext = intersect(supported_fs, supported_fs_ext)
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +090060 supported_fs_mkdir = intersect(supported_fs, supported_fs_mkdir)
Akashi, Takahirod49e7992018-09-11 16:06:03 +090061 supported_fs_unlink = intersect(supported_fs, supported_fs_unlink)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090062
63def pytest_generate_tests(metafunc):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +090064 """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 Takahiro615af9a2018-09-11 15:59:19 +090075 if 'fs_obj_basic' in metafunc.fixturenames:
76 metafunc.parametrize('fs_obj_basic', supported_fs_basic,
77 indirect=True, scope='module')
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +090078 if 'fs_obj_ext' in metafunc.fixturenames:
79 metafunc.parametrize('fs_obj_ext', supported_fs_ext,
80 indirect=True, scope='module')
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +090081 if 'fs_obj_mkdir' in metafunc.fixturenames:
82 metafunc.parametrize('fs_obj_mkdir', supported_fs_mkdir,
83 indirect=True, scope='module')
Akashi, Takahirod49e7992018-09-11 16:06:03 +090084 if 'fs_obj_unlink' in metafunc.fixturenames:
85 metafunc.parametrize('fs_obj_unlink', supported_fs_unlink,
86 indirect=True, scope='module')
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090087
88#
89# Helper functions
90#
91def fstype_to_ubname(fs_type):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +090092 """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 Takahiro615af9a2018-09-11 15:59:19 +0900104 if re.match('fat', fs_type):
105 return 'fat'
106 else:
107 return fs_type
108
109def check_ubconfig(config, fs_type):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900110 """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 Takahiro615af9a2018-09-11 15:59:19 +0900122 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
128def mk_fs(config, fs_type, size, id):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900129 """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 Takahiro615af9a2018-09-11 15:59:19 +0900139 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 Hiblot5390ced2019-02-13 12:15:22 +0100146 elif fs_type == 'ext4':
147 mkfs_opt = '-O ^metadata_csum'
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900148 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
170def tool_is_in_path(tool):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900171 """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 Glasse9f4d872018-12-27 08:11:13 -0700179 for path in os.environ['PATH'].split(os.pathsep):
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900180 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
185fuse_mounted = False
186
187def mount_fs(fs_type, device, mount_point):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900188 """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 Takahiro615af9a2018-09-11 15:59:19 +0900198 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 Glasse9f4d872018-12-27 08:11:13 -0700207 mount_opt = 'loop,rw'
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900208 if re.match('fat', fs_type):
Simon Glasse9f4d872018-12-27 08:11:13 -0700209 mount_opt += ',umask=0000'
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900210
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 Takahiro89101f82018-09-27 16:07:22 +0900219def umount_fs(mount_point):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900220 """Unmount a volume.
221
222 Args:
223 mount_point: Mount point.
224
225 Return:
226 Nothing.
227 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900228 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()
240def fs_obj_basic(request, u_boot_config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900241 """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 Takahiro615af9a2018-09-11 15:59:19 +0900251 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 Takahiro89101f82018-09-27 16:07:22 +0900331 umount_fs(mount_dir)
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900332 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 Takahiro89101f82018-09-27 16:07:22 +0900338 umount_fs(mount_dir)
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900339 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()
348def fs_obj_ext(request, u_boot_config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900349 """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 Takahirodde5d3f2018-09-11 15:59:20 +0900359 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 Takahiro89101f82018-09-27 16:07:22 +0900418 umount_fs(mount_dir)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900419 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 Takahiro89101f82018-09-27 16:07:22 +0900425 umount_fs(mount_dir)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900426 call('rmdir %s' % mount_dir, shell=True)
427 if fs_img:
428 call('rm -f %s' % fs_img, shell=True)
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +0900429
430#
431# Fixture for mkdir test
432#
433# NOTE: yield_fixture was deprecated since pytest-3.0
434@pytest.yield_fixture()
435def fs_obj_mkdir(request, u_boot_config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900436 """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 Takahiro1e90c2c2018-09-11 15:59:21 +0900446 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, Takahirod49e7992018-09-11 16:06:03 +0900462
463#
464# Fixture for unlink test
465#
466# NOTE: yield_fixture was deprecated since pytest-3.0
467@pytest.yield_fixture()
468def fs_obj_unlink(request, u_boot_config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900469 """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, Takahirod49e7992018-09-11 16:06:03 +0900479 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 Takahiro89101f82018-09-27 16:07:22 +0900517 umount_fs(mount_dir)
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900518 except CalledProcessError:
519 pytest.skip('Setup failed for filesystem: ' + fs_type)
520 return
521 else:
522 yield [fs_ubtype, fs_img]
523 finally:
Akashi Takahiro89101f82018-09-27 16:07:22 +0900524 umount_fs(mount_dir)
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900525 call('rmdir %s' % mount_dir, shell=True)
526 if fs_img:
527 call('rm -f %s' % fs_img, shell=True)