Skip to content

Commit 8b742c2

Browse files
committed
Added test for s3 delete error handling, fixed bug
1 parent 9f817f7 commit 8b742c2

File tree

3 files changed

+61
-24
lines changed

3 files changed

+61
-24
lines changed

datajoint/external.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,12 @@ def used(self):
314314
return self & [FreeTable(self.connection, ref['referencing_table']).proj(hash=ref['column_name'])
315315
for ref in self.references]
316316

317-
def delete(self, *, delete_external_files=None, limit=None, display_progress=True):
317+
def delete(self, *, delete_external_files=None, limit=None, display_progress=True, errors_as_string = True):
318318
"""
319319
:param delete_external_files: True or False. If False, only the tracking info is removed from the
320320
external store table but the external files remain intact. If True, then the external files
321321
themselves are deleted too.
322+
:param errors_as_string: If True any errors returned when deleting from external files will be strings
322323
:param limit: (integer) limit the number of items to delete
323324
:param display_progress: if True, display progress as files are cleaned up
324325
:return: if deleting external files, returns errors
@@ -346,7 +347,9 @@ def delete(self, *, delete_external_files=None, limit=None, display_progress=Tru
346347
try:
347348
self._remove_external_file(external_path)
348349
except Exception as error:
349-
error_list.append((uuid, external_path, str(error)))
350+
if errors_as_string:
351+
error = str(error)
352+
error_list.append((uuid, external_path, error))
350353
return error_list
351354

352355

datajoint/s3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ def remove_object(self, name):
8585
except minio.error.MinioException:
8686
raise errors.DataJointError('Failed to delete %s from s3 storage' % name)
8787
except ValueError:
88-
raise errors.DataJointError('minIO ValueError')
88+
raise errors.DataJointError('minIO ValueError something is wrong with the strings passed')

tests/test_s3.py

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from . import S3_CONN_INFO
2-
import time
32
from minio import Minio
4-
from minio.objectlockconfig import ObjectLockConfig
5-
from minio.commonconfig import COMPLIANCE, ENABLED, GOVERNANCE
3+
import minio
4+
import json
65
import urllib3
76
import certifi
87
from nose.tools import assert_true, raises
9-
108
from .schema_external import schema, SimpleRemote
119
from datajoint.errors import DataJointError
1210

@@ -59,27 +57,63 @@ def test_connection_secure():
5957
@raises(DataJointError)
6058
def test_remove_object_exception():
6159
#https://github.com/datajoint/datajoint-python/issues/952
62-
63-
# Initialize httpClient with relevant timeout.
64-
http_client = urllib3.PoolManager(
65-
timeout=30, cert_reqs='CERT_REQUIRED',
66-
ca_certs=certifi.where(),
67-
retries=urllib3.Retry(total=3, backoff_factor=0.2,
68-
status_forcelist=[
69-
500, 502,
70-
503, 504]))
71-
7260
# Initialize minioClient with an endpoint and access/secret keys.
7361
minio_client = Minio(
74-
S3_CONN_INFO['endpoint'],
75-
access_key=S3_CONN_INFO['access_key'],
76-
secret_key=S3_CONN_INFO['secret_key'],
77-
secure=True,
78-
http_client=http_client)
62+
'minio:9000',
63+
access_key= 'jeffjeff',
64+
secret_key= 'jeffjeff',
65+
secure=False)
66+
67+
minio_admin = minio.MinioAdmin(target = 'myminio')
68+
minio_admin.user_add(access_key = 'jeffjeff', secret_key = 'jeffjeff')
7969

70+
testpolicy = {
71+
"Version": "2012-10-17",
72+
"Statement": [
73+
{
74+
"Action": [
75+
"s3:GetBucketLocation",
76+
"s3:ListBucket",
77+
"s3:ListBucketMultipartUploads",
78+
"s3:ListAllMyBuckets"
79+
],
80+
"Effect": "Allow",
81+
"Resource": [
82+
"arn:aws:s3:::datajoint.test",
83+
"arn:aws:s3:::datajoint.migrate"
84+
],
85+
"Sid": ""
86+
},
87+
{
88+
"Action": [
89+
"s3:GetObject",
90+
"s3:ListMultipartUploadParts"
91+
],
92+
"Effect": "Allow",
93+
"Resource": [
94+
"arn:aws:s3:::datajoint.test/*",
95+
"arn:aws:s3:::datajoint.migrate/*"
96+
],
97+
"Sid": ""
98+
}
99+
]
100+
}
101+
with open('/tmp/policy.json', 'w') as f:
102+
f.write(json.dumps(testpolicy))
103+
minio_admin.policy_add(policy_name = 'test', policy_file = '/tmp/policy.json')
104+
minio_admin.policy_set(policy_name = 'test', user = 'jeffjeff')
80105

81-
schema.external['share'].s3.bucket = ''
82-
schema.external['share'].s3.remove_object('test')
106+
107+
test = [1,[1,2,3]]
108+
SimpleRemote.insert1(test)
109+
SimpleRemote.delete()
110+
111+
schema.external['share'].s3.client = minio_client
112+
# This method returns a list of errors
113+
error_list = schema.external['share'].delete(delete_external_files = True, errors_as_string = False)
114+
raise error_list[0][2]
115+
# schema.external['share'].s3.secret_key = ''
116+
# schema.external['share'].s3.remove_object('test')
83117

84118

85119

0 commit comments

Comments
 (0)