- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试将单元测试添加到我的 AWS 脚本中。我一直在使用 botocore.stub 来 stub API 调用。
我需要为各种调用添加分页,但我似乎找不到一种方法来编写包含分页的测试。
这是一个非分页测试的例子,我想知道如何重构这个测试和函数来使用分页:
# -*- coding: utf-8 -*-
import unittest
import boto3
from botocore.stub import Stubber
from datetime import datetime
def describe_images(client, repository):
return client.describe_images(repositoryName=repository)
class TestCase(unittest.TestCase):
def setUp(self):
self.client = boto3.client('ecr')
def test_describe_images(self):
describe_images_response = {
'imageDetails': [
{
'registryId': 'string',
'repositoryName': 'string',
'imageDigest': 'string',
'imageTags': [
'string',
],
'imageSizeInBytes': 123,
'imagePushedAt': datetime(2015, 1, 1)
},
],
'nextToken': 'string'
}
stubber = Stubber(self.client)
expected_params = {'repositoryName': 'repo_name'}
stubber.add_response(
'describe_images',
describe_images_response,
expected_params
)
with stubber:
response = describe_images(self.client, 'repo_name')
self.assertEqual(describe_images_response, response)
if __name__ == '__main__':
unittest.main()
如果我更新函数以包含这样的分页:
def describe_images(client, repository):
paginator = client.get_paginator('describe_images')
response_iterator = paginator.paginate(
repositoryName=repository
)
return response_iterator
我们似乎有所进展。由于平等性发生了变化,测试失败了:
F
======================================================================
FAIL: test_describe_images (__main__.TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "desc_imgs_paginated.py", line 47, in test_describe_images
self.assertEqual(describe_images_response, response)
AssertionError: {'imageDetails': [{'registryId': 'string'[178 chars]ing'} != <botocore.paginate.PageIterator object at 0x1058649b0>
----------------------------------------------------------------------
Ran 1 test in 0.075s
FAILED (failures=1)
当我尝试遍历生成器时::
def describe_images(client, repository):
paginator = client.get_paginator('describe_images')
response_iterator = paginator.paginate(
repositoryName=repository
)
return [r for r in response_iterator]
我收到以下错误:
E
======================================================================
ERROR: test_describe_images (__main__.TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "desc_imgs_paginated.py", line 45, in test_describe_images
response = describe_images(self.client, repo_name)
File "desc_imgs_paginated.py", line 14, in describe_images
return '.join([r for r in response_iterator])
File "desc_imgs_paginated.py", line 14, in <listcomp>
return '.join([r for r in response_iterator])
File "lib/python3.6/site-packages/botocore/paginate.py", line 255, in __iter__
response = self._make_request(current_kwargs)
File "lib/python3.6/site-packages/botocore/paginate.py", line 332, in _make_request
return self._method(**current_kwargs)
File "lib/python3.6/site-packages/botocore/client.py", line 312, in _api_call
return self._make_api_call(operation_name, kwargs)
File "lib/python3.6/site-packages/botocore/client.py", line 579, in _make_api_call
api_params, operation_model, context=request_context)
File "lib/python3.6/site-packages/botocore/client.py", line 631, in _convert_to_request_dict
params=api_params, model=operation_model, context=context)
File "lib/python3.6/site-packages/botocore/hooks.py", line 227, in emit
return self._emit(event_name, kwargs)
File "lib/python3.6/site-packages/botocore/hooks.py", line 210, in _emit
response = handler(**kwargs)
File "lib/python3.6/site-packages/botocore/stub.py", line 337, in _assert_expected_params
self._assert_expected_call_order(model, params)
File "lib/python3.6/site-packages/botocore/stub.py", line 323, in _assert_expected_call_order
pformat(params)))
botocore.exceptions.StubResponseError: Error getting response stub for operation DescribeImages: Unexpected API Call: called with parameters:
{nextToken: string, repositoryName: repo_name}
----------------------------------------------------------------------
Ran 1 test in 0.051s
FAILED (errors=1)
我是否缺少正确的测试方法?或者这是 boto3/botocore 中的错误?
最佳答案
问这个问题已经有一段时间了,但是因为没有答案..
在您的设置中,您提供了一个响应字典,如下所示
describe_images_response = {
'imageDetails': [
{
'registryId': 'string',
'repositoryName': 'string',
'imageDigest': 'string',
'imageTags': [
'string',
],
'imageSizeInBytes': 123,
'imagePushedAt': datetime(2015, 1, 1)
},
],
'nextToken': 'string'
}
这里的关键是第一个响应将包含一个 nextToken
值。这将导致来自分页器的第二个请求。因此,您必须为 stub 提供额外的响应,最终您需要以不包含nextToken
现在回顾一下您的设置,只有一个 add_response
调用 stub
stubber.add_response(
'describe_images',
describe_images_response,
expected_params
)
最终结果是,当分页器发出第二个请求时,设置中没有指定响应。
这会导致异常,希望现在的消息更有意义
botocore.exceptions.StubResponseError: Error getting response stub for operation DescribeImages: Unexpected API Call: called with parameters:
{nextToken: string, repositoryName: repo_name}
由于没有设置第二个响应,你得到了一个意外的请求异常,在这个请求中你可以看到nextToken
参数的规范。
关于python - 使用分页测试 boto3/botocore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47263342/
是否可以阅读以前版本的文档?尝试过,寻找答案,但到目前为止没有成功。 http://botocore.readthedocs.io/en/latest/ https://github.com/boto
我正在尝试将最近的 Amazon 转录服务用于: transcribe = boto3.client('transcribe') 我收到以下错误: botocore.exceptions.Unknow
我正在尝试将最近的 Amazon 转录服务用于: transcribe = boto3.client('transcribe') 我收到以下错误: botocore.exceptions.Unknow
我一直在尝试将单元测试添加到我的 AWS 脚本中。我一直在使用 botocore.stub 来 stub API 调用。 我需要为各种调用添加分页,但我似乎找不到一种方法来编写包含分页的测试。 这是一
在python3中使用boto3和warrant。 headless 系统。尝试使用授权的 ASWSRP 使用 python 脚本登录。使用用户池存在更改临时密码的问题。所以......我有代码首先尝
我有用 Python 编写的 aws 识别代码,它由 Node API 运行,在 Windows 系统上运行良好,但当我在 Linux 上部署它时,我遇到了这个问题:- botocore.errorf
在 Amazon SNS 中使用 boto3 时出现以下错误。我只想捕获 InvalidParameterException,我该怎么做? Traceback (most recent call la
我正在使用 AWS Cognito制作 OAuth服务器。我现在正在创建异常处理程序,以防使用不存在,但 requests打算得到一个 ipdb> pk 'David' ipdb> res = sel
我无法弄清楚为什么加载了编码 utf-8 的文件(其内容为“稍后删除我”)在散列时会在 botocore 中导致异常。 with io.open('deleteme','r', encoding='u
Boto3 is using their own botocore (这是有道理的)。 但是,exiting botocore不支持新的Transcribe API我真的很想使用它。 我想使用this
运行以下代码时: import boto3 BUCKET = 'bwd-plfb' s3 = boto3.client('s3',use_ssl = False) resp = s3.list_obj
在使用 Python 中的 botocore 库围绕 AWS-EC2 做一些自动化时,我注意到许多 HTTPS 连接仍然建立,忙于做其他事情的进程不再需要这些连接(所以杀死他们或重新编码他们退出不是一
当我简单地运行以下代码时,我总是收到此错误。 s3 = boto3.resource('s3') bucket_name = "python-sdk-sample-%s" % uuid.uuid4()
我正在使用 unittest 来测试使用 boto3 调用 AWS 的函数。 该函数如下所示: import boto3 def my_function(): client = boto3.c
我正在尝试用旧事件存储中的数据填充 DynamoDB 数据库由 PostgreSQL 数据库组成。在运行完大部分数据库条目后,在尝试调用 put_item 函数时抛出此错误。 botocore.exc
我已经安装了 boto3 但它仍然报错没有名为 botocore.session 的模块 import botocore.session ImportError: No module named bo
当尝试使用此 python 代码连接到数据库以测试连接时。 import boto3 s3 = boto3.resource('s3') for b in s3.buckets.all(): p
在空间有限的小型嵌入式 Linux 设备上,我试图将大型 [10 Mb] Amazon (AWS) BotoCore 库 ( https://github.com/boto/botocore ) 放在
在我的 awscli 安装升级后,我遇到了这个错误。我无法弄清楚该错误的原因。谁能帮忙? AWS CLI 错误: Traceback (most recent call last): File "
我正在尝试编写“好”的 python 并通过以下方式捕获 S3 no such key 错误: session = botocore.session.get_session() client = se
我是一名优秀的程序员,十分优秀!