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")