10
votes

How to test Azure ARM Templates and validate them whether these are written correctly or not from local VM. I have tried it from Power Shell but it just validate only. I want to Unit Test the ARM templates

1
Did you use this powershell cmdlet to test ? Test-AzureRmResourceGroupDeploymentWayne Yang

1 Answers

15
votes

You can do unit test the ARM Templates with PESTER. If you are unfamiliar with pester, you can refer to this document.

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. The file can be found here: https://github.com/bentaylorwork/azure-arm-templates/blob/master/disk-management-selection/tests/unit.tests.ps1 You can save it to your local machine as test.ps1 file.

Running The Test

NOTE: The blog's script has an error with not defined $parameterHash, So , you can use my following scripts to execute:

<# 
    Steps to run:
    1) Login to Azure
    2) Select correct subscription
    3) Alter the path below to where you have the have saved the pester test locally
#>

$pesterParamters = @{
    Path       = 'C:\Users\Administrator\Desktop\test.ps1'
    Parameters = @{
                        templateUri                 = 'https://raw.githubusercontent.com/bentaylorwork/azure-arm-templates/master/disk-management-selection/azuredeploy.json'
                        templateParameterObject     = @{
                            resourcePrefix = 'pester'
                            adminPassword  = 'SuperSecurePlainTextPassword123!!'
                        }
                  }
}

$parameterHash= @{
                            resourcePrefix = 'pester'
                            adminPassword  = 'SuperSecurePlainTextPassword123!!'
                        }

Invoke-Pester -Script $pesterParamters

Example Output From A Successful Test

enter image description here

You can see more details about Unit testing conditions in ARM templates with pester in this blog.

Additionally, I also recommend a tool to check ARM templates: Azure ARM templates checker. It is a quick and dirty tool to check if all parameters or variables used in the template have been defined. You can see more details about ARM templates checker in this link.