PageRenderTime 59ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/unit/customizations/s3/test_comparator.py

https://gitlab.com/github-cloud-corp/aws-cli
Python | 235 lines | 212 code | 6 blank | 17 comment | 0 complexity | e8c07fdb2b8d64638aec57ac4bdcdec0 MD5 | raw file
  1. # Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"). You
  4. # may not use this file except in compliance with the License. A copy of
  5. # the License is located at
  6. #
  7. # http://aws.amazon.com/apache2.0/
  8. #
  9. # or in the "license" file accompanying this file. This file is
  10. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  11. # ANY KIND, either express or implied. See the License for the specific
  12. # language governing permissions and limitations under the License.
  13. import datetime
  14. import unittest
  15. from mock import Mock
  16. from awscli.customizations.s3.comparator import Comparator
  17. from awscli.customizations.s3.filegenerator import FileStat
  18. class ComparatorTest(unittest.TestCase):
  19. def setUp(self):
  20. self.sync_strategy = Mock()
  21. self.not_at_src_sync_strategy = Mock()
  22. self.not_at_dest_sync_strategy = Mock()
  23. self.comparator = Comparator(self.sync_strategy,
  24. self.not_at_dest_sync_strategy,
  25. self.not_at_src_sync_strategy)
  26. def test_compare_key_equal_should_not_sync(self):
  27. """
  28. Confirm the appropriate action is taken when the soruce compare key
  29. is equal to the destination compare key.
  30. """
  31. # Try when the sync strategy says not to sync the file.
  32. self.sync_strategy.determine_should_sync.return_value = False
  33. src_files = []
  34. dest_files = []
  35. ref_list = []
  36. result_list = []
  37. time = datetime.datetime.now()
  38. src_file = FileStat(src='', dest='',
  39. compare_key='comparator_test.py', size=10,
  40. last_update=time, src_type='local',
  41. dest_type='s3', operation_name='upload')
  42. dest_file = FileStat(src='', dest='',
  43. compare_key='comparator_test.py', size=10,
  44. last_update=time, src_type='s3',
  45. dest_type='local', operation_name='')
  46. src_files.append(src_file)
  47. dest_files.append(dest_file)
  48. files = self.comparator.call(iter(src_files), iter(dest_files))
  49. for filename in files:
  50. result_list.append(filename)
  51. self.assertEqual(result_list, ref_list)
  52. # Try when the sync strategy says to sync the file.
  53. self.sync_strategy.determine_should_sync.return_value = True
  54. ref_list = []
  55. result_list = []
  56. files = self.comparator.call(iter(src_files), iter(dest_files))
  57. ref_list.append(src_file)
  58. for filename in files:
  59. result_list.append(filename)
  60. self.assertEqual(result_list, ref_list)
  61. def test_compare_key_less(self):
  62. """
  63. Confirm the appropriate action is taken when the soruce compare key
  64. is less than the destination compare key.
  65. """
  66. self.not_at_src_sync_strategy.determine_should_sync.return_value = False
  67. # Try when the sync strategy says to sync the file.
  68. self.not_at_dest_sync_strategy.determine_should_sync.return_value = True
  69. src_files = []
  70. dest_files = []
  71. ref_list = []
  72. result_list = []
  73. time = datetime.datetime.now()
  74. src_file = FileStat(src='', dest='',
  75. compare_key='bomparator_test.py', size=10,
  76. last_update=time, src_type='local',
  77. dest_type='s3', operation_name='upload')
  78. dest_file = FileStat(src='', dest='',
  79. compare_key='comparator_test.py', size=10,
  80. last_update=time, src_type='s3',
  81. dest_type='local', operation_name='')
  82. src_files.append(src_file)
  83. dest_files.append(dest_file)
  84. ref_list.append(src_file)
  85. files = self.comparator.call(iter(src_files), iter(dest_files))
  86. for filename in files:
  87. result_list.append(filename)
  88. self.assertEqual(result_list, ref_list)
  89. # Now try when the sync strategy says not to sync the file.
  90. self.not_at_dest_sync_strategy.determine_should_sync.return_value = False
  91. result_list = []
  92. ref_list = []
  93. files = self.comparator.call(iter(src_files), iter(dest_files))
  94. for filename in files:
  95. result_list.append(filename)
  96. self.assertEqual(result_list, ref_list)
  97. def test_compare_key_greater(self):
  98. """
  99. Confirm the appropriate action is taken when the soruce compare key
  100. is greater than the destination compare key.
  101. """
  102. self.not_at_dest_sync_strategy.determine_should_sync.return_value = False
  103. # Try when the sync strategy says to sync the file.
  104. self.not_at_src_sync_strategy.determine_should_sync.return_value = True
  105. src_files = []
  106. dest_files = []
  107. ref_list = []
  108. result_list = []
  109. time = datetime.datetime.now()
  110. src_file = FileStat(src='', dest='',
  111. compare_key='domparator_test.py', size=10,
  112. last_update=time, src_type='local',
  113. dest_type='s3', operation_name='upload')
  114. dest_file = FileStat(src='', dest='',
  115. compare_key='comparator_test.py', size=10,
  116. last_update=time, src_type='s3',
  117. dest_type='local', operation_name='')
  118. src_files.append(src_file)
  119. dest_files.append(dest_file)
  120. ref_list.append(dest_file)
  121. files = self.comparator.call(iter(src_files), iter(dest_files))
  122. for filename in files:
  123. result_list.append(filename)
  124. self.assertEqual(result_list, ref_list)
  125. # Now try when the sync strategy says not to sync the file.
  126. self.not_at_src_sync_strategy.determine_should_sync.return_value = False
  127. result_list = []
  128. ref_list = []
  129. files = self.comparator.call(iter(src_files), iter(dest_files))
  130. for filename in files:
  131. result_list.append(filename)
  132. self.assertEqual(result_list, ref_list)
  133. def test_empty_src(self):
  134. """
  135. Confirm the appropriate action is taken when there are no more source
  136. files to take.
  137. """
  138. # Try when the sync strategy says to sync the file.
  139. self.not_at_src_sync_strategy.determine_should_sync.return_value = True
  140. src_files = []
  141. dest_files = []
  142. ref_list = []
  143. result_list = []
  144. time = datetime.datetime.now()
  145. dest_file = FileStat(src='', dest='',
  146. compare_key='comparator_test.py', size=10,
  147. last_update=time, src_type='s3',
  148. dest_type='local', operation_name='')
  149. dest_files.append(dest_file)
  150. ref_list.append(dest_file)
  151. files = self.comparator.call(iter(src_files), iter(dest_files))
  152. for filename in files:
  153. result_list.append(filename)
  154. self.assertEqual(result_list, ref_list)
  155. # Now try when the sync strategy says not to sync the file.
  156. self.not_at_src_sync_strategy.determine_should_sync.return_value = False
  157. result_list = []
  158. ref_list = []
  159. files = self.comparator.call(iter(src_files), iter(dest_files))
  160. for filename in files:
  161. result_list.append(filename)
  162. self.assertEqual(result_list, ref_list)
  163. def test_empty_dest(self):
  164. """
  165. Confirm the appropriate action is taken when there are no more dest
  166. files to take.
  167. """
  168. # Try when the sync strategy says to sync the file.
  169. self.not_at_dest_sync_strategy.determine_should_sync.return_value = True
  170. src_files = []
  171. dest_files = []
  172. ref_list = []
  173. result_list = []
  174. time = datetime.datetime.now()
  175. src_file = FileStat(src='', dest='',
  176. compare_key='domparator_test.py', size=10,
  177. last_update=time, src_type='local',
  178. dest_type='s3', operation_name='upload')
  179. src_files.append(src_file)
  180. ref_list.append(src_file)
  181. files = self.comparator.call(iter(src_files), iter(dest_files))
  182. for filename in files:
  183. result_list.append(filename)
  184. self.assertEqual(result_list, ref_list)
  185. # Now try when the sync strategy says not to sync the file.
  186. self.not_at_dest_sync_strategy.determine_should_sync.return_value = False
  187. result_list = []
  188. ref_list = []
  189. files = self.comparator.call(iter(src_files), iter(dest_files))
  190. for filename in files:
  191. result_list.append(filename)
  192. self.assertEqual(result_list, ref_list)
  193. def test_empty_src_dest(self):
  194. """
  195. Confirm the appropriate action is taken when there are no more
  196. files to take for both source and destination.
  197. """
  198. src_files = []
  199. dest_files = []
  200. ref_list = []
  201. result_list = []
  202. files = self.comparator.call(iter(src_files), iter(dest_files))
  203. for filename in files:
  204. result_list.append(filename)
  205. self.assertEqual(result_list, ref_list)
  206. if __name__ == "__main__":
  207. unittest.main()