azure_system_helpers.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. import json
  18. import os
  19. import random
  20. import string
  21. from contextlib import contextmanager
  22. import pytest
  23. from airflow.exceptions import AirflowException
  24. from airflow.models import Connection
  25. from airflow.providers.microsoft.azure.hooks.fileshare import AzureFileShareHook
  26. from airflow.utils.process_utils import patch_environ
  27. from tests.test_utils import AIRFLOW_MAIN_FOLDER
  28. from tests.test_utils.system_tests_class import SystemTest
  29. AZURE_DAG_FOLDER = os.path.join(
  30. AIRFLOW_MAIN_FOLDER, "airflow", "providers", "microsoft", "azure", "example_dags"
  31. )
  32. WASB_CONNECTION_ID = os.environ.get("WASB_CONNECTION_ID", "wasb_default")
  33. DATA_LAKE_CONNECTION_ID = os.environ.get("AZURE_DATA_LAKE_CONNECTION_ID", 'azure_data_lake_default')
  34. DATA_LAKE_CONNECTION_TYPE = os.environ.get("AZURE_DATA_LAKE_CONNECTION_TYPE", 'azure_data_lake')
  35. @contextmanager
  36. def provide_wasb_default_connection(key_file_path: str):
  37. """
  38. Context manager to provide a temporary value for wasb_default connection
  39. :param key_file_path: Path to file with wasb_default credentials .json file.
  40. """
  41. if not key_file_path.endswith(".json"):
  42. raise AirflowException("Use a JSON key file.")
  43. with open(key_file_path) as credentials:
  44. creds = json.load(credentials)
  45. conn = Connection(
  46. conn_id=WASB_CONNECTION_ID,
  47. conn_type="wasb",
  48. host=creds.get("host", None),
  49. login=creds.get("login", None),
  50. password=creds.get("password", None),
  51. extra=json.dumps(creds.get('extra', None)),
  52. )
  53. with patch_environ({f"AIRFLOW_CONN_{conn.conn_id.upper()}": conn.get_uri()}):
  54. yield
  55. @contextmanager
  56. def provide_azure_data_lake_default_connection(key_file_path: str):
  57. """
  58. Context manager to provide a temporary value for azure_data_lake_default connection
  59. :param key_file_path: Path to file with azure_data_lake_default credentials .json file.
  60. """
  61. required_fields = {'login', 'password', 'extra'}
  62. if not key_file_path.endswith(".json"):
  63. raise AirflowException("Use a JSON key file.")
  64. with open(key_file_path) as credentials:
  65. creds = json.load(credentials)
  66. missing_keys = required_fields - creds.keys()
  67. if missing_keys:
  68. message = f"{missing_keys} fields are missing"
  69. raise AirflowException(message)
  70. conn = Connection(
  71. conn_id=DATA_LAKE_CONNECTION_ID,
  72. conn_type=DATA_LAKE_CONNECTION_TYPE,
  73. host=creds.get("host", None),
  74. login=creds.get("login", None),
  75. password=creds.get("password", None),
  76. extra=json.dumps(creds.get('extra', None)),
  77. )
  78. with patch_environ({f"AIRFLOW_CONN_{conn.conn_id.upper()}": conn.get_uri()}):
  79. yield
  80. @contextmanager
  81. def provide_azure_fileshare(share_name: str, azure_fileshare_conn_id: str, file_name: str, directory: str):
  82. AzureSystemTest.prepare_share(
  83. share_name=share_name,
  84. azure_fileshare_conn_id=azure_fileshare_conn_id,
  85. file_name=file_name,
  86. directory=directory,
  87. )
  88. yield
  89. AzureSystemTest.delete_share(share_name=share_name, azure_fileshare_conn_id=azure_fileshare_conn_id)
  90. @pytest.mark.system("azure")
  91. class AzureSystemTest(SystemTest):
  92. @classmethod
  93. def create_share(cls, share_name: str, azure_fileshare_conn_id: str):
  94. hook = AzureFileShareHook(azure_fileshare_conn_id=azure_fileshare_conn_id)
  95. hook.create_share(share_name)
  96. @classmethod
  97. def delete_share(cls, share_name: str, azure_fileshare_conn_id: str):
  98. hook = AzureFileShareHook(azure_fileshare_conn_id=azure_fileshare_conn_id)
  99. hook.delete_share(share_name)
  100. @classmethod
  101. def create_directory(cls, share_name: str, azure_fileshare_conn_id: str, directory: str):
  102. hook = AzureFileShareHook(azure_fileshare_conn_id=azure_fileshare_conn_id)
  103. hook.create_directory(share_name=share_name, directory_name=directory)
  104. @classmethod
  105. def upload_file_from_string(
  106. cls,
  107. string_data: str,
  108. share_name: str,
  109. azure_fileshare_conn_id: str,
  110. file_name: str,
  111. directory: str,
  112. ):
  113. hook = AzureFileShareHook(azure_fileshare_conn_id=azure_fileshare_conn_id)
  114. hook.load_string(
  115. string_data=string_data,
  116. share_name=share_name,
  117. directory_name=directory,
  118. file_name=file_name,
  119. )
  120. @classmethod
  121. def prepare_share(cls, share_name: str, azure_fileshare_conn_id: str, file_name: str, directory: str):
  122. """
  123. Create share with a file in given directory. If directory is None, file is in root dir.
  124. """
  125. cls.create_share(share_name=share_name, azure_fileshare_conn_id=azure_fileshare_conn_id)
  126. cls.create_directory(
  127. share_name=share_name, azure_fileshare_conn_id=azure_fileshare_conn_id, directory=directory
  128. )
  129. string_data = "".join(random.choice(string.ascii_letters) for _ in range(1024))
  130. cls.upload_file_from_string(
  131. string_data=string_data,
  132. share_name=share_name,
  133. azure_fileshare_conn_id=azure_fileshare_conn_id,
  134. file_name=file_name,
  135. directory=directory,
  136. )