Azure ARM Template Unit Test

二次信任 提交于 2019-11-30 14:21:04

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!