test_azure_4_download.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. from azure_storage.methods import client_prep, extract_account_name
  2. from azure_storage.azure_download import AzureContainerDownload, AzureDownload, cli, container_download, \
  3. file_download, folder_download
  4. from unittest.mock import patch
  5. import argparse
  6. import pathlib
  7. import pytest
  8. import shutil
  9. import azure
  10. import os
  11. @pytest.fixture(name='variables', scope='module')
  12. def setup():
  13. class Variables:
  14. def __init__(self):
  15. # Extract the account name and connection string from the system keyring prior to running tests
  16. self.passphrase = 'AzureStorage'
  17. self.account_name = extract_account_name(passphrase=self.passphrase)
  18. self.container_name = '000000container'
  19. self.test_path = os.path.abspath(os.path.dirname(__file__))
  20. self.file_path = os.path.join(self.test_path, 'files')
  21. self.output_path = os.path.join(self.file_path, 'downloads')
  22. return Variables()
  23. def test_client_prep(variables):
  24. variables.container_name, variables.connect_str, variables.blob_service_client, variables.container_client = \
  25. client_prep(container_name=variables.container_name,
  26. passphrase=variables.passphrase,
  27. account_name=variables.account_name)
  28. assert type(variables.container_client) is azure.storage.blob._container_client.ContainerClient
  29. @pytest.mark.parametrize('file_name',
  30. ['file_1.txt',
  31. 'container_integration/file_2.txt',
  32. 'nested_container/nested_folder/nested_folder_2/nested_folder_test_1.txt',
  33. 'ABC/123/nested_folder_test_1.txt'])
  34. def test_download_file(variables, file_name):
  35. output_path = os.path.join(variables.output_path, 'files')
  36. os.makedirs(output_path, exist_ok=True)
  37. AzureDownload.download_file(container_client=variables.container_client,
  38. blob_service_client=variables.blob_service_client,
  39. container_name=variables.container_name,
  40. object_name=file_name,
  41. output_path=output_path)
  42. assert os.path.isfile(os.path.join(output_path, os.path.basename(file_name)))
  43. @pytest.mark.parametrize('file_name',
  44. ['file_2.txt',
  45. 'container_integration_1/file_2.txt',
  46. 'nested_container/nested_folder/nested_folder_2/nested_folder_test_1.zip',
  47. 'ABC/1234/nested_folder_test_1.txt'])
  48. def test_download_file_invalid(variables, file_name):
  49. output_path = os.path.join(variables.output_path, 'files')
  50. with pytest.raises(SystemExit):
  51. AzureDownload.download_file(container_client=variables.container_client,
  52. blob_service_client=variables.blob_service_client,
  53. container_name=variables.container_name,
  54. object_name=file_name,
  55. output_path=output_path)
  56. def test_download_file_invalid_category(variables):
  57. with pytest.raises(SystemExit):
  58. file_downloader = AzureDownload(object_name='file_1.txt',
  59. container_name='000000000container',
  60. output_path=variables.output_path,
  61. account_name=variables.account_name,
  62. passphrase=variables.passphrase,
  63. category='file')
  64. file_downloader.main()
  65. def test_download_file_invalid_container(variables):
  66. with pytest.raises(SystemExit):
  67. file_downloader = AzureDownload(object_name='file_1.txt',
  68. container_name=variables.container_name,
  69. output_path=variables.output_path,
  70. account_name=variables.account_name,
  71. passphrase=variables.passphrase,
  72. category='container')
  73. file_downloader.main()
  74. @patch('argparse.ArgumentParser.parse_args')
  75. def test_download_file_integration(mock_args, variables):
  76. output_path = os.path.join(variables.output_path, 'files_integration')
  77. file_name = 'nested/file_2.txt'
  78. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  79. account_name=variables.account_name,
  80. container_name=variables.container_name,
  81. output_path=output_path,
  82. verbosity='info',
  83. file=file_name)
  84. arguments = cli()
  85. file_download(arguments)
  86. assert os.path.isfile(os.path.join(output_path, os.path.basename(file_name)))
  87. @patch('argparse.ArgumentParser.parse_args')
  88. def test_download_file_integration_missing(mock_args, variables):
  89. output_path = os.path.join(variables.output_path, 'files_integration')
  90. file_name = 'nested/file_3.txt'
  91. with pytest.raises(SystemExit):
  92. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  93. account_name=variables.account_name,
  94. container_name=variables.container_name,
  95. output_path=output_path,
  96. verbosity='info',
  97. file=file_name)
  98. arguments = cli()
  99. file_download(arguments)
  100. @pytest.mark.parametrize('folder_name,check_file',
  101. [('container_integration/', 'file_2.txt'),
  102. ('nested_container/nested_folder/nested_folder_2', 'nested_folder_test_1.txt'),
  103. ('ABC/123/', 'nested_folder_test_1.txt')])
  104. def test_download_folder(variables, folder_name, check_file):
  105. output_path = os.path.join(variables.output_path, 'folders')
  106. os.makedirs(output_path, exist_ok=True)
  107. AzureDownload.download_folder(container_client=variables.container_client,
  108. blob_service_client=variables.blob_service_client,
  109. container_name=variables.container_name,
  110. object_name=folder_name,
  111. output_path=output_path)
  112. assert os.path.isfile(os.path.join(output_path, folder_name, os.path.basename(check_file)))
  113. def test_download_folder_tilde(variables):
  114. path_obj = pathlib.Path(variables.output_path)
  115. path = f'~{os.sep}{path_obj.relative_to(pathlib.Path.home())}'
  116. folder_downloader = AzureDownload(object_name='container_integration/',
  117. container_name=variables.container_name,
  118. output_path=path,
  119. account_name=variables.account_name,
  120. passphrase=variables.passphrase,
  121. category='folder')
  122. folder_downloader.main()
  123. def test_download_folder_invalid_path(variables):
  124. if not os.environ.get('CIRCLECI'):
  125. with pytest.raises(SystemExit):
  126. path = '/invalid'
  127. folder_downloader = AzureDownload(object_name='container_integration/',
  128. container_name=variables.container_name,
  129. output_path=path,
  130. account_name=variables.account_name,
  131. passphrase=variables.passphrase,
  132. category='folder')
  133. folder_downloader.main()
  134. def test_download_folder_invalid_container(variables):
  135. with pytest.raises(SystemExit):
  136. folder_downloader = AzureDownload(object_name='container_integration/',
  137. container_name='000000000container',
  138. output_path=variables.output_path,
  139. account_name=variables.account_name,
  140. passphrase=variables.passphrase,
  141. category='folder')
  142. folder_downloader.main()
  143. def test_download_container_tilde(variables):
  144. path_obj = pathlib.Path(variables.output_path)
  145. path = f'~{os.sep}{path_obj.relative_to(pathlib.Path.home())}'
  146. container_downloader = AzureContainerDownload(container_name=variables.container_name,
  147. output_path=path,
  148. account_name=variables.account_name,
  149. passphrase=variables.passphrase)
  150. container_downloader.main()
  151. def test_download_container_invalid_path(variables):
  152. if not os.environ.get('CIRCLECI'):
  153. with pytest.raises(SystemExit):
  154. path = '/invalid'
  155. container_downloader = AzureContainerDownload(container_name=variables.container_name,
  156. output_path=path,
  157. account_name=variables.account_name,
  158. passphrase=variables.passphrase)
  159. container_downloader.main()
  160. def test_download_container_invalid(variables):
  161. with pytest.raises(SystemExit):
  162. container_downloader = AzureContainerDownload(container_name='000000000container',
  163. output_path=variables.output_path,
  164. account_name=variables.account_name,
  165. passphrase=variables.passphrase)
  166. container_downloader.main()
  167. @pytest.mark.parametrize('folder_name',
  168. ['container-integration/',
  169. 'nested_container/nested_folder_1/nested_folder_2',
  170. 'ABC/1234/'])
  171. def test_download_folder_invalid(variables, folder_name):
  172. output_path = os.path.join(variables.output_path, 'folders')
  173. with pytest.raises(SystemExit):
  174. AzureDownload.download_folder(container_client=variables.container_client,
  175. blob_service_client=variables.blob_service_client,
  176. container_name=variables.container_name,
  177. object_name=folder_name,
  178. output_path=output_path)
  179. @patch('argparse.ArgumentParser.parse_args')
  180. def test_download_folder_integration(mock_args, variables):
  181. output_path = os.path.join(variables.output_path, 'folder_integration')
  182. folder_name = 'container_integration/nested_folder'
  183. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  184. account_name=variables.account_name,
  185. container_name=variables.container_name,
  186. output_path=output_path,
  187. verbosity='info',
  188. folder=folder_name)
  189. arguments = cli()
  190. folder_download(arguments)
  191. assert os.path.isfile(os.path.join(output_path, folder_name, 'folder_test_1.txt'))
  192. @patch('argparse.ArgumentParser.parse_args')
  193. def test_download_folder_integration_missing(mock_args, variables):
  194. output_path = os.path.join(variables.output_path, 'folder_integration')
  195. folder_name = 'container_integration/nested_folder_3'
  196. with pytest.raises(SystemExit):
  197. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  198. account_name=variables.account_name,
  199. container_name=variables.container_name,
  200. output_path=output_path,
  201. verbosity='info',
  202. folder=folder_name)
  203. arguments = cli()
  204. folder_download(arguments)
  205. @patch('argparse.ArgumentParser.parse_args')
  206. def test_download_container_integration(mock_args, variables):
  207. output_path = os.path.join(variables.output_path, 'container_integration')
  208. os.makedirs(output_path, exist_ok=True)
  209. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  210. account_name=variables.account_name,
  211. container_name=variables.container_name,
  212. output_path=output_path,
  213. verbosity='info')
  214. arguments = cli()
  215. container_download(arguments)
  216. assert os.path.isfile(os.path.join(output_path, variables.container_name,
  217. 'container_integration/single_nested/triple_nested_file.txt'))
  218. def test_remove_downloads(variables):
  219. shutil.rmtree(variables.output_path)
  220. assert not os.path.isdir(variables.output_path)