[PATCH 03/17] test: Split out mk_fs function into a helper
Simon Glass
sjg at chromium.org
Sun Oct 30 02:47:05 CET 2022
This function is useful for other tests. Move it into common code.
Signed-off-by: Simon Glass <sjg at chromium.org>
---
test/py/tests/fs_helper.py | 54 ++++++++++++++++++++++++++++
test/py/tests/test_fs/conftest.py | 58 ++++---------------------------
2 files changed, 60 insertions(+), 52 deletions(-)
create mode 100644 test/py/tests/fs_helper.py
diff --git a/test/py/tests/fs_helper.py b/test/py/tests/fs_helper.py
new file mode 100644
index 00000000000..2d8f1f63add
--- /dev/null
+++ b/test/py/tests/fs_helper.py
@@ -0,0 +1,54 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Helper functions for dealing with filesystems
+#
+# Copyright (c) 2018, Linaro Limited
+# Author: Takahiro Akashi <takahiro.akashi at linaro.org>
+
+import re
+import os
+from subprocess import call, check_call, check_output, CalledProcessError
+
+def mk_fs(config, fs_type, size, prefix):
+ """Create a file system volume
+
+ Args:
+ fs_type (str): File system type, e.g. 'ext4'
+ size (int): Size of file system in bytes
+ prefix (str): Prefix string of volume's file name
+ """
+ fs_img = '%s.%s.img' % (prefix, fs_type)
+ fs_img = config.persistent_data_dir + '/' + fs_img
+
+ if fs_type == 'fat16':
+ mkfs_opt = '-F 16'
+ elif fs_type == 'fat32':
+ mkfs_opt = '-F 32'
+ else:
+ mkfs_opt = ''
+
+ if re.match('fat', fs_type):
+ fs_lnxtype = 'vfat'
+ else:
+ fs_lnxtype = fs_type
+
+ count = (size + 1048576 - 1) / 1048576
+
+ # Some distributions do not add /sbin to the default PATH, where mkfs lives
+ if '/sbin' not in os.environ["PATH"].split(os.pathsep):
+ os.environ["PATH"] += os.pathsep + '/sbin'
+
+ try:
+ check_call('rm -f %s' % fs_img, shell=True)
+ check_call('dd if=/dev/zero of=%s bs=1M count=%d'
+ % (fs_img, count), shell=True)
+ check_call('mkfs.%s %s %s'
+ % (fs_lnxtype, mkfs_opt, fs_img), shell=True)
+ if fs_type == 'ext4':
+ sb_content = check_output('tune2fs -l %s' % fs_img, shell=True).decode()
+ if 'metadata_csum' in sb_content:
+ check_call('tune2fs -O ^metadata_csum %s' % fs_img, shell=True)
+ return fs_img
+ except CalledProcessError:
+ call('rm -f %s' % fs_img, shell=True)
+ raise
diff --git a/test/py/tests/test_fs/conftest.py b/test/py/tests/test_fs/conftest.py
index b638284e076..9329ec6f1b2 100644
--- a/test/py/tests/test_fs/conftest.py
+++ b/test/py/tests/test_fs/conftest.py
@@ -9,6 +9,7 @@ import re
from subprocess import call, check_call, check_output, CalledProcessError
from fstest_defs import *
import u_boot_utils as util
+from tests import fs_helper
supported_fs_basic = ['fat16', 'fat32', 'ext4']
supported_fs_ext = ['fat16', 'fat32']
@@ -132,53 +133,6 @@ def check_ubconfig(config, fs_type):
pytest.skip('.config feature "%s_WRITE" not enabled'
% fs_type.upper())
-def mk_fs(config, fs_type, size, id):
- """Create a file system volume.
-
- Args:
- fs_type: File system type.
- size: Size of file system in MiB.
- id: Prefix string of volume's file name.
-
- Return:
- Nothing.
- """
- fs_img = '%s.%s.img' % (id, fs_type)
- fs_img = config.persistent_data_dir + '/' + fs_img
-
- if fs_type == 'fat16':
- mkfs_opt = '-F 16'
- elif fs_type == 'fat32':
- mkfs_opt = '-F 32'
- else:
- mkfs_opt = ''
-
- if re.match('fat', fs_type):
- fs_lnxtype = 'vfat'
- else:
- fs_lnxtype = fs_type
-
- count = (size + 1048576 - 1) / 1048576
-
- # Some distributions do not add /sbin to the default PATH, where mkfs lives
- if '/sbin' not in os.environ["PATH"].split(os.pathsep):
- os.environ["PATH"] += os.pathsep + '/sbin'
-
- try:
- check_call('rm -f %s' % fs_img, shell=True)
- check_call('dd if=/dev/zero of=%s bs=1M count=%d'
- % (fs_img, count), shell=True)
- check_call('mkfs.%s %s %s'
- % (fs_lnxtype, mkfs_opt, fs_img), shell=True)
- if fs_type == 'ext4':
- sb_content = check_output('tune2fs -l %s' % fs_img, shell=True).decode()
- if 'metadata_csum' in sb_content:
- check_call('tune2fs -O ^metadata_csum %s' % fs_img, shell=True)
- return fs_img
- except CalledProcessError:
- call('rm -f %s' % fs_img, shell=True)
- raise
-
# from test/py/conftest.py
def tool_is_in_path(tool):
"""Check whether a given command is available on host.
@@ -283,7 +237,7 @@ def fs_obj_basic(request, u_boot_config):
try:
# 3GiB volume
- fs_img = mk_fs(u_boot_config, fs_type, 0xc0000000, '3GB')
+ fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0xc0000000, '3GB')
except CalledProcessError as err:
pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
return
@@ -405,7 +359,7 @@ def fs_obj_ext(request, u_boot_config):
try:
# 128MiB volume
- fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
+ fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
except CalledProcessError as err:
pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
return
@@ -500,7 +454,7 @@ def fs_obj_mkdir(request, u_boot_config):
try:
# 128MiB volume
- fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
+ fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
except:
pytest.skip('Setup failed for filesystem: ' + fs_type)
return
@@ -534,7 +488,7 @@ def fs_obj_unlink(request, u_boot_config):
try:
# 128MiB volume
- fs_img = mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
+ fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x8000000, '128MB')
except CalledProcessError as err:
pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
return
@@ -617,7 +571,7 @@ def fs_obj_symlink(request, u_boot_config):
try:
# 1GiB volume
- fs_img = mk_fs(u_boot_config, fs_type, 0x40000000, '1GB')
+ fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x40000000, '1GB')
except CalledProcessError as err:
pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
return
--
2.38.1.273.g43a17bfeac-goog
More information about the U-Boot
mailing list