|
| 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 | + |
| 19 | +import uuid |
| 20 | +from unittest import mock |
| 21 | + |
| 22 | +from pyiceberg.io.fsspec import FsspecFileIO |
| 23 | +from pyiceberg.typedef import Properties |
| 24 | + |
| 25 | +UNIFIED_AWS_SESSION_PROPERTIES = { |
| 26 | + "client.access-key-id": "client.access-key-id", |
| 27 | + "client.secret-access-key": "client.secret-access-key", |
| 28 | + "client.region": "client.region", |
| 29 | + "client.session-token": "client.session-token", |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +def test_fsspec_s3_session_properties_with_profile() -> None: |
| 34 | + session_properties: Properties = { |
| 35 | + "s3.profile-name": "test-profile", |
| 36 | + "s3.endpoint": "http://localhost:9000", |
| 37 | + **UNIFIED_AWS_SESSION_PROPERTIES, |
| 38 | + } |
| 39 | + |
| 40 | + with mock.patch("s3fs.S3FileSystem") as mock_s3fs, mock.patch("aiobotocore.session.AioSession") as mock_aio_session: |
| 41 | + s3_fileio = FsspecFileIO(properties=session_properties) |
| 42 | + filename = str(uuid.uuid4()) |
| 43 | + |
| 44 | + s3_fileio.new_input(location=f"s3://warehouse/{filename}") |
| 45 | + |
| 46 | + mock_aio_session.assert_called_with(profile="test-profile") |
| 47 | + mock_s3fs.assert_called_with( |
| 48 | + anon=False, |
| 49 | + client_kwargs={ |
| 50 | + "endpoint_url": "http://localhost:9000", |
| 51 | + "aws_access_key_id": "client.access-key-id", |
| 52 | + "aws_secret_access_key": "client.secret-access-key", |
| 53 | + "region_name": "client.region", |
| 54 | + "aws_session_token": "client.session-token", |
| 55 | + }, |
| 56 | + config_kwargs={}, |
| 57 | + session=mock_aio_session(), |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def test_fsspec_s3_session_properties_with_client_profile() -> None: |
| 62 | + session_properties: Properties = { |
| 63 | + "client.profile-name": "test-profile", |
| 64 | + "s3.endpoint": "http://localhost:9000", |
| 65 | + **UNIFIED_AWS_SESSION_PROPERTIES, |
| 66 | + } |
| 67 | + |
| 68 | + with mock.patch("s3fs.S3FileSystem") as mock_s3fs, mock.patch("aiobotocore.session.AioSession") as mock_aio_session: |
| 69 | + s3_fileio = FsspecFileIO(properties=session_properties) |
| 70 | + filename = str(uuid.uuid4()) |
| 71 | + |
| 72 | + s3_fileio.new_input(location=f"s3://warehouse/{filename}") |
| 73 | + |
| 74 | + mock_aio_session.assert_called_with(profile="test-profile") |
| 75 | + mock_s3fs.assert_called_with( |
| 76 | + anon=False, |
| 77 | + client_kwargs={ |
| 78 | + "endpoint_url": "http://localhost:9000", |
| 79 | + "aws_access_key_id": "client.access-key-id", |
| 80 | + "aws_secret_access_key": "client.secret-access-key", |
| 81 | + "region_name": "client.region", |
| 82 | + "aws_session_token": "client.session-token", |
| 83 | + }, |
| 84 | + config_kwargs={}, |
| 85 | + session=mock_aio_session(), |
| 86 | + ) |
0 commit comments