Anyone have experience writing python tests for pu...
# python
Anyone have experience writing python tests for pulumi? Read @big-piano-35669ā€™s blog post and trying to kick the tires on this. Is there an example of the tests from that blog but written in Python?
I am looking for the equivalent of this in the python lib: but I'm not seeing it. Is testing not supported in Python? cc @gentle-diamond-70147
i was asking the same several weeks ago (how to test python pulumi stuff). no answer
but in my mind its quite important
I'm paraphrasing from an email where Joe answered this separately, but... This is doable, but takes a few more settings to work within Python. We'll open an issue to make this more "first class
From Joe:
Copy code
I did a slight refactoring of the AWS WebServer example at <>, and wrote a little test below that checks to ensure we don't have unrestricted CIDR blocks on security groups. A few things to note: (1) the project structure needs to change a little rather than declaring everything in to facilitate importing, (2) we call a few pulumi.runtime.settings._set_x(..) helpers in our test before allocating anything, and (3) slightly awkwardly, I had to do the import inside of the test case itself, to avoid promises being allocated on a separate event loop than the test is running on.
šŸ‘ 1
Copy code
import asyncio
import unittest

# Import the Pulumi SDK and turn on test mode *before* allocating anything.
import pulumi

## Tests come next:

def async_test(coro):
    def wrapper(*args, **kwargs):
        loop = asyncio.new_event_loop()
        loop.run_until_complete(coro(*args, **kwargs))
    return wrapper

class InstanceTests(unittest.TestCase):
    async def test_no_internet_access(self):
        # Now import the infrastructure module which will allocate our resource objects.
        from infra import group, server

        # Rendezvous with the resource's resulting ingress rules.
        ingress_fut = asyncio.Future()
        group.ingress.apply(lambda ig: ingress_fut.set_result(ig))
        ingress = await ingress_fut
        # Now loop through them and reject any that are open to the Internet (
        for rule in ingress:
            for cidr in rule['cidr_blocks']:
                self.assertNotEqual('', cidr)
šŸ‘ 1
Apologies for missing this folks, we will be sure to blog this in the coming week or two.
Hi Joe/Cameron, thanks for this! I'm able to use the pulumi.runtime.settings helpers to run an integration test. When I try to unpack the
output for a resource, e.g.
using the same async + apply strategy you've shown here, it comes back as
. Is that expected? In my test, I am checking that an S3 bucket name is what I set the name to. So in
Copy code
bucket = s3.Bucket('my-bucket')
I am trying to assert that
await id_future == 'my-bucket'
but the id is
. Am I missing something? cc @white-balloon-205 @bitter-oil-46081
I believe that the issue is that when we are running in testing mode, we only reflect back the set of input properties (i.e. what you would pass in the args dictionary to
. Since we can not actually run the resource providers (since there's no
invocation in the mix) we can't know what
it is going to set in general. Since there's no
input to the resource (normally the provider computes it based on the other inputs) it doesn't get a real output.