Techdebt: Replace sure with regular assertions in SQS (#6634)
This commit is contained in:
parent
f297c4216f
commit
1626c35ac5
@ -1,22 +1,18 @@
|
|||||||
|
"""Test different server responses."""
|
||||||
import datetime
|
import datetime
|
||||||
import re
|
import re
|
||||||
import sure # noqa # pylint: disable=unused-import
|
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import moto.server as server
|
import moto.server as server
|
||||||
|
|
||||||
"""
|
|
||||||
Test the different server responses
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def test_sqs_list_identities():
|
def test_sqs_list_identities():
|
||||||
backend = server.create_backend_app("sqs")
|
backend = server.create_backend_app("sqs")
|
||||||
test_client = backend.test_client()
|
test_client = backend.test_client()
|
||||||
|
|
||||||
res = test_client.get("/?Action=ListQueues")
|
res = test_client.get("/?Action=ListQueues")
|
||||||
res.data.should.contain(b"ListQueuesResponse")
|
assert b"ListQueuesResponse" in res.data
|
||||||
|
|
||||||
# Make sure that we can receive messages from queues whose name contains dots (".")
|
# Make sure that we can receive messages from queues whose name contains dots (".")
|
||||||
# The AWS API mandates that the names of FIFO queues use the suffix ".fifo"
|
# The AWS API mandates that the names of FIFO queues use the suffix ".fifo"
|
||||||
@ -35,11 +31,11 @@ def test_sqs_list_identities():
|
|||||||
)
|
)
|
||||||
|
|
||||||
message = re.search("<Body>(.*?)</Body>", res.data.decode("utf-8")).groups()[0]
|
message = re.search("<Body>(.*?)</Body>", res.data.decode("utf-8")).groups()[0]
|
||||||
message.should.equal("test-message")
|
assert message == "test-message"
|
||||||
|
|
||||||
res = test_client.get("/?Action=ListQueues&QueueNamePrefix=other")
|
res = test_client.get("/?Action=ListQueues&QueueNamePrefix=other")
|
||||||
res.data.should.contain(b"otherqueue.fifo")
|
assert b"otherqueue.fifo" in res.data
|
||||||
res.data.should_not.contain(b"testqueue")
|
assert b"testqueue" not in res.data
|
||||||
|
|
||||||
|
|
||||||
def test_messages_polling():
|
def test_messages_polling():
|
||||||
@ -91,7 +87,8 @@ def test_no_messages_polling_timeout():
|
|||||||
wait_seconds = 5
|
wait_seconds = 5
|
||||||
start = datetime.datetime.utcnow()
|
start = datetime.datetime.utcnow()
|
||||||
test_client.get(
|
test_client.get(
|
||||||
f"/123/{queue_name}?Action=ReceiveMessage&MaxNumberOfMessages=1&WaitTimeSeconds={wait_seconds}"
|
f"/123/{queue_name}?Action=ReceiveMessage&"
|
||||||
|
f"MaxNumberOfMessages=1&WaitTimeSeconds={wait_seconds}"
|
||||||
)
|
)
|
||||||
end = datetime.datetime.utcnow()
|
end = datetime.datetime.utcnow()
|
||||||
duration = end - start
|
duration = end - start
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,13 +1,13 @@
|
|||||||
import boto3
|
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import sure # noqa # pylint: disable=unused-import
|
|
||||||
from moto import mock_sqs, mock_cloudformation
|
|
||||||
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
|
|
||||||
from string import Template
|
from string import Template
|
||||||
from random import randint
|
from random import randint
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import boto3
|
||||||
|
|
||||||
|
from moto import mock_sqs, mock_cloudformation
|
||||||
|
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
|
||||||
|
|
||||||
simple_queue = Template(
|
simple_queue = Template(
|
||||||
"""{
|
"""{
|
||||||
"AWSTemplateFormatVersion": "2010-09-09",
|
"AWSTemplateFormatVersion": "2010-09-09",
|
||||||
@ -56,13 +56,13 @@ def test_describe_stack_subresources():
|
|||||||
cf.create_stack(StackName=stack_name, TemplateBody=template_body)
|
cf.create_stack(StackName=stack_name, TemplateBody=template_body)
|
||||||
|
|
||||||
queue_urls = client.list_queues(QueueNamePrefix=q_name)["QueueUrls"]
|
queue_urls = client.list_queues(QueueNamePrefix=q_name)["QueueUrls"]
|
||||||
assert any([f"{ACCOUNT_ID}/{q_name}" in url for url in queue_urls])
|
assert any({f"{ACCOUNT_ID}/{q_name}" in url for url in queue_urls})
|
||||||
|
|
||||||
stack = res.Stack(stack_name)
|
stack = res.Stack(stack_name)
|
||||||
for s in stack.resource_summaries.all():
|
for s in stack.resource_summaries.all():
|
||||||
s.resource_type.should.equal("AWS::SQS::Queue")
|
assert s.resource_type == "AWS::SQS::Queue"
|
||||||
s.logical_id.should.equal("QueueGroup")
|
assert s.logical_id == "QueueGroup"
|
||||||
s.physical_resource_id.should.contain(f"/{q_name}")
|
assert f"/{q_name}" in s.physical_resource_id
|
||||||
|
|
||||||
|
|
||||||
@mock_sqs
|
@mock_sqs
|
||||||
@ -77,14 +77,14 @@ def test_list_stack_resources():
|
|||||||
cf.create_stack(StackName=stack_name, TemplateBody=template_body)
|
cf.create_stack(StackName=stack_name, TemplateBody=template_body)
|
||||||
|
|
||||||
queue_urls = client.list_queues(QueueNamePrefix=q_name)["QueueUrls"]
|
queue_urls = client.list_queues(QueueNamePrefix=q_name)["QueueUrls"]
|
||||||
assert any([f"{ACCOUNT_ID}/{q_name}" in url for url in queue_urls])
|
assert any({f"{ACCOUNT_ID}/{q_name}" in url for url in queue_urls})
|
||||||
|
|
||||||
queue = cf.list_stack_resources(StackName=stack_name)["StackResourceSummaries"][0]
|
queue = cf.list_stack_resources(StackName=stack_name)["StackResourceSummaries"][0]
|
||||||
|
|
||||||
queue.should.have.key("ResourceType").equal("AWS::SQS::Queue")
|
assert queue["ResourceType"] == "AWS::SQS::Queue"
|
||||||
queue.should.have.key("LogicalResourceId").should.equal("QueueGroup")
|
assert queue["LogicalResourceId"] == "QueueGroup"
|
||||||
expected_url = f"https://sqs.us-east-1.amazonaws.com/{ACCOUNT_ID}/{q_name}"
|
expected_url = f"https://sqs.us-east-1.amazonaws.com/{ACCOUNT_ID}/{q_name}"
|
||||||
queue.should.have.key("PhysicalResourceId").should.equal(expected_url)
|
assert queue["PhysicalResourceId"] == expected_url
|
||||||
|
|
||||||
|
|
||||||
@mock_sqs
|
@mock_sqs
|
||||||
@ -103,7 +103,7 @@ def test_create_from_cloudformation_json_with_tags():
|
|||||||
queue_url = [url for url in all_urls if url.endswith(q_name)][0]
|
queue_url = [url for url in all_urls if url.endswith(q_name)][0]
|
||||||
|
|
||||||
queue_tags = client.list_queue_tags(QueueUrl=queue_url)["Tags"]
|
queue_tags = client.list_queue_tags(QueueUrl=queue_url)["Tags"]
|
||||||
queue_tags.should.equal({"keyname1": "value1", "keyname2": "value2"})
|
assert queue_tags == {"keyname1": "value1", "keyname2": "value2"}
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudformation
|
@mock_cloudformation
|
||||||
@ -127,11 +127,11 @@ def test_update_stack():
|
|||||||
|
|
||||||
client = boto3.client("sqs", region_name="us-west-1")
|
client = boto3.client("sqs", region_name="us-west-1")
|
||||||
queues = client.list_queues(QueueNamePrefix=q_name)["QueueUrls"]
|
queues = client.list_queues(QueueNamePrefix=q_name)["QueueUrls"]
|
||||||
queues.should.have.length_of(1)
|
assert len(queues) == 1
|
||||||
attrs = client.get_queue_attributes(QueueUrl=queues[0], AttributeNames=["All"])[
|
attrs = client.get_queue_attributes(QueueUrl=queues[0], AttributeNames=["All"])[
|
||||||
"Attributes"
|
"Attributes"
|
||||||
]
|
]
|
||||||
attrs["VisibilityTimeout"].should.equal("60")
|
assert attrs["VisibilityTimeout"] == "60"
|
||||||
|
|
||||||
# when updating
|
# when updating
|
||||||
sqs_template["Resources"]["QueueGroup"]["Properties"]["VisibilityTimeout"] = 100
|
sqs_template["Resources"]["QueueGroup"]["Properties"]["VisibilityTimeout"] = 100
|
||||||
@ -140,11 +140,11 @@ def test_update_stack():
|
|||||||
|
|
||||||
# then the attribute should be updated
|
# then the attribute should be updated
|
||||||
queues = client.list_queues(QueueNamePrefix=q_name)["QueueUrls"]
|
queues = client.list_queues(QueueNamePrefix=q_name)["QueueUrls"]
|
||||||
queues.should.have.length_of(1)
|
assert len(queues) == 1
|
||||||
attrs = client.get_queue_attributes(QueueUrl=queues[0], AttributeNames=["All"])[
|
attrs = client.get_queue_attributes(QueueUrl=queues[0], AttributeNames=["All"])[
|
||||||
"Attributes"
|
"Attributes"
|
||||||
]
|
]
|
||||||
attrs["VisibilityTimeout"].should.equal("100")
|
assert attrs["VisibilityTimeout"] == "100"
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudformation
|
@mock_cloudformation
|
||||||
@ -167,14 +167,14 @@ def test_update_stack_and_remove_resource():
|
|||||||
cf.create_stack(StackName=stack_name, TemplateBody=sqs_template_json)
|
cf.create_stack(StackName=stack_name, TemplateBody=sqs_template_json)
|
||||||
|
|
||||||
client = boto3.client("sqs", region_name="us-west-1")
|
client = boto3.client("sqs", region_name="us-west-1")
|
||||||
client.list_queues(QueueNamePrefix=q_name)["QueueUrls"].should.have.length_of(1)
|
assert len(client.list_queues(QueueNamePrefix=q_name)["QueueUrls"]) == 1
|
||||||
|
|
||||||
sqs_template["Resources"].pop("QueueGroup")
|
sqs_template["Resources"].pop("QueueGroup")
|
||||||
sqs_template_json = json.dumps(sqs_template)
|
sqs_template_json = json.dumps(sqs_template)
|
||||||
cf.update_stack(StackName=stack_name, TemplateBody=sqs_template_json)
|
cf.update_stack(StackName=stack_name, TemplateBody=sqs_template_json)
|
||||||
|
|
||||||
# No queues exist, so the key is not passed through
|
# No queues exist, so the key is not passed through
|
||||||
client.list_queues(QueueNamePrefix=q_name).shouldnt.have.key("QueueUrls")
|
assert "QueueUrls" not in client.list_queues(QueueNamePrefix=q_name)
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudformation
|
@mock_cloudformation
|
||||||
@ -190,7 +190,7 @@ def test_update_stack_and_add_resource():
|
|||||||
cf.create_stack(StackName=stack_name, TemplateBody=sqs_template_json)
|
cf.create_stack(StackName=stack_name, TemplateBody=sqs_template_json)
|
||||||
|
|
||||||
client = boto3.client("sqs", region_name="us-west-1")
|
client = boto3.client("sqs", region_name="us-west-1")
|
||||||
client.list_queues(QueueNamePrefix=q_name).shouldnt.have.key("QueueUrls")
|
assert "QueueUrls" not in client.list_queues(QueueNamePrefix=q_name)
|
||||||
|
|
||||||
sqs_template = {
|
sqs_template = {
|
||||||
"AWSTemplateFormatVersion": "2010-09-09",
|
"AWSTemplateFormatVersion": "2010-09-09",
|
||||||
@ -204,7 +204,7 @@ def test_update_stack_and_add_resource():
|
|||||||
sqs_template_json = json.dumps(sqs_template)
|
sqs_template_json = json.dumps(sqs_template)
|
||||||
cf.update_stack(StackName=stack_name, TemplateBody=sqs_template_json)
|
cf.update_stack(StackName=stack_name, TemplateBody=sqs_template_json)
|
||||||
|
|
||||||
client.list_queues(QueueNamePrefix=q_name)["QueueUrls"].should.have.length_of(1)
|
assert len(client.list_queues(QueueNamePrefix=q_name)["QueueUrls"]) == 1
|
||||||
|
|
||||||
|
|
||||||
@mock_sqs
|
@mock_sqs
|
||||||
@ -223,4 +223,4 @@ def test_create_queue_passing_integer_as_name():
|
|||||||
cf.create_stack(StackName=stack_name, TemplateBody=template_body)
|
cf.create_stack(StackName=stack_name, TemplateBody=template_body)
|
||||||
|
|
||||||
queue_urls = client.list_queues(QueueNamePrefix=q_name[0:6])["QueueUrls"]
|
queue_urls = client.list_queues(QueueNamePrefix=q_name[0:6])["QueueUrls"]
|
||||||
queue_urls.should.have.length_of(1)
|
assert len(queue_urls) == 1
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import boto3
|
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
import boto3
|
||||||
|
|
||||||
from moto import mock_lambda, mock_sqs, mock_logs
|
from moto import mock_lambda, mock_sqs, mock_logs
|
||||||
from tests.markers import requires_docker
|
from tests.markers import requires_docker
|
||||||
from tests.test_awslambda.test_lambda import get_test_zip_file1, get_role_name
|
from tests.test_awslambda.test_lambda import get_test_zip_file1, get_role_name
|
||||||
@ -132,8 +133,8 @@ def test_invoke_function_from_sqs_fifo_queue():
|
|||||||
try:
|
try:
|
||||||
body = json.loads(event.get("message"))
|
body = json.loads(event.get("message"))
|
||||||
atts = body["Records"][0]["attributes"]
|
atts = body["Records"][0]["attributes"]
|
||||||
atts.should.have.key("MessageGroupId").equals("mg1")
|
assert atts["MessageGroupId"] == "mg1"
|
||||||
atts.should.have.key("MessageDeduplicationId")
|
assert "MessageDeduplicationId" in atts
|
||||||
return
|
return
|
||||||
except: # noqa: E722 Do not use bare except
|
except: # noqa: E722 Do not use bare except
|
||||||
pass
|
pass
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import unittest
|
import unittest
|
||||||
import boto3
|
|
||||||
from moto import mock_sts, mock_sqs
|
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import boto3
|
||||||
|
|
||||||
|
from moto import mock_sts, mock_sqs
|
||||||
|
|
||||||
|
|
||||||
class TestStsAssumeRole(unittest.TestCase):
|
class TestStsAssumeRole(unittest.TestCase):
|
||||||
@mock_sqs
|
@mock_sqs
|
||||||
@ -14,7 +16,7 @@ class TestStsAssumeRole(unittest.TestCase):
|
|||||||
|
|
||||||
# verify function exists
|
# verify function exists
|
||||||
all_urls = sqs.list_queues()["QueueUrls"]
|
all_urls = sqs.list_queues()["QueueUrls"]
|
||||||
all_urls.should.contain(queue_url)
|
assert queue_url in all_urls
|
||||||
|
|
||||||
# assume role to another aws account
|
# assume role to another aws account
|
||||||
account_b = "111111111111"
|
account_b = "111111111111"
|
||||||
@ -33,4 +35,4 @@ class TestStsAssumeRole(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# client2 belongs to another account, where there are no queues
|
# client2 belongs to another account, where there are no queues
|
||||||
client2.list_queues().shouldnt.have.key("QueueUrls")
|
assert "QueueUrls" not in client2.list_queues()
|
||||||
|
Loading…
Reference in New Issue
Block a user