test_azure_1_upload.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. from azure_storage.methods import confirm_account_match, create_blob_service_client, create_container, \
  2. extract_account_name, extract_connection_string, setup_logging, validate_container_name
  3. from azure_storage.azure_upload import AzureUpload, cli, file_upload, folder_upload
  4. from unittest.mock import patch
  5. import argparse
  6. import pytest
  7. import random
  8. import string
  9. import azure
  10. import os
  11. letters = string.ascii_lowercase
  12. long_container = ''.join(random.choice(letters) for i in range(65))
  13. @pytest.fixture(name='variables', scope='module')
  14. def setup():
  15. class Variables:
  16. def __init__(self):
  17. # Extract the account name and connection string from the system keyring prior to running tests
  18. self.passphrase = 'AzureStorage'
  19. self.account_name = extract_account_name(passphrase=self.passphrase)
  20. self.connection_string = extract_connection_string(passphrase=self.passphrase,
  21. account_name=self.account_name)
  22. self.container_name = '00000container'
  23. self.test_path = os.path.abspath(os.path.dirname(__file__))
  24. self.file_path = os.path.join(self.test_path, 'files')
  25. self.storage_tier = 'Hot'
  26. self.verbosity = 'info'
  27. return Variables()
  28. def test_account_name(variables):
  29. assert variables.account_name
  30. def test_connection_string(variables):
  31. assert variables.connection_string.startswith('DefaultEndpointsProtocol')
  32. def test_account_match(variables):
  33. assert confirm_account_match(account_name=variables.account_name,
  34. connect_str=variables.connection_string)
  35. @pytest.mark.parametrize('test_input,expected',
  36. [('12345', '12345'),
  37. ('123___45', '123-45'),
  38. ('--12345---', '12345'),
  39. ('0', '0000'),
  40. ('ABCD', 'abcd'),
  41. ('Abc123-', 'abc123'),
  42. ('@#2$5@7#', '257'),
  43. (long_container, long_container[:62])])
  44. def test_validate_container_name(test_input, expected):
  45. assert validate_container_name(container_name=test_input) == expected
  46. @pytest.mark.parametrize('test_input', ['---',
  47. '@#$@#'
  48. ''])
  49. def test_validate_container_name_fail(test_input):
  50. with pytest.raises(SystemExit):
  51. validate_container_name(container_name=test_input)
  52. def test_create_blob_service_client_invalid_connection_str():
  53. with pytest.raises(SystemExit):
  54. create_blob_service_client(connect_str='invalid_connection_string')
  55. def test_create_blob_service_client_valid(variables):
  56. variables.blob_service_client = create_blob_service_client(connect_str=variables.connection_string)
  57. assert type(variables.blob_service_client) == azure.storage.blob._blob_service_client.BlobServiceClient
  58. def test_missing_container(variables):
  59. containers = variables.blob_service_client.list_containers(include_metadata=True)
  60. assert variables.container_name not in [container['name'] for container in containers]
  61. def test_upload_file_nonexistent_container(variables):
  62. with pytest.raises(SystemExit):
  63. AzureUpload.upload_file(object_name=os.path.join(variables.file_path, 'file_1.txt'),
  64. blob_service_client=variables.blob_service_client,
  65. container_name=variables.container_name,
  66. account_name=variables.account_name,
  67. path=None,
  68. storage_tier=variables.storage_tier)
  69. def test_create_container(variables):
  70. variables.container_client = create_container(blob_service_client=variables.blob_service_client,
  71. container_name=variables.container_name)
  72. def test_container_presence(variables):
  73. containers = variables.blob_service_client.list_containers()
  74. assert variables.container_name in [container['name'] for container in containers]
  75. @pytest.mark.parametrize('file_name,path',
  76. [('file_1.txt', ''),
  77. ('file_2.txt', 'nested'),
  78. ('folder/nested_file_1.txt', ''),
  79. ('folder/nested_file_2.txt', 'nested_folder')])
  80. def test_upload_file(variables, file_name, path):
  81. AzureUpload.upload_file(object_name=os.path.join(variables.file_path, file_name),
  82. blob_service_client=variables.blob_service_client,
  83. container_name=variables.container_name,
  84. account_name=variables.account_name,
  85. path=path,
  86. storage_tier=variables.storage_tier)
  87. blobs = variables.container_client.list_blobs()
  88. assert os.path.join(path, os.path.basename(file_name)) in [blob.name for blob in blobs]
  89. @pytest.mark.parametrize('file_name,path',
  90. [('file_1.txt', ''),
  91. ('file_2.txt', 'nested'),
  92. ('folder/nested_file_1.txt', ''),
  93. ('folder/nested_file_2.txt', 'nested_folder')])
  94. def test_upload_file_exists(variables, file_name, path):
  95. with pytest.raises(SystemExit):
  96. AzureUpload.upload_file(object_name=os.path.join(variables.file_path, file_name),
  97. blob_service_client=variables.blob_service_client,
  98. container_name=variables.container_name,
  99. account_name=variables.account_name,
  100. path=path,
  101. storage_tier=variables.storage_tier)
  102. def test_upload_file_nonexistent(variables):
  103. with pytest.raises(SystemExit):
  104. AzureUpload.upload_file(object_name=os.path.join(variables.file_path, 'file_3.txt'),
  105. blob_service_client=variables.blob_service_client,
  106. container_name=variables.container_name,
  107. account_name=variables.account_name,
  108. path='',
  109. storage_tier=variables.storage_tier)
  110. def test_upload_file_none_path(variables):
  111. file_name = os.path.join(variables.file_path, 'folder/nested_file_2.txt')
  112. AzureUpload.upload_file(object_name=file_name,
  113. blob_service_client=variables.blob_service_client,
  114. container_name=variables.container_name,
  115. account_name=variables.account_name,
  116. path=None,
  117. storage_tier=variables.storage_tier)
  118. blobs = variables.container_client.list_blobs()
  119. assert os.path.basename(file_name) in [blob.name for blob in blobs]
  120. @pytest.mark.parametrize('file_name,path',
  121. [('file_1.txt', 'cool'),
  122. ('file_1', ''),
  123. ('file_1.gz', ''),
  124. ('file_2.txt', 'cool/nested'),
  125. ('folder/nested/double_nested_file_1.txt', ''),
  126. ('folder/nested/double_nested/triple_nested_file.txt', 'cool/nested')])
  127. def test_upload_file_cool(variables, file_name, path):
  128. storage_tier = 'Cool'
  129. AzureUpload.upload_file(object_name=os.path.join(variables.file_path, file_name),
  130. blob_service_client=variables.blob_service_client,
  131. container_name=variables.container_name,
  132. account_name=variables.account_name,
  133. path=path,
  134. storage_tier=storage_tier)
  135. blobs = variables.container_client.list_blobs()
  136. for blob in blobs:
  137. if blob.name == os.path.join(path, file_name):
  138. assert blob.blob_tier == storage_tier
  139. def test_upload_file_invalid_category(variables):
  140. with pytest.raises(SystemExit):
  141. file_uploader = AzureUpload(object_name=os.path.join(variables.file_path, 'file_1.txt'),
  142. account_name=variables.account_name,
  143. container_name=variables.container_name,
  144. passphrase=variables.passphrase,
  145. path='cool',
  146. storage_tier=variables.storage_tier,
  147. category='container')
  148. file_uploader.main()
  149. @patch('argparse.ArgumentParser.parse_args')
  150. def test_upload_file_integration(mock_args, variables):
  151. file_name = 'file_2.txt'
  152. path = str()
  153. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  154. account_name=variables.account_name,
  155. container_name=variables.container_name,
  156. verbosity='info',
  157. file=os.path.join(variables.file_path, file_name),
  158. reset_path=path,
  159. storage_tier=variables.storage_tier)
  160. arguments = cli()
  161. file_upload(args=arguments)
  162. blobs = variables.container_client.list_blobs()
  163. assert os.path.join(path, os.path.basename(file_name)) in [blob.name for blob in blobs]
  164. @patch('argparse.ArgumentParser.parse_args')
  165. def test_upload_file_integration_invalid_file(mock_args, variables):
  166. with pytest.raises(SystemExit):
  167. file_name = 'file_5.txt'
  168. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  169. account_name=variables.account_name,
  170. container_name=variables.container_name,
  171. verbosity='info',
  172. file=os.path.join(variables.file_path, file_name),
  173. reset_path='',
  174. storage_tier=variables.storage_tier)
  175. arguments = cli()
  176. file_upload(args=arguments)
  177. @pytest.mark.parametrize('folder_name,path,check_file',
  178. [('folder_2', '', 'folder_test_1.txt'),
  179. ('folder_2', 'nested_folder', 'folder_test_1.txt'),
  180. ('folder_2/nested_folder_2', '', 'nested_folder_test_1.txt'),
  181. ('folder_2/nested_folder_2', 'nested_folder_5', 'nested_folder_test_1.txt')])
  182. def test_upload_folder(variables, folder_name, path, check_file):
  183. AzureUpload.upload_folder(object_name=os.path.join(variables.file_path, folder_name),
  184. blob_service_client=variables.blob_service_client,
  185. container_name=variables.container_name,
  186. account_name=variables.account_name,
  187. path=path,
  188. storage_tier=variables.storage_tier)
  189. blobs = variables.container_client.list_blobs()
  190. assert os.path.join(path, os.path.basename(check_file)) in [blob.name for blob in blobs]
  191. @pytest.mark.parametrize('folder_name,path,check_file',
  192. [('folder_2', 'cool', 'folder_test_1.txt'),
  193. ('folder/nested/double_nested', '', 'double_nested_file_2'),
  194. ('folder_2/nested_folder_2', 'cool/nested', 'nested_folder_test_1.txt'),
  195. ('folder_2/nested_folder_2', 'cool_nested_folder_5', 'nested_folder_test_1.txt')])
  196. def test_upload_folder_cool(variables, folder_name, path, check_file):
  197. storage_tier = 'Cool'
  198. AzureUpload.upload_folder(object_name=os.path.join(variables.file_path, folder_name),
  199. blob_service_client=variables.blob_service_client,
  200. container_name=variables.container_name,
  201. account_name=variables.account_name,
  202. path=path,
  203. storage_tier=storage_tier)
  204. blobs = variables.container_client.list_blobs()
  205. for blob in blobs:
  206. if blob.name == os.path.join(path, check_file):
  207. assert blob.blob_tier == storage_tier
  208. @pytest.mark.parametrize('folder_name,check_file',
  209. [('folder_2/nested_folder_2', 'nested_folder_test_1.txt'),
  210. ('folder_2/nested_folder_2', 'nested_folder_test_1.txt')])
  211. def test_upload_folder_none_path(variables, folder_name, check_file):
  212. AzureUpload.upload_folder(object_name=os.path.join(variables.file_path, folder_name),
  213. blob_service_client=variables.blob_service_client,
  214. container_name=variables.container_name,
  215. account_name=variables.account_name,
  216. path=None,
  217. storage_tier=variables.storage_tier)
  218. blobs = variables.container_client.list_blobs()
  219. assert check_file in [blob.name for blob in blobs]
  220. @patch('argparse.ArgumentParser.parse_args')
  221. def test_upload_folder_integration(mock_args, variables):
  222. folder_name = 'folder/nested/double_nested'
  223. path = 'single_nested'
  224. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  225. account_name=variables.account_name,
  226. container_name=variables.container_name,
  227. verbosity='info',
  228. folder=os.path.join(variables.file_path, folder_name),
  229. reset_path=path,
  230. storage_tier=variables.storage_tier)
  231. arguments = cli()
  232. folder_upload(args=arguments)
  233. blobs = variables.container_client.list_blobs()
  234. assert os.path.join(path, 'triple_nested_file.txt') in [blob.name for blob in blobs]
  235. @patch('argparse.ArgumentParser.parse_args')
  236. def test_upload_folder_integration_invalid(mock_args, variables):
  237. with pytest.raises(SystemExit):
  238. folder_name = 'folder/nested/double_nested/triple_nested'
  239. path = 'triple_nested'
  240. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  241. account_name=variables.account_name,
  242. container_name=variables.container_name,
  243. verbosity='info',
  244. folder=os.path.join(variables.file_path, folder_name),
  245. reset_path=path,
  246. storage_tier=variables.storage_tier)
  247. arguments = cli()
  248. folder_upload(args=arguments)
  249. def test_setup_logging(variables):
  250. setup_logging(arguments=variables)