Fix Mock 4.0.3 compatibility - Unpatch only once during teardown (#3541)

* #3535 - Unpatch only once during teardown

* EnvVar patching - Fix Python2 bug

* Allow latest version of mock-library
This commit is contained in:
Bert Blommers 2021-02-15 08:42:16 +00:00 committed by GitHub
commit d7b8419791
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 83 additions and 8 deletions

View file

@ -0,0 +1,34 @@
import os
import sure # noqa
from moto import mock_ec2, mock_s3
KEY = "AWS_ACCESS_KEY_ID"
def test_aws_keys_are_patched():
with mock_ec2():
patched_value = os.environ[KEY]
patched_value.should.equal("foobar_key")
def test_aws_keys_can_be_none():
"""
Verify that the os.environ[KEY] can be None
Patching the None-value shouldn't be an issue
"""
original = os.environ.get(KEY, "value-set-by-user")
# Delete the original value by the user
try:
del os.environ[KEY]
except KeyError:
pass # Value might not be set on this system in the first place
try:
# Verify that the os.environ[KEY] is patched
with mock_s3():
patched_value = os.environ[KEY]
patched_value.should.equal("foobar_key")
# Verify that the os.environ[KEY] is unpatched, and reverts to None
assert os.environ.get(KEY) is None
finally:
# Reset the value original - don't want to change the users system
os.environ[KEY] = original

View file

@ -0,0 +1,18 @@
import unittest
import boto3
from moto import mock_s3
@mock_s3
class ClassDecoratorTest(unittest.TestCase):
"""
https://github.com/spulec/moto/issues/3535
An update to the mock-package introduced a failure during teardown.
This test is in place to catch any similar failures with our mocking approach
"""
def test_instantiation_succeeds(self):
s3 = boto3.client("s3", region_name="us-east-1")
assert s3 is not None