test_azure_6_list.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. from azure_storage.methods import client_prep, create_blob_service_client, extract_account_name, \
  2. extract_connection_string
  3. from azure_storage.azure_list import AzureContainerList, AzureList, azure_search, cli, container_search
  4. from unittest.mock import patch
  5. import argparse
  6. import pathlib
  7. import pytest
  8. import os
  9. @pytest.fixture(name='variables', scope='module')
  10. def setup():
  11. class Variables:
  12. def __init__(self):
  13. self.passphrase = 'AzureStorage'
  14. self.container_name = '000000container'
  15. self.account_name = extract_account_name(passphrase=self.passphrase)
  16. self.test_path = os.path.abspath(os.path.dirname(__file__))
  17. self.file_path = os.path.join(self.test_path, 'files')
  18. self.output_file = os.path.join(self.file_path, 'search_outputs.txt')
  19. self.connection_string = extract_connection_string(passphrase=self.passphrase,
  20. account_name=self.account_name)
  21. self.blob_service_client = create_blob_service_client(connect_str=self.connection_string)
  22. return Variables()
  23. def read_output_file(output_file):
  24. contents = list()
  25. with open(output_file, 'r') as output:
  26. for line in output:
  27. contents.append(line.rstrip())
  28. return contents
  29. def delete_output_file(output_file):
  30. os.remove(output_file)
  31. def parse_file_outputs(check_file, contents):
  32. hit = False
  33. for entry in contents:
  34. if check_file in [os.path.basename(component) for component in entry.split('\t')]:
  35. hit = True
  36. return hit
  37. def test_client_prep(variables):
  38. variables.container_name, variables.connect_str, variables.blob_service_client, variables.container_client = \
  39. client_prep(container_name=variables.container_name,
  40. passphrase=variables.passphrase,
  41. account_name=variables.account_name)
  42. assert variables.connect_str.startswith('DefaultEndpointsProtocol')
  43. @pytest.mark.parametrize('expression',
  44. ['000000container',
  45. '0000\\d{2}container',
  46. '0000\\d{2}\\D{9}',
  47. '0000*',
  48. '*container'])
  49. def test_list_containers(variables, expression):
  50. AzureContainerList.list_containers(
  51. blob_service_client=variables.blob_service_client,
  52. expression=expression,
  53. print_container=False,
  54. output_file=variables.output_file)
  55. contents = read_output_file(output_file=variables.output_file)
  56. assert variables.container_name in contents
  57. delete_output_file(output_file=variables.output_file)
  58. @pytest.mark.parametrize('expression',
  59. ['000000container1',
  60. '0000\\d{3}container',
  61. '0000\\d{2}\\D{10}'
  62. '100000*',
  63. '*containers'])
  64. def test_list_containers_invalid(variables, expression):
  65. with pytest.raises(FileNotFoundError):
  66. AzureContainerList.list_containers(
  67. blob_service_client=variables.blob_service_client,
  68. expression=expression,
  69. print_container=False,
  70. output_file=variables.output_file)
  71. read_output_file(output_file=variables.output_file)
  72. def test_list_container_tilde(variables):
  73. path_obj = pathlib.Path(variables.file_path)
  74. path = f'~{os.sep}{path_obj.relative_to(pathlib.Path.home())}'
  75. list_containers = AzureContainerList(
  76. expression=variables.container_name,
  77. account_name=variables.account_name,
  78. output_file=os.path.join(path, 'search_outputs.txt'),
  79. passphrase=variables.passphrase
  80. )
  81. list_containers.main()
  82. contents = read_output_file(output_file=variables.output_file)
  83. assert variables.container_name in contents
  84. delete_output_file(output_file=variables.output_file)
  85. def test_list_container_invalid_path(variables):
  86. output_file = os.path.join('/invalid', 'search_outputs.txt')
  87. if not os.environ.get('CIRCLECI'):
  88. with pytest.raises(SystemExit):
  89. AzureContainerList(
  90. expression=variables.container_name,
  91. account_name=variables.account_name,
  92. output_file=output_file,
  93. passphrase=variables.passphrase
  94. )
  95. def test_list_container_directory_provided(variables):
  96. with pytest.raises(SystemExit):
  97. AzureContainerList(
  98. expression=variables.container_name,
  99. account_name=variables.account_name,
  100. output_file=variables.file_path,
  101. passphrase=variables.passphrase
  102. )
  103. def test_list_container_no_output_file(variables):
  104. list_containers = AzureContainerList(
  105. expression=variables.container_name,
  106. account_name=variables.account_name,
  107. output_file=str(),
  108. passphrase=variables.passphrase
  109. )
  110. list_containers.main()
  111. assert not os.path.isfile(variables.output_file)
  112. @patch('argparse.ArgumentParser.parse_args')
  113. def test_list_container_integration(mock_args, variables):
  114. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  115. account_name=variables.account_name,
  116. expression='0*',
  117. verbosity='info',
  118. output_file=variables.output_file)
  119. arguments = cli()
  120. container_search(arguments)
  121. contents = read_output_file(output_file=variables.output_file)
  122. assert variables.container_name in contents
  123. delete_output_file(output_file=variables.output_file)
  124. @pytest.mark.parametrize('container,expression,check_file',
  125. [('000000container', 'file_1', 'file_1'),
  126. ('000000container', 'file_1.gz', 'file_1.gz'),
  127. ('000000container', 'container_integration/*_5/*.txt', 'nested_folder_test_1.txt'),
  128. ('000000container', '*.txt', 'file_2.txt'),
  129. ('000000container', '\\D{14}3/*_1.txt', 'nested_folder_test_1.txt'),
  130. ('000000container', 'h\\D{2}/nes*', 'nested_folder_test_1.txt')])
  131. def test_list_file(variables, container, expression, check_file):
  132. AzureList.list_files(container_client=variables.container_client,
  133. expression=expression,
  134. output_file=variables.output_file,
  135. container_name=container)
  136. contents = read_output_file(output_file=variables.output_file)
  137. hit = parse_file_outputs(check_file=check_file,
  138. contents=contents)
  139. assert hit
  140. delete_output_file(output_file=variables.output_file)
  141. def test_list_file_tilde(variables):
  142. path_obj = pathlib.Path(variables.file_path)
  143. path = f'~{os.sep}{path_obj.relative_to(pathlib.Path.home())}'
  144. output_file = os.path.join(path, 'search_outputs.txt')
  145. check_file = 'triple_nested_file.txt'
  146. list_files = AzureList(
  147. container_name=variables.container_name,
  148. expression=check_file,
  149. account_name=variables.account_name,
  150. output_file=output_file,
  151. passphrase=variables.passphrase
  152. )
  153. list_files.main()
  154. contents = read_output_file(output_file=variables.output_file)
  155. hit = parse_file_outputs(check_file=check_file,
  156. contents=contents)
  157. assert hit
  158. delete_output_file(output_file=variables.output_file)
  159. def test_list_file_invalid_path(variables):
  160. output_file = os.path.join('/invalid', 'search_outputs.txt')
  161. if not os.environ.get('CIRCLECI'):
  162. with pytest.raises(SystemExit):
  163. AzureList(
  164. container_name=variables.container_name,
  165. expression=None,
  166. account_name=variables.account_name,
  167. output_file=output_file,
  168. passphrase=variables.passphrase
  169. )
  170. def test_list_file_directory_provided(variables):
  171. with pytest.raises(SystemExit):
  172. AzureList(
  173. container_name=variables.container_name,
  174. expression=None,
  175. account_name=variables.account_name,
  176. output_file=variables.file_path,
  177. passphrase=variables.passphrase
  178. )
  179. def test_list_file_no_output_file(variables):
  180. list_files = AzureList(
  181. container_name=variables.container_name,
  182. expression=None,
  183. account_name=variables.account_name,
  184. output_file=str(),
  185. passphrase=variables.passphrase
  186. )
  187. list_files.main()
  188. assert not os.path.isfile(variables.output_file)
  189. @patch('argparse.ArgumentParser.parse_args')
  190. def test_list_file_integration(mock_args, variables):
  191. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  192. account_name=variables.account_name,
  193. container_name=variables.container_name,
  194. expression='con*/c*/triple*file.txt',
  195. verbosity='info',
  196. output_file=variables.output_file)
  197. arguments = cli()
  198. azure_search(arguments)
  199. contents = read_output_file(output_file=variables.output_file)
  200. hit = False
  201. for entry in contents:
  202. if 'triple_nested_file.txt' in [os.path.basename(component) for component in entry.split('\t')]:
  203. hit = True
  204. assert hit
  205. delete_output_file(output_file=variables.output_file)
  206. @patch('argparse.ArgumentParser.parse_args')
  207. def test_list_file_container_expression_integration(mock_args, variables):
  208. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  209. account_name=variables.account_name,
  210. container_name='0*',
  211. expression='nested_c*/double*file_1.txt',
  212. verbosity='info',
  213. output_file=variables.output_file)
  214. arguments = cli()
  215. azure_search(arguments)
  216. contents = read_output_file(output_file=variables.output_file)
  217. hit = False
  218. for entry in contents:
  219. if 'double_nested_file_1.txt' in [os.path.basename(component) for component in entry.split('\t')]:
  220. hit = True
  221. assert hit
  222. delete_output_file(output_file=variables.output_file)