Merge pull request #19 from spulec/master

Merge upstream
This commit is contained in:
Bert Blommers 2019-11-22 06:21:53 +00:00 committed by GitHub
commit 1d888a576d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 62 additions and 57 deletions

View File

@ -1,4 +1,4 @@
dist: xenial dist: bionic
language: python language: python
services: services:
- docker - docker

View File

@ -302,7 +302,9 @@ class Job(threading.Thread, BaseModel):
self.job_id = str(uuid.uuid4()) self.job_id = str(uuid.uuid4())
self.job_definition = job_def self.job_definition = job_def
self.job_queue = job_queue self.job_queue = job_queue
self.job_state = "SUBMITTED" # One of SUBMITTED | PENDING | RUNNABLE | STARTING | RUNNING | SUCCEEDED | FAILED self.job_state = (
"SUBMITTED"
) # One of SUBMITTED | PENDING | RUNNABLE | STARTING | RUNNING | SUCCEEDED | FAILED
self.job_queue.jobs.append(self) self.job_queue.jobs.append(self)
self.job_started_at = datetime.datetime(1970, 1, 1) self.job_started_at = datetime.datetime(1970, 1, 1)
self.job_stopped_at = datetime.datetime(1970, 1, 1) self.job_stopped_at = datetime.datetime(1970, 1, 1)

View File

@ -177,7 +177,9 @@ class CloudFormationResponse(BaseResponse):
start = stack_ids.index(token) + 1 start = stack_ids.index(token) + 1
else: else:
start = 0 start = 0
max_results = 50 # using this to mske testing of paginated stacks more convenient than default 1 MB max_results = (
50
) # using this to mske testing of paginated stacks more convenient than default 1 MB
stacks_resp = stacks[start : start + max_results] stacks_resp = stacks[start : start + max_results]
next_token = None next_token = None
if len(stacks) > (start + max_results): if len(stacks) > (start + max_results):

View File

@ -44,6 +44,7 @@ class BaseMockAWS(object):
"AWS_ACCESS_KEY_ID": "foobar_key", "AWS_ACCESS_KEY_ID": "foobar_key",
"AWS_SECRET_ACCESS_KEY": "foobar_secret", "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) self.env_variables_mocks = mock.patch.dict(os.environ, FAKE_KEYS)
if self.__class__.nested_count == 0: if self.__class__.nested_count == 0:
@ -62,6 +63,7 @@ class BaseMockAWS(object):
self.stop() self.stop()
def start(self, reset=True): def start(self, reset=True):
self.default_session_mock.start()
self.env_variables_mocks.start() self.env_variables_mocks.start()
self.__class__.nested_count += 1 self.__class__.nested_count += 1
@ -72,6 +74,7 @@ class BaseMockAWS(object):
self.enable_patching() self.enable_patching()
def stop(self): def stop(self):
self.default_session_mock.stop()
self.env_variables_mocks.stop() self.env_variables_mocks.stop()
self.__class__.nested_count -= 1 self.__class__.nested_count -= 1

View File

@ -307,7 +307,7 @@ class BaseResponse(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
def _convert(elem, is_last): def _convert(elem, is_last):
if not re.match("^{.*}$", elem): if not re.match("^{.*}$", elem):
return elem return elem
name = elem.replace("{", "").replace("}", "") name = elem.replace("{", "").replace("}", "").replace("+", "")
if is_last: if is_last:
return "(?P<%s>[^/]*)" % name return "(?P<%s>[^/]*)" % name
return "(?P<%s>.*)" % name return "(?P<%s>.*)" % name

View File

@ -153,7 +153,7 @@ class DataSyncResponse(BaseResponse):
task_execution_arn = self._get_param("TaskExecutionArn") task_execution_arn = self._get_param("TaskExecutionArn")
task_execution = self.datasync_backend._get_task_execution(task_execution_arn) task_execution = self.datasync_backend._get_task_execution(task_execution_arn)
result = json.dumps( result = json.dumps(
{"TaskExecutionArn": task_execution.arn, "Status": task_execution.status,} {"TaskExecutionArn": task_execution.arn, "Status": task_execution.status}
) )
if task_execution.status == "SUCCESS": if task_execution.status == "SUCCESS":
self.datasync_backend.tasks[task_execution.task_arn].status = "AVAILABLE" self.datasync_backend.tasks[task_execution.task_arn].status = "AVAILABLE"

View File

@ -4,6 +4,4 @@ from .responses import DataSyncResponse
url_bases = ["https?://(.*?)(datasync)(.*?).amazonaws.com"] url_bases = ["https?://(.*?)(datasync)(.*?).amazonaws.com"]
url_paths = { url_paths = {"{0}/$": DataSyncResponse.dispatch}
"{0}/$": DataSyncResponse.dispatch,
}

View File

@ -316,8 +316,7 @@ class EventsBackend(BaseBackend):
if not event_bus: if not event_bus:
raise JsonRESTError( raise JsonRESTError(
"ResourceNotFoundException", "ResourceNotFoundException", "Event bus {} does not exist.".format(name)
"Event bus {} does not exist.".format(name),
) )
return event_bus return event_bus

View File

@ -261,10 +261,7 @@ class EventsHandler(BaseResponse):
name = self._get_param("Name") name = self._get_param("Name")
event_bus = self.events_backend.describe_event_bus(name) event_bus = self.events_backend.describe_event_bus(name)
response = { response = {"Name": event_bus.name, "Arn": event_bus.arn}
"Name": event_bus.name,
"Arn": event_bus.arn,
}
if event_bus.policy: if event_bus.policy:
response["Policy"] = event_bus.policy response["Policy"] = event_bus.policy
@ -285,10 +282,7 @@ class EventsHandler(BaseResponse):
response = [] response = []
for event_bus in self.events_backend.list_event_buses(name_prefix): for event_bus in self.events_backend.list_event_buses(name_prefix):
event_bus_response = { event_bus_response = {"Name": event_bus.name, "Arn": event_bus.arn}
"Name": event_bus.name,
"Arn": event_bus.arn,
}
if event_bus.policy: if event_bus.policy:
event_bus_response["Policy"] = event_bus.policy event_bus_response["Policy"] = event_bus.policy

View File

@ -788,7 +788,9 @@ class AccountSummary(BaseModel):
self._groups_per_user_quota = 10 self._groups_per_user_quota = 10
self._attached_policies_per_user_quota = 10 self._attached_policies_per_user_quota = 10
self._policies_quota = 1500 self._policies_quota = 1500
self._account_mfa_enabled = 0 # Haven't found any information being able to activate MFA for the root account programmatically self._account_mfa_enabled = (
0
) # Haven't found any information being able to activate MFA for the root account programmatically
self._access_keys_per_user_quota = 2 self._access_keys_per_user_quota = 2
self._assume_role_policy_size_quota = 2048 self._assume_role_policy_size_quota = 2048
self._policy_versions_in_use_quota = 10000 self._policy_versions_in_use_quota = 10000

View File

@ -88,7 +88,9 @@ class IAMPolicyDocumentValidator:
self._policy_document = policy_document self._policy_document = policy_document
self._policy_json = {} self._policy_json = {}
self._statements = [] self._statements = []
self._resource_error = "" # the first resource error found that does not generate a legacy parsing error self._resource_error = (
""
) # the first resource error found that does not generate a legacy parsing error
def validate(self): def validate(self):
try: try:

View File

@ -563,7 +563,7 @@ def test_create_stage():
api_id = response["id"] api_id = response["id"]
create_method_integration(client, api_id) create_method_integration(client, api_id)
response = client.create_deployment(restApiId=api_id, stageName=stage_name,) response = client.create_deployment(restApiId=api_id, stageName=stage_name)
deployment_id = response["id"] deployment_id = response["id"]
response = client.get_deployment(restApiId=api_id, deploymentId=deployment_id) response = client.get_deployment(restApiId=api_id, deploymentId=deployment_id)

View File

@ -1001,10 +1001,10 @@ def test_list_versions_by_function_for_nonexistent_function():
@mock_lambda @mock_lambda
@mock_sqs @mock_sqs
def test_create_event_source_mapping(): 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") 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( func = conn.create_function(
FunctionName="testFunction", FunctionName="testFunction",
Runtime="python2.7", Runtime="python2.7",
@ -1030,11 +1030,11 @@ def test_create_event_source_mapping():
@mock_lambda @mock_lambda
@mock_sqs @mock_sqs
def test_invoke_function_from_sqs(): def test_invoke_function_from_sqs():
logs_conn = boto3.client("logs") logs_conn = boto3.client("logs", region_name="us-east-1")
sqs = boto3.resource("sqs") sqs = boto3.resource("sqs", region_name="us-east-1")
queue = sqs.create_queue(QueueName="test-sqs-queue1") 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( func = conn.create_function(
FunctionName="testFunction", FunctionName="testFunction",
Runtime="python2.7", Runtime="python2.7",
@ -1054,7 +1054,7 @@ def test_invoke_function_from_sqs():
assert response["EventSourceArn"] == queue.attributes["QueueArn"] assert response["EventSourceArn"] == queue.attributes["QueueArn"]
assert response["State"] == "Enabled" 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") sqs_client.send_message(QueueUrl=queue.url, MessageBody="test")
start = time.time() start = time.time()
while (time.time() - start) < 30: while (time.time() - start) < 30:
@ -1081,8 +1081,8 @@ def test_invoke_function_from_sqs():
@mock_lambda @mock_lambda
@mock_dynamodb2 @mock_dynamodb2
def test_invoke_function_from_dynamodb(): def test_invoke_function_from_dynamodb():
logs_conn = boto3.client("logs") logs_conn = boto3.client("logs", region_name="us-east-1")
dynamodb = boto3.client("dynamodb") dynamodb = boto3.client("dynamodb", region_name="us-east-1")
table_name = "table_with_stream" table_name = "table_with_stream"
table = dynamodb.create_table( table = dynamodb.create_table(
TableName=table_name, 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( func = conn.create_function(
FunctionName="testFunction", FunctionName="testFunction",
Runtime="python2.7", Runtime="python2.7",
@ -1141,11 +1141,11 @@ def test_invoke_function_from_dynamodb():
@mock_lambda @mock_lambda
@mock_sqs @mock_sqs
def test_invoke_function_from_sqs_exception(): def test_invoke_function_from_sqs_exception():
logs_conn = boto3.client("logs") logs_conn = boto3.client("logs", region_name="us-east-1")
sqs = boto3.resource("sqs") sqs = boto3.resource("sqs", region_name="us-east-1")
queue = sqs.create_queue(QueueName="test-sqs-queue1") 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( func = conn.create_function(
FunctionName="testFunction", FunctionName="testFunction",
Runtime="python2.7", Runtime="python2.7",
@ -1201,10 +1201,10 @@ def test_invoke_function_from_sqs_exception():
@mock_lambda @mock_lambda
@mock_sqs @mock_sqs
def test_list_event_source_mappings(): 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") 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( func = conn.create_function(
FunctionName="testFunction", FunctionName="testFunction",
Runtime="python2.7", Runtime="python2.7",
@ -1233,10 +1233,10 @@ def test_list_event_source_mappings():
@mock_lambda @mock_lambda
@mock_sqs @mock_sqs
def test_get_event_source_mapping(): 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") 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( func = conn.create_function(
FunctionName="testFunction", FunctionName="testFunction",
Runtime="python2.7", Runtime="python2.7",
@ -1263,10 +1263,10 @@ def test_get_event_source_mapping():
@mock_lambda @mock_lambda
@mock_sqs @mock_sqs
def test_update_event_source_mapping(): 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") 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( func1 = conn.create_function(
FunctionName="testFunction", FunctionName="testFunction",
Runtime="python2.7", Runtime="python2.7",
@ -1307,10 +1307,10 @@ def test_update_event_source_mapping():
@mock_lambda @mock_lambda
@mock_sqs @mock_sqs
def test_delete_event_source_mapping(): 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") 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( func1 = conn.create_function(
FunctionName="testFunction", FunctionName="testFunction",
Runtime="python2.7", Runtime="python2.7",

View File

@ -3119,8 +3119,8 @@ def test_sorted_query_with_numerical_sort_key():
# https://github.com/spulec/moto/issues/1874 # https://github.com/spulec/moto/issues/1874
@mock_dynamodb2 @mock_dynamodb2
def test_item_size_is_under_400KB(): def test_item_size_is_under_400KB():
dynamodb = boto3.resource("dynamodb") dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
client = boto3.client("dynamodb") client = boto3.client("dynamodb", region_name="us-east-1")
dynamodb.create_table( dynamodb.create_table(
TableName="moto-test", TableName="moto-test",
@ -3172,7 +3172,7 @@ def assert_failure_due_to_item_size(func, **kwargs):
@mock_dynamodb2 @mock_dynamodb2
# https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html#DDB-Query-request-KeyConditionExpression # https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html#DDB-Query-request-KeyConditionExpression
def test_hash_key_cannot_use_begins_with_operations(): 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( table = dynamodb.create_table(
TableName="test-table", TableName="test-table",
KeySchema=[{"AttributeName": "key", "KeyType": "HASH"}], KeySchema=[{"AttributeName": "key", "KeyType": "HASH"}],
@ -3201,7 +3201,7 @@ def test_hash_key_cannot_use_begins_with_operations():
@mock_dynamodb2 @mock_dynamodb2
def test_update_supports_complex_expression_attribute_values(): def test_update_supports_complex_expression_attribute_values():
client = boto3.client("dynamodb") client = boto3.client("dynamodb", region_name="us-east-1")
client.create_table( client.create_table(
AttributeDefinitions=[{"AttributeName": "SHA256", "AttributeType": "S"}], AttributeDefinitions=[{"AttributeName": "SHA256", "AttributeType": "S"}],
@ -3237,7 +3237,7 @@ def test_update_supports_complex_expression_attribute_values():
@mock_dynamodb2 @mock_dynamodb2
def test_update_supports_list_append(): def test_update_supports_list_append():
client = boto3.client("dynamodb") client = boto3.client("dynamodb", region_name="us-east-1")
client.create_table( client.create_table(
AttributeDefinitions=[{"AttributeName": "SHA256", "AttributeType": "S"}], AttributeDefinitions=[{"AttributeName": "SHA256", "AttributeType": "S"}],
@ -3272,7 +3272,7 @@ def test_update_supports_list_append():
@mock_dynamodb2 @mock_dynamodb2
def test_update_catches_invalid_list_append_operation(): def test_update_catches_invalid_list_append_operation():
client = boto3.client("dynamodb") client = boto3.client("dynamodb", region_name="us-east-1")
client.create_table( client.create_table(
AttributeDefinitions=[{"AttributeName": "SHA256", "AttributeType": "S"}], AttributeDefinitions=[{"AttributeName": "SHA256", "AttributeType": "S"}],
@ -3335,7 +3335,7 @@ def test_update_item_if_original_value_is_none():
table.update_item( table.update_item(
Key={"job_id": "a"}, Key={"job_id": "a"},
UpdateExpression="SET job_name = :output", UpdateExpression="SET job_name = :output",
ExpressionAttributeValues={":output": "updated",}, ExpressionAttributeValues={":output": "updated"},
) )
table.scan()["Items"][0]["job_name"].should.equal("updated") table.scan()["Items"][0]["job_name"].should.equal("updated")
@ -3354,7 +3354,7 @@ def test_update_nested_item_if_original_value_is_none():
table.update_item( table.update_item(
Key={"job_id": "a"}, Key={"job_id": "a"},
UpdateExpression="SET job_details.job_name = :output", UpdateExpression="SET job_details.job_name = :output",
ExpressionAttributeValues={":output": "updated",}, ExpressionAttributeValues={":output": "updated"},
) )
table.scan()["Items"][0]["job_details"]["job_name"].should.equal("updated") table.scan()["Items"][0]["job_details"]["job_name"].should.equal("updated")

View File

@ -213,7 +213,7 @@ class TestEdges:
resp = conn.update_table( resp = conn.update_table(
TableName="test-streams", TableName="test-streams",
StreamSpecification={"StreamViewType": "KEYS_ONLY"}, StreamSpecification={"StreamViewType": "KEYS_ONLY", "StreamEnabled": True},
) )
assert "StreamSpecification" in resp["TableDescription"] assert "StreamSpecification" in resp["TableDescription"]
assert resp["TableDescription"]["StreamSpecification"] == { assert resp["TableDescription"]["StreamSpecification"] == {
@ -226,7 +226,10 @@ class TestEdges:
with assert_raises(conn.exceptions.ResourceInUseException): with assert_raises(conn.exceptions.ResourceInUseException):
resp = conn.update_table( resp = conn.update_table(
TableName="test-streams", TableName="test-streams",
StreamSpecification={"StreamViewType": "OLD_IMAGES"}, StreamSpecification={
"StreamViewType": "OLD_IMAGES",
"StreamEnabled": True,
},
) )
def test_stream_with_range_key(self): def test_stream_with_range_key(self):
@ -243,7 +246,7 @@ class TestEdges:
{"AttributeName": "color", "AttributeType": "S"}, {"AttributeName": "color", "AttributeType": "S"},
], ],
ProvisionedThroughput={"ReadCapacityUnits": 1, "WriteCapacityUnits": 1}, ProvisionedThroughput={"ReadCapacityUnits": 1, "WriteCapacityUnits": 1},
StreamSpecification={"StreamViewType": "NEW_IMAGES"}, StreamSpecification={"StreamViewType": "NEW_IMAGES", "StreamEnabled": True},
) )
stream_arn = resp["TableDescription"]["LatestStreamArn"] stream_arn = resp["TableDescription"]["LatestStreamArn"]

View File

@ -1924,7 +1924,9 @@ def test_attributes():
) )
attrs = resp["attributes"] attrs = resp["attributes"]
NUM_CUSTOM_ATTRIBUTES = 4 # 2 specific to individual machines and 1 global, going to both machines (2 + 1*2) NUM_CUSTOM_ATTRIBUTES = (
4
) # 2 specific to individual machines and 1 global, going to both machines (2 + 1*2)
NUM_DEFAULT_ATTRIBUTES = 4 NUM_DEFAULT_ATTRIBUTES = 4
len(attrs).should.equal( len(attrs).should.equal(
NUM_CUSTOM_ATTRIBUTES + (NUM_DEFAULT_ATTRIBUTES * len(instances)) NUM_CUSTOM_ATTRIBUTES + (NUM_DEFAULT_ATTRIBUTES * len(instances))

View File

@ -2526,9 +2526,7 @@ def test_get_account_summary():
) )
client.create_instance_profile(InstanceProfileName="test-profile") client.create_instance_profile(InstanceProfileName="test-profile")
client.create_open_id_connect_provider( client.create_open_id_connect_provider(Url="https://example.com", ThumbprintList=[])
Url="https://example.com", ThumbprintList=[],
)
response_policy = client.create_policy( response_policy = client.create_policy(
PolicyName="test-policy", PolicyDocument=MOCK_POLICY PolicyName="test-policy", PolicyDocument=MOCK_POLICY
) )

View File

@ -524,7 +524,7 @@ def _get_account_id():
global account_id global account_id
if account_id: if account_id:
return account_id return account_id
sts = boto3.client("sts") sts = boto3.client("sts", region_name=region)
identity = sts.get_caller_identity() identity = sts.get_caller_identity()
account_id = identity["Account"] account_id = identity["Account"]
return account_id return account_id