Skip to content

Commit 784ac42

Browse files
committed
working version
1 parent c98431a commit 784ac42

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

tests/unit/test_mock_s3.py

+32-37
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
11
import os
22
import s3fs
33
import pathlib
4-
from tempfile import NamedTemporaryFile
5-
import logging
64
import boto3
75
import moto
86
import pyfive
97
import pytest
10-
from botocore.exceptions import ClientError
8+
import h5netcdf
9+
10+
from tempfile import NamedTemporaryFile
1111

1212

13-
def file_upload(upload_file_bucket, file_name, file_path):
14-
if os.path.exists(file_path):
15-
with open(file_path, 'r') as f:
16-
xml = f.read()
17-
else:
18-
logging.error("File '%s' does not exist." % file_path)
19-
tools.exit_gracefully(botocore.log)
20-
try:
13+
def spoof_s3(bucket, file_name, file_path):
14+
# "put" file
15+
if os.path.exists(file_path):
16+
with open(file_path, "rb") as file_contents:
2117
conn = boto3.session.Session()
2218
s3 = conn.resource('s3')
23-
object = s3.Object(upload_file_bucket, file_name)
24-
result = object.put(Body=xml)
19+
object = s3.Object(bucket, file_name)
20+
result = object.put(Body=file_contents)
2521
res = result.get('ResponseMetadata')
2622
if res.get('HTTPStatusCode') == 200:
27-
logging.info('File Uploaded Successfully')
23+
print('File Uploaded Successfully')
2824
else:
29-
logging.info('File Not Uploaded Successfully')
30-
return res
31-
except ClientError as e:
32-
logging.error(e)
25+
print('File Not Uploaded Successfully')
3326

27+
# "download" file
28+
s3 = boto3.resource('s3')
29+
# arg0: file in bucket; arg1: file to download to
30+
target_file = "test.nc"
31+
s3file = s3.Bucket(bucket).download_file(file_name, target_file)
32+
print(os.path.isfile(target_file))
3433

35-
def file_load(bucket, file_name):
36-
conn = boto3.session.Session()
37-
s3 = conn.resource('s3')
38-
object = s3.Object(bucket, file_name)
39-
result = object.get(Range="0=2")
40-
print("S3 Test mock file:", result)
34+
# "access" file "remotely" with s3fs
35+
fs = s3fs.S3FileSystem()
36+
with open('testobj.nc', 'wb') as ncdata:
37+
object.download_fileobj(ncdata)
38+
with open('testobj.nc', 'rb') as ncdata:
39+
ncfile = h5netcdf.File(ncdata, 'r', invalid_netcdf=True)
40+
print(ncfile) # it works but...
4141

42-
ds = pyfive.File(result)
43-
return ds
42+
return res
4443

4544

4645
@pytest.fixture(scope='session')
@@ -54,8 +53,7 @@ def aws_credentials():
5453

5554
try:
5655
tmp = NamedTemporaryFile(delete=False)
57-
# you many need to change 'aws_prof_dev_qa' to be your profile name
58-
tmp.write(b"""[aws_prof_dev_qa]
56+
tmp.write(b"""[wild weasel]
5957
aws_access_key_id = testing
6058
aws_secret_access_key = testing""")
6159
tmp.close()
@@ -77,12 +75,9 @@ def empty_bucket(aws_credentials):
7775
moto_fake.stop()
7876

7977

80-
def test_file_upload(empty_bucket):
81-
with NamedTemporaryFile() as tmp:
82-
tmp.write(b'Hi')
83-
file_name = pathlib.Path(tmp.name).name
84-
85-
result = file_upload("MY_BUCKET", file_name, tmp.name)
86-
result2 = file_load("MY_BUCKET", file_name)
87-
88-
assert result.get('HTTPStatusCode') == 200
78+
def test_s3file_spoofing(empty_bucket):
79+
ncfile = "./tests/test_data/daily_data.nc"
80+
file_path = pathlib.Path(ncfile)
81+
file_name = pathlib.Path(ncfile).name
82+
result = spoof_s3("MY_BUCKET", file_name, file_path)
83+
assert result.get('HTTPStatusCode') == 200

0 commit comments

Comments
 (0)