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 is using Pulumi Automation API and Python Unittest to simulate integration test in Pulumi without native python integration library.
| UnitTest | Integration Test | Target |
|---|---|---|
| YES | YES | Input |
| No | YES | Output |
Test Case#
To create a 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
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_KEYaws_secret_access_key = YOUR_SECRET_KEYRunning the tests#
pip install -r requirements.txtpython -m unittest test_s3_it.pyTest 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: