Unit Testing Conditions In Azure Resource Manager (ARM) Templates With Pester

When testing code with “if” statements you will want to test the different code paths to see if they produce the expected results. With the introduction of conditions in Azure Resource Manager (ARM) templates, it also introduces the need to test if you get the required Azure resources based on the condition.

A great blog on ARM template conditions can be found here if you need some background https://azure.microsoft.com/en-gb/blog/create-flexible-arm-templates-using-conditions-and-logical-functions/.

To test an ARM template you must to find out how Azure will interpret the template and what resources will be deployed from it. This can be done from capturing the debug output of Test-AzureRmResourceGroupDeployment, there can be issues with this CmdLet not picking up on certain things. The idea for getting what resources would be deployed without actually deploying them came from here marcvaneijk/AzureResourceManager on GitHub. I shortened the code up slightly to make it a little more readable which is below.

The output of the above shows what resources Azure would provision if we were to deploy a template. See below.

test-azurermtemplate-debug-output

From this we can then create tests to check if the correct resources are provisioned based on the condition.

Example ARM Template

The example template being tested allows for the selection of whether managed or un-managed disk are used for the VM. The template can be found here https://github.com/bentaylorwork/azure-arm-templates/tree/master/disk-management-selection.

Example Pester Test

The Pester test below will check if the correct disk types are being deployed based on user input of whether the vm’s disks should be based on managed or un-managed disks.

Running The Test

Example Output From A Successful Test

successfulPestertest

This is a work in progress and there are things that can be improved on etc. Any feedback would be more than welcome.

About the author

Ben Taylor

Cloud and PowerShell enthusiast with a penchant for automation and CI.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *