Skip to content

Commit

Permalink
Move env variable mocking and undo when stopping. CC #2058, #2172.
Browse files Browse the repository at this point in the history
  • Loading branch information
spulec committed Jul 8, 2019
1 parent af0205b commit 79cd1e6
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions moto/core/models.py
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

0 comments on commit 79cd1e6

Please sign in to comment.