Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions LNX-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ services:
- MINIO_ACCESS_KEY=datajoint
- MINIO_SECRET_KEY=datajoint
# ports:
# - "9000:9000"
# - "80:80"
# volumes:
# - ./minio/config:/root/.minio
# - ./minio/data:/data
command: server /data
command: server --address ":80" /data
healthcheck:
test: ["CMD", "curl", "--fail", "http://minio:9000/minio/health/live"]
test: ["CMD", "curl", "--fail", "http://minio:80/minio/health/live"]
timeout: 5s
retries: 60
interval: 1s
fakeservices.datajoint.io:
<<: *net
image: raphaelguzman/nginx:v0.0.4
image: raphaelguzman/nginx:v0.0.5
environment:
- ADD_db_TYPE=DATABASE
- ADD_db_ENDPOINT=db:3306
- ADD_minio_TYPE=MINIO
- ADD_minio_ENDPOINT=minio:9000
- ADD_minio_ENDPOINT=minio:80
- ADD_minio_PREFIX=/
# ports:
# - "9000:9000"
# - "80:80"
# - "443:443"
# - "3306:3306"
depends_on:
Expand All @@ -60,7 +60,7 @@ services:
- DJ_TEST_HOST=fakeservices.datajoint.io
- DJ_TEST_USER=datajoint
- DJ_TEST_PASSWORD=datajoint
- S3_ENDPOINT=fakeservices.datajoint.io:9000
- S3_ENDPOINT=fakeservices.datajoint.io
- S3_ACCESS_KEY=datajoint
- S3_SECRET_KEY=datajoint
- S3_BUCKET=datajoint-test
Expand Down
6 changes: 6 additions & 0 deletions datajoint/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ class MissingExternalFile(DataJointError):
"""


class BucketInaccessible(DataJointError):
"""
Error raised when a S3 bucket is inaccessible
"""


# environment variables to control availability of experimental features

ADAPTED_TYPE_SWITCH = "DJ_SUPPORT_ADAPTED_TYPES"
Expand Down
3 changes: 3 additions & 0 deletions datajoint/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def __init__(self, connection, store=None, database=None):
if not self.is_declared:
self.declare()
self._s3 = None
if self.spec['protocol'] == 'file' and not Path(self.spec['location']).is_dir():
raise FileNotFoundError('Inaccessible local directory %s' %
self.spec['location']) from None

@property
def definition(self):
Expand Down
8 changes: 4 additions & 4 deletions datajoint/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class Folder:
A Folder instance manipulates a flat folder of objects within an S3-compatible object store
"""
def __init__(self, endpoint, bucket, access_key, secret_key, *, secure=False, **_):
self.client = minio.Minio(endpoint, access_key=access_key, secret_key=secret_key, secure=secure)
self.client = minio.Minio(endpoint, access_key=access_key, secret_key=secret_key,
secure=secure)
self.bucket = bucket
if not self.client.bucket_exists(bucket):
warnings.warn('Creating bucket "%s"' % self.bucket)
self.client.make_bucket(self.bucket)
raise errors.BucketInaccessible('Inaccessible s3 bucket %s' % bucket) from None

def put(self, name, buffer):
return self.client.put_object(
Expand Down Expand Up @@ -63,6 +63,6 @@ def get_size(self, name):

def remove_object(self, name):
try:
self.client.remove_objects(self.bucket, str(name))
self.client.remove_object(self.bucket, str(name))
except minio.ResponseError:
return errors.DataJointError('Failed to delete %s from s3 storage' % name)
14 changes: 7 additions & 7 deletions local-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ services:
- MINIO_ACCESS_KEY=datajoint
- MINIO_SECRET_KEY=datajoint
# ports:
# - "9000:9000"
# - "80:80"
# To persist MinIO data and config
# volumes:
# - ./minio/data:/data
# - ./minio/config:/root/.minio
command: server /data
command: server --address ":80" /data
healthcheck:
test: ["CMD", "curl", "--fail", "http://minio:9000/minio/health/live"]
test: ["CMD", "curl", "--fail", "http://minio:80/minio/health/live"]
timeout: 5s
retries: 60
interval: 1s
fakeservices.datajoint.io:
<<: *net
image: raphaelguzman/nginx:v0.0.4
image: raphaelguzman/nginx:v0.0.5
environment:
- ADD_db_TYPE=DATABASE
- ADD_db_ENDPOINT=db:3306
- ADD_minio_TYPE=MINIO
- ADD_minio_ENDPOINT=minio:9000
- ADD_minio_ENDPOINT=minio:80
- ADD_minio_PREFIX=/
ports:
- "9000:9000"
- "80:80"
- "443:443"
- "3306:3306"
depends_on:
Expand All @@ -63,7 +63,7 @@ services:
- DJ_TEST_USER=datajoint
- DJ_TEST_PASSWORD=datajoint
# If running tests locally, make sure to add entry in /etc/hosts for 127.0.0.1 fakeservices.datajoint.io
- S3_ENDPOINT=fakeservices.datajoint.io:9000
- S3_ENDPOINT=fakeservices.datajoint.io
- S3_ACCESS_KEY=datajoint
- S3_SECRET_KEY=datajoint
- S3_BUCKET=datajoint-test
Expand Down
27 changes: 14 additions & 13 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
secret_key=environ.get('S3_SECRET_KEY', 'datajoint'),
bucket=environ.get('S3_BUCKET', 'datajoint-test'))

S3_MIGRATE_BUCKET = [path.stem for path in Path(
Path(__file__).resolve().parent,
'external-legacy-data', 's3').iterdir()][0]

# Prefix for all databases used during testing
PREFIX = environ.get('DJ_TEST_DB_PREFIX', 'djtest')
conn_root = dj.conn(**CONN_INFO_ROOT)
Expand Down Expand Up @@ -129,23 +133,22 @@ def setup_package():
source = Path(
Path(__file__).resolve().parent,
'external-legacy-data','s3')
bucket = "migrate-test"
region = "us-east-1"
try:
minioClient.make_bucket(bucket, location=region)
minioClient.make_bucket(S3_MIGRATE_BUCKET, location=region)
except minio.error.BucketAlreadyOwnedByYou:
pass

pathlist = Path(source).glob('**/*')
for path in pathlist:
if os.path.isfile(str(path)) and ".sql" not in str(path):
minioClient.fput_object(
bucket, str(Path(
os.path.relpath(str(path),str(Path(source,bucket))))
S3_MIGRATE_BUCKET, str(Path(
os.path.relpath(str(path),str(Path(source,S3_MIGRATE_BUCKET))))
.as_posix()), str(path))
# Add S3
try:
minioClient.make_bucket("datajoint-test", location=region)
minioClient.make_bucket(S3_CONN_INFO['bucket'], location=region)
except minio.error.BucketAlreadyOwnedByYou:
pass

Expand Down Expand Up @@ -176,19 +179,17 @@ def teardown_package():
remove("dj_local_conf.json")

# Remove old S3
bucket = "migrate-test"
objs = list(minioClient.list_objects_v2(
bucket, recursive=True))
objs = [minioClient.remove_object(bucket,
S3_MIGRATE_BUCKET, recursive=True))
objs = [minioClient.remove_object(S3_MIGRATE_BUCKET,
o.object_name.encode('utf-8')) for o in objs]
minioClient.remove_bucket(bucket)
minioClient.remove_bucket(S3_MIGRATE_BUCKET)

# Remove S3
bucket = "datajoint-test"
objs = list(minioClient.list_objects_v2(bucket, recursive=True))
objs = [minioClient.remove_object(bucket,
objs = list(minioClient.list_objects_v2(S3_CONN_INFO['bucket'], recursive=True))
objs = [minioClient.remove_object(S3_CONN_INFO['bucket'],
o.object_name.encode('utf-8')) for o in objs]
minioClient.remove_bucket(bucket)
minioClient.remove_bucket(S3_CONN_INFO['bucket'])

# Remove old File Content
shutil.rmtree(str(Path(os.path.expanduser('~'),'temp')))
8 changes: 4 additions & 4 deletions tests/test_blob_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import datajoint as dj
import os
from pathlib import Path
from . import S3_CONN_INFO
from . import S3_CONN_INFO, S3_MIGRATE_BUCKET
from . import CONN_INFO
from datajoint.migrate import _migrate_dj011_blob
dj.config['enable_python_native_blobs'] = True
Expand All @@ -20,20 +20,20 @@ def test_convert():
default_store: dict(
protocol='s3',
endpoint=S3_CONN_INFO['endpoint'],
bucket='migrate-test',
bucket=S3_MIGRATE_BUCKET,
location='store',
access_key=S3_CONN_INFO['access_key'],
secret_key=S3_CONN_INFO['secret_key']),
'shared': dict(
protocol='s3',
endpoint=S3_CONN_INFO['endpoint'],
bucket='migrate-test',
bucket=S3_MIGRATE_BUCKET,
location='maps',
access_key=S3_CONN_INFO['access_key'],
secret_key=S3_CONN_INFO['secret_key']),
'local': dict(
protocol='file',
location=str(Path(os.path.expanduser('~'),'temp','migrate-test')))
location=str(Path(os.path.expanduser('~'),'temp',S3_MIGRATE_BUCKET)))
}
dj.config['cache'] = str(Path(os.path.expanduser('~'),'temp','dj-cache'))

Expand Down
7 changes: 5 additions & 2 deletions tests/test_filepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def test_filepath(store="repo"):

# put the same file twice to ensure storing once
uuid1 = ext.upload_filepath(str(managed_file))
uuid2 = ext.upload_filepath(str(managed_file)) # no duplication should arise if file is the same
# no duplication should arise if file is the same
uuid2 = ext.upload_filepath(str(managed_file))
assert_equal(uuid1, uuid2)

# remove to ensure downloading
Expand All @@ -79,6 +80,7 @@ def test_filepath(store="repo"):

# cleanup
ext.delete(delete_external_files=True)
assert_false(ext.exists(ext._make_external_filepath(str(Path(relpath, filename)))))


def test_filepath_s3():
Expand Down Expand Up @@ -115,7 +117,8 @@ def test_duplicate_error(store="repo"):
ext.upload_filepath(str(managed_file))
with managed_file.open('wb') as f:
f.write(os.urandom(300))
ext.upload_filepath(str(managed_file)) # this should raise exception because the file has changed
# this should raise exception because the file has changed
ext.upload_filepath(str(managed_file))


def test_duplicate_error_s3():
Expand Down
18 changes: 13 additions & 5 deletions tests/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ def test_connection():

# Initialize httpClient with relevant timeout.
http_client = urllib3.PoolManager(
timeout=30, cert_reqs='CERT_REQUIRED', ca_certs=certifi.where(),
retries=urllib3.Retry(total=3, backoff_factor=0.2, status_forcelist=[500, 502, 503, 504]))
timeout=30, cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where(),
retries=urllib3.Retry(total=3, backoff_factor=0.2,
status_forcelist=[
500, 502,
503, 504]))

# Initialize minioClient with an endpoint and access/secret keys.
minio_client = Minio(
Expand All @@ -30,12 +34,16 @@ def test_connection_secure():

# Initialize httpClient with relevant timeout.
http_client = urllib3.PoolManager(
timeout=30, cert_reqs='CERT_REQUIRED', ca_certs=certifi.where(),
retries=urllib3.Retry(total=3, backoff_factor=0.2, status_forcelist=[500, 502, 503, 504]))
timeout=30, cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where(),
retries=urllib3.Retry(total=3, backoff_factor=0.2,
status_forcelist=[
500, 502,
503, 504]))

# Initialize minioClient with an endpoint and access/secret keys.
minio_client = Minio(
S3_CONN_INFO['endpoint'].split(':')[0] + ':443',
S3_CONN_INFO['endpoint'],
access_key=S3_CONN_INFO['access_key'],
secret_key=S3_CONN_INFO['secret_key'],
secure=True,
Expand Down