Azure ARM 模板单元测试

Azure ARM Template Unit Test

如何测试 Azure ARM 模板并验证它们是否从本地 VM 正确写入。 我已经从 Power Shell 尝试过它,但它只是验证而已。我想对 ARM 模板进行单元测试

您可以使用 PESTER 对 ARM 模板进行单元测试。不熟悉pester的可以参考this document.

示例 ARM 模板

正在测试的示例模板允许选择托管磁盘还是非托管磁盘用于 VM。模板可以在这里找到 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 文件保存到本地计算机。

运行 测试

注意: 博客的脚本有一个未定义的错误$parameterHash,所以,你可以使用我下面的脚本来执行:

<# 
    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

成功测试的示例输出

您可以在 this blog.

中查看有关带有​​ pester 的 ARM 模板中单元测试条件的更多详细信息

此外,我还推荐一个检查 ARM 模板的工具:Azure ARM 模板检查器。它是一个快速但肮脏的工具,用于检查模板中是否使用了所有参数或变量已被定义。您可以在 this link.

中查看有关 ARM 模板检查器的更多详细信息