允许python测试轻松模拟boto库的库
moto的Python项目详细描述
MOTO-模拟AWS服务
一言以蔽之
moto是一个允许您的测试轻松模拟aws服务的库。
假设您有以下要测试的python代码:
importboto3classMyModel(object):def__init__(self,name,value):self.name=nameself.value=valuedefsave(self):s3=boto3.client('s3',region_name='us-east-1')s3.put_object(Bucket='mybucket',Key=self.name,Body=self.value)
花点时间想想你过去是怎么测试的。
现在看看如何用Moto测试它:
importboto3frommotoimportmock_s3frommymoduleimportMyModel@mock_s3deftest_my_model_save():conn=boto3.resource('s3',region_name='us-east-1')# We need to create the bucket since this is all in Moto's 'virtual' AWS accountconn.create_bucket(Bucket='mybucket')model_instance=MyModel('steve','is awesome')model_instance.save()body=conn.Object('mybucket','steve').get()['Body'].read().decode("utf-8")assertbody=='is awesome'
随着decorator包装测试,对s3的所有调用都将自动模拟出来模拟保存桶和键的状态。
它变得更好了!moto不只是针对python代码,也不只是针对s3。有关使用其他语言运行Moto的更多信息,请查看standalone server mode下面是已实现的其他aws服务的状态:
|-------------------------------------------------------------------------------------|| Service Name | Decorator | Development Status ||-------------------------------------------------------------------------------------|| ACM | @mock_acm | all endpoints done ||-------------------------------------------------------------------------------------|| API Gateway | @mock_apigateway | core endpoints done ||-------------------------------------------------------------------------------------|| Autoscaling | @mock_autoscaling | core endpoints done ||-------------------------------------------------------------------------------------|| Cloudformation | @mock_cloudformation | core endpoints done ||-------------------------------------------------------------------------------------|| Cloudwatch | @mock_cloudwatch | basic endpoints done ||-------------------------------------------------------------------------------------|| CloudwatchEvents | @mock_events | all endpoints done ||-------------------------------------------------------------------------------------|| Cognito Identity | @mock_cognitoidentity | basic endpoints done ||-------------------------------------------------------------------------------------|| Cognito Identity Provider | @mock_cognitoidp | basic endpoints done ||-------------------------------------------------------------------------------------|| Config | @mock_config | basic endpoints done ||-------------------------------------------------------------------------------------|| Data Pipeline | @mock_datapipeline | basic endpoints done ||-------------------------------------------------------------------------------------|| DynamoDB | @mock_dynamodb | core endpoints done || DynamoDB2 | @mock_dynamodb2 | all endpoints + partial indexes ||-------------------------------------------------------------------------------------|| EC2 | @mock_ec2 | core endpoints done || - AMI | | core endpoints done || - EBS | | core endpoints done || - Instances | | all endpoints done || - Security Groups | | core endpoints done || - Tags | | all endpoints done ||-------------------------------------------------------------------------------------|| ECR | @mock_ecr | basic endpoints done ||-------------------------------------------------------------------------------------|| ECS | @mock_ecs | basic endpoints done ||-------------------------------------------------------------------------------------|| ELB | @mock_elb | core endpoints done ||-------------------------------------------------------------------------------------|| ELBv2 | @mock_elbv2 | all endpoints done ||-------------------------------------------------------------------------------------|| EMR | @mock_emr | core endpoints done ||-------------------------------------------------------------------------------------|| Glacier | @mock_glacier | core endpoints done ||-------------------------------------------------------------------------------------|| IAM | @mock_iam | core endpoints done ||-------------------------------------------------------------------------------------|| IoT | @mock_iot | core endpoints done || | @mock_iotdata | core endpoints done ||-------------------------------------------------------------------------------------|| Kinesis | @mock_kinesis | core endpoints done ||-------------------------------------------------------------------------------------|| KMS | @mock_kms | basic endpoints done ||-------------------------------------------------------------------------------------|| Lambda | @mock_lambda | basic endpoints done, requires || | | docker ||-------------------------------------------------------------------------------------|| Logs | @mock_logs | basic endpoints done ||-------------------------------------------------------------------------------------|| Organizations | @mock_organizations | some core endpoints done ||-------------------------------------------------------------------------------------|| Polly | @mock_polly | all endpoints done ||-------------------------------------------------------------------------------------|| RDS | @mock_rds | core endpoints done ||-------------------------------------------------------------------------------------|| RDS2 | @mock_rds2 | core endpoints done ||-------------------------------------------------------------------------------------|| Redshift | @mock_redshift | core endpoints done ||-------------------------------------------------------------------------------------|| Route53 | @mock_route53 | core endpoints done ||-------------------------------------------------------------------------------------|| S3 | @mock_s3 | core endpoints done ||-------------------------------------------------------------------------------------|| SecretsManager | @mock_secretsmanager | basic endpoints done ||-------------------------------------------------------------------------------------|| SES | @mock_ses | all endpoints done ||-------------------------------------------------------------------------------------|| SNS | @mock_sns | all endpoints done ||-------------------------------------------------------------------------------------|| SQS | @mock_sqs | core endpoints done ||-------------------------------------------------------------------------------------|| SSM | @mock_ssm | core endpoints done ||-------------------------------------------------------------------------------------|| STS | @mock_sts | core endpoints done ||-------------------------------------------------------------------------------------|| SWF | @mock_swf | basic endpoints done ||-------------------------------------------------------------------------------------|| X-Ray | @mock_xray | all endpoints done ||-------------------------------------------------------------------------------------|
完整的端点列表implementation coverage
另一个例子
假设您有一个用于启动新ec2实例的函数:
importboto3defadd_servers(ami_id,count):client=boto3.client('ec2',region_name='us-west-1')client.run_instances(ImageId=ami_id,MinCount=count,MaxCount=count)
要测试它:
from.importadd_serversfrommotoimportmock_ec2@mock_ec2deftest_add_servers():add_servers('ami-1234abcd',2)client=boto3.client('ec2',region_name='us-west-1')instances=client.describe_instances()['Reservations'][0]['Instances']assertlen(instances)==2instance1=instances[0]assertinstance1['ImageId']=='ami-1234abcd'
使用moto 1.0.X和boto2
moto 1.0.X模拟装饰器是为boto3定义的,不能与boto2一起使用使用@mock_awssvc_deprecated来处理boto2。
使用moto和boto2
frommotoimportmock_ec2_deprecatedimportboto@mock_ec2_deprecateddeftest_something_with_ec2():ec2_conn=boto.ec2.connect_to_region('us-east-1')ec2_conn.get_only_instances(instance_ids='i-123456')
同时使用boto2和boto3时,可以这样做以避免混淆:
frommotoimportmock_ec2_deprecatedasmock_ec2_b2frommotoimportmock_ec2
用法
所有服务都可以用作装饰器、上下文管理器或原始形式
装潢师
@mock_s3deftest_my_model_save():# Create Bucket so that test can runconn=boto3.resource('s3',region_name='us-east-1')conn.create_bucket(Bucket='mybucket')model_instance=MyModel('steve','is awesome')model_instance.save()body=conn.Object('mybucket','steve').get()['Body'].read().decode()assertbody=='is awesome'
上下文管理器
deftest_my_model_save():withmock_s3():conn=boto3.resource('s3',region_name='us-east-1')conn.create_bucket(Bucket='mybucket')model_instance=MyModel('steve','is awesome')model_instance.save()body=conn.Object('mybucket','steve').get()['Body'].read().decode()assertbody=='is awesome'
原始使用
deftest_my_model_save():mock=mock_s3()mock.start()conn=boto3.resource('s3',region_name='us-east-1')conn.create_bucket(Bucket='mybucket')model_instance=MyModel('steve','is awesome')model_instance.save()assertconn.Object('mybucket','steve').get()['Body'].read().decode()=='is awesome'mock.stop()
独立服务器模式
moto还有一个独立的服务器模式。这样你就可以利用 moto的后端结构,即使您不使用python。
它使用flask,这不是默认依赖项。您可以安装 服务器“额外”包,带有:
pipinstall"moto[server]"
然后可以运行服务启动它:
$ moto_server ec2 * Running on http://127.0.0.1:5000/
您也可以通过端口:
$ moto_server ec2 -p3000 * Running on http://127.0.0.1:3000/
如果你想在外部使用服务器,你可以通过一个IP 作为主机名绑定到的地址或允许任何外部 与0.0.0.0的接口:
$ moto_server ec2 -H 0.0.0.0 * Running on http://0.0.0.0:5000/
请注意,这可能允许其他网络用户访问 服务器。
然后转到localhost查看正在运行的实例列表(它将是空的,因为您还没有添加任何实例)。
如果您想将boto与此结合使用(强烈建议您改用上面更简单的装饰符),最简单的方法是创建具有以下值的boto配置文件(~/.boto
):
[Boto]
is_secure = False
https_validate_certificates = False
proxy_port = 5000
proxy = 127.0.0.1
如果要将boto3与此一起使用,可以将endpoint_url
传递给资源
boto3.resource(service_name='s3',region_name='us-west-1',endpoint_url='http://localhost:5000',)
安装
$ pip install moto
释放量
释放是从travisci完成的。紧随其后: https://docs.travis-ci.com/user/deployment/pypi/
- 提交到
master
分支将开发部署到pypi。 - 对标记的提交对pypi进行真正的部署。