我正在使用 moto 和 boto3 的 @mock_s3 装饰器模拟 AWS 对 s3 的调用。我正在我的根目录中使用“覆盖运行”运行测试。
每当我的测试涉及任何涉及列出 AWS S3 内容的命令时,我都会收到此错误:
File "/Users/Name/Documents/project-name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 1044, in visit_For
if node.test:
AttributeError: 'For' object has no attribute 'test'
因此错误而失败的一些命令示例如下:
s3_client = s3_session.client("s3")
s3_client.list_buckets() #fails at this line
和
s3_session = boto3.session.Session()
s3_client = s3_session.client("s3")
paginator = s3_client.get_paginator("list_objects_v2")
iterator = paginator.paginate(Bucket="mybucket", Prefix="mypath")
keys = iterator.search(query)
regex = re.compile('(\S)')
data_files = list(filter(regex.search, keys)) #fails at this line
在运行这些代码行之前,我使用 boto3 在这些存储桶中创建存储桶和对象。
这是一个测试示例:
@mock_s3
class TestModel(TestCase):
def test_my_model_save(self):
s3_client = boto3.client('s3', region_name='us-east-1')
s3_client.create_bucket(Bucket='mybucket') # printing the response from this returns {'ResponseMetadata': {'HTTPStatusCode': 200, 'HTTPHeaders': {}, 'RetryAttempts': 0}}
s3_client.put_object(Bucket='mybucket', Key="mypath", Body="details") # printing the response from this returns {'ResponseMetadata': {'HTTPStatusCode': 200, 'HTTPHeaders': {'etag': '"27792947ed5d5da7c0d1f43327ed9dab"', 'last-modified': 'Fri, 15 Mar 2019 20:15:44 GMT', 'content-length': '7'}, 'RetryAttempts': 0}, 'ETag': '"27792947ed5d5da7c0d1f43327ed9dab"'}
print(s3_client.list_buckets()) #code breaks here
我认识到这个测试不完整(缺少断言并且没有实际测试我的代码),这只是一个证明错误的通用案例。
我还注意到 create_bucket 和 put_object 的响应似乎与这两个函数的 boto3 文档不匹配(请参阅上面示例测试代码中的注释)。
这是执行我的测试时的完整回溯:
Traceback (most recent call last):
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/core/models.py", line 74, in wrapper
result = func(*args, **kwargs)
File "/Users/user_name/Documents/project_name/api/tests/test_my_Model.py", line 24, in test_my_model_save
print(s3_client.list_buckets())
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/client.py", line 320, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/client.py", line 611, in _make_api_call
operation_model, request_dict)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/endpoint.py", line 102, in make_request
return self._send_request(request_dict, operation_model)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/endpoint.py", line 136, in _send_request
success_response, exception):
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/endpoint.py", line 210, in _needs_retry
caught_exception=caught_exception, request_dict=request_dict)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 356, in emit
return self._emitter.emit(aliased_event_name, **kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 228, in emit
return self._emit(event_name, kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 211, in _emit
response = handler(**kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 183, in __call__
if self._checker(attempts, response, caught_exception):
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 251, in __call__
caught_exception)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 269, in _should_retry
return self._checker(attempt_number, response, caught_exception)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 317, in __call__
caught_exception)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 223, in __call__
attempt_number, caught_exception)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 359, in _check_caught_exception
raise caught_exception
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/endpoint.py", line 176, in _get_response
responses = self._event_emitter.emit(event_name, request=request)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 356, in emit
return self._emitter.emit(aliased_event_name, **kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 228, in emit
return self._emit(event_name, kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 211, in _emit
response = handler(**kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/core/models.py", line 292, in __call__
status, headers, body = response_callback(request, request.url, request.headers)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/s3/responses.py", line 116, in bucket_response
response = self._bucket_response(request, full_url, headers)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/s3/responses.py", line 138, in _bucket_response
return self.all_buckets()
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/s3/responses.py", line 53, in all_buckets
template = self.response_template(S3_ALL_BUCKETS)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/core/responses.py", line 103, in response_template
return self.environment.get_template(template_id)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/loaders.py", line 125, in load
code = environment.compile(source, name, filename)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/environment.py", line 581, in compile
defer_init=defer_init)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/environment.py", line 543, in _generate
optimized=self.optimized)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 82, in generate
generator.visit(node)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/visitor.py", line 38, in visit
return f(node, *args, **kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 754, in visit_Template
self.blockvisit(node.body, frame)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 378, in blockvisit
self.visit(node, frame)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/visitor.py", line 38, in visit
return f(node, *args, **kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 1044, in visit_For
if node.test:
AttributeError: 'For' object has no attribute 'test'
我正在使用最新版本的 moto 和 boto3。
这里可能存在什么问题?