Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:wicked:qubes-build
python-moto
moto-pr3541-fix-mock-compat.patch
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File moto-pr3541-fix-mock-compat.patch of Package python-moto
From 58c9be743836f48aa27aa2ec99dced87fc53f01a Mon Sep 17 00:00:00 2001 From: Bert Blommers <info@bertblommers.nl> Date: Fri, 11 Dec 2020 09:58:26 +0000 Subject: [PATCH 1/3] #3535 - Unpatch only once during teardown --- moto/core/models.py | 13 +++++++++---- tests/test_s3/test_s3_classdecorator.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 tests/test_s3/test_s3_classdecorator.py diff --git a/moto/core/models.py b/moto/core/models.py index 2cd67188a..e48f1d238 100644 --- a/moto/core/models.py +++ b/moto/core/models.py @@ -35,6 +35,7 @@ class BaseMockAWS(object): nested_count = 0 + mocks_active = False def __init__(self, backends): from moto.instance_metadata import instance_metadata_backend @@ -74,8 +75,10 @@ def __exit__(self, *args): self.stop() def start(self, reset=True): - self.default_session_mock.start() - self.env_variables_mocks.start() + if not self.__class__.mocks_active: + self.default_session_mock.start() + self.env_variables_mocks.start() + self.__class__.mocks_active = True self.__class__.nested_count += 1 if reset: @@ -85,14 +88,16 @@ def start(self, reset=True): self.enable_patching() def stop(self): - self.default_session_mock.stop() - self.env_variables_mocks.stop() self.__class__.nested_count -= 1 if self.__class__.nested_count < 0: raise RuntimeError("Called stop() before start().") if self.__class__.nested_count == 0: + if self.__class__.mocks_active: + self.default_session_mock.stop() + self.env_variables_mocks.stop() + self.__class__.mocks_active = False self.disable_patching() def decorate_callable(self, func, reset): diff --git a/tests/test_s3/test_s3_classdecorator.py b/tests/test_s3/test_s3_classdecorator.py new file mode 100644 index 000000000..9eff2e838 --- /dev/null +++ b/tests/test_s3/test_s3_classdecorator.py @@ -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 From 002869f7c2c6a46dd6fdcc151ca6cb0f7957047f Mon Sep 17 00:00:00 2001 From: Bert Blommers <info@bertblommers.nl> Date: Sat, 12 Dec 2020 14:27:27 +0000 Subject: [PATCH 2/3] EnvVar patching - Fix Python2 bug --- moto/core/models.py | 27 +++++++++++++++---- tests/test_core/test_environ_patching.py | 34 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 tests/test_core/test_environ_patching.py diff --git a/moto/core/models.py b/moto/core/models.py index e48f1d238..9545c9e8c 100644 --- a/moto/core/models.py +++ b/moto/core/models.py @@ -51,13 +51,12 @@ 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 = { + self.FAKE_KEYS = { "AWS_ACCESS_KEY_ID": "foobar_key", "AWS_SECRET_ACCESS_KEY": "foobar_secret", } + self.ORIG_KEYS = {} self.default_session_mock = mock.patch("boto3.DEFAULT_SESSION", None) - self.env_variables_mocks = mock.patch.dict(os.environ, FAKE_KEYS) if self.__class__.nested_count == 0: self.reset() @@ -77,7 +76,7 @@ def __exit__(self, *args): def start(self, reset=True): if not self.__class__.mocks_active: self.default_session_mock.start() - self.env_variables_mocks.start() + self.mock_env_variables() self.__class__.mocks_active = True self.__class__.nested_count += 1 @@ -96,7 +95,7 @@ def stop(self): if self.__class__.nested_count == 0: if self.__class__.mocks_active: self.default_session_mock.stop() - self.env_variables_mocks.stop() + self.unmock_env_variables() self.__class__.mocks_active = False self.disable_patching() @@ -144,6 +143,24 @@ def decorate_class(self, klass): continue return klass + def mock_env_variables(self): + # "Mock" the AWS credentials as they can't be mocked in Botocore currently + # self.env_variables_mocks = mock.patch.dict(os.environ, FAKE_KEYS) + # self.env_variables_mocks.start() + for k, v in self.FAKE_KEYS.items(): + self.ORIG_KEYS[k] = os.environ.get(k, None) + os.environ[k] = v + + def unmock_env_variables(self): + # This doesn't work in Python2 - for some reason, unmocking clears the entire os.environ dict + # Obviously bad user experience, and also breaks pytest - as it uses PYTEST_CURRENT_TEST as an env var + # self.env_variables_mocks.stop() + for k, v in self.ORIG_KEYS.items(): + if v: + os.environ[k] = v + else: + del os.environ[k] + class HttprettyMockAWS(BaseMockAWS): def reset(self): diff --git a/tests/test_core/test_environ_patching.py b/tests/test_core/test_environ_patching.py new file mode 100644 index 000000000..d4e15c78a --- /dev/null +++ b/tests/test_core/test_environ_patching.py @@ -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 From a6c529c22804034ac24b0659b0901be90ed669ac Mon Sep 17 00:00:00 2001 From: Bert Blommers <info@bertblommers.nl> Date: Thu, 7 Jan 2021 13:21:08 +0000 Subject: [PATCH 3/3] Allow latest version of mock-library --- requirements-dev.txt | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 2df056d85..692a1cbf3 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -21,7 +21,7 @@ beautifulsoup4==4.6.0 # The below pins mirror the Python version-conditional pins in setup.py # Jinja2>=2.10.1; python_version >= '3.6' -mock<=4.0.2; python_version >= '3.6' +mock; python_version >= '3.6' more-itertools; python_version >= '3.6' setuptools; python_version >= '3.6' sshpubkeys>=3.1.0; python_version >= '3.6' diff --git a/setup.py b/setup.py index 0aab0bcff..913565eb4 100755 --- a/setup.py +++ b/setup.py @@ -70,7 +70,7 @@ def get_version(): else: install_requires += [ "Jinja2>=2.10.1", - "mock<=4.0.2", + "mock", "more-itertools", "setuptools", "zipp",
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor