test_azure_7_delete.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. from azure_storage.methods import client_prep, delete_container, delete_file, delete_folder, extract_account_name
  2. from azure_storage.azure_delete import AzureDelete, cli, container_delete, file_delete, \
  3. folder_delete
  4. from unittest.mock import patch
  5. import argparse
  6. import pytest
  7. import azure
  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. return Variables()
  17. def test_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 variables.connect_str.startswith('DefaultEndpointsProtocol')
  23. @pytest.mark.parametrize('file_name',
  24. ['file_1.txt',
  25. 'container_integration/file_2.txt',
  26. 'nested_container/nested_folder/nested_folder_2/nested_folder_test_1.txt',
  27. 'ABC/123/nested_folder_test_1.txt'])
  28. def test_delete_file(variables, file_name):
  29. delete_file(container_client=variables.container_client,
  30. object_name=file_name,
  31. blob_service_client=variables.blob_service_client,
  32. container_name=variables.container_name)
  33. blobs = variables.container_client.list_blobs()
  34. assert file_name not in [blob.name for blob in blobs]
  35. @pytest.mark.parametrize('file_name',
  36. ['file_3.txt',
  37. 'container_integration/file_2.txt',
  38. 'nested_container/nested_folder/nested_folder_2/nested_folder_test_1.txt',
  39. 'ABC/123/nested_folder_test_1.txt'])
  40. def test_delete_file_missing(variables, file_name):
  41. with pytest.raises(SystemExit):
  42. delete_file(container_client=variables.container_client,
  43. object_name=file_name,
  44. blob_service_client=variables.blob_service_client,
  45. container_name=variables.container_name)
  46. def test_delete_file_invalid_category(variables):
  47. with pytest.raises(SystemExit):
  48. del_file = AzureDelete(object_name='file_1.txt',
  49. container_name=variables.container_name,
  50. account_name=variables.account_name,
  51. passphrase=variables.passphrase,
  52. retention_time=8,
  53. category='container')
  54. del_file.main()
  55. @patch('argparse.ArgumentParser.parse_args')
  56. def test_delete_file_integration(mock_args, variables):
  57. file_name = 'nested/file_2.txt'
  58. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  59. account_name=variables.account_name,
  60. container_name=variables.container_name,
  61. verbosity='info',
  62. file=file_name,
  63. retention_time=1)
  64. arguments = cli()
  65. file_delete(arguments)
  66. blobs = variables.container_client.list_blobs()
  67. assert os.path.basename(file_name) not in [blob.name for blob in blobs]
  68. @pytest.mark.parametrize('retention_time',
  69. [0,
  70. 1000])
  71. @patch('argparse.ArgumentParser.parse_args')
  72. def test_delete_file_integration_invalid_retention_time(mock_args, variables, retention_time):
  73. file_name = 'nested/file_2.txt'
  74. with pytest.raises(SystemExit):
  75. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  76. account_name=variables.account_name,
  77. container_name=variables.container_name,
  78. verbosity='info',
  79. file=file_name,
  80. retention_time=retention_time)
  81. arguments = cli()
  82. file_delete(arguments)
  83. @patch('argparse.ArgumentParser.parse_args')
  84. def test_delete_file_integration_missing(mock_args, variables):
  85. file_name = 'nested/file_2.txt'
  86. with pytest.raises(SystemExit):
  87. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  88. account_name=variables.account_name,
  89. container_name=variables.container_name,
  90. verbosity='info',
  91. file=file_name,
  92. retention_time=1)
  93. arguments = cli()
  94. file_delete(arguments)
  95. @pytest.mark.parametrize('folder_name,check_file',
  96. [('container_integration/', 'nested_folder_test_1.txt'),
  97. ('nested_container/nested_folder/', 'nested_file_2.txt'),
  98. ('ABC/', 'nested_folder_test_1.txt')])
  99. def test_delete_folder(variables, folder_name, check_file):
  100. delete_folder(container_client=variables.container_client,
  101. object_name=folder_name,
  102. blob_service_client=variables.blob_service_client,
  103. container_name=variables.container_name,
  104. account_name=variables.account_name)
  105. blobs = variables.container_client.list_blobs()
  106. assert os.path.join(folder_name, check_file) not in [blob.name for blob in blobs]
  107. @pytest.mark.parametrize('folder_name,check_file',
  108. [('container_integration/', 'nested_folder_test_1.txt'),
  109. ('nested_container/nested_folder/', 'nested_file_2.txt'),
  110. ('ABC/', 'nested_folder_test_1.txt')])
  111. def test_delete_folder_missing(variables, folder_name, check_file):
  112. with pytest.raises(SystemExit):
  113. delete_folder(container_client=variables.container_client,
  114. object_name=folder_name,
  115. blob_service_client=variables.blob_service_client,
  116. container_name=variables.container_name,
  117. account_name=variables.account_name)
  118. @patch('argparse.ArgumentParser.parse_args')
  119. def test_delete_folder_integration(mock_args, variables):
  120. folder_name = 'nested_folder_3'
  121. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  122. account_name=variables.account_name,
  123. container_name=variables.container_name,
  124. verbosity='info',
  125. folder=folder_name,
  126. retention_time=1)
  127. arguments = cli()
  128. folder_delete(arguments)
  129. blobs = variables.container_client.list_blobs()
  130. assert os.path.join(folder_name, 'nested_folder_test_1.txt') not in [blob.name for blob in blobs]
  131. @patch('argparse.ArgumentParser.parse_args')
  132. def test_delete_folder_integration_missing(mock_args, variables):
  133. folder_name = 'nested_folder_3'
  134. with pytest.raises(SystemExit):
  135. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  136. account_name=variables.account_name,
  137. container_name=variables.container_name,
  138. verbosity='info',
  139. folder=folder_name,
  140. retention_time=1)
  141. arguments = cli()
  142. folder_delete(arguments)
  143. def test_delete_container_missing(variables):
  144. with pytest.raises(SystemExit):
  145. delete_container(blob_service_client=variables.blob_service_client,
  146. container_name='000000000container',
  147. account_name=variables.account_name)
  148. @patch('argparse.ArgumentParser.parse_args')
  149. def test_delete_container_integration(mock_args, variables):
  150. mock_args.return_value = argparse.Namespace(passphrase=variables.passphrase,
  151. account_name=variables.account_name,
  152. container_name=variables.container_name,
  153. verbosity='info')
  154. arguments = cli()
  155. container_delete(arguments)
  156. with pytest.raises(azure.core.exceptions.ResourceExistsError):
  157. variables.blob_service_client.create_container(variables.container_name)