|
| 1 | +import pytest |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | +from fence.resources.aws.boto_manager import BotoManager |
| 4 | + |
| 5 | + |
| 6 | +class TestData: |
| 7 | + """Generate bucket test data that aims to mirror the default example Fence config file.""" |
| 8 | + def __init__(self): |
| 9 | + self.config = {} |
| 10 | + self.buckets = {} |
| 11 | + |
| 12 | + def single_bucket(self): |
| 13 | + self.config = { |
| 14 | + 'CRED1': {'access_key': 'key1', 'secret_key': 'secret1'}, |
| 15 | + } |
| 16 | + self.buckets = { |
| 17 | + 'bucket1': {'cred': 'CRED1', 'region': 'us-east-1', 'endpoint_url': 'https://example.com'}, |
| 18 | + } |
| 19 | + return self |
| 20 | + |
| 21 | + def multiple_buckets(self): |
| 22 | + single_bucket = self.single_bucket() |
| 23 | + self.config = single_bucket.config | { |
| 24 | + 'CRED2': {'access_key': 'key2', 'secret_key': 'secret2'}, |
| 25 | + } |
| 26 | + self.buckets = single_bucket.buckets | { |
| 27 | + 'bucket2': {'cred': 'CRED2', 'region': 'us-east-1'}, |
| 28 | + 'bucket3': {'cred': '*'}, |
| 29 | + 'bucket4': {'cred': 'CRED1', 'region': 'us-east-1', 'role-arn': 'arn:aws:iam::role1'} |
| 30 | + } |
| 31 | + return self |
| 32 | + |
| 33 | + |
| 34 | +@patch('fence.resources.aws.boto_manager.client') |
| 35 | +def test_create_s3_client_single(mock_client): |
| 36 | + test_data = TestData().single_bucket() |
| 37 | + config = test_data.config |
| 38 | + buckets = test_data.buckets |
| 39 | + logger = MagicMock() |
| 40 | + boto_manager = BotoManager(config, buckets, logger) |
| 41 | + |
| 42 | + s3_clients = boto_manager.create_s3_clients(config, buckets) |
| 43 | + |
| 44 | + # Assert that the correct call was made to the client function |
| 45 | + mock_client.assert_any_call('s3', access_key='key1', secret_key='key1', endpoint_url='https://example.com') |
| 46 | + |
| 47 | + # Assert that the returned dictionary contains the correct client |
| 48 | + assert len(s3_clients) == 1 |
| 49 | + assert 'bucket1' in s3_clients |
| 50 | + |
| 51 | + |
| 52 | +@patch('fence.resources.aws.boto_manager.client') |
| 53 | +def test_create_s3_clients_multiple(mock_client): |
| 54 | + test_data = TestData().multiple_buckets() |
| 55 | + config = test_data.config |
| 56 | + buckets = test_data.buckets |
| 57 | + logger = MagicMock() |
| 58 | + boto_manager = BotoManager(config, buckets, logger) |
| 59 | + |
| 60 | + # Call the method under test |
| 61 | + s3_clients = boto_manager.create_s3_clients(config, buckets) |
| 62 | + |
| 63 | + # Assert that the correct calls were made to the client function |
| 64 | + mock_client.assert_any_call('s3', access_key='key1', secret_key='secret1', endpoint_url='https://example.com') |
| 65 | + mock_client.assert_any_call('s3', access_key='key2', secret_key='secret2') |
| 66 | + mock_client.assert_any_call('s3') |
| 67 | + mock_client.assert_any_call('s3', access_key='key1', secret_key='secret1') |
| 68 | + |
| 69 | + # Assert that the returned dictionary contains the correct clients |
| 70 | + assert len(s3_clients) == 4 |
| 71 | + assert 'bucket1' in s3_clients |
| 72 | + assert 'bucket2' in s3_clients |
| 73 | + assert 'bucket3' in s3_clients |
| 74 | + assert 'bucket4' in s3_clients |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize("bucket", ['bucket1', 'bucket2', 'bucket3', 'bucket4']) |
| 78 | +@patch('fence.resources.aws.boto_manager.client') |
| 79 | +def test_delete_data_file(mock_client, bucket): |
| 80 | + test_data = TestData().multiple_buckets() |
| 81 | + config = test_data.config |
| 82 | + buckets = test_data.buckets |
| 83 | + logger = MagicMock() |
| 84 | + boto_manager = BotoManager(config, buckets, logger) |
| 85 | + |
| 86 | + # Mock the response of list_objects_v2 to include the desired key |
| 87 | + prefix = 'data/file.txt' |
| 88 | + mock_list_objects_v2_response = { |
| 89 | + 'Contents': [{'Key': prefix}] |
| 90 | + } |
| 91 | + # Set up the mock S3 client and its list_objects_v2 and delete_object methods |
| 92 | + mock_s3_client = mock_client.return_value |
| 93 | + mock_s3_client.list_objects_v2.return_value = mock_list_objects_v2_response |
| 94 | + |
| 95 | + result = boto_manager.delete_data_file(bucket, prefix) |
| 96 | + |
| 97 | + # Create S3 clients for each of the buckets |
| 98 | + _ = boto_manager.create_s3_clients(config, buckets) |
| 99 | + s3_client = boto_manager.get_s3_client(bucket) |
| 100 | + s3_client.list_objects_v2.assert_called_once_with( |
| 101 | + Bucket=bucket, Prefix=prefix, Delimiter="/" |
| 102 | + ) |
| 103 | + s3_client.delete_object.assert_called_once_with(Bucket=bucket, Key='data/file.txt') |
| 104 | + |
| 105 | + # Assert the expected result |
| 106 | + assert result == ("", 204) |
0 commit comments