我正在为boto3
函数编写一些测试并使用moto
库来模拟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 发出请求?