IaC Unit Test using Pester Test
A unit test is a way of testing a unit - the smallest piece of code that can be logically isolated in a system.
What about unit testing on IaC, after all it’s a code. Yes we need to write unit test cases for them as well. Pester Test help us here.
In this post I will walk you through, how you can use Pester Testing framework to write unit test cases for IaC.
Pester Test
Pester is an open source unit testing framework developed specifically to test PowerShell code. It is a PowerShell module that is used to write tests for PowerShell code to confirm that what it does is what you expect. In a nutshell, Pester is the code that’s written “on top of” your code to act as quality assurance. Fortunately, Pester itself is written in PowerShell, so you don’t have to be a software developer to learn how to use it.
Since Pester is just a PowerShell module, we can easily download and install it from the PowerShell Gallery. Downloading from the PowerShell Gallery will get you the latest version.
Install Module
Install-Module -Name Pester
You can use any PS editor to write Pester test.
Structure
Describe 'Describe level'{
context 'context level'{
it 'it level'{
}
}
}
sample template I am using is available @ 101-webapp-custom-deployment-slots
Unit Test
## Get template location
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
Describe "Template: 101-webapp-custom-deployment-slots" -Tags Unit, azuredeploy {
Context "azuredeploy" {
It "Has a valid JSON template syntax" {
"$here\azuredeploy.json" | Should Exist
}
It "Converts from azuredeploy.JSON and has the expected template schema" {
$expectedProperties = '$schema',
'contentVersion',
'parameters',
'variables',
'resources',
'outputs'
$templateProperties = (get-content "$here\azuredeploy.json" | ConvertFrom-Json -ErrorAction SilentlyContinue) | Get-Member -MemberType NoteProperty | % Name
$templateProperties | Should Be $expectedProperties
}
It "parameters file Exist " {
"$here\$parmFile" | Should Exist
}
It "Converts from parameter files and has the expected template schema" {
$expectedProperties = '$schema',
'contentVersion',
'parameters'
$templateProperties = (get-content "$here\$paramFile" | ConvertFrom-Json -ErrorAction SilentlyContinue) | Get-Member -MemberType NoteProperty | % Name
$templateProperties | Should Be $expectedProperties
}
}
}
Context "azuredeploy Template Resources" {
It "Creates the expected Azure resources" {
$expectedResources='Microsoft.Web/serverfarms', 'Microsoft.Web/sites', 'Microsoft.Web/sites/slots'
$templateResources = (get-content "$here\Templates\azuredeploy.json" | ConvertFrom-Json -ErrorAction SilentlyContinue).Resources.type
$templateResources | Should Be $expectedResources
}
}
}
Save this file as Filename.Test.ps1
Execute to test locally
Invoke-Pester .\Filename.Test.ps1
Integrate with VSTS CI pipeline Pester Unit Build Task
Related Posts
- Include Pester Test as part of VSTS Build
- People, Process, Product - DevOps - SonarQube Tool Assessment- 2
- People, Process, Product - DevOps - SonarQube Tool Assessment- 1
- PowerShell Desired State Configuration - Part 2
- VSTS 101