Using Pester for release validation in VSTS


Lately I’m using VSTS (Visual Studio Team Service) more often and I have also implemented several Release Pipelines.

In a demo about Infrastructure as Code I wanted to create a *release validation test in the Release of my Release Pipeline.

My Infrastructure as Code demo environment shows the deployment of a NodeJS Web Application in Azure. All the needed Azure infrastructure to host the Web Application is configured in an ARM Template as code.

After a successful Build a Release is triggered in the Release Pipeline in VSTS and the first task in the Release is the the deployment of the ARM Template in Azure.

Pester Release Validation Test

After the Azure Resources deployment in the Deploy ARM Template task I wanted to test the following before continuing with the next steps of the Release:

  • Resource Group
  • Hosting Plan
  • App Service

These possible tests come from the Resources that are configured in the ARM Template.

ARM Template

Pester

How would you test the existence of above Resources using Pester?

demo.tests.ps1 file:

#region variables
$ResourceGroupName = 'demo-d-01-rg'
$HostingPlanName = 'demo-d-01-hp'
$AppServiceName = 'demo-d-01-as'
#endregion

Describe -Name 'Resource Group Resource' -Tags 'ARM' -Fixture {
    It -name 'Passed Resource Group existence' -test {
        Find-AzureRmResource -ResourceGroupNameContains $ResourceGroupName | Should Not Be $null
    }

    It -name 'Passed Hosting Plan existence' -test {
        Find-AzureRmResource -ResourceNameEquals $HostingPlanName | Should Not Be $null
    }

    It -name 'Passed App Service existence' -test {
        Find-AzureRmResource -ResourceNameEquals $AppServiceName | Should Not Be $null
    }
}

I created a Pester test which tests if the expected Azure Resources are deployed before deploying the Web App within those Resources in Azure.

Keep in mind that some of those variables used in the ARM Template have parameters which are configured in the Release Pipeline as variables.

How do you get the above Pester tests triggered in the Release Step Pester Release Validation test?

We will use an Azure PowerShell Task (Run a PowerShell script within an Azure environment) which allows us to connect to Azure because the Pester tests need to talk to Azure for validation.

I also created an InvokePesterTest.ps1 script which has a parameter for the Pester tests I want to run.

InvokePesterTests.ps1

<#
    Invoke Pester Test from VSTS Release Task
#>

Param([Parameter(Mandatory = $true,
        ValueFromPipelineByPropertyName = $true,
        Position = 0)]
    $TestScript)

#region Install Pester
Write-Host "Installing Pester from PSGallery"
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
Install-Module -Name Pester -Force -Scope CurrentUser
#endregion

#region call Pester script
Write-Host "Calling Pester test script"
#Invoke-Pester -Script $TestScript -PassThru
$result = Invoke-Pester -Script $TestScript -PassThru
if ($result.failedCount -ne 0) { 
    Write-Error "Pester returned errors"
}
#endregion

Because the Pester Module in not installed on the Release Agent we need to install the Package Provider and the Pester Module first before we can run the Pester script. We can only install these in the CurrentUser scope.

Don’t forget to use the Force switch too otherwise you see the following error message in the Release: “Exception calling “ShouldContinue” with “2” argument(s): “Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available.”

The last step is configuring the PowerShell Task in the Release.

releaseconfig

Because we need to connect to Azure you need configure the Azure Connection and Subscription.

Script path is the location where the InvokePesterTests.ps1 artifacts is stored after the Build step.

The script argument is the location where the demo.tests.ps1 is hosted after the Build step.

Result
If now one of your Pester Validation tests fails the release will stop and no Web App will be deployed.

Failed Pester test

Hope this blog post was helpful.

‘* Release Validation is a term Michael Green mentioned when I asked on Twitter if it was OK to use Pester tests in the Release of a Release Pipeline.

Twitter conversation

In the Pester Book on LeanPub from Adam Bertram you can read that you can also use Pester for infrastructure testing or validation.
“Infrastructure validation isn’t about testing code per se. It’s typically about testing what the code changes in the environment.” I highly recommend you reading this book.

Comments (0)

Skip to main content