Skip to content

Commit

Permalink
Fix s3utils errors
Browse files Browse the repository at this point in the history
Add docstring to the clean_test_environment function
  • Loading branch information
Fireye04 committed Mar 24, 2023
1 parent 04f6662 commit b8f9b96
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
27 changes: 27 additions & 0 deletions python/lsst/resources/s3utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from unittest import TestCase

__all__ = (
"clean_test_environment",
"getS3Client",
"s3CheckFileExists",
"bucketExists",
Expand Down Expand Up @@ -108,6 +114,27 @@ class _TooManyRequestsException(Exception):
max_retry_time = 60


def clean_test_environment(testcase: TestCase) -> None:
"""Clear S3_ENDPOINT_URL then reset it at the end of a test.
Parameters
----------
testcase: `TestCase`, `self`
Reference to the test being run; used to add a cleanup function.
"""
endpoint = os.environ.get("S3_ENDPOINT_URL")

if not endpoint:
return
os.environ["S3_ENDPOINT_URL"] = ""

def cleanup() -> None:
if endpoint is not None:
os.environ["S3_ENDPOINT_URL"] = endpoint

testcase.addCleanup(cleanup)


def getS3Client() -> boto3.client:
"""Create a S3 client with AWS (default) or the specified endpoint.
Expand Down
11 changes: 2 additions & 9 deletions tests/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
# Use of this source code is governed by a 3-clause BSD-style
# license that can be found in the LICENSE file.

import os
import unittest

from lsst.resources import ResourcePath
from lsst.resources.s3utils import clean_test_environment
from lsst.resources.tests import GenericReadWriteTestCase, GenericTestCase

try:
Expand Down Expand Up @@ -44,11 +44,7 @@ def setUp(self):
# Enable S3 mocking of tests.
self.mock_s3.start()

self.endpoint = None

if "S3_ENDPOINT_URL" in os.environ:
self.endpoint = os.environ["S3_ENDPOINT_URL"]
os.environ["S3_ENDPOINT_URL"] = ""
clean_test_environment(self)

# set up some fake credentials if they do not exist
# self.usingDummyCredentials = setAwsEnvCredentials()
Expand All @@ -74,9 +70,6 @@ def tearDown(self):
bucket = s3.Bucket(self.netloc)
bucket.delete()

if self.endpoint is not None:
os.environ["S3_ENDPOINT_URL"] = self.endpoint

# Stop the S3 mock.
self.mock_s3.stop()

Expand Down
4 changes: 4 additions & 0 deletions tests/test_s3utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import unittest

from lsst.resources.s3utils import clean_test_environment

try:
import boto3
from moto import mock_s3
Expand Down Expand Up @@ -55,6 +57,8 @@ def setUp(self):
# set up some fake credentials if they do not exist
self.usingDummyCredentials = setAwsEnvCredentials()

clean_test_environment(self)

self.client = getS3Client()
try:
self.client.create_bucket(Bucket=self.bucketName)
Expand Down

0 comments on commit b8f9b96

Please sign in to comment.