我应该能够将moto SNS通知订阅到moto SQS队列吗?

时间:2018-06-22 12:55:24

标签: python-2.7 mocking amazon-sqs amazon-sns moto

我正在使用python 2.7,boto(2)和moto服务器,并且我使用BOTO_CONFIG环境变量指向我的本地moto服务器。 我可以适当地模拟一切,直到发布方法。

我创建了2个独立的进程,其中一个用于sqs,一个用于sns

moto_server sqs -p5001
moto_server sns -p5002

我创建了队列,主题和订阅,效果很好

127.0.0.1 - - [21/Jun/2018 19:19:05] "GET http://queue.amazonaws.com/?Action=CreateQueue&QueueName=my_local_queue&Version=2012-11-05 HTTP/1.1" 200 -
127.0.0.1 - - [21/Jun/2018 19:19:05] "GET http://sns.us-east-1.amazonaws.com/?Action=CreateTopic&ContentType=JSON&Name=my_local_topic&Version=2010-03-31 HTTP/1.1" 200 -
127.0.0.1 - - [21/Jun/2018 19:19:05] "GET http://sns.us-east-1.amazonaws.com/?Action=Subscribe&ContentType=JSON&Endpoint=arn%3Aaws%3Asqs%3Aus-east-1%3A123456789012%3Amy_local_queue&Protocol=sqs&TopicArn=arn%3Aaws%3Asns%3Aus-east-1%3A123456789012%3Amy_local_topic&Version=2010-03-31 HTTP/1.1" 200 -
127.0.0.1 - - [21/Jun/2018 19:19:05] "GET http://queue.amazonaws.com/123456789012/my_local_queue?Action=GetQueueAttributes&AttributeName=Policy&Version=2012-11-05 HTTP/1.1" 200 -
127.0.0.1 - - [21/Jun/2018 19:19:05] "GET http://queue.amazonaws.com/123456789012/my_local_queue?Action=SetQueueAttributes&Attribute.Name=Policy&Attribute.Value=%7B%22Version%22%3A%20%222008-10-17%22%2C%20%22Statement%22%3A%20%5B%7B%22Resource%22%3A%20%22arn%3Aaws%3Asqs%3Aus-east-1%3A123456789012%3Amy_local_queue%22%2C%20%22Effect%22%3A%20%22Allow%22%2C%20%22Sid%22%3A%20%223ca50596674e991d6a8a04932ab998ac%22%2C%20%22Action%22%3A%20%22SQS%3ASendMessage%22%2C%20%22Condition%22%3A%20%7B%22StringLike%22%3A%20%7B%22aws%3ASourceArn%22%3A%20%22arn%3Aaws%3Asns%3Aus-east-1%3A123456789012%3Amy_local_topic%22%7D%7D%2C%20%22Principal%22%3A%20%7B%22AWS%22%3A%20%22%2A%22%7D%7D%5D%7D&Version=2012-11-05 HTTP/1.1" 200 -

但是当我向SNS发布消息时,出现此错误

127.0.0.1 - - [21/Jun/2018 19:22:08] "POST http://sns.us-east-1.amazonaws.com/ HTTP/1.1" 500 -
Error on request:
Traceback (most recent call last):
  File "/_venv/lib/python2.7/site-packages/werkzeug/serving.py", line 270, in run_wsgi
    execute(self.server.app)
  File "/_venv/lib/python2.7/site-packages/werkzeug/serving.py", line 258, in execute
    application_iter = app(environ, start_response)
  File "/_venv/lib/python2.7/site-packages/moto/server.py", line 93, in __call__
    return backend_app(environ, start_response)
  File "/_venv/lib/python2.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/_venv/lib/python2.7/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/_venv/lib/python2.7/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/_venv/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/_venv/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/_venv/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/_venv/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/_venv/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/_venv/lib/python2.7/site-packages/moto/core/utils.py", line 139, in __call__
    result = self.callback(request, request.url, {})
  File "/_venv/lib/python2.7/site-packages/moto/core/responses.py", line 116, in dispatch
    return cls()._dispatch(*args, **kwargs)
  File "/_venv/lib/python2.7/site-packages/moto/core/responses.py", line 199, in _dispatch
    return self.call_action()
  File "/_venv/lib/python2.7/site-packages/moto/core/responses.py", line 273, in call_action
    response = method()
  File "/_venv/lib/python2.7/site-packages/moto/sns/responses.py", line 274, in publish
    message_attributes=message_attributes)
  File "/_venv/lib/python2.7/site-packages/moto/sns/models.py", line 325, in publish
    message_attributes=message_attributes)
  File "/_venv/lib/python2.7/site-packages/moto/sns/models.py", line 50, in publish
    message_attributes=message_attributes)
  File "/_venv/lib/python2.7/site-packages/moto/sns/models.py", line 97, in publish
    sqs_backends[region].send_message(queue_name, enveloped_message)
  File "/_venv/lib/python2.7/site-packages/moto/sqs/models.py", line 405, in send_message
    queue = self.get_queue(queue_name)
  File "/_venv/lib/python2.7/site-packages/moto/sqs/models.py", line 390, in get_queue
    raise QueueDoesNotExist()
QueueDoesNotExist

我迷路了,我觉得我缺少明显的东西,但找不到它,我检查了BOTO_CONFIG是否已复制到SNS子进程中(我假设同样使用boto并指向我的本地moto SQS ,但也许这是我对情况的误解)

任何指针或想法将不胜感激!

这是我用来设置所有内容的代码的片段,发布的调用很简单,因此不包含在内

# (...)
self.__events_queue = self.get_queue()
SNSConnection.get_instance().get_sns_connection().create_topic(topic=conf.aws.sns_topic_arn.split(':')[5])
SNSConnection.get_instance().get_sns_connection().subscribe_sqs_queue(conf.aws.sns_topic_arn, self.__events_queue)

def get_queue(self):
    conf = config.Config.get_instance()
    conn = boto.sqs.connect_to_region(
        conf.aws.region,
        aws_access_key_id=conf.aws.access_key_id,
        aws_secret_access_key=conf.aws.secret_access_key
    )
    q = conn.get_queue(conf.aws.sqs_queue_name)
    if not q:
        q = conn.create_queue(conf.aws.sqs_queue_name)
    return q

我也将此内容发布到了project's github,但是我在这里可能还有更多的运气

1 个答案:

答案 0 :(得分:0)

我总是怀疑问题出在两个不同的moto进程上,但是不知道您可以在同一进程中运行两个不同的moto服务。
显然是possible

如果您像moto docker文件一样启动进程,一切都会按预期进行

moto_server -H 0.0.0.0