使用 Pester 模拟 azure cli 命令
Mock azure cli commands with Pester
我有很多 Azure Cli 脚本,我已将它们放入 powershell 函数中,例如:
function NewAppRegistration($name, $replyUrls, $resourceAccessesFilePath) {
$appRegistration = az ad app create `
--display-name $name `
--reply-urls $replyUrls `
--oauth2-allow-implicit-flow true `
--available-to-other-tenants false `
-o json |
ConvertFrom-Json
if (Test-Path $resourceAccessesFilePath) {
$appRegistration = az ad app update `
--id $appRegistration.appId `
--required-resource-accesses $resourceAccessesFilePath
}
return $appRegistration
}
我想模拟 az ad app create
,但我不知道如何继续,而且我也没有找到任何示例来说明如何做到这一点。当然,我最终可以为每个 az
命令创建我自己的 powershell 函数,并且我可以模拟这些函数,但我想知道它是否可以变得更简单?
我认为不可能模拟不是用 PowerShell 编写的函数,除非您将这些命令包装在 PowerShell 函数中。但是,有一个由 Microsoft 编写的本机 PowerShell 模块可能适合您的需要。您可以轻松模拟这些函数而无需创建包装函数。
https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-5.7.0
模拟 bash 命令,如 az
或任何其他类型的命令是:
Mock az {}
对于断言:
Should -Invoke -CommandName "az" -Exactly -Times 1 -ParameterFilter {
"$args" -match "ad app create" -and
"$args" -match "--display-name $expectedName" -and
"$args" -match "--reply-urls" -and (-not (Compare-Object $args[6] $expectedReplyUrls)) -and
"$args" -match "--oauth2-allow-implicit-flow true" -and
"$args" -match "--available-to-other-tenants false" -and
"$args" -match "-o json"
}
Pester 未对此进行记录,此解决方案基于 on this github issue
您可以为您的 az 命令配置模拟:
Mock az -MockWith { '{"data": "expected az json response"}' } -ParameterFilter { "$args" -match "ad app create" }
Mock az -MockWith { '{"data": "expected az json response"}' } -ParameterFilter { "$args" -match "ad app update" }
您可以使用模拟响应来测试您所有的脚本代码
我有很多 Azure Cli 脚本,我已将它们放入 powershell 函数中,例如:
function NewAppRegistration($name, $replyUrls, $resourceAccessesFilePath) {
$appRegistration = az ad app create `
--display-name $name `
--reply-urls $replyUrls `
--oauth2-allow-implicit-flow true `
--available-to-other-tenants false `
-o json |
ConvertFrom-Json
if (Test-Path $resourceAccessesFilePath) {
$appRegistration = az ad app update `
--id $appRegistration.appId `
--required-resource-accesses $resourceAccessesFilePath
}
return $appRegistration
}
我想模拟 az ad app create
,但我不知道如何继续,而且我也没有找到任何示例来说明如何做到这一点。当然,我最终可以为每个 az
命令创建我自己的 powershell 函数,并且我可以模拟这些函数,但我想知道它是否可以变得更简单?
我认为不可能模拟不是用 PowerShell 编写的函数,除非您将这些命令包装在 PowerShell 函数中。但是,有一个由 Microsoft 编写的本机 PowerShell 模块可能适合您的需要。您可以轻松模拟这些函数而无需创建包装函数。
https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-5.7.0
模拟 bash 命令,如 az
或任何其他类型的命令是:
Mock az {}
对于断言:
Should -Invoke -CommandName "az" -Exactly -Times 1 -ParameterFilter {
"$args" -match "ad app create" -and
"$args" -match "--display-name $expectedName" -and
"$args" -match "--reply-urls" -and (-not (Compare-Object $args[6] $expectedReplyUrls)) -and
"$args" -match "--oauth2-allow-implicit-flow true" -and
"$args" -match "--available-to-other-tenants false" -and
"$args" -match "-o json"
}
Pester 未对此进行记录,此解决方案基于 on this github issue
您可以为您的 az 命令配置模拟:
Mock az -MockWith { '{"data": "expected az json response"}' } -ParameterFilter { "$args" -match "ad app create" }
Mock az -MockWith { '{"data": "expected az json response"}' } -ParameterFilter { "$args" -match "ad app update" }
您可以使用模拟响应来测试您所有的脚本代码