test_azure_8_automate.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. from azure_storage.methods import create_batch_dict, create_blob_service_client, create_container, \
  2. extract_account_name, extract_connection_string
  3. from azure_storage.azure_automate import file_upload, folder_upload, container_sas, file_sas, folder_sas, \
  4. container_move, file_move, folder_move, container_download, file_download, folder_download, container_tier, \
  5. file_tier, folder_tier, container_delete, file_delete, folder_delete, batch, cli
  6. from unittest.mock import patch
  7. import argparse
  8. import pytest
  9. import shutil
  10. import os
  11. @pytest.fixture(name='variables', scope='module')
  12. def setup():
  13. class Variables:
  14. def __init__(self):
  15. self.passphrase = 'AzureStorage'
  16. self.account_name = extract_account_name(passphrase=self.passphrase)
  17. self.connection_string = extract_connection_string(passphrase=self.passphrase,
  18. account_name=self.account_name)
  19. self.blob_service_client = create_blob_service_client(connect_str=self.connection_string)
  20. self.container_name = '0container'
  21. self.target_container = '00container'
  22. self.file_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'files')
  23. self.batch_path = os.path.join(self.file_path, 'batch')
  24. self.sas_path = os.path.join(self.file_path, 'sas')
  25. self.download_path = os.path.join(self.file_path, 'download')
  26. os.makedirs(self.download_path, exist_ok=True)
  27. return Variables()
  28. def create_container_client(variables, container_name):
  29. container_client = create_container(blob_service_client=variables.blob_service_client,
  30. container_name=container_name)
  31. return container_client
  32. def read_contents(output_file):
  33. contents = open(output_file, 'r').readlines()
  34. return contents
  35. def delete_output_file(output_file):
  36. os.remove(output_file)
  37. assert not os.path.isfile(output_file)
  38. @patch('argparse.ArgumentParser.parse_args')
  39. def test_batch_upload_file_integration(mock_args, variables):
  40. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  41. account_name=variables.account_name,
  42. verbosity='info',
  43. batch_file=os.path.join(variables.batch_path, 'upload_file.tsv'))
  44. arguments = cli()
  45. file_upload(args=arguments)
  46. variables.container_client = create_container_client(variables=variables,
  47. container_name=variables.container_name)
  48. blobs = variables.container_client.list_blobs()
  49. assert 'upload/file_1.txt' in [blob.name for blob in blobs]
  50. @patch('argparse.ArgumentParser.parse_args')
  51. def test_batch_upload_folder_integration(mock_args, variables):
  52. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  53. account_name=variables.account_name,
  54. verbosity='info',
  55. batch_file=os.path.join(variables.batch_path, 'upload_folder.tsv'))
  56. arguments = cli()
  57. folder_upload(args=arguments)
  58. blobs = variables.container_client.list_blobs()
  59. assert 'nested_folder/nested/double_nested_file_1.txt' in [blob.name for blob in blobs]
  60. @patch('argparse.ArgumentParser.parse_args')
  61. def test_batch_file_sas_integration(mock_args, variables):
  62. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  63. account_name=variables.account_name,
  64. verbosity='info',
  65. batch_file=os.path.join(variables.batch_path, 'sas_file.tsv'))
  66. arguments = cli()
  67. file_sas(args=arguments)
  68. contents = read_contents(output_file=os.path.join(variables.sas_path, 'sas_test.txt'))
  69. assert contents[0] \
  70. .startswith(f'https://{variables.account_name}.blob.core.windows.net/{variables.container_name}/')
  71. delete_output_file(output_file='sas_urls.txt')
  72. delete_output_file(output_file=os.path.join(variables.sas_path, 'sas_test.txt'))
  73. delete_output_file(output_file=os.path.join(variables.sas_path, 'sas_test_1.txt'))
  74. @patch('argparse.ArgumentParser.parse_args')
  75. def test_batch_folder_sas_integration(mock_args, variables):
  76. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  77. account_name=variables.account_name,
  78. verbosity='info',
  79. batch_file=os.path.join(variables.batch_path, 'sas_folder.tsv'))
  80. arguments = cli()
  81. folder_sas(args=arguments)
  82. contents = read_contents(output_file=os.path.join(variables.sas_path, 'sas_test_1.txt'))
  83. assert contents[0] \
  84. .startswith(f'https://{variables.account_name}.blob.core.windows.net/{variables.container_name}/')
  85. delete_output_file(output_file='sas_test.txt')
  86. delete_output_file(output_file='sas_urls.txt')
  87. delete_output_file(output_file=os.path.join(variables.sas_path, 'sas_test_1.txt'))
  88. @patch('argparse.ArgumentParser.parse_args')
  89. def test_batch_container_sas_integration(mock_args, variables):
  90. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  91. account_name=variables.account_name,
  92. verbosity='info',
  93. batch_file=os.path.join(variables.batch_path, 'sas_container.tsv'))
  94. arguments = cli()
  95. container_sas(args=arguments)
  96. contents = read_contents(output_file=os.path.join(variables.sas_path, 'sas_test_1.txt'))
  97. assert contents[0] \
  98. .startswith(f'https://{variables.account_name}.blob.core.windows.net/{variables.container_name}/')
  99. delete_output_file(output_file='sas_urls.txt')
  100. delete_output_file(output_file=os.path.join(variables.sas_path, 'sas_test.txt'))
  101. delete_output_file(output_file=os.path.join(variables.sas_path, 'sas_test_1.txt'))
  102. shutil.rmtree(variables.sas_path)
  103. @patch('argparse.ArgumentParser.parse_args')
  104. def test_batch_file_move_integration(mock_args, variables):
  105. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  106. account_name=variables.account_name,
  107. verbosity='info',
  108. batch_file=os.path.join(variables.batch_path, 'move_file.tsv'))
  109. arguments = cli()
  110. file_move(args=arguments)
  111. variables.target_client = create_container_client(variables=variables,
  112. container_name=variables.target_container)
  113. blobs = variables.target_client.list_blobs()
  114. assert 'move_files/folder_test_1.txt' in [blob.name for blob in blobs]
  115. @patch('argparse.ArgumentParser.parse_args')
  116. def test_batch_folder_move_integration(mock_args, variables):
  117. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  118. account_name=variables.account_name,
  119. verbosity='info',
  120. batch_file=os.path.join(variables.batch_path, 'move_folder.tsv'))
  121. arguments = cli()
  122. folder_move(args=arguments)
  123. blobs = variables.target_client.list_blobs()
  124. assert 'move_folder/file_1.txt' in [blob.name for blob in blobs]
  125. @patch('argparse.ArgumentParser.parse_args')
  126. def test_batch_container_move_integration(mock_args, variables):
  127. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  128. account_name=variables.account_name,
  129. verbosity='info',
  130. batch_file=os.path.join(variables.batch_path, 'move_container.tsv'))
  131. arguments = cli()
  132. container_move(args=arguments)
  133. blobs = variables.target_client.list_blobs()
  134. assert 'nested_1/upload/file_1.txt' in [blob.name for blob in blobs]
  135. @patch('argparse.ArgumentParser.parse_args')
  136. def test_batch_file_download_integration(mock_args, variables):
  137. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  138. account_name=variables.account_name,
  139. verbosity='info',
  140. batch_file=os.path.join(variables.batch_path, 'download_file.tsv'))
  141. arguments = cli()
  142. file_download(args=arguments)
  143. assert os.path.isfile('file_1.txt')
  144. delete_output_file(output_file='file_1.txt')
  145. @patch('argparse.ArgumentParser.parse_args')
  146. def test_batch_folder_download_integration(mock_args, variables):
  147. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  148. account_name=variables.account_name,
  149. verbosity='info',
  150. batch_file=os.path.join(variables.batch_path, 'download_folder.tsv'))
  151. arguments = cli()
  152. folder_download(args=arguments)
  153. assert os.path.isfile(os.path.join('move_folder', 'file_1.txt'))
  154. shutil.rmtree('move_folder')
  155. @patch('argparse.ArgumentParser.parse_args')
  156. def test_batch_container_download_integration(mock_args, variables):
  157. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  158. account_name=variables.account_name,
  159. verbosity='info',
  160. batch_file=os.path.join(variables.batch_path, 'download_container.tsv'))
  161. arguments = cli()
  162. container_download(args=arguments)
  163. assert os.path.isfile(os.path.join(variables.download_path, variables.target_container, 'nested_1', 'file_1.txt'))
  164. shutil.rmtree(variables.target_container)
  165. @patch('argparse.ArgumentParser.parse_args')
  166. def test_batch_file_tier_integration(mock_args, variables):
  167. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  168. account_name=variables.account_name,
  169. verbosity='info',
  170. batch_file=os.path.join(variables.batch_path, 'tier_file.tsv'))
  171. arguments = cli()
  172. file_tier(args=arguments)
  173. blobs = variables.target_client.list_blobs()
  174. for blob in blobs:
  175. if blob.name == os.path.join('nested_1', 'file_1.txt'):
  176. assert blob.blob_tier == 'Cool'
  177. @patch('argparse.ArgumentParser.parse_args')
  178. def test_batch_folder_tier_integration(mock_args, variables):
  179. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  180. account_name=variables.account_name,
  181. verbosity='info',
  182. batch_file=os.path.join(variables.batch_path, 'tier_folder.tsv'))
  183. arguments = cli()
  184. folder_tier(args=arguments)
  185. blobs = variables.target_client.list_blobs()
  186. for blob in blobs:
  187. if blob.name == os.path.join('nested_1', 'renamed', 'nested_file_2.txt'):
  188. assert blob.blob_tier == 'Cool'
  189. @patch('argparse.ArgumentParser.parse_args')
  190. def test_batch_container_tier_integration(mock_args, variables):
  191. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  192. account_name=variables.account_name,
  193. verbosity='info',
  194. batch_file=os.path.join(variables.batch_path, 'tier_container.tsv'))
  195. arguments = cli()
  196. container_tier(args=arguments)
  197. blobs = variables.target_client.list_blobs()
  198. for blob in blobs:
  199. if blob.name == os.path.join('nested_1', 'file_1.txt'):
  200. assert blob.blob_tier == 'Cool'
  201. @patch('argparse.ArgumentParser.parse_args')
  202. def test_batch_file_delete_integration(mock_args, variables):
  203. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  204. account_name=variables.account_name,
  205. verbosity='info',
  206. batch_file=os.path.join(variables.batch_path, 'delete_file.tsv'))
  207. arguments = cli()
  208. file_delete(args=arguments)
  209. blobs = variables.target_client.list_blobs()
  210. assert 'nested_1/renamed/nested/double_nested_file_2.txt' not in [blob.name for blob in blobs]
  211. @patch('argparse.ArgumentParser.parse_args')
  212. def test_batch_folder_delete_integration(mock_args, variables):
  213. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  214. account_name=variables.account_name,
  215. verbosity='info',
  216. batch_file=os.path.join(variables.batch_path, 'delete_folder.tsv'))
  217. arguments = cli()
  218. folder_delete(args=arguments)
  219. blobs = variables.target_client.list_blobs()
  220. assert 'move_folder/file_1.txt' not in [blob.name for blob in blobs]
  221. @patch('argparse.ArgumentParser.parse_args')
  222. def test_batch_container_delete_integration(mock_args, variables):
  223. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  224. account_name=variables.account_name,
  225. verbosity='info',
  226. batch_file=os.path.join(variables.batch_path, 'delete_container.tsv'))
  227. arguments = cli()
  228. container_delete(args=arguments)
  229. with pytest.raises(SystemExit) as e:
  230. create_container_client(variables=variables,
  231. container_name=variables.target_container)
  232. assert 'The specified container is being deleted. Try operation later.' in str(e)
  233. @patch('argparse.ArgumentParser.parse_args')
  234. def test_batch(mock_args, variables):
  235. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  236. account_name=variables.account_name,
  237. verbosity='info',
  238. batch_file=os.path.join(variables.batch_path, 'batch.tsv'))
  239. arguments = cli()
  240. batch(args=arguments)
  241. delete_output_file(output_file='file_1.txt')
  242. delete_output_file(output_file='sas_test.txt')
  243. delete_output_file(output_file='sas_urls.txt')
  244. shutil.rmtree('move_folder')
  245. shutil.rmtree('0000container')
  246. shutil.rmtree(variables.download_path)
  247. shutil.rmtree(variables.sas_path)
  248. with pytest.raises(SystemExit) as e:
  249. create_container_client(variables=variables,
  250. container_name='0000container')
  251. assert 'The specified container is being deleted. Try operation later.' in str(e)
  252. @patch('argparse.ArgumentParser.parse_args')
  253. def test_batch_invalid_batch_path(mock_args, variables):
  254. with pytest.raises(SystemExit):
  255. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  256. account_name=variables.account_name,
  257. verbosity='info',
  258. batch_file=os.path.join('/invalid', 'batch.tsv'))
  259. arguments = cli()
  260. batch(args=arguments)
  261. @patch('argparse.ArgumentParser.parse_args')
  262. def test_batch_invalid_batch_file(mock_args, variables):
  263. with pytest.raises(SystemExit):
  264. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  265. account_name=variables.account_name,
  266. verbosity='info',
  267. batch_file=os.path.join(variables.batch_path, 'invalid.tsv'))
  268. arguments = cli()
  269. batch(args=arguments)
  270. def test_invalid_batch_file(variables):
  271. with pytest.raises(SystemExit):
  272. create_batch_dict(batch_file=os.path.join(variables.batch_path, 'invalid.tsv'),
  273. headers=list())
  274. @patch('argparse.ArgumentParser.parse_args')
  275. def test_batch_malformed_batch_file(mock_args, variables):
  276. with pytest.raises(SystemExit):
  277. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  278. account_name=variables.account_name,
  279. verbosity='info',
  280. batch_file=os.path.join(variables.batch_path, 'batch_invalid.tsv'))
  281. arguments = cli()
  282. batch(args=arguments)
  283. @patch('argparse.ArgumentParser.parse_args')
  284. def test_batch_extra_columns_batch_file(mock_args, variables):
  285. with pytest.raises(SystemExit):
  286. mock_args.return_value = argparse.Namespace(
  287. passphrase=variables.passphrase,
  288. account_name=variables.account_name,
  289. verbosity='info',
  290. batch_file=os.path.join(variables.batch_path, 'batch_invalid_1.tsv'))
  291. arguments = cli()
  292. batch(args=arguments)