Skip to main content

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 is using Pulumi Automation API and Python Unittest to simulate integration test in Pulumi without native python integration library.

UnitTestIntegration TestTarget
YESYESInput
NoYESOutput

Test Case#

To create a 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

Precondition#

You need to create the credential 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#

pip install -r requirements.txt
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)
...

Further steps#

Learn more about testing Pulumi programs and Automation API:

Related

The infrastructure as code platform for any cloud.