Skip to content

Commit ab460d5

Browse files
authored
Test using new abstract test harness (#713)
* Abstract test harness * Add S3fsFixtures.supports_empty_directories * Update in line with latest fsspec
1 parent 676b7b6 commit ab460d5

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

s3fs/tests/derived/__init__.py

Whitespace-only changes.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import json
2+
import os
3+
import pytest
4+
import requests
5+
import time
6+
7+
from fsspec.tests.abstract import AbstractFixtures
8+
from s3fs.core import S3FileSystem
9+
10+
11+
test_bucket_name = "test"
12+
secure_bucket_name = "test-secure"
13+
versioned_bucket_name = "test-versioned"
14+
port = 5555
15+
endpoint_uri = "http://127.0.0.1:%s/" % port
16+
17+
18+
class S3fsFixtures(AbstractFixtures):
19+
@pytest.fixture(scope="class")
20+
def fs(self, _s3_base, _get_boto3_client):
21+
print("FS")
22+
client = _get_boto3_client
23+
client.create_bucket(Bucket=test_bucket_name, ACL="public-read")
24+
25+
client.create_bucket(Bucket=versioned_bucket_name, ACL="public-read")
26+
client.put_bucket_versioning(
27+
Bucket=versioned_bucket_name, VersioningConfiguration={"Status": "Enabled"}
28+
)
29+
30+
# initialize secure bucket
31+
client.create_bucket(Bucket=secure_bucket_name, ACL="public-read")
32+
policy = json.dumps(
33+
{
34+
"Version": "2012-10-17",
35+
"Id": "PutObjPolicy",
36+
"Statement": [
37+
{
38+
"Sid": "DenyUnEncryptedObjectUploads",
39+
"Effect": "Deny",
40+
"Principal": "*",
41+
"Action": "s3:PutObject",
42+
"Resource": "arn:aws:s3:::{bucket_name}/*".format(
43+
bucket_name=secure_bucket_name
44+
),
45+
"Condition": {
46+
"StringNotEquals": {
47+
"s3:x-amz-server-side-encryption": "aws:kms"
48+
}
49+
},
50+
}
51+
],
52+
}
53+
)
54+
client.put_bucket_policy(Bucket=secure_bucket_name, Policy=policy)
55+
56+
S3FileSystem.clear_instance_cache()
57+
s3 = S3FileSystem(anon=False, client_kwargs={"endpoint_url": endpoint_uri})
58+
s3.invalidate_cache()
59+
yield s3
60+
61+
@pytest.fixture
62+
def fs_path(self):
63+
return test_bucket_name
64+
65+
def supports_empty_directories(self):
66+
return False
67+
68+
@pytest.fixture(scope="class")
69+
def _get_boto3_client(self):
70+
from botocore.session import Session
71+
72+
# NB: we use the sync botocore client for setup
73+
session = Session()
74+
return session.create_client("s3", endpoint_url=endpoint_uri)
75+
76+
@pytest.fixture(scope="class")
77+
def _s3_base(self):
78+
# writable local S3 system
79+
import shlex
80+
import subprocess
81+
82+
try:
83+
# should fail since we didn't start server yet
84+
r = requests.get(endpoint_uri)
85+
except:
86+
pass
87+
else:
88+
if r.ok:
89+
raise RuntimeError("moto server already up")
90+
if "AWS_SECRET_ACCESS_KEY" not in os.environ:
91+
os.environ["AWS_SECRET_ACCESS_KEY"] = "foo"
92+
if "AWS_ACCESS_KEY_ID" not in os.environ:
93+
os.environ["AWS_ACCESS_KEY_ID"] = "foo"
94+
proc = subprocess.Popen(
95+
shlex.split("moto_server s3 -p %s" % port),
96+
stderr=subprocess.DEVNULL,
97+
stdout=subprocess.DEVNULL,
98+
stdin=subprocess.DEVNULL,
99+
)
100+
101+
timeout = 5
102+
while timeout > 0:
103+
try:
104+
print("polling for moto server")
105+
r = requests.get(endpoint_uri)
106+
if r.ok:
107+
break
108+
except:
109+
pass
110+
timeout -= 0.1
111+
time.sleep(0.1)
112+
print("server up")
113+
yield
114+
print("moto done")
115+
proc.terminate()
116+
proc.wait()

s3fs/tests/derived/s3fs_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fsspec.tests.abstract as abstract
2+
from s3fs.tests.derived.s3fs_fixtures import S3fsFixtures
3+
4+
5+
class TestS3fsCopy(abstract.AbstractCopyTests, S3fsFixtures):
6+
pass
7+
8+
9+
class TestS3fsGet(abstract.AbstractGetTests, S3fsFixtures):
10+
pass
11+
12+
13+
class TestS3fsPut(abstract.AbstractPutTests, S3fsFixtures):
14+
pass

0 commit comments

Comments
 (0)