https://pulumi.com logo
#getting-started
Title
# getting-started
f

flaky-school-82490

06/28/2021, 2:28 PM
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

billowy-army-68599

06/28/2021, 3:07 PM
Sorry I’m not very good with dotnet. @tall-librarian-49374 @brave-planet-10645 maybe?
t

tall-librarian-49374

06/28/2021, 3:16 PM
ListStorageAccountKeys returns a result with a property
keys
so that’s what you need to return as an output.
f

flaky-school-82490

06/28/2021, 3:28 PM
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

tall-librarian-49374

06/28/2021, 3:31 PM
Oh, sorry, I misread your message. But the resource doesn’t return any keys, the invoke is required to get them.
f

flaky-school-82490

06/28/2021, 3:39 PM
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

tall-librarian-49374

06/28/2021, 3:42 PM
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

flaky-school-82490

06/28/2021, 4:37 PM
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

tall-librarian-49374

06/28/2021, 4:43 PM
Right. So your mock should return mock data for that function in
CallAsync
f

flaky-school-82490

06/28/2021, 5:30 PM
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

tall-librarian-49374

06/28/2021, 5:51 PM
“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

flaky-school-82490

06/29/2021, 7:26 AM
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

tall-librarian-49374

06/29/2021, 7:36 AM
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

flaky-school-82490

06/29/2021, 5:51 PM
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

tall-librarian-49374

06/29/2021, 6:54 PM
f

flaky-school-82490

06/29/2021, 7:29 PM
I see thanks. I ended up just removing that test then. Here is the PR: https://github.com/pulumi/examples/pull/1035.
3 Views