Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

Integration testing of Pulumi programs in Python

A Integration Testing of s3 use case

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set testing-integration-py
cd pulumi-examples/testing-integration-py

This integration test uses the Pulumi Automation API and Python unittest to simulate an integration test in Pulumi without a native Python integration library.

UnitTestIntegration TestTarget
YESYESInput
NoYESOutput

Test case#

To create an S3 bucket and upload a file:

  1. Create the stack with a bucket
  2. Verify S3 bucket name and region in Output
  3. Using client to check the S3 name again
  4. Using client to upload a file
  5. Deleting that file
  6. Delete the stack with the bucket

Prerequisites#

  1. Install Pulumi
  2. Configure AWS credentials
  3. Install Python

You need to create the credentials file yourself. By default, its location is at ~/.aws/credentials. Your access key needs the correct permissions for S3 bucket and object creation/deletion.

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

Running the tests#

  1. Create a Python virtualenv, activate it, and install dependencies:

    Terminal window
    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
  2. Run the tests:

    Terminal window
    python -m unittest test_s3_it.py

Test life cycle#

  • Create a stack and export the desired outputs.
  • Validate any output values you defined in advance.
  • In the end, don’t forget to destroy the stack.
from pulumi import automation as auto
class TestS3(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
....
cls.stack = auto.create_or_select_stack(stack_name=cls.STACK_NAME, work_dir=cls.WORK_DIR)
cls.stack.up(output=print)
cls.outputs = cls.stack.outputs()
...
@classmethod
def tearDownClass(cls) -> None:
cls.stack.destroy(on_output=print)
cls.stack.workspace.remove_stack(cls.STACK_NAME)
def test_s3_output_case(self):
...
bucket_region = self.outputs.get(OUTPUT_KEY_REGION)
self.assertEqual(self.REGION_NAME, bucket_region.value)
...

Learn more#

Learn more about testing Pulumi programs and the Automation API:

Related

The infrastructure as code platform for any cloud.