Application Data Protection as a Service in OpenStack
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_checkpoint_collection.py 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. from datetime import datetime
  13. import mock
  14. from oslo_utils import timeutils
  15. from karbor.services.protection.bank_plugin import Bank
  16. from karbor.services.protection.checkpoint import CheckpointCollection
  17. from karbor.tests import base
  18. from karbor.tests.unit.protection.fakes import fake_protection_plan
  19. from karbor.tests.unit.protection.test_bank import _InMemoryBankPlugin
  20. from karbor.tests.unit.protection.test_bank import _InMemoryLeasePlugin
  21. class CheckpointCollectionTest(base.TestCase):
  22. def _create_test_collection(self):
  23. return CheckpointCollection(Bank(_InMemoryBankPlugin()),
  24. _InMemoryLeasePlugin())
  25. def test_create_checkpoint(self):
  26. collection = self._create_test_collection()
  27. checkpoint = collection.create(fake_protection_plan())
  28. checkpoint.status = "finished"
  29. checkpoint.commit()
  30. self.assertEqual(
  31. checkpoint.status,
  32. collection.get(checkpoint_id=checkpoint.id).status,
  33. )
  34. def test_list_checkpoints(self):
  35. collection = self._create_test_collection()
  36. plan = fake_protection_plan()
  37. provider_id = plan['provider_id']
  38. project_id = plan['project_id']
  39. result = {collection.create(plan).id for i in range(10)}
  40. self.assertEqual(set(collection.list_ids(
  41. project_id=project_id, provider_id=provider_id)), result)
  42. def test_list_checkpoints_by_plan_id(self):
  43. collection = self._create_test_collection()
  44. plan_1 = fake_protection_plan()
  45. plan_1["id"] = "fake_plan_id_1"
  46. plan_1['provider_id'] = "fake_provider_id_1"
  47. plan_1["project_id"] = "fake_project_id_1"
  48. provider_id_1 = plan_1['provider_id']
  49. checkpoints_plan_1 = {collection.create(plan_1).id for i in range(10)}
  50. plan_2 = fake_protection_plan()
  51. plan_2["id"] = "fake_plan_id_2"
  52. plan_2['provider_id'] = "fake_provider_id_2"
  53. plan_2["project_id"] = "fake_project_id_2"
  54. provider_id_2 = plan_1['provider_id']
  55. checkpoints_plan_2 = {collection.create(plan_2).id for i in range(10)}
  56. self.assertEqual(set(collection.list_ids(
  57. project_id="fake_project_id_1", provider_id=provider_id_1,
  58. plan_id="fake_plan_id_1")), checkpoints_plan_1)
  59. self.assertEqual(set(collection.list_ids(
  60. project_id="fake_project_id_2", provider_id=provider_id_2,
  61. plan_id="fake_plan_id_2")), checkpoints_plan_2)
  62. def test_list_checkpoints_by_plan_id_and_filter_by_start_date(self):
  63. collection = self._create_test_collection()
  64. date1 = datetime.strptime("2018-11-12", "%Y-%m-%d")
  65. date2 = datetime.strptime("2018-11-13", "%Y-%m-%d")
  66. timeutils.utcnow = mock.MagicMock()
  67. timeutils.utcnow.return_value = date1
  68. plan = fake_protection_plan()
  69. plan["id"] = "fake_plan_id"
  70. plan['provider_id'] = "fake_provider_id"
  71. plan["project_id"] = "fake_project_id"
  72. provider_id = plan['provider_id']
  73. checkpoints_plan_date1 = {
  74. collection.create(plan).id for i in range(10)}
  75. timeutils.utcnow = mock.MagicMock()
  76. timeutils.utcnow.return_value = date2
  77. checkpoints_plan_date2 = {
  78. collection.create(plan).id for i in range(10)}
  79. self.assertEqual(set(collection.list_ids(
  80. project_id="fake_project_id", provider_id=provider_id,
  81. plan_id="fake_plan_id", start_date=date1, end_date=date1)),
  82. checkpoints_plan_date1)
  83. self.assertEqual(set(collection.list_ids(
  84. project_id="fake_project_id", provider_id=provider_id,
  85. plan_id="fake_plan_id", start_date=date2)),
  86. checkpoints_plan_date2)
  87. def test_list_checkpoints_by_plan_with_marker(self):
  88. collection = self._create_test_collection()
  89. plan = fake_protection_plan()
  90. plan["id"] = "fake_plan_id"
  91. plan['provider_id'] = "fake_provider_id"
  92. plan["project_id"] = "fake_project_id"
  93. provider_id = plan['provider_id']
  94. checkpoints_plan = {collection.create(plan, {
  95. 'checkpoint_id': i}).id for i in range(10)}
  96. checkpoints_sorted = sorted(checkpoints_plan)
  97. self.assertEqual(len(collection.list_ids(
  98. project_id="fake_project_id", provider_id=provider_id,
  99. plan_id="fake_plan_id", marker=checkpoints_sorted[0])) < 10, True)
  100. def test_list_checkpoints_by_date(self):
  101. collection = self._create_test_collection()
  102. date1 = datetime.strptime("2016-06-12", "%Y-%m-%d")
  103. timeutils.utcnow = mock.MagicMock()
  104. timeutils.utcnow.return_value = date1
  105. plan = fake_protection_plan()
  106. provider_id = plan['provider_id']
  107. project_id = plan['project_id']
  108. checkpoints_date_1 = {collection.create(plan).id for i in range(10)}
  109. date2 = datetime.strptime("2016-06-13", "%Y-%m-%d")
  110. timeutils.utcnow = mock.MagicMock()
  111. timeutils.utcnow.return_value = date2
  112. checkpoints_date_2 = {collection.create(plan).id for i in range(10)}
  113. self.assertEqual(set(collection.list_ids(
  114. project_id=project_id,
  115. provider_id=provider_id,
  116. start_date=date1,
  117. end_date=date1)),
  118. checkpoints_date_1)
  119. self.assertEqual(set(collection.list_ids(
  120. project_id=project_id,
  121. provider_id=provider_id,
  122. start_date=date2,
  123. end_date=date2)),
  124. checkpoints_date_2)
  125. def test_list_checkpoints_by_date_with_marker(self):
  126. collection = self._create_test_collection()
  127. date = datetime.strptime("2018-11-12", "%Y-%m-%d")
  128. timeutils.utcnow = mock.MagicMock()
  129. timeutils.utcnow.return_value = date
  130. plan = fake_protection_plan()
  131. plan["id"] = "fake_plan_id"
  132. plan['provider_id'] = "fake_provider_id"
  133. plan["project_id"] = "fake_project_id"
  134. provider_id = plan['provider_id']
  135. checkpoints_plan = {collection.create(plan, {
  136. 'checkpoint_id': i}).id for i in range(10)}
  137. checkpoints_sorted = sorted(checkpoints_plan)
  138. self.assertEqual(len(collection.list_ids(
  139. project_id="fake_project_id", provider_id=provider_id,
  140. start_date=date,
  141. marker=checkpoints_sorted[0])) < 10, True)
  142. def test_delete_checkpoint(self):
  143. collection = self._create_test_collection()
  144. plan = fake_protection_plan()
  145. provider_id = plan['provider_id']
  146. project_id = plan['project_id']
  147. result = {collection.create(plan).id for i in range(10)}
  148. checkpoint = collection.get(result.pop())
  149. checkpoint.purge()
  150. self.assertEqual(set(collection.list_ids(
  151. project_id=project_id, provider_id=provider_id)), result)
  152. def test_write_checkpoint_with_invalid_lease(self):
  153. collection = self._create_test_collection()
  154. checkpoint = collection.create(fake_protection_plan())
  155. collection._bank_lease.check_lease_validity = mock.MagicMock()
  156. collection._bank_lease.check_lease_validity.return_value = False
  157. checkpoint.status = "finished"
  158. self.assertNotEqual(
  159. checkpoint.status,
  160. collection.get(checkpoint_id=checkpoint.id).status,
  161. )