Browse Source

Fix listing checkpoints by plan and start_date

Now when listing checkpoints by plan and doing filter
by start_date, it will fail with ValueError. It is
because the process of datetime is not right when specify
start_date and plan_id at the same time. This patch will
fix it.

Closes-Bug: #1803035

Change-Id: Ifa2c0e03a0b83d0e5a59e53425cec5be663e13fb
jiaopengju 5 months ago
parent
commit
705040dbe3

+ 5
- 2
karbor/services/protection/checkpoint.py View File

@@ -354,8 +354,11 @@ class CheckpointCollection(object):
354 354
                                                           marker=marker,
355 355
                                                           sort_dir=sort_dir,
356 356
                                                           context=context):
357
-                date = datetime.strptime(key.split("/")[-3], "%Y-%m-%d")
358
-                checkpoint_project_id = key.split("/")[-2]
357
+                date_cursor = -2 if (prefix.find('by-plan') >= 0) else -3
358
+                project_id_cursor = -3 if (prefix.find('by-plan') >= 0) else -2
359
+                date = datetime.strptime(
360
+                    key.split("/")[date_cursor], "%Y-%m-%d")
361
+                checkpoint_project_id = key.split("/")[project_id_cursor]
359 362
                 if start_date <= date <= end_date and (
360 363
                         checkpoint_project_id == project_id):
361 364
                     ids.append(key[key.find("@") + 1:])

+ 26
- 0
karbor/tests/unit/protection/test_checkpoint_collection.py View File

@@ -69,6 +69,32 @@ class CheckpointCollectionTest(base.TestCase):
69 69
             project_id="fake_project_id_2", provider_id=provider_id_2,
70 70
             plan_id="fake_plan_id_2")), checkpoints_plan_2)
71 71
 
72
+    def test_list_checkpoints_by_plan_id_and_filter_by_start_date(self):
73
+        collection = self._create_test_collection()
74
+        date1 = datetime.strptime("2018-11-12", "%Y-%m-%d")
75
+        date2 = datetime.strptime("2018-11-13", "%Y-%m-%d")
76
+        timeutils.utcnow = mock.MagicMock()
77
+        timeutils.utcnow.return_value = date1
78
+        plan = fake_protection_plan()
79
+        plan["id"] = "fake_plan_id"
80
+        plan['provider_id'] = "fake_provider_id"
81
+        plan["project_id"] = "fake_project_id"
82
+        provider_id = plan['provider_id']
83
+        checkpoints_plan_date1 = {
84
+            collection.create(plan).id for i in range(10)}
85
+        timeutils.utcnow = mock.MagicMock()
86
+        timeutils.utcnow.return_value = date2
87
+        checkpoints_plan_date2 = {
88
+            collection.create(plan).id for i in range(10)}
89
+        self.assertEqual(set(collection.list_ids(
90
+            project_id="fake_project_id", provider_id=provider_id,
91
+            plan_id="fake_plan_id", start_date=date1, end_date=date1)),
92
+            checkpoints_plan_date1)
93
+        self.assertEqual(set(collection.list_ids(
94
+            project_id="fake_project_id", provider_id=provider_id,
95
+            plan_id="fake_plan_id", start_date=date2)),
96
+            checkpoints_plan_date2)
97
+
72 98
     def test_list_checkpoints_by_date(self):
73 99
         collection = self._create_test_collection()
74 100
         date1 = datetime.strptime("2016-06-12", "%Y-%m-%d")

Loading…
Cancel
Save