test_azure_5_tier.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. from azure_storage.methods import client_prep, extract_account_name
  2. from azure_storage.azure_tier import AzureContainerTier, AzureTier, cli, container_tier, file_tier, folder_tier
  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. self.passphrase = 'AzureStorage'
  13. self.account_name = extract_account_name(passphrase=self.passphrase)
  14. self.container_name = '000000container'
  15. self.storage_tier = 'Cool'
  16. return Variables()
  17. def test_tier_client_prep(variables):
  18. variables.container_name, variables.connect_str, variables.blob_service_client, variables.container_client = \
  19. client_prep(container_name=variables.container_name,
  20. passphrase=variables.passphrase,
  21. account_name=variables.account_name)
  22. assert type(variables.blob_service_client) == azure.storage.blob._blob_service_client.BlobServiceClient
  23. @pytest.mark.parametrize('file_name',
  24. ['file_1.txt',
  25. 'file_1.txt',
  26. 'container_integration/file_2.txt',
  27. 'nested_container/nested_folder/nested_folder_2/nested_folder_test_1.txt',
  28. 'ABC/123/nested_folder_test_1.txt'])
  29. def test_file_tier_cool(variables, file_name):
  30. AzureTier.file_tier(container_client=variables.container_client,
  31. object_name=file_name,
  32. blob_service_client=variables.blob_service_client,
  33. container_name=variables.container_name,
  34. storage_tier=variables.storage_tier)
  35. blobs = variables.container_client.list_blobs()
  36. for blob in blobs:
  37. if blob.name == file_name:
  38. assert blob.blob_tier == variables.storage_tier
  39. @pytest.mark.parametrize('file_name',
  40. ['file_1.txt',
  41. 'file_1.txt',
  42. 'container_integration/file_2.txt',
  43. 'nested_container/nested_folder/nested_folder_2/nested_folder_test_1.txt',
  44. 'ABC/123/nested_folder_test_1.txt'])
  45. def test_file_tier_hot(variables, file_name):
  46. storage_tier = 'Hot'
  47. AzureTier.file_tier(container_client=variables.container_client,
  48. object_name=file_name,
  49. blob_service_client=variables.blob_service_client,
  50. container_name=variables.container_name,
  51. storage_tier=storage_tier)
  52. blobs = variables.container_client.list_blobs()
  53. for blob in blobs:
  54. if blob.name == file_name:
  55. assert blob.blob_tier == storage_tier
  56. @pytest.mark.parametrize('file_name',
  57. ['file_3.txt',
  58. 'container_integration_2/file_2.txt',
  59. 'nested_container/nested_folder/nested_folder_2/nested_folder_test_14.txt',
  60. 'ABC/321/nested_folder_test_1.txt'])
  61. def test_file_tier_missing(variables, file_name):
  62. with pytest.raises(SystemExit):
  63. AzureTier.file_tier(container_client=variables.container_client,
  64. object_name=file_name,
  65. blob_service_client=variables.blob_service_client,
  66. container_name=variables.container_name,
  67. storage_tier=variables.storage_tier)
  68. def test_file_tier_invalid_category(variables):
  69. with pytest.raises(SystemExit):
  70. file_tier_set = AzureTier(container_name=variables.container_name,
  71. object_name='file_1.txt',
  72. account_name=variables.account_name,
  73. passphrase=variables.passphrase,
  74. storage_tier=variables.storage_tier,
  75. category='container')
  76. file_tier_set.main()
  77. def test_file_tier_invalid_container(variables):
  78. with pytest.raises(SystemExit):
  79. file_tier_set = AzureTier(container_name='000000000container',
  80. object_name='file_1.txt',
  81. account_name=variables.account_name,
  82. passphrase=variables.passphrase,
  83. storage_tier=variables.storage_tier,
  84. category='file')
  85. file_tier_set.main()
  86. @patch('argparse.ArgumentParser.parse_args')
  87. def test_file_tier_integration(mock_args, variables):
  88. file_name = 'container_integration/file_2.txt'
  89. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  90. account_name=variables.account_name,
  91. container_name=variables.container_name,
  92. verbosity='info',
  93. file=file_name,
  94. storage_tier=variables.storage_tier)
  95. arguments = cli()
  96. file_tier(arguments)
  97. blobs = variables.container_client.list_blobs()
  98. for blob in blobs:
  99. if blob.name == file_name:
  100. assert blob.blob_tier == variables.storage_tier
  101. @pytest.mark.parametrize('folder_name,check_file',
  102. [('container_integration/', 'nested_folder_test_1.txt'),
  103. ('container_integration/', 'nested_folder_test_1.txt'),
  104. ('nested_container/nested_folder', 'nested_file_2.txt'),
  105. ('ABC/', 'nested_folder_test_1.txt')])
  106. def test_folder_tier_cool(variables, folder_name, check_file):
  107. AzureTier.folder_tier(container_client=variables.container_client,
  108. object_name=folder_name,
  109. blob_service_client=variables.blob_service_client,
  110. container_name=variables.container_name,
  111. storage_tier=variables.storage_tier)
  112. blobs = variables.container_client.list_blobs()
  113. for blob in blobs:
  114. if blob.name == os.path.join(folder_name, check_file):
  115. assert blob.blob_tier == variables.storage_tier
  116. @pytest.mark.parametrize('folder_name,check_file',
  117. [('container_integration/', 'nested_folder_test_1.txt'),
  118. ('container_integration/', 'nested_folder_test_1.txt'),
  119. ('nested_container/nested_folder/', 'nested_file_2.txt'),
  120. ('ABC/', 'nested_folder_test_1.txt')])
  121. def test_folder_tier_hot(variables, folder_name, check_file):
  122. storage_tier = 'Hot'
  123. AzureTier.folder_tier(container_client=variables.container_client,
  124. object_name=folder_name,
  125. blob_service_client=variables.blob_service_client,
  126. container_name=variables.container_name,
  127. storage_tier=storage_tier)
  128. blobs = variables.container_client.list_blobs()
  129. for blob in blobs:
  130. if blob.name == os.path.join(folder_name, check_file):
  131. assert blob.blob_tier == storage_tier
  132. @pytest.mark.parametrize('folder_name',
  133. ['container_integration_4/',
  134. 'nested_container_13/nested_folder/',
  135. '123/ABC/'])
  136. def test_folder_tier_missing(variables, folder_name):
  137. with pytest.raises(SystemExit):
  138. AzureTier.folder_tier(container_client=variables.container_client,
  139. object_name=folder_name,
  140. blob_service_client=variables.blob_service_client,
  141. container_name=variables.container_name,
  142. storage_tier=variables.storage_tier)
  143. def test_folder_tier_invalid_container(variables):
  144. with pytest.raises(SystemExit):
  145. file_tier_set = AzureTier(container_name='000000000container',
  146. object_name='container_integration',
  147. account_name=variables.account_name,
  148. passphrase=variables.passphrase,
  149. storage_tier=variables.storage_tier,
  150. category='folder')
  151. file_tier_set.main()
  152. @patch('argparse.ArgumentParser.parse_args')
  153. def test_folder_tier_integration_cool(mock_args, variables):
  154. folder_name = 'container_integration/'
  155. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  156. account_name=variables.account_name,
  157. container_name=variables.container_name,
  158. verbosity='info',
  159. folder=folder_name,
  160. storage_tier=variables.storage_tier)
  161. arguments = cli()
  162. folder_tier(arguments)
  163. blobs = variables.container_client.list_blobs()
  164. for blob in blobs:
  165. if blob.name == os.path.join(folder_name, 'nested_folder_test_1.txt'):
  166. assert blob.blob_tier == variables.storage_tier
  167. @patch('argparse.ArgumentParser.parse_args')
  168. def test_folder_tier_integration_hot(mock_args, variables):
  169. folder_name = 'container_integration/'
  170. storage_tier = 'Hot'
  171. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  172. account_name=variables.account_name,
  173. container_name=variables.container_name,
  174. verbosity='info',
  175. folder=folder_name,
  176. storage_tier=storage_tier)
  177. arguments = cli()
  178. folder_tier(arguments)
  179. blobs = variables.container_client.list_blobs()
  180. for blob in blobs:
  181. if blob.name == os.path.join(folder_name, 'nested_folder_test_1.txt'):
  182. assert blob.blob_tier == storage_tier
  183. def test_container_tier_cool(variables):
  184. AzureContainerTier.container_tier(container_client=variables.container_client,
  185. blob_service_client=variables.blob_service_client,
  186. container_name=variables.container_name,
  187. storage_tier=variables.storage_tier)
  188. blobs = variables.container_client.list_blobs()
  189. for blob in blobs:
  190. if blob.name == 'file_1.txt':
  191. assert blob.blob_tier == variables.storage_tier
  192. def test_container_tier_hot(variables):
  193. storage_tier = 'Hot'
  194. AzureContainerTier.container_tier(container_client=variables.container_client,
  195. blob_service_client=variables.blob_service_client,
  196. container_name=variables.container_name,
  197. storage_tier=storage_tier)
  198. blobs = variables.container_client.list_blobs()
  199. for blob in blobs:
  200. if blob.name == 'file_1.txt':
  201. assert blob.blob_tier == storage_tier
  202. @patch('argparse.ArgumentParser.parse_args')
  203. def test_container_tier_integration_cool(mock_args, variables):
  204. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  205. account_name=variables.account_name,
  206. container_name=variables.container_name,
  207. verbosity='info',
  208. storage_tier=variables.storage_tier)
  209. arguments = cli()
  210. container_tier(arguments)
  211. blobs = variables.container_client.list_blobs()
  212. for blob in blobs:
  213. if blob.name == os.path.join('container_integration', 'nested_folder_test_1.txt'):
  214. assert blob.blob_tier == variables.storage_tier
  215. @patch('argparse.ArgumentParser.parse_args')
  216. def test_container_tier_integration_hot(mock_args, variables):
  217. storage_tier = 'Hot'
  218. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  219. account_name=variables.account_name,
  220. container_name=variables.container_name,
  221. verbosity='info',
  222. storage_tier=storage_tier)
  223. arguments = cli()
  224. container_tier(arguments)
  225. blobs = variables.container_client.list_blobs()
  226. for blob in blobs:
  227. if blob.name == os.path.join('container_integration', 'nested_folder_test_1.txt'):
  228. assert blob.blob_tier == storage_tier
  229. @patch('argparse.ArgumentParser.parse_args')
  230. def test_container_tier_integration_missing(mock_args, variables):
  231. with pytest.raises(SystemExit):
  232. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  233. account_name=variables.account_name,
  234. container_name='00000container',
  235. verbosity='info',
  236. storage_tier=variables.storage_tier)
  237. arguments = cli()
  238. container_tier(arguments)
  239. def test_cli():
  240. os.system('AzureTier -h')