From: Patrick Donnelly Date: Tue, 14 Sep 2021 17:45:33 +0000 (-0400) Subject: qa: refactor and generalize create_n_files X-Git-Tag: v16.2.8~51^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=b018ccf0b79495515e01f7c73b5bbbf82ce780d6;p=ceph.git 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 (cherry picked from commit 395d20a2b7cfd081c1f1a172da14d183e3fe9003) --- diff --git a/qa/tasks/cephfs/mount.py b/qa/tasks/cephfs/mount.py index 9c12d5272b2f..ba90701af2be 100644 --- a/qa/tasks/cephfs/mount.py +++ b/qa/tasks/cephfs/mount.py @@ -1027,30 +1027,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 a7aefedb183e..bbcdf9769727 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")