|
| 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 | + |
| 18 | +import logging |
| 19 | +import os |
| 20 | +from random import randint |
| 21 | +import unittest |
| 22 | + |
| 23 | + |
| 24 | +from adlfs import AzureBlobFileSystem |
| 25 | + |
| 26 | +from gravitino import ( |
| 27 | + gvfs, |
| 28 | + GravitinoClient, |
| 29 | + Catalog, |
| 30 | + Fileset, |
| 31 | +) |
| 32 | +from gravitino.filesystem.gvfs_config import GVFSConfig |
| 33 | +from tests.integration.test_gvfs_with_abs import TestGvfsWithABS |
| 34 | + |
| 35 | + |
| 36 | +logger = logging.getLogger(__name__) |
| 37 | + |
| 38 | + |
| 39 | +def azure_abs_with_credential_is_prepared(): |
| 40 | + return ( |
| 41 | + os.environ.get("ABS_ACCOUNT_NAME_FOR_CREDENTIAL") |
| 42 | + and os.environ.get("ABS_ACCOUNT_KEY_FOR_CREDENTIAL") |
| 43 | + and os.environ.get("ABS_CONTAINER_NAME_FOR_CREDENTIAL") |
| 44 | + and os.environ.get("ABS_TENANT_ID_FOR_CREDENTIAL") |
| 45 | + and os.environ.get("ABS_CLIENT_ID_FOR_CREDENTIAL") |
| 46 | + and os.environ.get("ABS_CLIENT_SECRET_FOR_CREDENTIAL") |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +@unittest.skipUnless( |
| 51 | + azure_abs_with_credential_is_prepared(), |
| 52 | + "Azure Blob Storage credential test is not prepared.", |
| 53 | +) |
| 54 | +class TestGvfsWithCredentialABS(TestGvfsWithABS): |
| 55 | + # Before running this test, please set the make sure azure-bundle-xxx.jar has been |
| 56 | + # copy to the $GRAVITINO_HOME/catalogs/hadoop/libs/ directory |
| 57 | + azure_abs_account_key = os.environ.get("ABS_ACCOUNT_KEY_FOR_CREDENTIAL") |
| 58 | + azure_abs_account_name = os.environ.get("ABS_ACCOUNT_NAME_FOR_CREDENTIAL") |
| 59 | + azure_abs_container_name = os.environ.get("ABS_CONTAINER_NAME_FOR_CREDENTIAL") |
| 60 | + azure_abs_tenant_id = os.environ.get("ABS_TENANT_ID_FOR_CREDENTIAL") |
| 61 | + azure_abs_client_id = os.environ.get("ABS_CLIENT_ID_FOR_CREDENTIAL") |
| 62 | + azure_abs_client_secret = os.environ.get("ABS_CLIENT_SECRET_FOR_CREDENTIAL") |
| 63 | + |
| 64 | + metalake_name: str = "TestGvfsWithCredentialABS_metalake" + str(randint(1, 10000)) |
| 65 | + |
| 66 | + def setUp(self): |
| 67 | + self.options = { |
| 68 | + GVFSConfig.GVFS_FILESYSTEM_AZURE_ACCOUNT_NAME: self.azure_abs_account_name, |
| 69 | + GVFSConfig.GVFS_FILESYSTEM_AZURE_ACCOUNT_KEY: self.azure_abs_account_key, |
| 70 | + } |
| 71 | + |
| 72 | + @classmethod |
| 73 | + def _init_test_entities(cls): |
| 74 | + cls.gravitino_admin_client.create_metalake( |
| 75 | + name=cls.metalake_name, comment="", properties={} |
| 76 | + ) |
| 77 | + cls.gravitino_client = GravitinoClient( |
| 78 | + uri="http://localhost:8090", metalake_name=cls.metalake_name |
| 79 | + ) |
| 80 | + |
| 81 | + cls.config = {} |
| 82 | + cls.conf = {} |
| 83 | + catalog = cls.gravitino_client.create_catalog( |
| 84 | + name=cls.catalog_name, |
| 85 | + catalog_type=Catalog.Type.FILESET, |
| 86 | + provider=cls.catalog_provider, |
| 87 | + comment="", |
| 88 | + properties={ |
| 89 | + "filesystem-providers": "abs", |
| 90 | + "azure-storage-account-name": cls.azure_abs_account_name, |
| 91 | + "azure-storage-account-key": cls.azure_abs_account_key, |
| 92 | + "azure-tenant-id": cls.azure_abs_tenant_id, |
| 93 | + "azure-client-id": cls.azure_abs_client_id, |
| 94 | + "azure-client-secret": cls.azure_abs_client_secret, |
| 95 | + "credential-providers": "adls-token", |
| 96 | + }, |
| 97 | + ) |
| 98 | + catalog.as_schemas().create_schema( |
| 99 | + schema_name=cls.schema_name, comment="", properties={} |
| 100 | + ) |
| 101 | + |
| 102 | + cls.fileset_storage_location: str = ( |
| 103 | + f"{cls.azure_abs_container_name}/{cls.catalog_name}/{cls.schema_name}/{cls.fileset_name}" |
| 104 | + ) |
| 105 | + cls.fileset_gvfs_location = ( |
| 106 | + f"gvfs://fileset/{cls.catalog_name}/{cls.schema_name}/{cls.fileset_name}" |
| 107 | + ) |
| 108 | + catalog.as_fileset_catalog().create_fileset( |
| 109 | + ident=cls.fileset_ident, |
| 110 | + fileset_type=Fileset.Type.MANAGED, |
| 111 | + comment=cls.fileset_comment, |
| 112 | + storage_location=( |
| 113 | + f"abfss://{cls.azure_abs_container_name}@{cls.azure_abs_account_name}.dfs.core.windows.net/" |
| 114 | + f"{cls.catalog_name}/{cls.schema_name}/{cls.fileset_name}" |
| 115 | + ), |
| 116 | + properties=cls.fileset_properties, |
| 117 | + ) |
| 118 | + |
| 119 | + cls.fs = AzureBlobFileSystem( |
| 120 | + account_name=cls.azure_abs_account_name, |
| 121 | + account_key=cls.azure_abs_account_key, |
| 122 | + ) |
| 123 | + |
| 124 | + # As the permission provided by the dynamic token is smaller compared to the permission provided by the static token |
| 125 | + # like account key and account name, the test case will fail if we do not override the test case. |
| 126 | + def test_mkdir(self): |
| 127 | + mkdir_dir = self.fileset_gvfs_location + "/test_mkdir" |
| 128 | + mkdir_actual_dir = self.fileset_storage_location + "/test_mkdir" |
| 129 | + fs = gvfs.GravitinoVirtualFileSystem( |
| 130 | + server_uri="http://localhost:8090", |
| 131 | + metalake_name=self.metalake_name, |
| 132 | + options=self.options, |
| 133 | + **self.conf, |
| 134 | + ) |
| 135 | + |
| 136 | + # it actually takes no effect. |
| 137 | + self.check_mkdir(mkdir_dir, mkdir_actual_dir, fs) |
| 138 | + |
| 139 | + # check whether it will automatically create the bucket if 'create_parents' |
| 140 | + # is set to True. |
| 141 | + new_bucket = self.azure_abs_container_name + "2" |
| 142 | + mkdir_actual_dir = mkdir_actual_dir.replace( |
| 143 | + self.azure_abs_container_name, new_bucket |
| 144 | + ) |
| 145 | + self.fs.mkdir(mkdir_actual_dir, create_parents=True) |
| 146 | + |
| 147 | + self.assertFalse(self.fs.exists(mkdir_actual_dir)) |
| 148 | + |
| 149 | + self.assertTrue(self.fs.exists(f"abfss://{new_bucket}")) |
| 150 | + |
| 151 | + def test_makedirs(self): |
| 152 | + mkdir_dir = self.fileset_gvfs_location + "/test_mkdir" |
| 153 | + mkdir_actual_dir = self.fileset_storage_location + "/test_mkdir" |
| 154 | + fs = gvfs.GravitinoVirtualFileSystem( |
| 155 | + server_uri="http://localhost:8090", |
| 156 | + metalake_name=self.metalake_name, |
| 157 | + options=self.options, |
| 158 | + **self.conf, |
| 159 | + ) |
| 160 | + |
| 161 | + # it actually takes no effect. |
| 162 | + self.check_mkdir(mkdir_dir, mkdir_actual_dir, fs) |
| 163 | + |
| 164 | + # check whether it will automatically create the bucket if 'create_parents' |
| 165 | + # is set to True. |
| 166 | + new_bucket = self.azure_abs_container_name + "1" |
| 167 | + new_mkdir_actual_dir = mkdir_actual_dir.replace( |
| 168 | + self.azure_abs_container_name, new_bucket |
| 169 | + ) |
| 170 | + self.fs.makedirs(new_mkdir_actual_dir) |
| 171 | + self.assertFalse(self.fs.exists(mkdir_actual_dir)) |
0 commit comments