test_azure_3_move.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. from azure_storage.methods import extract_account_name, move_prep
  2. from azure_storage.azure_move import AzureContainerMove, AzureMove, cli, container_move, file_move, folder_move
  3. from unittest.mock import patch
  4. import argparse
  5. import pytest
  6. import azure
  7. import os
  8. @pytest.fixture(name='variables', scope='module')
  9. def setup():
  10. class Variables:
  11. def __init__(self):
  12. # Extract the account name and connection string from the system keyring prior to running tests
  13. self.passphrase = 'AzureStorage'
  14. self.account_name = extract_account_name(passphrase=self.passphrase)
  15. self.container_name = '00000container'
  16. self.target_container = '000000container'
  17. self.storage_tier = 'Hot'
  18. return Variables()
  19. def run_move_prep(variables):
  20. variables.container_name, variables.target_container, variables.blob_service_client, \
  21. variables.source_container_client, variables.target_container_client = move_prep(
  22. passphrase=variables.passphrase,
  23. account_name=variables.account_name,
  24. container_name=variables.container_name,
  25. target_container=variables.target_container)
  26. def test_move_prep(variables):
  27. run_move_prep(variables=variables)
  28. assert type(variables.source_container_client) is azure.storage.blob._container_client.ContainerClient
  29. def test_move_prep_target_exists(variables):
  30. run_move_prep(variables=variables)
  31. assert type(variables.target_container_client) is azure.storage.blob._container_client.ContainerClient
  32. @pytest.mark.parametrize('file_name,path',
  33. [('file_1.txt', ''),
  34. ('file_1', ''),
  35. ('file_1.gz', ''),
  36. ('file_2.txt', 'nested'),
  37. ('nested_folder/folder_test_1.txt', ''),
  38. ('nested_folder/nested_folder_2/nested_folder_test_1.txt', 'ABC'),
  39. ('nested_folder/nested_folder_2/nested_folder_test_1.txt', 'ABC/123'),
  40. ('nested/file_2.txt', 'nested_folder_2')])
  41. def test_move_file(variables, file_name, path):
  42. AzureMove.move_file(source_container_client=variables.source_container_client,
  43. object_name=file_name,
  44. blob_service_client=variables.blob_service_client,
  45. container_name=variables.container_name,
  46. target_container=variables.target_container,
  47. path=path,
  48. storage_tier=variables.storage_tier)
  49. blobs = variables.target_container_client.list_blobs()
  50. assert os.path.join(path, os.path.basename(file_name)) in [blob.name for blob in blobs]
  51. @pytest.mark.parametrize('file_name,path',
  52. [('file_7.txt', ''),
  53. ('nonexistent/file_2.txt', 'nested_2'),
  54. ('nested/file_3.txt', 'nested_folder_2')])
  55. def test_move_file_invalid(variables, file_name, path):
  56. with pytest.raises(SystemExit):
  57. AzureMove.move_file(source_container_client=variables.source_container_client,
  58. object_name=file_name,
  59. blob_service_client=variables.blob_service_client,
  60. container_name=variables.container_name,
  61. target_container=variables.target_container,
  62. path=path,
  63. storage_tier=variables.storage_tier)
  64. @pytest.mark.parametrize('file_name,path',
  65. [('nested_file_1.txt', ''),
  66. ('nested_folder/nested_file_2.txt', 'nested')])
  67. def test_move_file_cool(variables, file_name, path):
  68. storage_tier = 'Cool'
  69. AzureMove.move_file(source_container_client=variables.source_container_client,
  70. object_name=file_name,
  71. blob_service_client=variables.blob_service_client,
  72. container_name=variables.container_name,
  73. target_container=variables.target_container,
  74. path=path,
  75. storage_tier=storage_tier)
  76. blobs = variables.target_container_client.list_blobs()
  77. for blob in blobs:
  78. if blob.name == os.path.join(path, file_name):
  79. assert blob.blob_tier == storage_tier
  80. @pytest.mark.parametrize('folder_name,path,check_file',
  81. [('single_nested', '', 'triple_nested_file.txt'),
  82. ('nested_folder/nested_folder_2', 'nested_folder_3', 'nested_folder_test_1.txt'),
  83. ('nested_folder/nested_folder_2', '', 'nested_folder_test_1.txt')])
  84. def test_move_folder(variables, folder_name, path, check_file):
  85. AzureMove.move_folder(source_container_client=variables.source_container_client,
  86. object_name=folder_name,
  87. blob_service_client=variables.blob_service_client,
  88. container_name=variables.container_name,
  89. target_container=variables.target_container,
  90. path=path,
  91. storage_tier=variables.storage_tier,
  92. category='folder')
  93. blobs = variables.target_container_client.list_blobs()
  94. assert os.path.join(path, os.path.basename(check_file)) in [blob.name for blob in blobs]
  95. @pytest.mark.parametrize('folder_name,path,check_file',
  96. [('cool/nested_folder_2', '', 'nested_folder_test_1.txt'),
  97. ('cool_nested_folder_5', 'hot', 'nested_folder_test_1.txt')])
  98. def test_move_folder_cool(variables, folder_name, path, check_file):
  99. AzureMove.move_folder(source_container_client=variables.source_container_client,
  100. object_name=folder_name,
  101. blob_service_client=variables.blob_service_client,
  102. container_name=variables.container_name,
  103. target_container=variables.target_container,
  104. path=path,
  105. storage_tier=variables.storage_tier,
  106. category='folder')
  107. blobs = variables.target_container_client.list_blobs()
  108. for blob in blobs:
  109. if blob.name == os.path.join(path, check_file):
  110. assert blob.blob_tier == variables.storage_tier
  111. @pytest.mark.parametrize('folder_name,path',
  112. [('nested_folder_6', ''),
  113. ('nested_folder_2/nested_folder_2', 'nested_folder'),
  114. ('nested_folder/nested_folder_3', '')])
  115. def test_move_folder_invalid(variables, folder_name, path):
  116. with pytest.raises(SystemExit):
  117. AzureMove.move_folder(source_container_client=variables.source_container_client,
  118. object_name=folder_name,
  119. blob_service_client=variables.blob_service_client,
  120. container_name=variables.container_name,
  121. target_container=variables.target_container,
  122. path=path,
  123. storage_tier=variables.storage_tier,
  124. category='folder')
  125. def test_move_folder_invalid_category(variables):
  126. with pytest.raises(SystemExit):
  127. move_folder = AzureMove(object_name='cool/nested_folder_2',
  128. container_name=variables.container_name,
  129. account_name=variables.account_name,
  130. passphrase=variables.passphrase,
  131. target_container=variables.target_container,
  132. path=None,
  133. storage_tier=variables.storage_tier,
  134. category='container')
  135. move_folder.main()
  136. def test_move_container(variables):
  137. path = 'nested_container'
  138. AzureContainerMove.move_container(source_container_client=variables.source_container_client,
  139. blob_service_client=variables.blob_service_client,
  140. container_name=variables.container_name,
  141. target_container=variables.target_container,
  142. path=path,
  143. storage_tier=variables.storage_tier)
  144. blobs = variables.target_container_client.list_blobs()
  145. assert os.path.join(path, 'file_2.txt') in [blob.name for blob in blobs]
  146. @patch('argparse.ArgumentParser.parse_args')
  147. def test_move_file_integration(mock_args, variables):
  148. file_name = 'file_1.txt'
  149. reset_path = 'file_integration'
  150. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  151. account_name=variables.account_name,
  152. container_name=variables.container_name,
  153. target_container=variables.target_container,
  154. reset_path=reset_path,
  155. verbosity='info',
  156. file=file_name,
  157. storage_tier=variables.storage_tier)
  158. arguments = cli()
  159. file_move(arguments)
  160. blobs = variables.target_container_client.list_blobs()
  161. assert os.path.join(reset_path, file_name) in [blob.name for blob in blobs]
  162. @patch('argparse.ArgumentParser.parse_args')
  163. def test_move_file_integration_cool(mock_args, variables):
  164. file_name = 'nested_file_1.txt'
  165. reset_path = 'cool_file_integration'
  166. storage_tier = 'Cool'
  167. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  168. account_name=variables.account_name,
  169. container_name=variables.container_name,
  170. target_container=variables.target_container,
  171. reset_path=reset_path,
  172. verbosity='info',
  173. file=file_name,
  174. storage_tier=storage_tier)
  175. arguments = cli()
  176. file_move(arguments)
  177. blobs = variables.target_container_client.list_blobs()
  178. for blob in blobs:
  179. if blob.name == os.path.join(reset_path, file_name):
  180. assert blob.blob_tier == storage_tier
  181. @patch('argparse.ArgumentParser.parse_args')
  182. def test_move_folder_integration(mock_args, variables):
  183. folder_name = 'nested_folder_5'
  184. reset_path = 'folder_integration'
  185. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  186. account_name=variables.account_name,
  187. container_name=variables.container_name,
  188. target_container=variables.target_container,
  189. reset_path=reset_path,
  190. verbosity='info',
  191. folder=folder_name,
  192. storage_tier=variables.storage_tier)
  193. arguments = cli()
  194. folder_move(arguments)
  195. blobs = variables.target_container_client.list_blobs()
  196. assert os.path.join(reset_path, 'nested_folder_test_1.txt') in [blob.name for blob in blobs]
  197. @patch('argparse.ArgumentParser.parse_args')
  198. def test_move_container_integration(mock_args, variables):
  199. reset_path = 'container_integration'
  200. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  201. account_name=variables.account_name,
  202. container_name=variables.container_name,
  203. target_container=variables.target_container,
  204. reset_path=reset_path,
  205. verbosity='info',
  206. storage_tier=variables.storage_tier)
  207. arguments = cli()
  208. container_move(arguments)
  209. blobs = variables.target_container_client.list_blobs()
  210. assert os.path.join(reset_path, 'double_nested_file_1.txt') in [blob.name for blob in blobs]