summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Glass <[email protected]>2026-03-09 09:13:04 -0600
committerTom Rini <[email protected]>2026-03-23 09:18:30 -0600
commit3691b1e4ce07425dfe3a32c10cfcaf989be57734 (patch)
treec6a741231c072439e04b118a0229005df246ae70
parentd030dc34d67acaa51742d549cb83a81256fefac2 (diff)
test: Convert fs_helper to use a class
Create a class around mk_fs() (and later setup_image()) to handle the common tasks of image creation. Many callers of fs_helper.mk_fs() create their own scratch directories while users of fs_helper.setup_image() rely on one being returned. Unify this by adding 'srcdir' as a field while converting to a class. The class delegates to the existing mk_fs() function for the actual filesystem creation, adding lifecycle management for scratch directories and the image file. Signed-off-by: Simon Glass <[email protected]> Reviewed-by: Tom Rini <[email protected]>
-rw-r--r--test/py/tests/fs_helper.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/test/py/tests/fs_helper.py b/test/py/tests/fs_helper.py
index 72e18588288..800376b1e7d 100644
--- a/test/py/tests/fs_helper.py
+++ b/test/py/tests/fs_helper.py
@@ -7,13 +7,96 @@
import re
import os
+import shutil
from subprocess import call, check_call, check_output, CalledProcessError
from subprocess import DEVNULL
+import tempfile
# size_gran (int): Size granularity of file system image in bytes
SIZE_GRAN = 1 << 20
+class FsHelper:
+ """Creating a filesystem containing test files
+
+ Usage:
+ with FsHelper(ubman.config, 'ext4', 10, 'mmc1') as fsh:
+ # create files in the self.srcdir directory
+ fsh.mk_fs()
+ # Now use the filesystem
+
+ # The filesystem and srcdir are erased after the 'with' statement.
+
+ It is also possible to use an existing srcdir:
+
+ with FsHelper(ubman.config, 'fat32', 10, 'usb2') as fsh:
+ fsh.srcdir = src_dir
+ fsh.mk_fs()
+ ...
+
+ Properties:
+ fs_img (str): Filename for the filesystem image
+ """
+ def __init__(self, config, fs_type, size_mb, prefix):
+ """Set up a new object
+
+ Args:
+ config (u_boot_config): U-Boot configuration
+ fs_type (str): File system type: one of ext2, ext3, ext4, vfat,
+ fat12, fat16, fat32, exfat, fs_generic (which means vfat)
+ size_mb (int): Size of file system in MB
+ prefix (str): Prefix string of volume's file name
+ """
+ if fs_type not in ['fat12', 'fat16', 'fat32', 'vfat',
+ 'ext2', 'ext3', 'ext4',
+ 'exfat', 'fs_generic']:
+ raise ValueError(f"Unsupported filesystem type '{fs_type}'")
+
+ self.config = config
+ self.fs_type = fs_type
+ self.size_mb = size_mb
+ self.prefix = prefix
+ self.quiet = True
+ self.fs_img = None
+ self.tmpdir = None
+ self.srcdir = None
+ self._do_cleanup = False
+
+ def mk_fs(self):
+ """Make a new filesystem and copy in the files"""
+ self.setup()
+ self._do_cleanup = True
+ self.fs_img = mk_fs(self.config, self.fs_type, self.size_mb << 20,
+ self.prefix, self.srcdir, quiet=self.quiet)
+
+ def setup(self):
+ """Set up the srcdir ready to receive files"""
+ if not self.srcdir:
+ if self.config:
+ self.srcdir = os.path.join(self.config.persistent_data_dir,
+ f'{self.prefix}.{self.fs_type}.tmp')
+ if os.path.exists(self.srcdir):
+ shutil.rmtree(self.srcdir)
+ os.mkdir(self.srcdir)
+ else:
+ self.tmpdir = tempfile.TemporaryDirectory('fs_helper')
+ self.srcdir = self.tmpdir.name
+
+ def cleanup(self):
+ """Remove created image"""
+ if self.tmpdir:
+ self.tmpdir.cleanup()
+ if self._do_cleanup:
+ os.remove(self.fs_img)
+
+ def __enter__(self):
+ self.setup()
+ return self
+
+ def __exit__(self, extype, value, traceback):
+ self.cleanup()
+
+
def mk_fs(config, fs_type, size, prefix, src_dir=None, fs_img=None, quiet=False):
"""Create a file system volume