https://pulumi.com logo
#aws
Title
# aws
f

freezing-van-87649

10/14/2021, 1:40 PM
Anyone have any tips/tricks for mocking API calls in tests (python, specifically)? I’m using iam.get_policy_document a couple places in code I’m trying to test and writing a manual mock for it feels wrong
g

great-sunset-355

10/14/2021, 2:11 PM
if nothing works, maybe
VCRpy
would work
l

little-cartoon-10569

10/14/2021, 6:43 PM
Do you need to fake it out? That one should be ok to call from unit tests, it's a self-contained function. No async calls.
f

freezing-van-87649

10/14/2021, 7:23 PM
Is there a way to selective mock?
I’m doing mocks similar to what’s shown here.
Copy code
import pulumi

class MyMocks(pulumi.runtime.Mocks):
    def new_resource(self, args: pulumi.runtime.MockResourceArgs):
        return [args.name + '_id', args.inputs]
    def call(self, args: pulumi.runtime.MockCallArgs):
        return {}

pulumi.runtime.set_mocks(MyMocks())
so there aren’t any actual HTTP calls that
VCRpy
would help with
and I don’t see a clear way to conditionally proxy calls through to the actual provider
unless I create a provider and call it within the mock?
l

little-cartoon-10569

10/14/2021, 7:51 PM
Does that method use the normal Pulumi technique of building up what's essentially an AST for resources and turning it into a diffable state? I thought that method is just a wrapper around JSON.stringify and similar.. could be wrong though.
If it's handled in the normal Pulumi way, I'd suggest that the easiest thing to do is to duplicate the code into your own codebase and call it from the
call()
method.
Let me have a look at the source.
Ah no, I'm thinking of
new PolicyDocument
. Ignore all that, sorry, I've completely mislead you.
However it is related: to fake this out (sorry, I won't use "mock", none of these things are true mocks and imo the work is misleading here), I would implement a cast to
PolicyDocument
in
call()
. Or maybe even throw an error and tell the user to stop using
getPolicyDocument()
since it's never necessary 🙂
Does the Python lib have the PolicyDocument type, like typescript does?
I've looked through the source, I can't see anything like it.
f

freezing-van-87649

10/14/2021, 8:13 PM
There’s a
GetPolicyDocumentResult
yeah, maybe I should just stop using that particular method and stick with vanilla dicts/lists
l

little-cartoon-10569

10/14/2021, 8:43 PM
Not GetPolicyDocumentResult, that's different. I was hoping you could use a nice feature in the typescript SDK that essentially does
getPolicyDocument()
inline, using some javacript magic. I've been looking for the code, since it would be feasible to duplicate it in the
call()
function and essentially re-implement
getPolicyDocument()
locally, skipping the AWS SDK.
Komal Ali pointed me at the code a few months ago, but I can't find it now 😞
f

freezing-van-87649

10/14/2021, 9:09 PM
Ideally I’d probably be able to just proxy the call through to the real provider, but it’s not a huge deal to work around
l

little-cartoon-10569

10/14/2021, 9:21 PM
Since that call would involve invoking the TF engine (assuming you're not using aws-native), that would be async and (compared to everything else in the unit test) slow. It would be better to reimplement, imo.
👍 1
And since it's just a JSON formatting exercise, I think it should be pretty easy to implement.
Unless you need to assert on the content of the doc, then you probably don't even have to format the JSON. Just return "{}" and leave it at that.
f

freezing-van-87649

10/14/2021, 9:30 PM
Yeah, I’m checking the content. For condition
X
, policy should be read only. For condition
Y
, policy should be read/write.