Hi,
I am want to write tests for the provider that I created. When reading the documentation and the scaffold code for a provider, I understand that for testing it is assumed that a real backend/external API should be used.
On the one end I understand that this will give good and reliable results. However, I am more inclined to mock those external APIs. I think it should be possible but it does not look straightforward to me yet. I spent quite some time reading the source code but I came to a point that I would rather ask then going over more lines of source code, verious provider api’s, versions etc.
I am using terraform-plugin-framework as recommended for new plugins.
The provider is initialized with an API key for the backend service. In the provider’s configure method, a client object that has access to the backend is sent in the provider.ConfigureResponse in the DataResourceData and ResourceData properties.
The resources read the provider data when their Configure method is called and the the Read/Delete/Update etc methods can then acccess the external resources.
I set this up following the various comments section in the scaffolding repo. This was very useful and I got it up and running quite fast.
For testing I want to mock the client that has access to the external API. I would think that I should get my mock client injected in the provider in some way. Where do I do that?
I have see that the TestCase object has a property called
ProviderFactories map[string]func() (*schema.Provider, error)
Of course I can add a constructor function in here, I cannot find how to populate any ResourceData or DataResourceData objects.
Is the idea that this should be done in through the ConfigureContextFunc property of schema.Provider?
The type definition is
type ConfigureContextFunc func(context.Context, *ResourceData) (interface{}, diag.Diagnostics)
So should I change *ResourceData here and assume that resources will pick up this object when they get configured?
if so then that would be nice to know. But How can I do the same thing for injecting DataResourceData?
If I am completely on the wrong track, I would welcome you gives some guidelines how to inject the mock client object.