DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. IaC Unit Testing Using Pester Test

IaC Unit Testing Using Pester Test

Learn how to use the Pester Test framework to write unit test cases for infrastructure as code.

Ajeet Chouksey user avatar by
Ajeet Chouksey
·
Aug. 30, 18 · Tutorial
Like (1)
Save
Tweet
Share
3.71K Views

Join the DZone community and get the full member experience.

Join For Free

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 infrastructure as code. Yes we need to write unit test cases for them as well. Pester Test can help us here.

In this post, I will walk you through how you can use the Pester Test 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 the Module

 Install-Module -Name Pester

You can use any PS editor to write Pester tests.

Structure

Describe 'Describe level'{
    context 'context level'{
        it 'it level'{            
        }
    }
}

The sample template I am using is available at 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 it to test locally:

Invoke-Pester .\Filename.Test.ps1

Integrate with a VSTS CI pipeline with the Pester Unit Build Task.

unit test

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • OpenVPN With Radius and Multi-Factor Authentication
  • MongoDB Time Series Benchmark and Review
  • How to Use Buildpacks to Build Java Containers
  • Tackling the Top 5 Kubernetes Debugging Challenges

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: