blob: 7325486cdb1a267286bdfcaca7c62e6bb63e5598 [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']
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +010016supported_fs_symlink = ['ext4']
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090017
18#
19# Filesystem test specific setup
20#
21def pytest_addoption(parser):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +090022 """Enable --fs-type option.
23
24 See pytest_configure() about how it works.
25
26 Args:
27 parser: Pytest command-line parser.
28
29 Returns:
30 Nothing.
31 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090032 parser.addoption('--fs-type', action='append', default=None,
33 help='Targeting Filesystem Types')
34
35def pytest_configure(config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +090036 """Restrict a file system(s) to be tested.
37
38 A file system explicitly named with --fs-type option is selected
39 if it belongs to a default supported_fs_xxx list.
40 Multiple options can be specified.
41
42 Args:
43 config: Pytest configuration.
44
45 Returns:
46 Nothing.
47 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090048 global supported_fs_basic
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +090049 global supported_fs_ext
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +090050 global supported_fs_mkdir
Akashi, Takahirod49e7992018-09-11 16:06:03 +090051 global supported_fs_unlink
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +010052 global supported_fs_symlink
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090053
54 def intersect(listA, listB):
55 return [x for x in listA if x in listB]
56
57 supported_fs = config.getoption('fs_type')
58 if supported_fs:
Simon Glasse9f4d872018-12-27 08:11:13 -070059 print('*** FS TYPE modified: %s' % supported_fs)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090060 supported_fs_basic = intersect(supported_fs, supported_fs_basic)
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +090061 supported_fs_ext = intersect(supported_fs, supported_fs_ext)
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +090062 supported_fs_mkdir = intersect(supported_fs, supported_fs_mkdir)
Akashi, Takahirod49e7992018-09-11 16:06:03 +090063 supported_fs_unlink = intersect(supported_fs, supported_fs_unlink)
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +010064 supported_fs_symlink = intersect(supported_fs, supported_fs_symlink)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090065
66def pytest_generate_tests(metafunc):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +090067 """Parametrize fixtures, fs_obj_xxx
68
69 Each fixture will be parametrized with a corresponding support_fs_xxx
70 list.
71
72 Args:
73 metafunc: Pytest test function.
74
75 Returns:
76 Nothing.
77 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090078 if 'fs_obj_basic' in metafunc.fixturenames:
79 metafunc.parametrize('fs_obj_basic', supported_fs_basic,
80 indirect=True, scope='module')
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +090081 if 'fs_obj_ext' in metafunc.fixturenames:
82 metafunc.parametrize('fs_obj_ext', supported_fs_ext,
83 indirect=True, scope='module')
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +090084 if 'fs_obj_mkdir' in metafunc.fixturenames:
85 metafunc.parametrize('fs_obj_mkdir', supported_fs_mkdir,
86 indirect=True, scope='module')
Akashi, Takahirod49e7992018-09-11 16:06:03 +090087 if 'fs_obj_unlink' in metafunc.fixturenames:
88 metafunc.parametrize('fs_obj_unlink', supported_fs_unlink,
89 indirect=True, scope='module')
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +010090 if 'fs_obj_symlink' in metafunc.fixturenames:
91 metafunc.parametrize('fs_obj_symlink', supported_fs_symlink,
92 indirect=True, scope='module')
AKASHI Takahiro615af9a2018-09-11 15:59:19 +090093
94#
95# Helper functions
96#
97def fstype_to_ubname(fs_type):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +090098 """Convert a file system type to an U-boot specific string
99
100 A generated string can be used as part of file system related commands
101 or a config name in u-boot. Currently fat16 and fat32 are handled
102 specifically.
103
104 Args:
105 fs_type: File system type.
106
107 Return:
108 A corresponding string for file system type.
109 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900110 if re.match('fat', fs_type):
111 return 'fat'
112 else:
113 return fs_type
114
115def check_ubconfig(config, fs_type):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900116 """Check whether a file system is enabled in u-boot configuration.
117
118 This function is assumed to be called in a fixture function so that
119 the whole test cases will be skipped if a given file system is not
120 enabled.
121
122 Args:
123 fs_type: File system type.
124
125 Return:
126 Nothing.
127 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900128 if not config.buildconfig.get('config_cmd_%s' % fs_type, None):
129 pytest.skip('.config feature "CMD_%s" not enabled' % fs_type.upper())
130 if not config.buildconfig.get('config_%s_write' % fs_type, None):
131 pytest.skip('.config feature "%s_WRITE" not enabled'
132 % fs_type.upper())
133
134def mk_fs(config, fs_type, size, id):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900135 """Create a file system volume.
136
137 Args:
138 fs_type: File system type.
139 size: Size of file system in MiB.
140 id: Prefix string of volume's file name.
141
142 Return:
143 Nothing.
144 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900145 fs_img = '%s.%s.img' % (id, fs_type)
146 fs_img = config.persistent_data_dir + '/' + fs_img
147
148 if fs_type == 'fat16':
149 mkfs_opt = '-F 16'
150 elif fs_type == 'fat32':
151 mkfs_opt = '-F 32'
152 else:
153 mkfs_opt = ''
154
155 if re.match('fat', fs_type):
156 fs_lnxtype = 'vfat'
157 else:
158 fs_lnxtype = fs_type
159
160 count = (size + 1048576 - 1) / 1048576
161
Andy Shevchenko70e473f2021-06-10 18:08:42 +0300162 # Some distributions do not add /sbin to the default PATH, where mkfs lives
163 if '/sbin' not in os.environ["PATH"].split(os.pathsep):
164 os.environ["PATH"] += os.pathsep + '/sbin'
165
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900166 try:
167 check_call('rm -f %s' % fs_img, shell=True)
168 check_call('dd if=/dev/zero of=%s bs=1M count=%d'
169 % (fs_img, count), shell=True)
170 check_call('mkfs.%s %s %s'
171 % (fs_lnxtype, mkfs_opt, fs_img), shell=True)
Stephen Warrenf7d10932020-08-04 11:28:33 -0600172 if fs_type == 'ext4':
173 sb_content = check_output('tune2fs -l %s' % fs_img, shell=True).decode()
174 if 'metadata_csum' in sb_content:
175 check_call('tune2fs -O ^metadata_csum %s' % fs_img, shell=True)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900176 return fs_img
177 except CalledProcessError:
178 call('rm -f %s' % fs_img, shell=True)
179 raise
180
181# from test/py/conftest.py
182def tool_is_in_path(tool):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900183 """Check whether a given command is available on host.
184
185 Args:
186 tool: Command name.
187
188 Return:
189 True if available, False if not.
190 """
Simon Glasse9f4d872018-12-27 08:11:13 -0700191 for path in os.environ['PATH'].split(os.pathsep):
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900192 fn = os.path.join(path, tool)
193 if os.path.isfile(fn) and os.access(fn, os.X_OK):
194 return True
195 return False
196
197fuse_mounted = False
198
199def mount_fs(fs_type, device, mount_point):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900200 """Mount a volume.
201
202 Args:
203 fs_type: File system type.
204 device: Volume's file name.
205 mount_point: Mount point.
206
207 Return:
208 Nothing.
209 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900210 global fuse_mounted
211
212 fuse_mounted = False
213 try:
214 if tool_is_in_path('guestmount'):
215 fuse_mounted = True
216 check_call('guestmount -a %s -m /dev/sda %s'
217 % (device, mount_point), shell=True)
218 else:
Simon Glasse9f4d872018-12-27 08:11:13 -0700219 mount_opt = 'loop,rw'
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900220 if re.match('fat', fs_type):
Simon Glasse9f4d872018-12-27 08:11:13 -0700221 mount_opt += ',umask=0000'
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900222
223 check_call('sudo mount -o %s %s %s'
224 % (mount_opt, device, mount_point), shell=True)
225
226 # may not be effective for some file systems
227 check_call('sudo chmod a+rw %s' % mount_point, shell=True)
228 except CalledProcessError:
229 raise
230
Akashi Takahiro89101f82018-09-27 16:07:22 +0900231def umount_fs(mount_point):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900232 """Unmount a volume.
233
234 Args:
235 mount_point: Mount point.
236
237 Return:
238 Nothing.
239 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900240 if fuse_mounted:
241 call('sync')
242 call('guestunmount %s' % mount_point, shell=True)
243 else:
244 call('sudo umount %s' % mount_point, shell=True)
245
246#
247# Fixture for basic fs test
248# derived from test/fs/fs-test.sh
249#
Tom Rini1b91cee2021-01-28 14:39:56 -0500250@pytest.fixture()
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900251def fs_obj_basic(request, u_boot_config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900252 """Set up a file system to be used in basic fs test.
253
254 Args:
255 request: Pytest request object.
256 u_boot_config: U-boot configuration.
257
258 Return:
259 A fixture for basic fs test, i.e. a triplet of file system type,
260 volume file name and a list of MD5 hashes.
261 """
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900262 fs_type = request.param
263 fs_img = ''
264
265 fs_ubtype = fstype_to_ubname(fs_type)
266 check_ubconfig(u_boot_config, fs_ubtype)
267
268 mount_dir = u_boot_config.persistent_data_dir + '/mnt'
269
270 small_file = mount_dir + '/' + SMALL_FILE
271 big_file = mount_dir + '/' + BIG_FILE
272
273 try:
274
275 # 3GiB volume
276 fs_img = mk_fs(u_boot_config, fs_type, 0xc0000000, '3GB')
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200277 except CalledProcessError as err:
278 pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
279 return
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900280
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200281 try:
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900282 check_call('mkdir -p %s' % mount_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200283 except CalledProcessError as err:
284 pytest.skip('Preparing mount folder failed for filesystem: ' + fs_type + '. {}'.format(err))
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200285 call('rm -f %s' % fs_img, shell=True)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300286 return
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200287
288 try:
289 # Mount the image so we can populate it.
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900290 mount_fs(fs_type, fs_img, mount_dir)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300291 except CalledProcessError as err:
292 pytest.skip('Mounting to folder failed for filesystem: ' + fs_type + '. {}'.format(err))
293 call('rmdir %s' % mount_dir, shell=True)
294 call('rm -f %s' % fs_img, shell=True)
295 return
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900296
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300297 try:
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900298 # Create a subdirectory.
299 check_call('mkdir %s/SUBDIR' % mount_dir, shell=True)
300
301 # Create big file in this image.
302 # Note that we work only on the start 1MB, couple MBs in the 2GB range
303 # and the last 1 MB of the huge 2.5GB file.
304 # So, just put random values only in those areas.
305 check_call('dd if=/dev/urandom of=%s bs=1M count=1'
306 % big_file, shell=True)
307 check_call('dd if=/dev/urandom of=%s bs=1M count=2 seek=2047'
308 % big_file, shell=True)
309 check_call('dd if=/dev/urandom of=%s bs=1M count=1 seek=2499'
310 % big_file, shell=True)
311
312 # Create a small file in this image.
313 check_call('dd if=/dev/urandom of=%s bs=1M count=1'
314 % small_file, shell=True)
315
316 # Delete the small file copies which possibly are written as part of a
317 # previous test.
318 # check_call('rm -f "%s.w"' % MB1, shell=True)
319 # check_call('rm -f "%s.w2"' % MB1, shell=True)
320
321 # Generate the md5sums of reads that we will test against small file
322 out = check_output(
323 'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400324 % small_file, shell=True).decode()
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900325 md5val = [ out.split()[0] ]
326
327 # Generate the md5sums of reads that we will test against big file
328 # One from beginning of file.
329 out = check_output(
330 'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400331 % big_file, shell=True).decode()
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900332 md5val.append(out.split()[0])
333
334 # One from end of file.
335 out = check_output(
336 'dd if=%s bs=1M skip=2499 count=1 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400337 % big_file, shell=True).decode()
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900338 md5val.append(out.split()[0])
339
340 # One from the last 1MB chunk of 2GB
341 out = check_output(
342 'dd if=%s bs=1M skip=2047 count=1 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400343 % big_file, shell=True).decode()
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900344 md5val.append(out.split()[0])
345
346 # One from the start 1MB chunk from 2GB
347 out = check_output(
348 'dd if=%s bs=1M skip=2048 count=1 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400349 % big_file, shell=True).decode()
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900350 md5val.append(out.split()[0])
351
352 # One 1MB chunk crossing the 2GB boundary
353 out = check_output(
354 'dd if=%s bs=512K skip=4095 count=2 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400355 % big_file, shell=True).decode()
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900356 md5val.append(out.split()[0])
357
Heinrich Schuchardtbc856172020-04-20 20:48:40 +0200358 except CalledProcessError as err:
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200359 pytest.skip('Setup failed for filesystem: ' + fs_type + '. {}'.format(err))
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300360 umount_fs(mount_dir)
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900361 return
362 else:
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300363 umount_fs(mount_dir)
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900364 yield [fs_ubtype, fs_img, md5val]
365 finally:
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900366 call('rmdir %s' % mount_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200367 call('rm -f %s' % fs_img, shell=True)
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900368
369#
370# Fixture for extended fs test
371#
Tom Rini1b91cee2021-01-28 14:39:56 -0500372@pytest.fixture()
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900373def fs_obj_ext(request, u_boot_config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900374 """Set up a file system to be used in extended fs test.
375
376 Args:
377 request: Pytest request object.
378 u_boot_config: U-boot configuration.
379
380 Return:
381 A fixture for extended fs test, i.e. a triplet of file system type,
382 volume file name and a list of MD5 hashes.
383 """
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900384 fs_type = request.param
385 fs_img = ''
386
387 fs_ubtype = fstype_to_ubname(fs_type)
388 check_ubconfig(u_boot_config, fs_ubtype)
389
390 mount_dir = u_boot_config.persistent_data_dir + '/mnt'
391
392 min_file = mount_dir + '/' + MIN_FILE
393 tmp_file = mount_dir + '/tmpfile'
394
395 try:
396
397 # 128MiB volume
398 fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200399 except CalledProcessError as err:
400 pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
401 return
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900402
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200403 try:
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900404 check_call('mkdir -p %s' % mount_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200405 except CalledProcessError as err:
406 pytest.skip('Preparing mount folder failed for filesystem: ' + fs_type + '. {}'.format(err))
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200407 call('rm -f %s' % fs_img, shell=True)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300408 return
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200409
410 try:
411 # Mount the image so we can populate it.
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900412 mount_fs(fs_type, fs_img, mount_dir)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300413 except CalledProcessError as err:
414 pytest.skip('Mounting to folder failed for filesystem: ' + fs_type + '. {}'.format(err))
415 call('rmdir %s' % mount_dir, shell=True)
416 call('rm -f %s' % fs_img, shell=True)
417 return
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900418
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300419 try:
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900420 # Create a test directory
421 check_call('mkdir %s/dir1' % mount_dir, shell=True)
422
423 # Create a small file and calculate md5
424 check_call('dd if=/dev/urandom of=%s bs=1K count=20'
425 % min_file, shell=True)
426 out = check_output(
427 'dd if=%s bs=1K 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400428 % min_file, shell=True).decode()
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900429 md5val = [ out.split()[0] ]
430
431 # Calculate md5sum of Test Case 4
432 check_call('dd if=%s of=%s bs=1K count=20'
433 % (min_file, tmp_file), shell=True)
434 check_call('dd if=%s of=%s bs=1K seek=5 count=20'
435 % (min_file, tmp_file), shell=True)
436 out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400437 % tmp_file, shell=True).decode()
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900438 md5val.append(out.split()[0])
439
440 # Calculate md5sum of Test Case 5
441 check_call('dd if=%s of=%s bs=1K count=20'
442 % (min_file, tmp_file), shell=True)
443 check_call('dd if=%s of=%s bs=1K seek=5 count=5'
444 % (min_file, tmp_file), shell=True)
445 out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400446 % tmp_file, shell=True).decode()
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900447 md5val.append(out.split()[0])
448
449 # Calculate md5sum of Test Case 7
450 check_call('dd if=%s of=%s bs=1K count=20'
451 % (min_file, tmp_file), shell=True)
452 check_call('dd if=%s of=%s bs=1K seek=20 count=20'
453 % (min_file, tmp_file), shell=True)
454 out = check_output('dd if=%s bs=1K 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400455 % tmp_file, shell=True).decode()
AKASHI Takahirodde5d3f2018-09-11 15:59:20 +0900456 md5val.append(out.split()[0])
457
458 check_call('rm %s' % tmp_file, shell=True)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900459 except CalledProcessError:
460 pytest.skip('Setup failed for filesystem: ' + fs_type)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300461 umount_fs(mount_dir)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900462 return
463 else:
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300464 umount_fs(mount_dir)
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900465 yield [fs_ubtype, fs_img, md5val]
466 finally:
AKASHI Takahiro615af9a2018-09-11 15:59:19 +0900467 call('rmdir %s' % mount_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200468 call('rm -f %s' % fs_img, shell=True)
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +0900469
470#
471# Fixture for mkdir test
472#
Tom Rini1b91cee2021-01-28 14:39:56 -0500473@pytest.fixture()
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +0900474def fs_obj_mkdir(request, u_boot_config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900475 """Set up a file system to be used in mkdir test.
476
477 Args:
478 request: Pytest request object.
479 u_boot_config: U-boot configuration.
480
481 Return:
482 A fixture for mkdir test, i.e. a duplet of file system type and
483 volume file name.
484 """
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +0900485 fs_type = request.param
486 fs_img = ''
487
488 fs_ubtype = fstype_to_ubname(fs_type)
489 check_ubconfig(u_boot_config, fs_ubtype)
490
491 try:
492 # 128MiB volume
493 fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
494 except:
495 pytest.skip('Setup failed for filesystem: ' + fs_type)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200496 return
AKASHI Takahiro1e90c2c2018-09-11 15:59:21 +0900497 else:
498 yield [fs_ubtype, fs_img]
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200499 call('rm -f %s' % fs_img, shell=True)
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900500
501#
502# Fixture for unlink test
503#
Tom Rini1b91cee2021-01-28 14:39:56 -0500504@pytest.fixture()
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900505def fs_obj_unlink(request, u_boot_config):
Akashi Takahirobcdd1f22018-09-27 16:07:23 +0900506 """Set up a file system to be used in unlink test.
507
508 Args:
509 request: Pytest request object.
510 u_boot_config: U-boot configuration.
511
512 Return:
513 A fixture for unlink test, i.e. a duplet of file system type and
514 volume file name.
515 """
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900516 fs_type = request.param
517 fs_img = ''
518
519 fs_ubtype = fstype_to_ubname(fs_type)
520 check_ubconfig(u_boot_config, fs_ubtype)
521
522 mount_dir = u_boot_config.persistent_data_dir + '/mnt'
523
524 try:
525
526 # 128MiB volume
527 fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200528 except CalledProcessError as err:
529 pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
530 return
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900531
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200532 try:
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900533 check_call('mkdir -p %s' % mount_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200534 except CalledProcessError as err:
535 pytest.skip('Preparing mount folder failed for filesystem: ' + fs_type + '. {}'.format(err))
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200536 call('rm -f %s' % fs_img, shell=True)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300537 return
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200538
539 try:
540 # Mount the image so we can populate it.
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900541 mount_fs(fs_type, fs_img, mount_dir)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300542 except CalledProcessError as err:
543 pytest.skip('Mounting to folder failed for filesystem: ' + fs_type + '. {}'.format(err))
544 call('rmdir %s' % mount_dir, shell=True)
545 call('rm -f %s' % fs_img, shell=True)
546 return
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900547
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300548 try:
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900549 # Test Case 1 & 3
550 check_call('mkdir %s/dir1' % mount_dir, shell=True)
551 check_call('dd if=/dev/urandom of=%s/dir1/file1 bs=1K count=1'
552 % mount_dir, shell=True)
553 check_call('dd if=/dev/urandom of=%s/dir1/file2 bs=1K count=1'
554 % mount_dir, shell=True)
555
556 # Test Case 2
557 check_call('mkdir %s/dir2' % mount_dir, shell=True)
Tom Rini7f24c192019-10-24 11:59:20 -0400558 for i in range(0, 20):
559 check_call('mkdir %s/dir2/0123456789abcdef%02x'
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900560 % (mount_dir, i), shell=True)
561
562 # Test Case 4
563 check_call('mkdir %s/dir4' % mount_dir, shell=True)
564
565 # Test Case 5, 6 & 7
566 check_call('mkdir %s/dir5' % mount_dir, shell=True)
567 check_call('dd if=/dev/urandom of=%s/dir5/file1 bs=1K count=1'
568 % mount_dir, shell=True)
569
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900570 except CalledProcessError:
571 pytest.skip('Setup failed for filesystem: ' + fs_type)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300572 umount_fs(mount_dir)
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900573 return
574 else:
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300575 umount_fs(mount_dir)
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900576 yield [fs_ubtype, fs_img]
577 finally:
Akashi, Takahirod49e7992018-09-11 16:06:03 +0900578 call('rmdir %s' % mount_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200579 call('rm -f %s' % fs_img, shell=True)
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100580
581#
582# Fixture for symlink fs test
583#
Tom Rini1b91cee2021-01-28 14:39:56 -0500584@pytest.fixture()
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100585def fs_obj_symlink(request, u_boot_config):
586 """Set up a file system to be used in symlink fs test.
587
588 Args:
589 request: Pytest request object.
590 u_boot_config: U-boot configuration.
591
592 Return:
593 A fixture for basic fs test, i.e. a triplet of file system type,
594 volume file name and a list of MD5 hashes.
595 """
596 fs_type = request.param
597 fs_img = ''
598
599 fs_ubtype = fstype_to_ubname(fs_type)
600 check_ubconfig(u_boot_config, fs_ubtype)
601
602 mount_dir = u_boot_config.persistent_data_dir + '/mnt'
603
604 small_file = mount_dir + '/' + SMALL_FILE
605 medium_file = mount_dir + '/' + MEDIUM_FILE
606
607 try:
608
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200609 # 1GiB volume
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100610 fs_img = mk_fs(u_boot_config, fs_type, 0x40000000, '1GB')
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200611 except CalledProcessError as err:
612 pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
613 return
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100614
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200615 try:
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100616 check_call('mkdir -p %s' % mount_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200617 except CalledProcessError as err:
618 pytest.skip('Preparing mount folder failed for filesystem: ' + fs_type + '. {}'.format(err))
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200619 call('rm -f %s' % fs_img, shell=True)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300620 return
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200621
622 try:
623 # Mount the image so we can populate it.
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100624 mount_fs(fs_type, fs_img, mount_dir)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300625 except CalledProcessError as err:
626 pytest.skip('Mounting to folder failed for filesystem: ' + fs_type + '. {}'.format(err))
627 call('rmdir %s' % mount_dir, shell=True)
628 call('rm -f %s' % fs_img, shell=True)
629 return
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100630
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300631 try:
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100632 # Create a subdirectory.
633 check_call('mkdir %s/SUBDIR' % mount_dir, shell=True)
634
635 # Create a small file in this image.
636 check_call('dd if=/dev/urandom of=%s bs=1M count=1'
637 % small_file, shell=True)
638
639 # Create a medium file in this image.
640 check_call('dd if=/dev/urandom of=%s bs=10M count=1'
641 % medium_file, shell=True)
642
643 # Generate the md5sums of reads that we will test against small file
644 out = check_output(
645 'dd if=%s bs=1M skip=0 count=1 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400646 % small_file, shell=True).decode()
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100647 md5val = [out.split()[0]]
648 out = check_output(
649 'dd if=%s bs=10M skip=0 count=1 2> /dev/null | md5sum'
Tom Rini784e27d2019-10-24 11:59:24 -0400650 % medium_file, shell=True).decode()
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100651 md5val.extend([out.split()[0]])
652
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100653 except CalledProcessError:
654 pytest.skip('Setup failed for filesystem: ' + fs_type)
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300655 umount_fs(mount_dir)
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100656 return
657 else:
Alper Nebi Yasak46132c22021-05-20 22:09:46 +0300658 umount_fs(mount_dir)
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100659 yield [fs_ubtype, fs_img, md5val]
660 finally:
Jean-Jacques Hiblot8aea8a62019-02-13 12:15:27 +0100661 call('rmdir %s' % mount_dir, shell=True)
Andy Shevchenkoe70ed8c2021-02-11 16:40:12 +0200662 call('rm -f %s' % fs_img, shell=True)