Trying to write a Unit Test for a storage account....
# getting-started
f
Trying to write a Unit Test for a storage account. Mocks very much inspired by this tutorial. I am stuck on mocking the account's keys so that Pulumi.AzureNative.Storage.ListStorageAccountKeys returns mocked values. I tried wild-guessing the account api so I could set fake values, to no avail. This is what I naively tried adding to NewResourceAsync:
Copy code
// Properties normally created azure-side
if (args.Type == "azure-native:storage:StorageAccount") 
{
    // primary keys
    outputs.Add("listStorageAccountKeys:0:keyName", "key1");
    outputs.Add("listStorageAccountKeys:0:permissions", "FULL");
    outputs.Add("listStorageAccountKeys:0:value", "SomeMockValue12345==");
}
@billowy-army-68599 Any thoughts on the correct output element formatting here?
b
Sorry I’m not very good with dotnet. @tall-librarian-49374 @brave-planet-10645 maybe?
t
ListStorageAccountKeys returns a result with a property
keys
so that’s what you need to return as an output.
f
Thanks @tall-librarian-49374, but I am not trying to mock ListStorageAccountKeys, but the resource itself. Any ideas on what key/values I should add to the ImmutableDictionary<string, object> to mock the storage account keys?
t
Oh, sorry, I misread your message. But the resource doesn’t return any keys, the invoke is required to get them.
f
My bad I should have been more explicit: I am basically trying to reproduce what you did here: https://github.com/pulumi/examples/blob/72c9480f4c1240f795f6020f50801733fbef37f2/testing-unit-cs-mocks/Testing.cs#L36 but for the account storage keys instead of the primaryWebEndpoint.
t
primaryWebEndpoint
is an output property of that resource, while your resource has no output property
keys
or similar. Do you have a Pulumi program that you are trying to test with mocks? It would have to have an invocation of the function to retrieve credentials.
f
Yes, a simple Pulumi program that creates a new StorageAccount and assigns the account primary key value to an output, using ListStorageAccountKeys. Basically the Azure Get Started project: https://www.pulumi.com/docs/get-started/azure/review-project/
t
Right. So your mock should return mock data for that function in
CallAsync
f
Right I see thanks. But I'll still need to somehow know how to serialize the expected return type as a key/value output.
Copy code
if (args.Token == "azure-native:storage:listStorageAccountKeys")
{
    output.Add("whatgoeshere?", "key1");
    output.Add("whatgoeshere?", "FULL");
    output.Add("whatgoeshere?", "SomeMockValue12345==");
}
return Task.FromResult((object)args.Args);
Or am I missing something else?
t
“whatgoeshere” should be “keys”
and then an array of dictionaries as the value
property names are shown in the docs for the function
Use them in lowerCamelCase (as in TypeScript)
f
Thank you so much @tall-librarian-49374! That's what I was missing. For completeness, this is the final snippet I added to CallAsync:
Copy code
if (args.Token == "azure-native:storage:listStorageAccountKeys")
{
    outputs.Add("keys", new[]
    {
        new Dictionary<string, string>
        {
            { "creationTime", DateTime.UtcNow.ToString("O")},
            { "keyName", "key1" },
            { "permissions", "FULL" },
            { "value", "MockPrimaryKey12345==" },
        },
        new Dictionary<string, string>
        {
            { "creationTime", DateTime.UtcNow.ToString("O")},
            { "keyName", "key2" },
            { "permissions", "FULL" },
            { "value", "MockSecondaryKey12345==" },
         }
    });
}
🎉 1
t
500 bonus points if you open a PR to the examples repo to update https://github.com/pulumi/examples/blob/master/testing-unit-cs-mocks/Testing.cs to Azure-Native 😉
f
500! @tall-librarian-49374 I'm about to create that PR, but I'm stuck on trying to get resource group name from a storage account instance:
t
f
I see thanks. I ended up just removing that test then. Here is the PR: https://github.com/pulumi/examples/pull/1035.