Azure ARM模板单元测试 [英] Azure ARM Template Unit Test
问题描述
如何测试Azure ARM模板并验证它们是否从本地VM正确编写. 我已经从Power Shell进行了尝试,但仅进行了验证.我要对ARM模板进行单元测试
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
推荐答案
您可以使用 PESTER 对ARM模板进行单元测试.如果您不了解pester,可以参考本文档.
You can do unit test the ARM Templates with PESTER. If you are unfamiliar with pester, you can refer to this document.
示例ARM模板
正在测试的示例模板允许选择是将托管磁盘还是非托管磁盘用于VM.可以在这里找到模板 https://github.com/bentaylorwork/azure-arm-templates/tree/master/disk-management-selection .
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.
例速测试
下面的Pester测试将根据用户输入的虚拟机磁盘是基于托管磁盘还是非托管磁盘来检查是否部署了正确的磁盘类型.该文件可以在这里找到:
https://github .com/bentaylorwork/azure-arm-templates/blob/master/disk-management-selection/tests/unit.tests.ps1 ,您可以将其保存为本地计算机上的test.ps1
文件.
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.
运行测试
注意:博客的脚本存在未定义$parameterHash
的错误,因此,您可以使用我的以下脚本执行:
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
成功测试的示例输出
您可以在此博客.
此外,我还建议使用一种检查ARM模板的工具: Azure ARM模板检查器.这是一种快速而肮脏的工具,用于检查模板中使用的所有参数或变量是否都已定义.您可以在此链接中查看有关ARM模板检查器的更多详细信息.
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.
这篇关于Azure ARM模板单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!