Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure ARM Template Unit Test

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

like image 732
Sachidanand Sharma Avatar asked Nov 14 '17 05:11

Sachidanand Sharma


People also ask

What is the difference between Azure blueprints and ARM templates?

A Template is the basic model from which each Server gets created. A Blueprint is a saved workflow that can be defined and re-played at any time on the platform.


1 Answers

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.

like image 125
Wayne Yang Avatar answered Nov 08 '22 10:11

Wayne Yang