|
1 | 1 | from . import S3_CONN_INFO |
2 | | - |
3 | 2 | from minio import Minio |
| 3 | +import json |
4 | 4 | import urllib3 |
5 | 5 | import certifi |
6 | | -from nose.tools import assert_true |
| 6 | +from nose.tools import assert_true, raises |
| 7 | +from .schema_external import schema, SimpleRemote |
| 8 | +from datajoint.errors import DataJointError |
| 9 | +import os |
| 10 | +from datajoint.hash import uuid_from_buffer |
| 11 | +from datajoint.blob import pack |
7 | 12 |
|
8 | 13 |
|
9 | 14 | class TestS3: |
@@ -51,3 +56,84 @@ def test_connection_secure(): |
51 | 56 | http_client=http_client) |
52 | 57 |
|
53 | 58 | assert_true(minio_client.bucket_exists(S3_CONN_INFO['bucket'])) |
| 59 | + |
| 60 | + @staticmethod |
| 61 | + @raises(DataJointError) |
| 62 | + def test_remove_object_exception(): |
| 63 | + # https://github.com/datajoint/datajoint-python/issues/952 |
| 64 | + |
| 65 | + # Initialize minioClient with an endpoint and access/secret keys. |
| 66 | + minio_client = Minio( |
| 67 | + 'minio:9000', |
| 68 | + access_key='jeffjeff', |
| 69 | + secret_key='jeffjeff', |
| 70 | + secure=False) |
| 71 | + |
| 72 | + # Create new user |
| 73 | + os.system('mc admin user add myminio jeffjeff jeffjeff') |
| 74 | + # json for test policy for permissionless user |
| 75 | + testpolicy = { |
| 76 | + "Version": "2012-10-17", |
| 77 | + "Statement": [ |
| 78 | + { |
| 79 | + "Action": [ |
| 80 | + "s3:GetBucketLocation", |
| 81 | + "s3:ListBucket", |
| 82 | + "s3:ListBucketMultipartUploads", |
| 83 | + "s3:ListAllMyBuckets" |
| 84 | + ], |
| 85 | + "Effect": "Allow", |
| 86 | + "Resource": [ |
| 87 | + "arn:aws:s3:::datajoint.test", |
| 88 | + "arn:aws:s3:::datajoint.migrate" |
| 89 | + ], |
| 90 | + "Sid": "" |
| 91 | + }, |
| 92 | + { |
| 93 | + "Action": [ |
| 94 | + "s3:GetObject", |
| 95 | + "s3:ListMultipartUploadParts" |
| 96 | + ], |
| 97 | + "Effect": "Allow", |
| 98 | + "Resource": [ |
| 99 | + "arn:aws:s3:::datajoint.test/*", |
| 100 | + "arn:aws:s3:::datajoint.migrate/*" |
| 101 | + ], |
| 102 | + "Sid": "" |
| 103 | + } |
| 104 | + ] |
| 105 | + } |
| 106 | + |
| 107 | + # Write test json to tmp directory so we can use it to create a new user policy |
| 108 | + with open('/tmp/policy.json', 'w') as f: |
| 109 | + f.write(json.dumps(testpolicy)) |
| 110 | + |
| 111 | + # Add the policy and apply it to the user |
| 112 | + os.system('mc admin policy add myminio test /tmp/policy.json') |
| 113 | + os.system('mc admin policy set myminio test user=jeffjeff') |
| 114 | + |
| 115 | + # Insert some test data and remove it so that the external table is populated |
| 116 | + test = [1, [1, 2, 3]] |
| 117 | + SimpleRemote.insert1(test) |
| 118 | + SimpleRemote.delete() |
| 119 | + |
| 120 | + # Save the old external table minio client |
| 121 | + old_client = schema.external['share'].s3.client |
| 122 | + |
| 123 | + # Apply our new minio client to the external table that has permissions restrictions |
| 124 | + schema.external['share'].s3.client = minio_client |
| 125 | + |
| 126 | + # This method returns a list of errors |
| 127 | + error_list = schema.external['share'].delete(delete_external_files=True, |
| 128 | + errors_as_string=False) |
| 129 | + |
| 130 | + # Teardown |
| 131 | + os.system('mc admin policy remove myminio test') |
| 132 | + os.system('mc admin user remove myminio jeffjeff') |
| 133 | + schema.external['share'].s3.client = old_client |
| 134 | + schema.external['share'].delete(delete_external_files=True) |
| 135 | + os.remove("/tmp/policy.json") |
| 136 | + |
| 137 | + # Raise the error we want if the error matches the expected uuid |
| 138 | + if str(error_list[0][0]) == str(uuid_from_buffer(pack(test[1]))): |
| 139 | + raise error_list[0][2] |
0 commit comments