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
This commit is contained in:
jiaopengju 2018-11-13 12:58:08 +08:00
parent fd17f48547
commit 705040dbe3
2 changed files with 31 additions and 2 deletions

View File

@ -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:])

View File

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