Decorator帮助在pytest中进行AWS/boto集成测试
potemkin-decorator的Python项目详细描述
potemkin decorator通过创建AWS资源简化了boto代码的“集成测试” 使用云层或地形。这为设置初始条件提供了一种方便的方法 而不是必须开发像“正在测试的代码”一样复杂的boto代码。在
基本用途
云形成
下面是从pytest调用CloudFormation的示例:
import potemkin
import boto3
@potemkin.CloudFormationStack(
'test/integration/test_templates/aes256_bucket.yml',
stack_name_stem='TestStack',
parameters={'BucketName': 'unclefreddie33388'},
aws_profile='myprofile',
teardown=False
)
def test_bucket_has_aes256_encryption(stack_outputs, stack_name):
full_bucket_name = stack_outputs['BucketNameOut']
s3 = boto3.Session(profile_name='myprofile').client('s3')
get_bucket_encryption_response = s3.get_bucket_encryption(
Bucket=full_bucket_name
)
assert get_bucket_encryption_response['ServerSideEncryptionConfiguration']['Rules'][0]['ApplyServerSideEncryptionByDefault']['SSEAlgorithm'] == 'AES256'
CloudFormationStack创建堆栈并将输出绑定到堆栈输出。pytest方法 可以调用更多的boto代码来操作装饰器创建的资源。在这种情况下, 测试只是断言初始条件是预期的。在
这基本上是与Ruby/rspec一起工作的“aws int test rspec helper”的python/pytest端口:
地形
以下是pytest表单调用的示例:
^{pr2}$TerraformResources创建资源并将输出绑定到tf_输出。pytest方法 可以调用更多的boto代码来操作装饰器创建的资源。在这种情况下, 测试只是断言初始条件是预期的。在
服务特定用途
potemkin decorator具有与特定AWS服务交互的附加功能
AWS配置
AWS配置在创建资源时启动评估,但评估已完成 异步地。它们可能需要几分钟才能完成。AWS配置函数将一直等到 配置规则对资源进行求值,然后返回求值。在
配置规则等待符合性结果结果
此函数将轮询aws配置,直到所有资源都有计算结果。然后检查这些评估 并返回一个真实值。这两种配置都可以使用 更改事件和周期性事件(通过设置evaluate=True)
@potemkin.CloudFormationStack('test/integration/test_templates/eip.yml',
stack_name_stem='EipTestStack')
def test_wait_for_compliance_results(stack_outputs, stack_name):
global expected_results
configservice = boto3.Session().client('config')
expected_results_success = {
stack_outputs['EIPOutput']: "NON_COMPLIANT",
stack_outputs['EIP2Output']: "NON_COMPLIANT"
}
assert config_rule_wait_for_compliance_results(
configservice,
rule_name='eip-attached',
expected_results=expected_results_success)
配置规则等待资源不在资源eh3>
此函数是config_rule_wait_for_compliance_results的助手,用于验证 一旦资源被删除,它们就会从AWS配置中删除。在
def test_wait_for_compliance_results_success_results():
configservice = boto3.Session().client('config')
resource_ids = list(expected_results.keys())
assert [] == config_rule_wait_for_absent_resources(
configservice, rule_name='eip-attached', resource_ids=resource_ids)
配置规则等待资源
此函数轮询aws config,直到对资源进行评估,然后返回它。用这个 具有配置更改触发器的配置规则的函数。如果您要检查多个 资源,考虑使用配置规则等待结果。在
import potemkin
import boto3
@potemkin.CloudFormationStack(
'test/integration/test_templates/aes256_bucket.yml',
stack_name_stem='TestStack',
parameters={'BucketName': 'unclefreddie33388'}
)
def test_bucket_encryption_rule(stack_outputs, stack_name):
configservice = boto3.Session().client('config')
results = config_rule_wait_for_resource(configservice,
resource_id='unclefreddie33388',
rule_name='config-rule-s3-encryption')
assert results['ComplianceType'] == 'NON_COMPLIANT'
评估配置规则并等待资源
这类似于config_rule_wait_resource,但它首先启动配置求值。用这个 对于具有周期触发器的配置规则。如果您要检查多个 资源,考虑使用配置规则等待结果。在
import potemkin
import boto3
@potemkin.CloudFormationStack(
'test/integration/test_templates/aes256_bucket.yml',
stack_name_stem='TestStack',
parameters={'BucketName': 'unclefreddie33388'}
)
def test_bucket_encryption_rule(stack_outputs, stack_name):
configservice = boto3.Session().client('config')
results = evaluate_config_rule_and_wait_for_resource(configservice,
resource_id='unclefreddie33388',
rule_name='config-rule-s3-encryption')
assert results['ComplianceType'] == 'NON_COMPLIANT'
- 项目
标签: