Skip to main content

The potemkin decorator facilitates "integration testing" boto code by creating AWS resources using Cloudformation or Terraform. This provides a convenient way to setup initial conditions instead of having to develop boto code that is likely as complex as the "code under test".

Basic Usage

CloudFormation

Here is an example CloudFormation invocation from pytest:

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'

The CloudFormationStack creates the stack and binds the outputs to stack_outputs. The pytest method could invoke more boto code to manipulate the resources created by the decorator. In this case, the test just asserts that the initial condition is what is expected.

This is basically a python/pytest port of "aws-int-test-rspec-helper" that worked with Ruby/RSpec:

Terraform

Here is an example Terraform invocation from pytest:

import potemkin
import boto3


@potemkin.TerraformResources(
  'test/integration/test_templates/terraform',
  parameters={'BucketName': 'unclefreddie33388'},
  aws_profile='myprofile',
  teardown=False
)
def test_bucket_has_aes256_encryption(tf_outputs):
  full_bucket_name = tf_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'

TerraformResources creates the resources and binds the outputs to tf_outputs. The pytest method could invoke more boto code to manipulate the resources created by the decorator. In this case, the test just asserts that the initial condition is what is expected.

Service Specific Usage

The potemkin decorator has additional functions for interacting with specific AWS services

AWS Config

AWS Config initiates evaluations when a resource is created, but the evaluations are completed asynchronously. They can take several minutes to complete. The AWS config functions wait until the config rule has an evaluation for the resource, then returns the evaluation.

config_rule_wait_for_compliance_results

This function polls aws config until all resource_ids have evaluations. It then checks those evaluations against expected results and returns a truthy value. This can be used by both configuration change events and periodic events (by setting 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)

config_rule_wait_for_absent_resources

This function is a companion to config_rule_wait_for_compliance_results and is used to validate that once resources are deleted they are removed from AWS config.

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)

config_rule_wait_for_resource

This function polls aws config until there is an evaluation for the resource, then returns it. Use this function for config rules with a configuration change trigger. If you are checking more than one resource, consider using config_rule_wait_for_compliance_results.

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'

evaluate_config_rule_and_wait_for_resource

This is similar to config_rule_wait_for_resource but it first initiates a config evaluation. Use this for config rules with a periodic trigger. If you are checking more than one resource, consider using config_rule_wait_for_compliance_results.

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'

Metadata

Release files for potemkin-decorator 0.0.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for potemkin-decorator 0.0.13
File Size Uploaded
potemkin-decorator-0.0.13.tar.gz 9.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for potemkin-decorator 0.0.13
File Interpreter ABI Platform
potemkin_decorator-0.0.13-py3-none-any.whl Python 3 none any Details

Total release size: 20.6 kB

Release files / potemkin-decorator-0.0.13.tar.gz

Download URL potemkin-decorator-0.0.13.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
40e3044abd86aae5ae5aed2f034e91d56be1bae5984ed80737cc2c2df674baec
BLAKE2b-256 checksum
How to use checksums
790b6228b04880a05ce8712d21f0ede45b5f01706e3479fa5dcdd70ba074ad7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

Release files / potemkin_decorator-0.0.13-py3-none-any.whl

Download URL potemkin_decorator-0.0.13-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aa096b70ea145d5ab6aa5768bf99b4d5e3fe7a81694c6e60b4d6667022811459
BLAKE2b-256 checksum
How to use checksums
3f3ba631069ca96621cb938977f46d1499af15860b65ab08ae1d488e224386ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

Release history Release notifications | RSS feed

This release

0.0.13 This release

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page