|
8 | 8 | import h5netcdf
|
9 | 9 |
|
10 | 10 | from tempfile import NamedTemporaryFile
|
| 11 | +from test_s3fs import s3 as tests3 |
| 12 | +from moto.moto_server.threaded_moto_server import ThreadedMotoServer |
| 13 | + |
| 14 | +# some spoofy server parameters |
| 15 | +port = 5555 |
| 16 | +endpoint_uri = "http://127.0.0.1:%s/" % port |
| 17 | + |
| 18 | +@pytest.fixture(scope="module") |
| 19 | +def s3_base(): |
| 20 | + # writable local S3 system |
| 21 | + |
| 22 | + # This fixture is module-scoped, meaning that we can re-use the MotoServer across all tests |
| 23 | + server = ThreadedMotoServer(ip_address="127.0.0.1", port=port) |
| 24 | + server.start() |
| 25 | + if "AWS_SECRET_ACCESS_KEY" not in os.environ: |
| 26 | + os.environ["AWS_SECRET_ACCESS_KEY"] = "foo" |
| 27 | + if "AWS_ACCESS_KEY_ID" not in os.environ: |
| 28 | + os.environ["AWS_ACCESS_KEY_ID"] = "foo" |
| 29 | + os.environ.pop("AWS_PROFILE", None) |
| 30 | + |
| 31 | + print("server up") |
| 32 | + yield |
| 33 | + print("moto done") |
| 34 | + server.stop() |
11 | 35 |
|
12 | 36 |
|
13 | 37 | def spoof_s3(bucket, file_name, file_path):
|
@@ -77,10 +101,38 @@ def empty_bucket(aws_credentials):
|
77 | 101 | finally:
|
78 | 102 | moto_fake.stop()
|
79 | 103 |
|
80 |
| - |
| 104 | +@pytest.mark.skip(reason="This test is now obsolete") |
81 | 105 | def test_s3file_spoofing(empty_bucket):
|
82 | 106 | ncfile = "./tests/test_data/daily_data.nc"
|
83 | 107 | file_path = pathlib.Path(ncfile)
|
84 | 108 | file_name = pathlib.Path(ncfile).name
|
| 109 | + # partial spoofing with only boto3+moto |
85 | 110 | result = spoof_s3("MY_BUCKET", file_name, file_path)
|
| 111 | + |
| 112 | + with s3.open(os.path.join("MY_BUCKET", file_name), "rb") as f: |
| 113 | + ncfile = h5netcdf.File(f, 'r', invalid_netcdf=True) |
86 | 114 | assert result.get('HTTPStatusCode') == 200
|
| 115 | + |
| 116 | + |
| 117 | +def test_s3file_spoofing_2(tests3): |
| 118 | + """ |
| 119 | + This test spoofs a complete s3fs FileSystem, |
| 120 | + creates a mock bucket inside it, then puts a REAL netCDF4 file in it, |
| 121 | + then it loads it as if it was an S3 file. This is proper |
| 122 | + Wild Weasel stuff right here. |
| 123 | + """ |
| 124 | + ncfile = "./tests/test_data/daily_data.nc" |
| 125 | + file_path = pathlib.Path(ncfile) |
| 126 | + file_name = pathlib.Path(ncfile).name |
| 127 | + |
| 128 | + # use s3fs proper |
| 129 | + bucket = "MY_BUCKET" |
| 130 | + tests3.mkdir(bucket) |
| 131 | + tests3.put(file_path, bucket) |
| 132 | + s3 = s3fs.S3FileSystem( |
| 133 | + anon=False, version_aware=True, client_kwargs={"endpoint_url": endpoint_uri} |
| 134 | + ) |
| 135 | + with s3.open(os.path.join("MY_BUCKET", file_name), "rb") as f: |
| 136 | + ncfile = h5netcdf.File(f, 'r', invalid_netcdf=True) |
| 137 | + print("File loaded from spoof S3 with h5netcdf:", ncfile) |
| 138 | + print(x) |
0 commit comments