Skip to content

TST: Use monkeypatch instead of custom func for env variables #53563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 9, 2023
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
2 changes: 0 additions & 2 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
from pandas._testing.contexts import (
decompress_file,
ensure_clean,
ensure_safe_environment_variables,
raises_chained_assignment_error,
set_timezone,
use_numexpr,
Expand Down Expand Up @@ -1104,7 +1103,6 @@ def shares_memory(left, right) -> bool:
"EMPTY_STRING_PATTERN",
"ENDIAN",
"ensure_clean",
"ensure_safe_environment_variables",
"equalContents",
"external_error_raised",
"FLOAT_EA_DTYPES",
Expand Down
16 changes: 0 additions & 16 deletions pandas/_testing/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,6 @@ def ensure_clean(
path.unlink()


@contextmanager
def ensure_safe_environment_variables() -> Generator[None, None, None]:
"""
Get a context manager to safely set environment variables

All changes will be undone on close, hence environment variables set
within this contextmanager will neither persist nor change global state.
"""
saved_environ = dict(os.environ)
try:
yield
finally:
os.environ.clear()
os.environ.update(saved_environ)


@contextmanager
def with_csv_dialect(name: str, **kwargs) -> Generator[None, None, None]:
"""
Expand Down
108 changes: 55 additions & 53 deletions pandas/tests/io/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import shlex
import subprocess
import time
Expand All @@ -13,8 +12,6 @@
)
import pandas.util._test_decorators as td

import pandas._testing as tm

import pandas.io.common as icom
from pandas.io.parsers import read_csv

Expand Down Expand Up @@ -58,7 +55,13 @@ def s3so(worker_id):


@pytest.fixture(scope="session")
def s3_base(worker_id):
def monkeysession():
with pytest.MonkeyPatch.context() as mp:
yield mp


@pytest.fixture(scope="session")
def s3_base(worker_id, monkeysession):
"""
Fixture for mocking S3 interaction.

Expand All @@ -68,56 +71,55 @@ def s3_base(worker_id):
pytest.importorskip("s3fs")
pytest.importorskip("boto3")

with tm.ensure_safe_environment_variables():
# temporary workaround as moto fails for botocore >= 1.11 otherwise,
# see https://github.com/spulec/moto/issues/1924 & 1952
os.environ.setdefault("AWS_ACCESS_KEY_ID", "foobar_key")
os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "foobar_secret")
if is_ci_environment():
if is_platform_arm() or is_platform_mac() or is_platform_windows():
# NOT RUN on Windows/macOS/ARM, only Ubuntu
# - subprocess in CI can cause timeouts
# - GitHub Actions do not support
# container services for the above OSs
# - CircleCI will probably hit the Docker rate pull limit
pytest.skip(
"S3 tests do not have a corresponding service in "
"Windows, macOS or ARM platforms"
)
else:
yield "http://localhost:5000"
# temporary workaround as moto fails for botocore >= 1.11 otherwise,
# see https://github.com/spulec/moto/issues/1924 & 1952
monkeysession.setenv("AWS_ACCESS_KEY_ID", "foobar_key")
monkeysession.setenv("AWS_SECRET_ACCESS_KEY", "foobar_secret")
if is_ci_environment():
if is_platform_arm() or is_platform_mac() or is_platform_windows():
# NOT RUN on Windows/macOS/ARM, only Ubuntu
# - subprocess in CI can cause timeouts
# - GitHub Actions do not support
# container services for the above OSs
# - CircleCI will probably hit the Docker rate pull limit
pytest.skip(
"S3 tests do not have a corresponding service in "
"Windows, macOS or ARM platforms"
)
else:
requests = pytest.importorskip("requests")
pytest.importorskip("moto", minversion="1.3.14")
pytest.importorskip("flask") # server mode needs flask too

# Launching moto in server mode, i.e., as a separate process
# with an S3 endpoint on localhost

worker_id = "5" if worker_id == "master" else worker_id.lstrip("gw")
endpoint_port = f"555{worker_id}"
endpoint_uri = f"http://127.0.0.1:{endpoint_port}/"

# pipe to null to avoid logging in terminal
with subprocess.Popen(
shlex.split(f"moto_server s3 -p {endpoint_port}"),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
) as proc:
timeout = 5
while timeout > 0:
try:
# OK to go once server is accepting connections
r = requests.get(endpoint_uri)
if r.ok:
break
except Exception:
pass
timeout -= 0.1
time.sleep(0.1)
yield endpoint_uri

proc.terminate()
yield "http://localhost:5000"
else:
requests = pytest.importorskip("requests")
pytest.importorskip("moto", minversion="1.3.14")
pytest.importorskip("flask") # server mode needs flask too

# Launching moto in server mode, i.e., as a separate process
# with an S3 endpoint on localhost

worker_id = "5" if worker_id == "master" else worker_id.lstrip("gw")
endpoint_port = f"555{worker_id}"
endpoint_uri = f"http://127.0.0.1:{endpoint_port}/"

# pipe to null to avoid logging in terminal
with subprocess.Popen(
shlex.split(f"moto_server s3 -p {endpoint_port}"),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
) as proc:
timeout = 5
while timeout > 0:
try:
# OK to go once server is accepting connections
r = requests.get(endpoint_uri)
if r.ok:
break
except Exception:
pass
timeout -= 0.1
time.sleep(0.1)
yield endpoint_uri

proc.terminate()


@pytest.fixture
Expand Down
20 changes: 9 additions & 11 deletions pandas/tests/io/test_s3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from io import BytesIO
import os

import pytest

Expand Down Expand Up @@ -34,17 +33,16 @@ def test_read_without_creds_from_pub_bucket():
@td.skip_if_no("s3fs")
@pytest.mark.network
@tm.network
def test_read_with_creds_from_pub_bucket():
def test_read_with_creds_from_pub_bucket(monkeypatch):
# Ensure we can read from a public bucket with credentials
# GH 34626
# Use Amazon Open Data Registry - https://registry.opendata.aws/gdelt

with tm.ensure_safe_environment_variables():
# temporary workaround as moto fails for botocore >= 1.11 otherwise,
# see https://github.com/spulec/moto/issues/1924 & 1952
os.environ.setdefault("AWS_ACCESS_KEY_ID", "foobar_key")
os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "foobar_secret")
df = read_csv(
"s3://gdelt-open-data/events/1981.csv", nrows=5, sep="\t", header=None
)
assert len(df) == 5
# temporary workaround as moto fails for botocore >= 1.11 otherwise,
# see https://github.com/spulec/moto/issues/1924 & 1952
monkeypatch.setenv("AWS_ACCESS_KEY_ID", "foobar_key")
monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "foobar_secret")
df = read_csv(
"s3://gdelt-open-data/events/1981.csv", nrows=5, sep="\t", header=None
)
assert len(df) == 5