|
| 1 | +from distutils.version import LooseVersion |
| 2 | +import os |
| 3 | + |
1 | 4 | import pytest
|
| 5 | + |
| 6 | +import pandas.util.testing as tm |
| 7 | + |
2 | 8 | from pandas.io.parsers import read_csv
|
3 | 9 |
|
4 | 10 |
|
@@ -37,43 +43,48 @@ def s3_resource(tips_file, jsonl_file):
|
37 | 43 | """
|
38 | 44 | pytest.importorskip('s3fs')
|
39 | 45 | boto3 = pytest.importorskip('boto3')
|
40 |
| - # GH-24092. See if boto.plugin skips the test or fails. |
41 |
| - try: |
42 |
| - pytest.importorskip("boto.plugin") |
43 |
| - except AttributeError: |
44 |
| - raise pytest.skip("moto/moto error") |
45 |
| - moto = pytest.importorskip('moto') |
46 |
| - |
47 |
| - test_s3_files = [ |
48 |
| - ('tips.csv', tips_file), |
49 |
| - ('tips.csv.gz', tips_file + '.gz'), |
50 |
| - ('tips.csv.bz2', tips_file + '.bz2'), |
51 |
| - ('items.jsonl', jsonl_file), |
52 |
| - ] |
53 |
| - |
54 |
| - def add_tips_files(bucket_name): |
55 |
| - for s3_key, file_name in test_s3_files: |
56 |
| - with open(file_name, 'rb') as f: |
57 |
| - conn.Bucket(bucket_name).put_object( |
58 |
| - Key=s3_key, |
59 |
| - Body=f) |
60 |
| - |
61 |
| - try: |
62 |
| - |
63 |
| - s3 = moto.mock_s3() |
64 |
| - s3.start() |
65 |
| - |
66 |
| - # see gh-16135 |
67 |
| - bucket = 'pandas-test' |
68 |
| - conn = boto3.resource("s3", region_name="us-east-1") |
69 |
| - |
70 |
| - conn.create_bucket(Bucket=bucket) |
71 |
| - add_tips_files(bucket) |
72 |
| - |
73 |
| - conn.create_bucket(Bucket='cant_get_it', ACL='private') |
74 |
| - add_tips_files('cant_get_it') |
75 |
| - yield conn |
76 |
| - except: # noqa: flake8 |
77 |
| - pytest.skip("failure to use s3 resource") |
78 |
| - finally: |
79 |
| - s3.stop() |
| 46 | + botocore = pytest.importorskip('botocore') |
| 47 | + |
| 48 | + if LooseVersion(botocore.__version__) < LooseVersion("1.11.0"): |
| 49 | + # botocore leaks an uncatchable ResourceWarning before 1.11.0; |
| 50 | + # see GH 23731 and https://github.com/boto/botocore/issues/1464 |
| 51 | + pytest.skip("botocore is leaking resources before 1.11.0") |
| 52 | + |
| 53 | + with tm.ensure_safe_environment_variables(): |
| 54 | + # temporary workaround as moto fails for botocore >= 1.11 otherwise, |
| 55 | + # see https://github.com/spulec/moto/issues/1924 & 1952 |
| 56 | + os.environ.setdefault("AWS_ACCESS_KEY_ID", "foobar_key") |
| 57 | + os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "foobar_secret") |
| 58 | + |
| 59 | + moto = pytest.importorskip('moto') |
| 60 | + |
| 61 | + test_s3_files = [ |
| 62 | + ('tips.csv', tips_file), |
| 63 | + ('tips.csv.gz', tips_file + '.gz'), |
| 64 | + ('tips.csv.bz2', tips_file + '.bz2'), |
| 65 | + ('items.jsonl', jsonl_file), |
| 66 | + ] |
| 67 | + |
| 68 | + def add_tips_files(bucket_name): |
| 69 | + for s3_key, file_name in test_s3_files: |
| 70 | + with open(file_name, 'rb') as f: |
| 71 | + conn.Bucket(bucket_name).put_object( |
| 72 | + Key=s3_key, |
| 73 | + Body=f) |
| 74 | + |
| 75 | + try: |
| 76 | + s3 = moto.mock_s3() |
| 77 | + s3.start() |
| 78 | + |
| 79 | + # see gh-16135 |
| 80 | + bucket = 'pandas-test' |
| 81 | + conn = boto3.resource("s3", region_name="us-east-1") |
| 82 | + |
| 83 | + conn.create_bucket(Bucket=bucket) |
| 84 | + add_tips_files(bucket) |
| 85 | + |
| 86 | + conn.create_bucket(Bucket='cant_get_it', ACL='private') |
| 87 | + add_tips_files('cant_get_it') |
| 88 | + yield conn |
| 89 | + finally: |
| 90 | + s3.stop() |
0 commit comments