Anyone have any tips/tricks for mocking API calls ...
# aws
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
if nothing works, maybe
would work
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.
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 [ + '_id', args.inputs]
    def call(self, args: pulumi.runtime.MockCallArgs):
        return {}

so there aren’t any actual HTTP calls that
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?
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
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
. Or maybe even throw an error and tell the user to stop using
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.
There’s a
yeah, maybe I should just stop using that particular method and stick with vanilla dicts/lists
Not GetPolicyDocumentResult, that's different. I was hoping you could use a nice feature in the typescript SDK that essentially does
inline, using some javacript magic. I've been looking for the code, since it would be feasible to duplicate it in the
function and essentially re-implement
locally, skipping the AWS SDK.
Komal Ali pointed me at the code a few months ago, but I can't find it now 😞
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
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.
Yeah, I’m checking the content. For condition
, policy should be read only. For condition
, policy should be read/write.