Skip to content
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

Move env variable mocking and undo when stopping. CC #2058, #2172. #2285

Merged
merged 1 commit into from
Jul 20, 2019
Merged
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
13 changes: 8 additions & 5 deletions moto/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from botocore.handlers import BUILTIN_HANDLERS
from botocore.awsrequest import AWSResponse

import mock
from moto import settings
import responses
from moto.packages.httpretty import HTTPretty
Expand All @@ -22,11 +23,6 @@
)


# "Mock" the AWS credentials as they can't be mocked in Botocore currently
os.environ.setdefault("AWS_ACCESS_KEY_ID", "foobar_key")
os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "foobar_secret")


class BaseMockAWS(object):
nested_count = 0

Expand All @@ -42,6 +38,10 @@ def __init__(self, backends):
self.backends_for_urls.update(self.backends)
self.backends_for_urls.update(default_backends)

# "Mock" the AWS credentials as they can't be mocked in Botocore currently
FAKE_KEYS = {"AWS_ACCESS_KEY_ID": "foobar_key", "AWS_SECRET_ACCESS_KEY": "foobar_secret"}
self.env_variables_mocks = mock.patch.dict(os.environ, FAKE_KEYS)

if self.__class__.nested_count == 0:
self.reset()

Expand All @@ -57,6 +57,8 @@ def __exit__(self, *args):
self.stop()

def start(self, reset=True):
self.env_variables_mocks.start()

self.__class__.nested_count += 1
if reset:
for backend in self.backends.values():
Expand All @@ -65,6 +67,7 @@ def start(self, reset=True):
self.enable_patching()

def stop(self):
self.env_variables_mocks.stop()
self.__class__.nested_count -= 1

if self.__class__.nested_count < 0:
Expand Down