Skip to content

Instantly share code, notes, and snippets.

@bentaylorwork
Last active October 13, 2017 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bentaylorwork/fb304c98e655fe2e8797fcb1b8138b6c to your computer and use it in GitHub Desktop.
Save bentaylorwork/fb304c98e655fe2e8797fcb1b8138b6c to your computer and use it in GitHub Desktop.
The below code takes the idea from the Azure Quick Start GitHub repo which allows you to specify place holders for ARM template parameters. The Pester test below replaces these place holders so the template can be tested without any user input.
$templatesFolderPath = Get-ChildItem -Path (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace('tests\unit', 'src') | Where-Object { $_.PSIsContainer -eq $true }
Describe "Test Template Againsts Endpoint" -Tags 'unit' {
foreach($templateFolderPath in $templatesFolderPath)
{
Context "Does The Template Deploy Correctly" {
$hashReturn = @{}
$ParametersObject = Get-Content -Path (Join-Path $templateFolderPath.FullName 'azuredeploy.parameters.json') | ConvertFrom-Json
($ParametersObject | Select-Object -ExpandProperty parameters).PSObject.Properties | ForEach-Object {
if($_.value.value -eq 'GEN-UNIQUE')
{
$hashReturn.Add($_.name, "a$(((New-Guid).guid -replace '-').Substring(0, 17))")
}
elseif($_.value.value -like "GEN-UNIQUE*")
{
$Number = ($_.value.value -split '-')[2]
if($Number -gt 32)
{
Throw 'Number out of bounds'
}
$hashReturn.Add($_.name, "a$(((New-Guid).guid -replace '-').Substring(0, $Number))")
}
elseif ($_.value.value -eq 'GEN-PASSWORD')
{
$hashReturn.Add($_.name, (ConvertTo-SecureString "aA!!$(((New-Guid).guid -replace '-').Substring(0, 9))" -AsPlainText -Force))
}
else
{
$hashReturn.Add($_.name, $_.value.value)
}
}
BeforeAll {
$metaData = Get-Content -path (Join-Path $templateFolderPath.FullName 'metadata.json') | ConvertFrom-Json
do {
$ResourceGroupName = (New-Guid).Guid
} until (-not (Get-AzureRmResourceGroup -Name $ResourceGroupName -Location ($metaData.location) -ErrorAction SilentlyContinue))
New-AzureRmResourceGroup -Name $ResourceGroupName -Location ($metaData.location) -Verbose
}
AfterAll {
Remove-AzureRmResourceGroup -Name $ResourceGroupName -Force
}
It "Does endpoint validation pass" {
Test-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName -TemplateFile (Join-Path $templateFolderPath.FullName 'azuredeploy.json') -TemplateParameterObject $hashReturn -Verbose 2> $templateError
$templateError | Should BeNullOrEmpty
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment