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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_with_all_tenants(self):
  43. collection = self._create_test_collection()
  44. plan_1 = fake_protection_plan()
  45. plan_1["id"] = "fake_plan_id_1"
  46. plan_1["project_id"] = "fake_project_id_1"
  47. provider_id_1 = plan_1['provider_id']
  48. checkpoints_plan_1 = {collection.create(plan_1).id for i in range(10)}
  49. plan_2 = fake_protection_plan()
  50. plan_2["id"] = "fake_plan_id_2"
  51. plan_2["project_id"] = "fake_project_id_2"
  52. checkpoints_plan_2 = {collection.create(plan_2).id for i in range(10)}
  53. checkpoints_plan_1.update(checkpoints_plan_2)
  54. self.assertEqual(set(collection.list_ids(
  55. project_id="fake_project_id_1", provider_id=provider_id_1,
  56. all_tenants=True)), checkpoints_plan_1)
  57. def test_list_checkpoints_by_plan_id(self):
  58. collection = self._create_test_collection()
  59. plan_1 = fake_protection_plan()
  60. plan_1["id"] = "fake_plan_id_1"
  61. plan_1['provider_id'] = "fake_provider_id_1"
  62. plan_1["project_id"] = "fake_project_id_1"
  63. provider_id_1 = plan_1['provider_id']
  64. checkpoints_plan_1 = {collection.create(plan_1).id for i in range(10)}
  65. plan_2 = fake_protection_plan()
  66. plan_2["id"] = "fake_plan_id_2"
  67. plan_2['provider_id'] = "fake_provider_id_2"
  68. plan_2["project_id"] = "fake_project_id_2"
  69. provider_id_2 = plan_1['provider_id']
  70. checkpoints_plan_2 = {collection.create(plan_2).id for i in range(10)}
  71. self.assertEqual(set(collection.list_ids(
  72. project_id="fake_project_id_1", provider_id=provider_id_1,
  73. plan_id="fake_plan_id_1")), checkpoints_plan_1)
  74. self.assertEqual(set(collection.list_ids(
  75. project_id="fake_project_id_2", provider_id=provider_id_2,
  76. plan_id="fake_plan_id_2")), checkpoints_plan_2)
  77. def test_list_checkpoints_by_plan_with_all_tenants(self):
  78. collection = self._create_test_collection()
  79. plan_1 = fake_protection_plan()
  80. plan_1["id"] = "fake_plan_id_1"
  81. plan_1["project_id"] = "fake_project_id_1"
  82. provider_id_1 = plan_1['provider_id']
  83. checkpoints_plan_1 = {collection.create(plan_1).id for i in range(10)}
  84. plan_1["project_id"] = "fake_project_id_2"
  85. checkpoints_plan_2 = {collection.create(plan_1).id for i in range(10)}
  86. checkpoints_plan_2.update(checkpoints_plan_1)
  87. self.assertEqual(set(collection.list_ids(
  88. project_id="fake_project_id_1", provider_id=provider_id_1,
  89. plan_id='fake_plan_id_1',
  90. all_tenants=True)), checkpoints_plan_2)
  91. def test_list_checkpoints_by_plan_id_and_filter_by_start_date(self):
  92. collection = self._create_test_collection()
  93. date1 = datetime.strptime("2018-11-12", "%Y-%m-%d")
  94. date2 = datetime.strptime("2018-11-13", "%Y-%m-%d")
  95. timeutils.utcnow = mock.MagicMock()
  96. timeutils.utcnow.return_value = date1
  97. plan = fake_protection_plan()
  98. plan["id"] = "fake_plan_id"
  99. plan['provider_id'] = "fake_provider_id"
  100. plan["project_id"] = "fake_project_id"
  101. provider_id = plan['provider_id']
  102. checkpoints_plan_date1 = {
  103. collection.create(plan).id for i in range(10)}
  104. timeutils.utcnow = mock.MagicMock()
  105. timeutils.utcnow.return_value = date2
  106. checkpoints_plan_date2 = {
  107. collection.create(plan).id for i in range(10)}
  108. self.assertEqual(set(collection.list_ids(
  109. project_id="fake_project_id", provider_id=provider_id,
  110. plan_id="fake_plan_id", start_date=date1, end_date=date1)),
  111. checkpoints_plan_date1)
  112. self.assertEqual(set(collection.list_ids(
  113. project_id="fake_project_id", provider_id=provider_id,
  114. plan_id="fake_plan_id", start_date=date2)),
  115. checkpoints_plan_date2)
  116. def test_list_checkpoints_by_plan_with_marker(self):
  117. collection = self._create_test_collection()
  118. plan = fake_protection_plan()
  119. plan["id"] = "fake_plan_id"
  120. plan['provider_id'] = "fake_provider_id"
  121. plan["project_id"] = "fake_project_id"
  122. provider_id = plan['provider_id']
  123. checkpoints_plan = {collection.create(plan, {
  124. 'checkpoint_id': i}).id for i in range(10)}
  125. checkpoints_sorted = sorted(checkpoints_plan)
  126. self.assertEqual(len(collection.list_ids(
  127. project_id="fake_project_id", provider_id=provider_id,
  128. plan_id="fake_plan_id", marker=checkpoints_sorted[0])) < 10, True)
  129. def test_list_checkpoints_by_date(self):
  130. collection = self._create_test_collection()
  131. date1 = datetime.strptime("2016-06-12", "%Y-%m-%d")
  132. timeutils.utcnow = mock.MagicMock()
  133. timeutils.utcnow.return_value = date1
  134. plan = fake_protection_plan()
  135. provider_id = plan['provider_id']
  136. project_id = plan['project_id']
  137. checkpoints_date_1 = {collection.create(plan).id for i in range(10)}
  138. date2 = datetime.strptime("2016-06-13", "%Y-%m-%d")
  139. timeutils.utcnow = mock.MagicMock()
  140. timeutils.utcnow.return_value = date2
  141. checkpoints_date_2 = {collection.create(plan).id for i in range(10)}
  142. self.assertEqual(set(collection.list_ids(
  143. project_id=project_id,
  144. provider_id=provider_id,
  145. start_date=date1,
  146. end_date=date1)),
  147. checkpoints_date_1)
  148. self.assertEqual(set(collection.list_ids(
  149. project_id=project_id,
  150. provider_id=provider_id,
  151. start_date=date2,
  152. end_date=date2)),
  153. checkpoints_date_2)
  154. def test_list_checkpoints_by_date_with_all_tenants(self):
  155. collection = self._create_test_collection()
  156. date1 = datetime.strptime("2018-11-15", "%Y-%m-%d")
  157. timeutils.utcnow = mock.MagicMock()
  158. timeutils.utcnow.return_value = date1
  159. plan_1 = fake_protection_plan()
  160. plan_1["id"] = "fake_plan_id_1"
  161. plan_1["project_id"] = "fake_project_id_1"
  162. provider_id_1 = plan_1['provider_id']
  163. checkpoints_1 = {collection.create(plan_1).id for i in range(10)}
  164. date2 = datetime.strptime("2018-11-17", "%Y-%m-%d")
  165. timeutils.utcnow = mock.MagicMock()
  166. timeutils.utcnow.return_value = date2
  167. plan_1["id"] = "fake_plan_id_2"
  168. plan_1["project_id"] = "fake_project_id_2"
  169. checkpoints_2 = {collection.create(plan_1).id for i in range(10)}
  170. checkpoints_2.update(checkpoints_1)
  171. self.assertEqual(set(collection.list_ids(
  172. project_id="fake_project_id_1", provider_id=provider_id_1,
  173. start_date=date1,
  174. all_tenants=True)), checkpoints_2)
  175. def test_list_checkpoints_by_date_with_marker(self):
  176. collection = self._create_test_collection()
  177. date = datetime.strptime("2018-11-12", "%Y-%m-%d")
  178. timeutils.utcnow = mock.MagicMock()
  179. timeutils.utcnow.return_value = date
  180. plan = fake_protection_plan()
  181. plan["id"] = "fake_plan_id"
  182. plan['provider_id'] = "fake_provider_id"
  183. plan["project_id"] = "fake_project_id"
  184. provider_id = plan['provider_id']
  185. checkpoints_plan = {collection.create(plan, {
  186. 'checkpoint_id': i}).id for i in range(10)}
  187. checkpoints_sorted = sorted(checkpoints_plan)
  188. self.assertEqual(len(collection.list_ids(
  189. project_id="fake_project_id", provider_id=provider_id,
  190. start_date=date,
  191. marker=checkpoints_sorted[0])) < 10, True)
  192. def test_delete_checkpoint(self):
  193. collection = self._create_test_collection()
  194. plan = fake_protection_plan()
  195. provider_id = plan['provider_id']
  196. project_id = plan['project_id']
  197. result = {collection.create(plan).id for i in range(10)}
  198. checkpoint = collection.get(result.pop())
  199. checkpoint.purge()
  200. self.assertEqual(set(collection.list_ids(
  201. project_id=project_id, provider_id=provider_id)), result)
  202. def test_write_checkpoint_with_invalid_lease(self):
  203. collection = self._create_test_collection()
  204. checkpoint = collection.create(fake_protection_plan())
  205. collection._bank_lease.check_lease_validity = mock.MagicMock()
  206. collection._bank_lease.check_lease_validity.return_value = False
  207. checkpoint.status = "finished"
  208. self.assertNotEqual(
  209. checkpoint.status,
  210. collection.get(checkpoint_id=checkpoint.id).status,
  211. )