|
1 | 1 | import os
|
2 | 2 | import s3fs
|
3 | 3 | import pathlib
|
4 |
| -import json |
5 |
| -import moto |
6 | 4 | import pytest
|
7 | 5 | import h5netcdf
|
8 | 6 |
|
9 | 7 | from tempfile import NamedTemporaryFile
|
10 |
| -from moto.moto_server.threaded_moto_server import ThreadedMotoServer |
11 | 8 | from activestorage.active import load_from_s3
|
12 | 9 |
|
13 | 10 |
|
14 |
| -# some spoofy server parameters |
| 11 | +# needed by the spoofed s3 filesystem |
15 | 12 | port = 5555
|
16 | 13 | endpoint_uri = "http://127.0.0.1:%s/" % port
|
17 |
| -test_bucket_name = "test" |
18 |
| -versioned_bucket_name = "test-versioned" |
19 |
| -secure_bucket_name = "test-secure" |
20 |
| - |
21 |
| -def get_boto3_client(): |
22 |
| - from botocore.session import Session |
23 |
| - |
24 |
| - # NB: we use the sync botocore client for setup |
25 |
| - session = Session() |
26 |
| - return session.create_client("s3", endpoint_url=endpoint_uri) |
27 |
| - |
28 |
| -@pytest.fixture(scope="module") |
29 |
| -def s3_base(): |
30 |
| - # writable local S3 system |
31 |
| - |
32 |
| - # This fixture is module-scoped, meaning that we can re-use the MotoServer across all tests |
33 |
| - ##### |
34 |
| - # lifted from https://github.com/fsspec/s3fs/blob/main/s3fs/tests/test_s3fs.py |
35 |
| - ##### |
36 |
| - server = ThreadedMotoServer(ip_address="127.0.0.1", port=port) |
37 |
| - server.start() |
38 |
| - if "AWS_SECRET_ACCESS_KEY" not in os.environ: |
39 |
| - os.environ["AWS_SECRET_ACCESS_KEY"] = "foo" |
40 |
| - if "AWS_ACCESS_KEY_ID" not in os.environ: |
41 |
| - os.environ["AWS_ACCESS_KEY_ID"] = "foo" |
42 |
| - os.environ.pop("AWS_PROFILE", None) |
43 |
| - |
44 |
| - print("server up") |
45 |
| - yield |
46 |
| - print("moto done") |
47 |
| - server.stop() |
48 |
| - |
49 |
| - |
50 |
| -@pytest.fixture() |
51 |
| -def s3fs_s3(s3_base): |
52 |
| - """ |
53 |
| - Create a fully functional "virtual" S3 FileSystem compatible with fsspec/s3fs. |
54 |
| - Method inspired by https://github.com/fsspec/s3fs/blob/main/s3fs/tests/test_s3fs.py |
55 |
| - """ |
56 |
| - client = get_boto3_client() |
57 |
| - client.create_bucket(Bucket=test_bucket_name, ACL="public-read") |
58 | 14 |
|
59 |
| - client.create_bucket(Bucket=versioned_bucket_name, ACL="public-read") |
60 |
| - client.put_bucket_versioning( |
61 |
| - Bucket=versioned_bucket_name, VersioningConfiguration={"Status": "Enabled"} |
62 |
| - ) |
63 | 15 |
|
64 |
| - # initialize secure bucket |
65 |
| - client.create_bucket(Bucket=secure_bucket_name, ACL="public-read") |
66 |
| - policy = json.dumps( |
67 |
| - { |
68 |
| - "Version": "2012-10-17", |
69 |
| - "Id": "PutObjPolicy", |
70 |
| - "Statement": [ |
71 |
| - { |
72 |
| - "Sid": "DenyUnEncryptedObjectUploads", |
73 |
| - "Effect": "Deny", |
74 |
| - "Principal": "*", |
75 |
| - "Action": "s3:PutObject", |
76 |
| - "Resource": "arn:aws:s3:::{bucket_name}/*".format( |
77 |
| - bucket_name=secure_bucket_name |
78 |
| - ), |
79 |
| - "Condition": { |
80 |
| - "StringNotEquals": { |
81 |
| - "s3:x-amz-server-side-encryption": "aws:kms" |
82 |
| - } |
83 |
| - }, |
84 |
| - } |
85 |
| - ], |
86 |
| - } |
87 |
| - ) |
| 16 | +def test_s3fs_s3(s3fs_s3): |
| 17 | + """Test mock S3 filesystem constructor.""" |
| 18 | + # this is an entire mock S3 FS |
| 19 | + mock_s3_filesystem = s3fs_s3 |
88 | 20 |
|
89 |
| - client.put_bucket_policy(Bucket=secure_bucket_name, Policy=policy) |
90 |
| - s3fs.S3FileSystem.clear_instance_cache() |
91 |
| - s3 = s3fs.S3FileSystem(anon=False, client_kwargs={"endpoint_url": endpoint_uri}) |
92 |
| - s3.invalidate_cache() |
| 21 | + # explore its attributes and methods |
| 22 | + print(dir(mock_s3_filesystem)) |
93 | 23 |
|
94 |
| - yield s3 |
| 24 | + assert not mock_s3_filesystem.anon |
| 25 | + assert not mock_s3_filesystem.version_aware |
| 26 | + assert mock_s3_filesystem.client_kwargs == {'endpoint_url': 'http://127.0.0.1:5555/'} |
95 | 27 |
|
96 | 28 |
|
97 | 29 | def spoof_boto3_s3(bucket, file_name, file_path):
|
|
0 commit comments