Skip to content

Commit 218cb36

Browse files
committed
RFC/Draft for configuration utility
1 parent a31599d commit 218cb36

File tree

7 files changed

+60
-34
lines changed

7 files changed

+60
-34
lines changed

.travis.yml

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@ install:
2424
else
2525
PYTHON_DOCKER_TAG=${TRAVIS_PYTHON_VERSION}-stretch
2626
fi
27-
docker run --rm -t --name motoserver -e TEST_SERVER_MODE=true -e AWS_SECRET_ACCESS_KEY=server_secret -e AWS_ACCESS_KEY_ID=server_key -v `pwd`:/moto -p 5000:5000 -v /var/run/docker.sock:/var/run/docker.sock python:${PYTHON_DOCKER_TAG} /moto/travis_moto_server.sh &
27+
docker run --rm -t \
28+
--name motoserver \
29+
-e MOTO_ENVIRONMENT=development \
30+
-e TEST_SERVER_MODE=true \
31+
-e AWS_SECRET_ACCESS_KEY=server_secret \
32+
-e AWS_ACCESS_KEY_ID=server_key \
33+
-v `pwd`:/moto \
34+
-v /var/run/docker.sock:/var/run/docker.sock \
35+
-p 5000:5000 \
36+
python:${PYTHON_DOCKER_TAG} /moto/travis_moto_server.sh &
2837
fi
2938
travis_retry pip install -r requirements-dev.txt
3039
travis_retry pip install docker>=2.5.1

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ include moto/ec2/resources/instance_type_offerings/*/*.json
55
include moto/ec2/resources/amis.json
66
include moto/cognitoidp/resources/*.json
77
include moto/dynamodb2/parsing/reserved_keywords.txt
8+
include moto/configuration.ini
89
recursive-include moto/templates *
910
recursive-include tests *

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ format:
2626
test-only:
2727
rm -f .coverage
2828
rm -rf cover
29-
@pytest -sv --cov=moto --cov-report html ./tests/ $(TEST_EXCLUDE)
29+
@MOTO_ENVIRONMENT=development pytest -sv --cov=moto --cov-report html ./tests/ $(TEST_EXCLUDE)
3030

3131
test: lint test-only
3232

3333
test_server:
34-
@TEST_SERVER_MODE=true pytest -sv --cov=moto --cov-report html ./tests/
34+
@TEST_SERVER_MODE=true @MOTO_ENVIRONMENT=development pytest -sv --cov=moto --cov-report html ./tests/
3535

3636
aws_managed_policies:
3737
scripts/update_managed_policies.py

moto/configuration.ini

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[production]
2+
DeduplicationTimeInSeconds = 300
3+
4+
[development]
5+
DeduplicationTimeInSeconds = 1

moto/sqs/models.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
unix_time_millis,
2121
tags_from_cloudformation_tags_list,
2222
)
23-
from .utils import generate_receipt_handle
23+
from .utils import generate_receipt_handle, read_config
2424
from .exceptions import (
2525
MessageAttributesInvalid,
2626
MessageNotInflight,
@@ -63,8 +63,6 @@
6363
# https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-metadata.html
6464
ATTRIBUTE_NAME_PATTERN = re.compile("^([a-z]|[A-Z]|[0-9]|[_.\\-])+$")
6565

66-
DEDUPLICATION_TIME_IN_SECONDS = 300
67-
6866

6967
class Message(BaseModel):
7068
def __init__(self, message_id, body):
@@ -231,9 +229,10 @@ class Queue(CloudFormationModel):
231229
"SendMessage",
232230
)
233231

234-
def __init__(self, name, region, **kwargs):
232+
def __init__(self, name, region, config, **kwargs):
235233
self.name = name
236234
self.region = region
235+
self.config = config
237236
self.tags = {}
238237
self.permissions = {}
239238

@@ -489,7 +488,7 @@ def add_message(self, message):
489488
diff = message.sent_timestamp - m.sent_timestamp
490489
# if a duplicate message is received within the deduplication time then it should
491490
# not be added to the queue
492-
if diff / 1000 < DEDUPLICATION_TIME_IN_SECONDS:
491+
if diff / 1000 < int(self.config["DeduplicationTimeInSeconds"]):
493492
return
494493

495494
self._messages.append(message)
@@ -561,16 +560,18 @@ def _filter_message_attributes(message, input_message_attributes):
561560

562561

563562
class SQSBackend(BaseBackend):
564-
def __init__(self, region_name):
563+
def __init__(self, region_name, config):
565564
self.region_name = region_name
565+
self.config = config
566566
self.queues = {}
567567
super(SQSBackend, self).__init__()
568568

569569
def reset(self):
570570
region_name = self.region_name
571+
config = self.config
571572
self._reset_model_refs()
572573
self.__dict__ = {}
573-
self.__init__(region_name)
574+
self.__init__(region_name, config)
574575

575576
def create_queue(self, name, tags=None, **kwargs):
576577
queue = self.queues.get(name)
@@ -580,7 +581,9 @@ def create_queue(self, name, tags=None, **kwargs):
580581
except KeyError:
581582
pass
582583

583-
new_queue = Queue(name, region=self.region_name, **kwargs)
584+
new_queue = Queue(
585+
name, region=self.region_name, config=self.config, **kwargs
586+
)
584587

585588
queue_attributes = queue.attributes
586589
new_queue_attributes = new_queue.attributes
@@ -595,7 +598,7 @@ def create_queue(self, name, tags=None, **kwargs):
595598
kwargs.pop("region")
596599
except KeyError:
597600
pass
598-
queue = Queue(name, region=self.region_name, **kwargs)
601+
queue = Queue(name, region=self.region_name, config=self.config, **kwargs)
599602
self.queues[name] = queue
600603

601604
if tags:
@@ -1008,9 +1011,10 @@ def list_queue_tags(self, queue_name):
10081011

10091012

10101013
sqs_backends = {}
1014+
config = read_config()
10111015
for region in Session().get_available_regions("sqs"):
1012-
sqs_backends[region] = SQSBackend(region)
1016+
sqs_backends[region] = SQSBackend(region, config)
10131017
for region in Session().get_available_regions("sqs", partition_name="aws-us-gov"):
1014-
sqs_backends[region] = SQSBackend(region)
1018+
sqs_backends[region] = SQSBackend(region, config)
10151019
for region in Session().get_available_regions("sqs", partition_name="aws-cn"):
1016-
sqs_backends[region] = SQSBackend(region)
1020+
sqs_backends[region] = SQSBackend(region, config)

moto/sqs/utils.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
from __future__ import unicode_literals
2+
import os
23
import random
34
import string
5+
import moto
46

57
from .exceptions import MessageAttributesInvalid
8+
from moto.settings import TEST_SERVER_MODE
9+
10+
try:
11+
import configparser
12+
except ModuleNotFoundError:
13+
# why are you using python 2.7?
14+
import ConfigParser as configparser
615

716

817
def generate_receipt_handle():
@@ -49,11 +58,7 @@ def parse_message_attributes(querystring, base="", value_namespace="Value."):
4958
)
5059

5160
data_type_parts = data_type[0].split(".")
52-
if data_type_parts[0] not in [
53-
"String",
54-
"Binary",
55-
"Number",
56-
]:
61+
if data_type_parts[0] not in ["String", "Binary", "Number"]:
5762
raise MessageAttributesInvalid(
5863
"The message attribute '{0}' has an invalid message attribute type, the set of supported type prefixes is Binary, Number, and String.".format(
5964
name[0]
@@ -83,3 +88,17 @@ def parse_message_attributes(querystring, base="", value_namespace="Value."):
8388
index += 1
8489

8590
return message_attributes
91+
92+
93+
def read_config():
94+
section = os.getenv("MOTO_ENVIRONMENT", "production")
95+
parser = configparser.ConfigParser()
96+
filename = "configuration.ini"
97+
config_path = os.path.join(os.getcwd(), "moto", filename)
98+
99+
if TEST_SERVER_MODE:
100+
# in server mode the config resides in the package
101+
config_path = os.path.join(os.path.dirname(moto.__file__), filename)
102+
103+
parser.read(config_path)
104+
return parser[section]

tests/test_sqs/test_sqs.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@
2020
from moto import mock_sqs, mock_sqs_deprecated, mock_lambda, mock_logs, settings
2121
from unittest import SkipTest
2222

23-
if sys.version_info[0] < 3:
24-
import mock
25-
from unittest import SkipTest
26-
else:
27-
from unittest import SkipTest, mock
23+
2824
import pytest
2925
from tests.helpers import requires_boto_gte
3026
from tests.test_awslambda.test_lambda import get_test_zip_file1, get_role_name
@@ -54,8 +50,6 @@
5450
}
5551
"""
5652

57-
MOCK_DEDUPLICATION_TIME_IN_SECONDS = 5
58-
5953

6054
@mock_sqs
6155
def test_create_fifo_queue_fail():
@@ -2343,22 +2337,16 @@ def test_fifo_queue_deduplication_withoutid(msg_1, msg_2, expected_count):
23432337
messages.should.have.length_of(expected_count)
23442338

23452339

2346-
@mock.patch(
2347-
"moto.sqs.models.DEDUPLICATION_TIME_IN_SECONDS", MOCK_DEDUPLICATION_TIME_IN_SECONDS
2348-
)
23492340
@mock_sqs
23502341
def test_fifo_queue_send_duplicate_messages_after_deduplication_time_limit():
2351-
if settings.TEST_SERVER_MODE:
2352-
raise SkipTest("Cant manipulate time in server mode")
2353-
23542342
sqs = boto3.resource("sqs", region_name="us-east-1")
23552343
msg_queue = sqs.create_queue(
23562344
QueueName="test-queue-dlq.fifo",
23572345
Attributes={"FifoQueue": "true", "ContentBasedDeduplication": "true"},
23582346
)
23592347

23602348
msg_queue.send_message(MessageBody="first", MessageGroupId="1")
2361-
time.sleep(MOCK_DEDUPLICATION_TIME_IN_SECONDS + 5)
2349+
time.sleep(5) # in tests deduplication time is 1 second
23622350
msg_queue.send_message(MessageBody="first", MessageGroupId="2")
23632351
messages = msg_queue.receive_messages(MaxNumberOfMessages=2)
23642352
messages.should.have.length_of(2)

0 commit comments

Comments
 (0)