我正在写boto3
函数的一些测试和使用moto
库来模拟boto3
。嘲笑boto3调用实际boto3
他们提供的例子是这样的:
import boto3
from moto import mock_ec2
def add_servers(ami_id, count):
client = boto3.client('ec2', region_name='us-west-1')
client.run_instances(ImageId=ami_id, MinCount=count, MaxCount=count)
@mock_ec2
def test_add_servers():
add_servers('ami-1234abcd', 2)
client = boto3.client('ec2', region_name='us-west-1')
instances = client.describe_instances()['Reservations'][0]['Instances']
assert len(instances) == 2
instance1 = instances[0]
assert instance1['ImageId'] == 'ami-1234abcd'
然而,当我尝试类似的东西,在这里使用一个简单的例子,这样做:
def start_instance(instance_id):
client = boto3.client('ec2')
client.start_instances(InstanceIds=[instance_id])
@mock_ec2
def test_start_instance():
start_instance('abc123')
client = boto3.client('ec2')
instances = client.describe_instances()
print instances
test_start_instance()
ClientError: An error occurred (InvalidInstanceID.NotFound) when calling the StartInstances operation: The instance ID '[u'abc123']' does not exist
为什么它实际上使请求AWS,当我清楚地具有包装在模拟器中的功能?
如果你需要一个真正的模拟S3服务,让你临时存储文件来测试S3的应用程序功能,那么还要研究像FakeS3这样的东西。 – mootmoot
我正在做更多的EC2工作,但我会牢记它。我看到的另一个很酷的库是“安慰剂”,它记录了您对AWS的实际调用,并将结果存储在一个目录中,然后可以调用该目录来进行测试。 – eagle
我通常会测试部署脚本以在t2.nano/micro实例上部署东西。我只是用实际实例替换t2实例来生产 – mootmoot