Browse Source

Fix listing checkpoints by plan error with FS bank

When listing checkpoints by plan with using FS bank,
it will fail with 'could not be found' error. The reason
is, in the bank, checkpoints by plan are stored as
"<bank_path>/karbor/indices/by-plan/<plan_id>/<project_id>
/<date>/<checkpoint>", but the file bank plugin only list
the <date> level, not include the checkpoint level. This
patch will fix it.

Closes-Bug: #1802815

Change-Id: I8c1356269277647490925e0181e2cd1dfca1fa4e
jiaopengju 5 months ago
parent
commit
fd17f48547

+ 8
- 12
karbor/services/protection/bank_plugins/file_system_bank_plugin.py View File

@@ -121,16 +121,11 @@ class FileSystemBankPlugin(BankPlugin):
121 121
             LOG.debug(_("Path is not a directory. name: %s"), obj_file_path)
122 122
             return ()
123 123
         try:
124
-            if os.path.isdir(obj_file_path):
125
-                return os.listdir(obj_file_path)
126
-            else:
127
-                base_dir = os.path.dirname(obj_file_path)
128
-                base_name = os.path.basename(obj_file_path)
129
-                return (
130
-                    base_dir + '/' + obj_file
131
-                    for obj_file in os.listdir(base_dir)
132
-                    if obj_file.startswith(base_name)
133
-                )
124
+            file_list = []
125
+            for root, sub_dirs, files in os.walk(obj_file_path):
126
+                for file_path in files:
127
+                    file_list.append(os.path.join(root, file_path))
128
+            return file_list
134 129
         except OSError:
135 130
             LOG.exception(_("List the object failed. path: %s"), obj_file_path)
136 131
             raise
@@ -186,6 +181,7 @@ class FileSystemBankPlugin(BankPlugin):
186 181
             LOG.error("List objects failed. err: %s", err)
187 182
             raise exception.BankListObjectsFailed(reason=err)
188 183
         else:
189
-            if prefix is not None:
190
-                file_lists = [(prefix + file_name) for file_name in file_lists]
184
+            container_path_length = len(self.object_container_path)
185
+            file_lists = [(
186
+                file_name[container_path_length:]) for file_name in file_lists]
191 187
             return file_lists[-limit:] if limit is not None else file_lists

+ 10
- 0
karbor/tests/unit/protection/test_file_system_bank_plugin.py View File

@@ -59,6 +59,16 @@ class FileSystemBankPluginTest(base.TestCase):
59 59
         self.assertIn('/list/key-1', objects)
60 60
         self.assertIn('/list/key-2', objects)
61 61
 
62
+    def test_list_objects_with_contain_sub_dir(self):
63
+        self.fs_bank_plugin.update_object("/list/key-1", "value-1")
64
+        self.fs_bank_plugin.update_object("/list/sub/key-2", "value-2")
65
+        self.fs_bank_plugin.update_object("/list/sub/key-3", "value-3")
66
+        objects = self.fs_bank_plugin.list_objects(prefix="/list/")
67
+        self.assertEqual(3, len(objects))
68
+        self.assertIn("/list/key-1", objects)
69
+        self.assertIn("/list/sub/key-2", objects)
70
+        self.assertIn("/list/sub/key-3", objects)
71
+
62 72
     def test_update_object(self):
63 73
         self.fs_bank_plugin.update_object("/key-1", "value-1")
64 74
         self.fs_bank_plugin.update_object("/key-1", "value-2")

Loading…
Cancel
Save