- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试测试一些使用 boto 的 python 代码。我不想尝试针对 AWS 进行集成测试,所以我尝试用 moto 模拟它,但它的行为并不像我预期的那样。
测试代码如下:
import io
import boto3
from moto import mock_ec2
from unittest.mock import patch
from argparse import Namespace
from awswl import commands
@mock_ec2
@patch('awswl.externalip.get_external_ip', return_value='192.0.2.1')
def test_list_command_lists_ipv4_and_ipv6_cidrs(exip_method):
# Given
options = Namespace()
options.sgid = "sg-123456"
ec2 = boto3.resource('ec2')
sg = ec2.create_security_group(
Description='Security Group for SSH Whitelisting',
GroupName='SSH Whitelist'
)
print("Created security group: {0}".format(sg.GroupId))
错误:
Testing started at 16:46 ...
/path/.virtualenvs/awswl-Ir8BWU8l/bin/python "/path/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/173.4301.16/PyCharm.app/Contents/helpers/pycharm/_jb_pytest_runner.py" --target test_commands.py::test_list_command_lists_ipv4_and_ipv6_cidrs
Launching py.test with arguments test_commands.py::test_list_command_lists_ipv4_and_ipv6_cidrs in /path/awswl/tests
============================= test session starts ==============================
platform darwin -- Python 3.6.4, pytest-3.4.2, py-1.5.2, pluggy-0.6.0
rootdir: /path/awswl, inifile:
collected 1 item
test_commands.py F
tests/test_commands.py:22 (test_list_command_lists_ipv4_and_ipv6_cidrs)
exip_method = <MagicMock name='get_external_ip' id='4547595848'>
@mock_ec2
@patch('awswl.externalip.get_external_ip', return_value='192.0.2.1')
def test_list_command_lists_ipv4_and_ipv6_cidrs(exip_method):
# Given
options = Namespace()
options.sgid = "sg-123456"
> ec2 = boto3.resource('ec2')
test_commands.py:30:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/boto3/__init__.py:92: in resource
return _get_default_session().resource(*args, **kwargs)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/boto3/session.py:389: in resource
aws_session_token=aws_session_token, config=config)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/boto3/session.py:263: in client
aws_session_token=aws_session_token, config=config)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/session.py:861: in create_client
client_config=config, api_version=api_version)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/client.py:76: in create_client
verify, credentials, scoped_config, client_config, endpoint_bridge)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/client.py:285: in _get_client_args
verify, credentials, scoped_config, client_config, endpoint_bridge)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/args.py:45: in get_client_args
endpoint_url, is_secure, scoped_config)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/args.py:111: in compute_client_args
service_name, region_name, endpoint_url, is_secure)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/client.py:358: in resolve
service_name, region_name)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/regions.py:122: in construct_endpoint
partition, service_name, region_name)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <botocore.regions.EndpointResolver object at 0x10f1a3908>
partition = OrderedDict([('defaults', OrderedDict([('hostname', '{service}.{region}.{dnsSuffix}'), ('protocols', ['https']), ('sig...1', OrderedDict()), ('us-east-2', OrderedDict()), ('us-west-1', OrderedDict()), ('us-west-2', OrderedDict())]))]))]))])
service_name = 'ec2', region_name = None
def _endpoint_for_partition(self, partition, service_name, region_name):
# Get the service from the partition, or an empty template.
service_data = partition['services'].get(
service_name, DEFAULT_SERVICE_DATA)
# Use the partition endpoint if no region is supplied.
if region_name is None:
if 'partitionEndpoint' in service_data:
region_name = service_data['partitionEndpoint']
else:
> raise NoRegionError()
E botocore.exceptions.NoRegionError: You must specify a region.
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/regions.py:135: NoRegionError
[100%]
=================================== FAILURES ===================================
_________________ test_list_command_lists_ipv4_and_ipv6_cidrs __________________
exip_method = <MagicMock name='get_external_ip' id='4547595848'>
@mock_ec2
@patch('awswl.externalip.get_external_ip', return_value='192.0.2.1')
def test_list_command_lists_ipv4_and_ipv6_cidrs(exip_method):
# Given
options = Namespace()
options.sgid = "sg-123456"
> ec2 = boto3.resource('ec2')
test_commands.py:30:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/boto3/__init__.py:92: in resource
return _get_default_session().resource(*args, **kwargs)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/boto3/session.py:389: in resource
aws_session_token=aws_session_token, config=config)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/boto3/session.py:263: in client
aws_session_token=aws_session_token, config=config)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/session.py:861: in create_client
client_config=config, api_version=api_version)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/client.py:76: in create_client
verify, credentials, scoped_config, client_config, endpoint_bridge)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/client.py:285: in _get_client_args
verify, credentials, scoped_config, client_config, endpoint_bridge)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/args.py:45: in get_client_args
endpoint_url, is_secure, scoped_config)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/args.py:111: in compute_client_args
service_name, region_name, endpoint_url, is_secure)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/client.py:358: in resolve
service_name, region_name)
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/regions.py:122: in construct_endpoint
partition, service_name, region_name)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <botocore.regions.EndpointResolver object at 0x10f1a3908>
partition = OrderedDict([('defaults', OrderedDict([('hostname', '{service}.{region}.{dnsSuffix}'), ('protocols', ['https']), ('sig...1', OrderedDict()), ('us-east-2', OrderedDict()), ('us-west-1', OrderedDict()), ('us-west-2', OrderedDict())]))]))]))])
service_name = 'ec2', region_name = None
def _endpoint_for_partition(self, partition, service_name, region_name):
# Get the service from the partition, or an empty template.
service_data = partition['services'].get(
service_name, DEFAULT_SERVICE_DATA)
# Use the partition endpoint if no region is supplied.
if region_name is None:
if 'partitionEndpoint' in service_data:
region_name = service_data['partitionEndpoint']
else:
> raise NoRegionError()
E botocore.exceptions.NoRegionError: You must specify a region.
../../../../../.virtualenvs/awswl-Ir8BWU8l/lib/python3.6/site-packages/botocore/regions.py:135: NoRegionError
=========================== 1 failed in 1.80 seconds ===========================
Process finished with exit code 0
它看起来像是在调用 boto,而不是用 Moto 模拟它,而且我得到了一个 NoRegionError,因为没有指定区域/配置文件。
我做错了什么?我假设是我,但我还没有弄清楚如何。 ;)
最佳答案
测试输出指出的问题可能与使用 boto3 创建资源时未指定任何区域有关。从你的测试输出:
botocore.exceptions.NoRegionError:您必须指定区域。
同时检查 moto's tests , 看起来区域也总是在那里指定。
只需使用示例区域初始化资源即可解决您的问题:
boto3.resource('ec2', region_name='eu-central-1')
关于python - moto 没有 mock ec2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49224322/
我希望有人能帮我解决这个问题。 前段时间我开发了一个应用程序,目的是在智能手机中同时记录前置麦克风和后置麦克风捕获的声音。基本上,我会像这样制作下一个对象: AudioRecord audioReco
我正在编写一个 lambda 函数,它获取 CW 日志组列表并对每个日志组运行“导出到 s3”任务。 我正在使用 pytest 编写自动化测试我正在使用 moto.mock_logs (除其他外),但
我有一个名为 check_stuff 的函数,它实例化一个对象并调用函数 describe_continuous_backups,但是,moto 尚不支持此功能,因此我需要自己手动模拟它。我有以下内容
总而言之,我一遍又一遍地在谷歌上搜索以找到解决方案,虽然我发现了一个关于相机释放等的错误,但我似乎无法让凸轮代码正常工作。每次我执行 takePicture 时,系统都会挂起,有时它会调用 Pictu
我不知道其他人是否也遇到过这种情况。连接到 Moto 360 (Android Wear) 上的 Sensor.TYPE_MAGNETIC_FIELD 传感器时,我没有收到任何更新。 以下代码都有效:
我目前正在尝试使用 Moto 和 @mock_dynamodb2 为我的 python 代码编写单元测试。到目前为止,我一直在测试我的“成功操作”测试用例。但是我很难让它为我的“失败案例”工作。 在我
通过 goCoder SDK 使用 Wowza Streaming Cloud 开始流式传输时,Moto g 和三星 s6 中的摄像头当时无法打开,但当我切换摄像头时,两个摄像头(后置和前置)都可以正
我需要访问 Moto 设备 (5.0.1) 上的数据漫游状态 if (Settings.Secure.getInt(context.getContentResolver(),Settings.Secu
上周买了新的 Moto G XT1033,手机工作正常,但作为开发人员,我发现在 Debug模式下很难,即使在开发人员选项中启用 USB 调试选项之后... 必要时出现错误 USB 驱动程序 未找到
我有一台 Windows 7 x64 Ultimate 计算机,我想在 Debug模式下连接我的 Moto X 2nd Gen (XT1097),以便我可以开发一些应用程序。 p> 问题是,Windo
我正在尝试测试一些使用 boto 的 python 代码。我不想尝试针对 AWS 进行集成测试,所以我尝试用 moto 模拟它,但它的行为并不像我预期的那样。 测试代码如下: import io im
我最近在 android wearable(MOTO 360) 上开发应用。 我的问题是,是否可以将传感器数据作为文件保存在我的 watch 中?或者我必须将这些数据发送回我的掌上电脑进行保存? 我尝
我编写了一个在圆形和方形 watch 上运行的应用程序,具有两个布局和一个 watchViewStub。由于 moto 360 在欧洲销售,我有很多报告说 Moto 360 没有加载圆形布局。使用模拟
我正在尝试通过蓝牙在我的 Moto 360 上进行调试。我正在关注这些 instructions但是当我输入 adb forward tcp:4444 localabstract:/adb-hub;
我正在测试一些 dynamodb 访问代码。在过去,对分页的不正确处理导致了错误(开发人员倾向于使用少量数据进行手动测试,因此很容易对分页的工作原理做出错误的假设,只有在处理实际数据量时才会出现这种假
在我的代码中的某处,lambda 被调用以返回 true/false 响应。我试图在我的单元测试中模拟这个 lambda 但没有成功。 这是我的代码: def _test_update_allowed
我正在尝试用 moto 模拟 AWS SQS,下面是我的代码 from myClass import get_msg_from_sqs from moto import mock_sqs #from
在我的单元测试中: def test_my_function_that_publishes_to_sns(): conn = boto3.client("sns", region_name="
我正在尝试模拟计算环境的创建,这需要一些其他资源,即 IAM 实例配置文件和服务角色。但是,当我创建这些 IAM 资源然后尝试在计算环境创建中使用它们时,事情失败了: Role arn:aws:iam
我想对从 aws 导入的文件进行测试。 我使用 moto 模拟 s3,以免弄乱实际数据。但是,现在 aws 似乎是空的,因此我决定在模拟的 s3 上上传一些测试文件。我该怎么做? 这是我的设置, 比赛
我是一名优秀的程序员,十分优秀!