Skip to content

Commit 353a836

Browse files
committed
simplify via conftest and add wee test for s3
1 parent bedf57f commit 353a836

File tree

1 file changed

+10
-78
lines changed

1 file changed

+10
-78
lines changed

tests/unit/test_mock_s3.py

+10-78
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,29 @@
11
import os
22
import s3fs
33
import pathlib
4-
import json
5-
import moto
64
import pytest
75
import h5netcdf
86

97
from tempfile import NamedTemporaryFile
10-
from moto.moto_server.threaded_moto_server import ThreadedMotoServer
118
from activestorage.active import load_from_s3
129

1310

14-
# some spoofy server parameters
11+
# needed by the spoofed s3 filesystem
1512
port = 5555
1613
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")
5814

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-
)
6315

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
8820

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))
9323

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/'}
9527

9628

9729
def spoof_boto3_s3(bucket, file_name, file_path):

0 commit comments

Comments
 (0)