diff --git a/aws-python/.gitignore b/aws-python/.gitignore index a3807e5b..5510ab51 100644 --- a/aws-python/.gitignore +++ b/aws-python/.gitignore @@ -1,2 +1,5 @@ *.pyc +__pycache__ +.pytest_cache venv/ +.venv/ diff --git a/aws-python/__main__.py b/aws-python/__main__.py index f94233fe..c934c2d4 100644 --- a/aws-python/__main__.py +++ b/aws-python/__main__.py @@ -1,10 +1,6 @@ -"""An AWS Python Pulumi program""" - import pulumi -from pulumi_aws import s3 -# Create an AWS resource (S3 Bucket) -bucket = s3.BucketV2('my-bucket') +import infra -# Export the name of the bucket -pulumi.export('bucket_name', bucket.id) +# Export the name of the bucket. +pulumi.export("bucket_name", infra.bucket.id) diff --git a/aws-python/infra.py b/aws-python/infra.py new file mode 100644 index 00000000..8e41e09e --- /dev/null +++ b/aws-python/infra.py @@ -0,0 +1,9 @@ +import pulumi +from pulumi_aws import s3 + +# Create an AWS resource (S3 Bucket) with tags. +bucket = s3.BucketV2("my-bucket", + tags={ + "Name": "My bucket", + }, +) diff --git a/aws-python/requirements.txt b/aws-python/requirements.txt index 72aee791..f2124ea1 100644 --- a/aws-python/requirements.txt +++ b/aws-python/requirements.txt @@ -1,2 +1,3 @@ pulumi>=3.0.0,<4.0.0 pulumi-aws>=6.0.2,<7.0.0 +pytest diff --git a/aws-python/tests/__init__.py b/aws-python/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/aws-python/tests/test_infra.py b/aws-python/tests/test_infra.py new file mode 100644 index 00000000..0489a44a --- /dev/null +++ b/aws-python/tests/test_infra.py @@ -0,0 +1,42 @@ +import asyncio +from typing import Awaitable, TypeVar + +import pulumi + + +# Test helper to convert a Pulumi Output to an Awaitable. +# This should only be used in tests. +T = TypeVar("T") +def awaitable(output: pulumi.Output[T]) -> Awaitable[T]: + loop = asyncio.get_running_loop() + future = loop.create_future() + output.apply(lambda x: future.set_result(x)) + return future + + +class MyMocks(pulumi.runtime.Mocks): + # Mock calls to create new resources and return a canned response. + def new_resource(self, args: pulumi.runtime.MockResourceArgs): + # Here, we're returning a same-shaped object for all resource types. + # We could, however, use the arguments passed into this function to + # customize the mocked-out properties of a particular resource. + # See the unit-testing docs for details: + # https://www.pulumi.com/docs/iac/concepts/testing/unit/ + return [args.name + "_id", args.inputs] + + # Mock function calls and return an empty response. + def call(self, args: pulumi.runtime.MockCallArgs): + return {} + +# Put Pulumi in unit-test mode, mocking all calls to cloud-provider APIs. +pulumi.runtime.set_mocks(MyMocks()) + +# Now import the code that creates resources, and then test it. +import infra + +# Example test. To run, uncomment and run `python -m pytest --disable-pytest-warnings`. +# @pulumi.runtime.test +# async def test_bucket_tags(): +# tags = await awaitable(infra.bucket.tags) +# assert tags, "bucket must have tags" +# assert "Name" in tags, "bucket must have a Name tag"