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:
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examplesgit -C pulumi-examples sparse-checkout set testing-integration-pycd pulumi-examples/testing-integration-pyThis integration test uses the Pulumi Automation API and Python unittest to simulate an integration test in Pulumi without a native Python integration library.
| UnitTest | Integration Test | Target |
|---|---|---|
| YES | YES | Input |
| No | YES | Output |
Test case#
To create an S3 bucket and upload a file:
- Create the stack with a bucket
- Verify S3 bucket name and region in Output
- Using client to check the S3 name again
- Using client to upload a file
- Deleting that file
- Delete the stack with the bucket
Prerequisites#
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_KEYaws_secret_access_key = YOUR_SECRET_KEYRunning the tests#
-
Create a Python virtualenv, activate it, and install dependencies:
Terminal window python3 -m venv venvsource venv/bin/activatepip install -r requirements.txt -
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: