我正在寫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