From 705040dbe3381c9ea63e71e66ed8618246baeb11 Mon Sep 17 00:00:00 2001 From: jiaopengju Date: Tue, 13 Nov 2018 12:58:08 +0800 Subject: [PATCH] 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 --- karbor/services/protection/checkpoint.py | 7 +++-- .../protection/test_checkpoint_collection.py | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/karbor/services/protection/checkpoint.py b/karbor/services/protection/checkpoint.py index 91079753..c88ed2d4 100644 --- a/karbor/services/protection/checkpoint.py +++ b/karbor/services/protection/checkpoint.py @@ -354,8 +354,11 @@ class CheckpointCollection(object): marker=marker, sort_dir=sort_dir, context=context): - date = datetime.strptime(key.split("/")[-3], "%Y-%m-%d") - checkpoint_project_id = key.split("/")[-2] + date_cursor = -2 if (prefix.find('by-plan') >= 0) else -3 + project_id_cursor = -3 if (prefix.find('by-plan') >= 0) else -2 + date = datetime.strptime( + key.split("/")[date_cursor], "%Y-%m-%d") + checkpoint_project_id = key.split("/")[project_id_cursor] if start_date <= date <= end_date and ( checkpoint_project_id == project_id): ids.append(key[key.find("@") + 1:]) diff --git a/karbor/tests/unit/protection/test_checkpoint_collection.py b/karbor/tests/unit/protection/test_checkpoint_collection.py index 7e3d83b4..6c968c35 100644 --- a/karbor/tests/unit/protection/test_checkpoint_collection.py +++ b/karbor/tests/unit/protection/test_checkpoint_collection.py @@ -69,6 +69,32 @@ class CheckpointCollectionTest(base.TestCase): project_id="fake_project_id_2", provider_id=provider_id_2, plan_id="fake_plan_id_2")), checkpoints_plan_2) + def test_list_checkpoints_by_plan_id_and_filter_by_start_date(self): + collection = self._create_test_collection() + date1 = datetime.strptime("2018-11-12", "%Y-%m-%d") + date2 = datetime.strptime("2018-11-13", "%Y-%m-%d") + timeutils.utcnow = mock.MagicMock() + timeutils.utcnow.return_value = date1 + plan = fake_protection_plan() + plan["id"] = "fake_plan_id" + plan['provider_id'] = "fake_provider_id" + plan["project_id"] = "fake_project_id" + provider_id = plan['provider_id'] + checkpoints_plan_date1 = { + collection.create(plan).id for i in range(10)} + timeutils.utcnow = mock.MagicMock() + timeutils.utcnow.return_value = date2 + checkpoints_plan_date2 = { + collection.create(plan).id for i in range(10)} + self.assertEqual(set(collection.list_ids( + project_id="fake_project_id", provider_id=provider_id, + plan_id="fake_plan_id", start_date=date1, end_date=date1)), + checkpoints_plan_date1) + self.assertEqual(set(collection.list_ids( + project_id="fake_project_id", provider_id=provider_id, + plan_id="fake_plan_id", start_date=date2)), + checkpoints_plan_date2) + def test_list_checkpoints_by_date(self): collection = self._create_test_collection() date1 = datetime.strptime("2016-06-12", "%Y-%m-%d")