]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
qa: refactor and generalize create_n_files
authorPatrick Donnelly <pdonnell@redhat.com>
Tue, 14 Sep 2021 17:45:33 +0000 (13:45 -0400)
committerPatrick Donnelly <pdonnell@redhat.com>
Thu, 7 Oct 2021 19:08:34 +0000 (15:08 -0400)
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 <pdonnell@redhat.com>
qa/tasks/cephfs/mount.py
qa/tasks/cephfs/test_recovery_fs.py

index 6224a7eae7b085a176a7edec6a9d7cdae86742ee..b4390d1d94abb9cec7800b12974769c0209e02ca 100644 (file)
@@ -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)
 
index a7aefedb183e366faca77995780ea6c6a177276c..bbcdf97697277023feaf8fb960afdb7335f4919e 100644 (file)
@@ -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")