From bd777cad44dc0c0ec979c366fce228429d182e2f Mon Sep 17 00:00:00 2001 From: Mike Grima Date: Sun, 17 Nov 2019 23:16:15 -0800 Subject: [PATCH] Fix for moto not unmocking. - Fixes #2575 - Also upgraded Travis CI to make use of Bionic instead of Xenial - This may also address concerns raised in #1793 --- .travis.yml | 2 +- moto/core/models.py | 3 ++ tests/test_awslambda/test_lambda.py | 40 +++++++++---------- tests/test_dynamodb2/test_dynamodb.py | 12 +++--- .../test_stepfunctions/test_stepfunctions.py | 2 +- 5 files changed, 31 insertions(+), 28 deletions(-) diff --git a/.travis.yml b/.travis.yml index f1854db54..3a602a76c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ -dist: xenial +dist: bionic language: python services: - docker diff --git a/moto/core/models.py b/moto/core/models.py index e0eae5858..5b19137c3 100644 --- a/moto/core/models.py +++ b/moto/core/models.py @@ -44,6 +44,7 @@ class BaseMockAWS(object): "AWS_ACCESS_KEY_ID": "foobar_key", "AWS_SECRET_ACCESS_KEY": "foobar_secret", } + 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: @@ -62,6 +63,7 @@ class BaseMockAWS(object): self.stop() def start(self, reset=True): + self.default_session_mock.start() self.env_variables_mocks.start() self.__class__.nested_count += 1 @@ -72,6 +74,7 @@ class BaseMockAWS(object): self.enable_patching() def stop(self): + self.default_session_mock.stop() self.env_variables_mocks.stop() self.__class__.nested_count -= 1 diff --git a/tests/test_awslambda/test_lambda.py b/tests/test_awslambda/test_lambda.py index 6b3d489ca..5d66b1537 100644 --- a/tests/test_awslambda/test_lambda.py +++ b/tests/test_awslambda/test_lambda.py @@ -1001,10 +1001,10 @@ def test_list_versions_by_function_for_nonexistent_function(): @mock_lambda @mock_sqs def test_create_event_source_mapping(): - sqs = boto3.resource("sqs") + sqs = boto3.resource("sqs", region_name="us-east-1") queue = sqs.create_queue(QueueName="test-sqs-queue1") - conn = boto3.client("lambda") + conn = boto3.client("lambda", region_name="us-east-1") func = conn.create_function( FunctionName="testFunction", Runtime="python2.7", @@ -1030,11 +1030,11 @@ def test_create_event_source_mapping(): @mock_lambda @mock_sqs def test_invoke_function_from_sqs(): - logs_conn = boto3.client("logs") - sqs = boto3.resource("sqs") + logs_conn = boto3.client("logs", region_name="us-east-1") + sqs = boto3.resource("sqs", region_name="us-east-1") queue = sqs.create_queue(QueueName="test-sqs-queue1") - conn = boto3.client("lambda") + conn = boto3.client("lambda", region_name="us-east-1") func = conn.create_function( FunctionName="testFunction", Runtime="python2.7", @@ -1054,7 +1054,7 @@ def test_invoke_function_from_sqs(): assert response["EventSourceArn"] == queue.attributes["QueueArn"] assert response["State"] == "Enabled" - sqs_client = boto3.client("sqs") + sqs_client = boto3.client("sqs", region_name="us-east-1") sqs_client.send_message(QueueUrl=queue.url, MessageBody="test") start = time.time() while (time.time() - start) < 30: @@ -1081,8 +1081,8 @@ def test_invoke_function_from_sqs(): @mock_lambda @mock_dynamodb2 def test_invoke_function_from_dynamodb(): - logs_conn = boto3.client("logs") - dynamodb = boto3.client("dynamodb") + logs_conn = boto3.client("logs", region_name="us-east-1") + dynamodb = boto3.client("dynamodb", region_name="us-east-1") table_name = "table_with_stream" table = dynamodb.create_table( TableName=table_name, @@ -1094,7 +1094,7 @@ def test_invoke_function_from_dynamodb(): }, ) - conn = boto3.client("lambda") + conn = boto3.client("lambda", region_name="us-east-1") func = conn.create_function( FunctionName="testFunction", Runtime="python2.7", @@ -1141,11 +1141,11 @@ def test_invoke_function_from_dynamodb(): @mock_lambda @mock_sqs def test_invoke_function_from_sqs_exception(): - logs_conn = boto3.client("logs") - sqs = boto3.resource("sqs") + logs_conn = boto3.client("logs", region_name="us-east-1") + sqs = boto3.resource("sqs", region_name="us-east-1") queue = sqs.create_queue(QueueName="test-sqs-queue1") - conn = boto3.client("lambda") + conn = boto3.client("lambda", region_name="us-east-1") func = conn.create_function( FunctionName="testFunction", Runtime="python2.7", @@ -1201,10 +1201,10 @@ def test_invoke_function_from_sqs_exception(): @mock_lambda @mock_sqs def test_list_event_source_mappings(): - sqs = boto3.resource("sqs") + sqs = boto3.resource("sqs", region_name="us-east-1") queue = sqs.create_queue(QueueName="test-sqs-queue1") - conn = boto3.client("lambda") + conn = boto3.client("lambda", region_name="us-east-1") func = conn.create_function( FunctionName="testFunction", Runtime="python2.7", @@ -1233,10 +1233,10 @@ def test_list_event_source_mappings(): @mock_lambda @mock_sqs def test_get_event_source_mapping(): - sqs = boto3.resource("sqs") + sqs = boto3.resource("sqs", region_name="us-east-1") queue = sqs.create_queue(QueueName="test-sqs-queue1") - conn = boto3.client("lambda") + conn = boto3.client("lambda", region_name="us-east-1") func = conn.create_function( FunctionName="testFunction", Runtime="python2.7", @@ -1263,10 +1263,10 @@ def test_get_event_source_mapping(): @mock_lambda @mock_sqs def test_update_event_source_mapping(): - sqs = boto3.resource("sqs") + sqs = boto3.resource("sqs", region_name="us-east-1") queue = sqs.create_queue(QueueName="test-sqs-queue1") - conn = boto3.client("lambda") + conn = boto3.client("lambda", region_name="us-east-1") func1 = conn.create_function( FunctionName="testFunction", Runtime="python2.7", @@ -1307,10 +1307,10 @@ def test_update_event_source_mapping(): @mock_lambda @mock_sqs def test_delete_event_source_mapping(): - sqs = boto3.resource("sqs") + sqs = boto3.resource("sqs", region_name="us-east-1") queue = sqs.create_queue(QueueName="test-sqs-queue1") - conn = boto3.client("lambda") + conn = boto3.client("lambda", region_name="us-east-1") func1 = conn.create_function( FunctionName="testFunction", Runtime="python2.7", diff --git a/tests/test_dynamodb2/test_dynamodb.py b/tests/test_dynamodb2/test_dynamodb.py index 7f6963870..2b0833d98 100644 --- a/tests/test_dynamodb2/test_dynamodb.py +++ b/tests/test_dynamodb2/test_dynamodb.py @@ -3119,8 +3119,8 @@ def test_sorted_query_with_numerical_sort_key(): # https://github.com/spulec/moto/issues/1874 @mock_dynamodb2 def test_item_size_is_under_400KB(): - dynamodb = boto3.resource("dynamodb") - client = boto3.client("dynamodb") + dynamodb = boto3.resource("dynamodb", region_name="us-east-1") + client = boto3.client("dynamodb", region_name="us-east-1") dynamodb.create_table( TableName="moto-test", @@ -3172,7 +3172,7 @@ def assert_failure_due_to_item_size(func, **kwargs): @mock_dynamodb2 # https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html#DDB-Query-request-KeyConditionExpression def test_hash_key_cannot_use_begins_with_operations(): - dynamodb = boto3.resource("dynamodb") + dynamodb = boto3.resource("dynamodb", region_name="us-east-1") table = dynamodb.create_table( TableName="test-table", KeySchema=[{"AttributeName": "key", "KeyType": "HASH"}], @@ -3201,7 +3201,7 @@ def test_hash_key_cannot_use_begins_with_operations(): @mock_dynamodb2 def test_update_supports_complex_expression_attribute_values(): - client = boto3.client("dynamodb") + client = boto3.client("dynamodb", region_name="us-east-1") client.create_table( AttributeDefinitions=[{"AttributeName": "SHA256", "AttributeType": "S"}], @@ -3237,7 +3237,7 @@ def test_update_supports_complex_expression_attribute_values(): @mock_dynamodb2 def test_update_supports_list_append(): - client = boto3.client("dynamodb") + client = boto3.client("dynamodb", region_name="us-east-1") client.create_table( AttributeDefinitions=[{"AttributeName": "SHA256", "AttributeType": "S"}], @@ -3272,7 +3272,7 @@ def test_update_supports_list_append(): @mock_dynamodb2 def test_update_catches_invalid_list_append_operation(): - client = boto3.client("dynamodb") + client = boto3.client("dynamodb", region_name="us-east-1") client.create_table( AttributeDefinitions=[{"AttributeName": "SHA256", "AttributeType": "S"}], diff --git a/tests/test_stepfunctions/test_stepfunctions.py b/tests/test_stepfunctions/test_stepfunctions.py index 77b9fbfb3..6c391f0d1 100644 --- a/tests/test_stepfunctions/test_stepfunctions.py +++ b/tests/test_stepfunctions/test_stepfunctions.py @@ -524,7 +524,7 @@ def _get_account_id(): global account_id if account_id: return account_id - sts = boto3.client("sts") + sts = boto3.client("sts", region_name=region) identity = sts.get_caller_identity() account_id = identity["Account"] return account_id