|
| 1 | +import os |
| 2 | +import s3fs |
| 3 | +import pathlib |
| 4 | +import pytest |
| 5 | +import h5netcdf |
| 6 | + |
| 7 | +from tempfile import NamedTemporaryFile |
| 8 | +from activestorage.active import load_from_s3 |
| 9 | + |
| 10 | + |
| 11 | +# needed by the spoofed s3 filesystem |
| 12 | +port = 5555 |
| 13 | +endpoint_uri = "http://127.0.0.1:%s/" % port |
| 14 | + |
| 15 | + |
| 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 |
| 20 | + |
| 21 | + # explore its attributes and methods |
| 22 | + print(dir(mock_s3_filesystem)) |
| 23 | + |
| 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/'} |
| 27 | + |
| 28 | + |
| 29 | +def spoof_boto3_s3(bucket, file_name, file_path): |
| 30 | + # this is a pure boto3 implementation |
| 31 | + # I am leaving it here just in case we'll ever need it in the future |
| 32 | + # NOTE: we are NOT including boto3 as dependency yet, until we ever need it |
| 33 | + |
| 34 | + # "put" file |
| 35 | + if os.path.exists(file_path): |
| 36 | + with open(file_path, "rb") as file_contents: |
| 37 | + conn = boto3.session.Session() |
| 38 | + s3 = conn.resource('s3') |
| 39 | + object = s3.Object(bucket, file_name) |
| 40 | + result = object.put(Body=file_contents) |
| 41 | + res = result.get('ResponseMetadata') |
| 42 | + if res.get('HTTPStatusCode') == 200: |
| 43 | + print('File Uploaded Successfully') |
| 44 | + else: |
| 45 | + print('File Not Uploaded Successfully') |
| 46 | + |
| 47 | + # "download" file |
| 48 | + s3 = boto3.resource('s3') |
| 49 | + # arg0: file in bucket; arg1: file to download to |
| 50 | + target_file = "test.nc" |
| 51 | + s3file = s3.Bucket(bucket).download_file(file_name, target_file) |
| 52 | + print(os.path.isfile(target_file)) |
| 53 | + |
| 54 | + # "access" file "remotely" with s3fs |
| 55 | + fs = s3fs.S3FileSystem(anon=True) |
| 56 | + with open('testobj.nc', 'wb') as ncdata: |
| 57 | + object.download_fileobj(ncdata) |
| 58 | + with open('testobj.nc', 'rb') as ncdata: |
| 59 | + ncfile = h5netcdf.File(ncdata, 'r', invalid_netcdf=True) |
| 60 | + print(ncfile) |
| 61 | + |
| 62 | + return res |
| 63 | + |
| 64 | + |
| 65 | +@pytest.fixture(scope='session') |
| 66 | +def aws_credentials(): |
| 67 | + """ |
| 68 | + Mocked AWS Credentials for moto. |
| 69 | + NOTE: Used ONLY by the pure boto3 test method spoof_boto3_s3. |
| 70 | + """ |
| 71 | + # NOTE: Used ONLY by the pure boto3 test method spoof_boto3_s3 |
| 72 | + os.environ['AWS_ACCESS_KEY_ID'] = 'testing' |
| 73 | + os.environ['AWS_SECRET_ACCESS_KEY'] = 'testing' |
| 74 | + os.environ['AWS_SECURITY_TOKEN'] = 'testing' |
| 75 | + os.environ['AWS_SESSION_TOKEN'] = 'testing' |
| 76 | + os.environ['AWS_DEFAULT_REGION'] = 'us-east-1' |
| 77 | + |
| 78 | + try: |
| 79 | + tmp = NamedTemporaryFile(delete=False) |
| 80 | + tmp.write(b"""[wild weasel] |
| 81 | + aws_access_key_id = testing |
| 82 | + aws_secret_access_key = testing""") |
| 83 | + tmp.close() |
| 84 | + os.environ['AWS_SHARED_CREDENTIALS_FILE'] = str(tmp.name) |
| 85 | + yield |
| 86 | + finally: |
| 87 | + os.unlink(tmp.name) |
| 88 | + |
| 89 | + |
| 90 | +@pytest.fixture(scope='function') |
| 91 | +def empty_bucket(aws_credentials): |
| 92 | + """Create an empty bucket.""" |
| 93 | + # NOTE: Used ONLY by the pure boto3 test method spoof_boto3_s3 |
| 94 | + moto_fake = moto.mock_aws() |
| 95 | + try: |
| 96 | + moto_fake.start() |
| 97 | + conn = boto3.resource('s3') |
| 98 | + conn.create_bucket(Bucket="MY_BUCKET") |
| 99 | + yield conn |
| 100 | + finally: |
| 101 | + moto_fake.stop() |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.skip(reason="This test uses the pure boto3 implement which we don't need at the moment.") |
| 105 | +def test_s3file_with_pure_boto3(empty_bucket): |
| 106 | + ncfile = "./tests/test_data/daily_data.nc" |
| 107 | + file_path = pathlib.Path(ncfile) |
| 108 | + file_name = pathlib.Path(ncfile).name |
| 109 | + # partial spoofing with only boto3+moto |
| 110 | + result = spoof_s3("MY_BUCKET", file_name, file_path) |
| 111 | + with s3.open(os.path.join("MY_BUCKET", file_name), "rb") as f: |
| 112 | + ncfile = h5netcdf.File(f, 'r', invalid_netcdf=True) |
| 113 | + assert result.get('HTTPStatusCode') == 200 |
| 114 | + |
| 115 | + |
| 116 | +def test_s3file_with_s3fs(s3fs_s3): |
| 117 | + """ |
| 118 | + This test spoofs a complete s3fs FileSystem via s3fs_s3, |
| 119 | + creates a mock bucket inside it, then puts a REAL netCDF4 file in it, |
| 120 | + then it loads it as if it was an S3 file. This is proper |
| 121 | + Wild Weasel stuff right here. |
| 122 | + """ |
| 123 | + # set up physical file and Path properties |
| 124 | + ncfile = "./tests/test_data/daily_data.nc" |
| 125 | + file_path = pathlib.Path(ncfile) |
| 126 | + file_name = pathlib.Path(ncfile).name |
| 127 | + |
| 128 | + # use mocked s3fs |
| 129 | + bucket = "MY_BUCKET" |
| 130 | + s3fs_s3.mkdir(bucket) |
| 131 | + s3fs_s3.put(file_path, bucket) |
| 132 | + s3 = s3fs.S3FileSystem( |
| 133 | + anon=False, version_aware=True, client_kwargs={"endpoint_url": endpoint_uri} |
| 134 | + ) |
| 135 | + |
| 136 | + # test load by h5netcdf |
| 137 | + with s3.open(os.path.join("MY_BUCKET", file_name), "rb") as f: |
| 138 | + print("File path", f.path) |
| 139 | + ncfile = h5netcdf.File(f, 'r', invalid_netcdf=True) |
| 140 | + print("File loaded from spoof S3 with h5netcdf:", ncfile) |
| 141 | + print(ncfile["ta"]) |
| 142 | + assert "ta" in ncfile |
| 143 | + |
| 144 | + # test Active |
| 145 | + storage_options = dict(anon=False, version_aware=True, |
| 146 | + client_kwargs={"endpoint_url": endpoint_uri}) |
| 147 | + with load_from_s3(os.path.join("MY_BUCKET", file_name), storage_options) as ac_file: |
| 148 | + print(ac_file) |
| 149 | + assert "ta" in ac_file |
0 commit comments