|
| 1 | +import json |
| 2 | +import os |
| 3 | +import pytest |
| 4 | +import requests |
| 5 | +import time |
| 6 | + |
| 7 | +from fsspec.tests.abstract import AbstractFixtures |
| 8 | +from s3fs.core import S3FileSystem |
| 9 | + |
| 10 | + |
| 11 | +test_bucket_name = "test" |
| 12 | +secure_bucket_name = "test-secure" |
| 13 | +versioned_bucket_name = "test-versioned" |
| 14 | +port = 5555 |
| 15 | +endpoint_uri = "http://127.0.0.1:%s/" % port |
| 16 | + |
| 17 | + |
| 18 | +class S3fsFixtures(AbstractFixtures): |
| 19 | + @pytest.fixture(scope="class") |
| 20 | + def fs(self, _s3_base, _get_boto3_client): |
| 21 | + print("FS") |
| 22 | + client = _get_boto3_client |
| 23 | + client.create_bucket(Bucket=test_bucket_name, ACL="public-read") |
| 24 | + |
| 25 | + client.create_bucket(Bucket=versioned_bucket_name, ACL="public-read") |
| 26 | + client.put_bucket_versioning( |
| 27 | + Bucket=versioned_bucket_name, VersioningConfiguration={"Status": "Enabled"} |
| 28 | + ) |
| 29 | + |
| 30 | + # initialize secure bucket |
| 31 | + client.create_bucket(Bucket=secure_bucket_name, ACL="public-read") |
| 32 | + policy = json.dumps( |
| 33 | + { |
| 34 | + "Version": "2012-10-17", |
| 35 | + "Id": "PutObjPolicy", |
| 36 | + "Statement": [ |
| 37 | + { |
| 38 | + "Sid": "DenyUnEncryptedObjectUploads", |
| 39 | + "Effect": "Deny", |
| 40 | + "Principal": "*", |
| 41 | + "Action": "s3:PutObject", |
| 42 | + "Resource": "arn:aws:s3:::{bucket_name}/*".format( |
| 43 | + bucket_name=secure_bucket_name |
| 44 | + ), |
| 45 | + "Condition": { |
| 46 | + "StringNotEquals": { |
| 47 | + "s3:x-amz-server-side-encryption": "aws:kms" |
| 48 | + } |
| 49 | + }, |
| 50 | + } |
| 51 | + ], |
| 52 | + } |
| 53 | + ) |
| 54 | + client.put_bucket_policy(Bucket=secure_bucket_name, Policy=policy) |
| 55 | + |
| 56 | + S3FileSystem.clear_instance_cache() |
| 57 | + s3 = S3FileSystem(anon=False, client_kwargs={"endpoint_url": endpoint_uri}) |
| 58 | + s3.invalidate_cache() |
| 59 | + yield s3 |
| 60 | + |
| 61 | + @pytest.fixture |
| 62 | + def fs_path(self): |
| 63 | + return test_bucket_name |
| 64 | + |
| 65 | + def supports_empty_directories(self): |
| 66 | + return False |
| 67 | + |
| 68 | + @pytest.fixture(scope="class") |
| 69 | + def _get_boto3_client(self): |
| 70 | + from botocore.session import Session |
| 71 | + |
| 72 | + # NB: we use the sync botocore client for setup |
| 73 | + session = Session() |
| 74 | + return session.create_client("s3", endpoint_url=endpoint_uri) |
| 75 | + |
| 76 | + @pytest.fixture(scope="class") |
| 77 | + def _s3_base(self): |
| 78 | + # writable local S3 system |
| 79 | + import shlex |
| 80 | + import subprocess |
| 81 | + |
| 82 | + try: |
| 83 | + # should fail since we didn't start server yet |
| 84 | + r = requests.get(endpoint_uri) |
| 85 | + except: |
| 86 | + pass |
| 87 | + else: |
| 88 | + if r.ok: |
| 89 | + raise RuntimeError("moto server already up") |
| 90 | + if "AWS_SECRET_ACCESS_KEY" not in os.environ: |
| 91 | + os.environ["AWS_SECRET_ACCESS_KEY"] = "foo" |
| 92 | + if "AWS_ACCESS_KEY_ID" not in os.environ: |
| 93 | + os.environ["AWS_ACCESS_KEY_ID"] = "foo" |
| 94 | + proc = subprocess.Popen( |
| 95 | + shlex.split("moto_server s3 -p %s" % port), |
| 96 | + stderr=subprocess.DEVNULL, |
| 97 | + stdout=subprocess.DEVNULL, |
| 98 | + stdin=subprocess.DEVNULL, |
| 99 | + ) |
| 100 | + |
| 101 | + timeout = 5 |
| 102 | + while timeout > 0: |
| 103 | + try: |
| 104 | + print("polling for moto server") |
| 105 | + r = requests.get(endpoint_uri) |
| 106 | + if r.ok: |
| 107 | + break |
| 108 | + except: |
| 109 | + pass |
| 110 | + timeout -= 0.1 |
| 111 | + time.sleep(0.1) |
| 112 | + print("server up") |
| 113 | + yield |
| 114 | + print("moto done") |
| 115 | + proc.terminate() |
| 116 | + proc.wait() |
0 commit comments