From 395d20a2b7cfd081c1f1a172da14d183e3fe9003 Mon Sep 17 00:00:00 2001 From: Patrick Donnelly Date: Tue, 14 Sep 2021 13:45:33 -0400 Subject: [PATCH] qa: refactor and generalize create_n_files Few things: - Allow calling fsync on directory (to support async create kernel). - Allow immediately unlinking the created file (for stray testing). - Close any file descriptors created. - Write unique content (the i variable) to each file. Signed-off-by: Patrick Donnelly --- qa/tasks/cephfs/mount.py | 49 ++++++++++++++++++++--------- qa/tasks/cephfs/test_recovery_fs.py | 2 +- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/qa/tasks/cephfs/mount.py b/qa/tasks/cephfs/mount.py index 6224a7eae7b..b4390d1d94a 100644 --- a/qa/tasks/cephfs/mount.py +++ b/qa/tasks/cephfs/mount.py @@ -1061,30 +1061,49 @@ class CephFSMount(object): self.background_procs.append(rproc) return rproc - def create_n_files(self, fs_path, count, sync=False): + def create_n_files(self, fs_path, count, sync=False, dirsync=False, unlink=False, finaldirsync=False): + """ + Create n files. + + :param sync: sync the file after writing + :param dirsync: sync the containing directory after closing the file + :param unlink: unlink the file after closing + :param finaldirsync: sync the containing directory after closing the last file + """ + assert(self.is_mounted()) abs_path = os.path.join(self.hostfs_mntpt, fs_path) - pyscript = dedent(""" - import sys - import time + pyscript = dedent(f""" import os n = {count} - abs_path = "{abs_path}" + path = "{abs_path}" - if not os.path.exists(os.path.dirname(abs_path)): - os.makedirs(os.path.dirname(abs_path)) + dpath = os.path.dirname(path) + fnameprefix = os.path.basename(path) + os.makedirs(dpath, exist_ok=True) - for i in range(0, n): - fname = "{{0}}_{{1}}".format(abs_path, i) - with open(fname, 'w') as f: - f.write('content') - if {sync}: - f.flush() - os.fsync(f.fileno()) - """).format(abs_path=abs_path, count=count, sync=str(sync)) + try: + dirfd = os.open(dpath, os.O_DIRECTORY) + + for i in range(n): + fpath = os.path.join(dpath, f"{{fnameprefix}}_{{i}}") + with open(fpath, 'w') as f: + f.write(f"{{i}}") + if {sync}: + f.flush() + os.fsync(f.fileno()) + if {unlink}: + os.unlink(fpath) + if {dirsync}: + os.fsync(dirfd) + if {finaldirsync}: + os.fsync(dirfd) + finally: + os.close(dirfd) + """) self.run_python(pyscript) diff --git a/qa/tasks/cephfs/test_recovery_fs.py b/qa/tasks/cephfs/test_recovery_fs.py index a7aefedb183..bbcdf976972 100644 --- a/qa/tasks/cephfs/test_recovery_fs.py +++ b/qa/tasks/cephfs/test_recovery_fs.py @@ -35,4 +35,4 @@ class TestFSRecovery(CephFSTestCase): self.fs.wait_for_daemons() # check data in file sytem is intact filepath = os_path_join(self.mount_a.hostfs_mntpt, 'file_on_fs_0') - self.assertEqual(self.mount_a.read_file(filepath), "content") + self.assertEqual(self.mount_a.read_file(filepath), "0") -- 2.39.5