Skip to main content

Infrastructure Test with Pester

· 3 min read
Naw Awn

In today's dynamic technology landscape, maintaining a robust and reliable infrastructure is crucial for businesses to ensure smooth operations. From time to time you will need to install Windows udpates, security patches, required software updates and etc., on your servers and reboot them as necessary.

In this blog post, let's explore the infrastructure test using pester. This is a simple test you can run to ensure the servers are running with required business applicaiton processes and services after the reboot. I happened to write more comprehensive tests script like this when I had to run the test on more than 50 servers in one go.

The pester script below simply checks:

  • The server IP address is correct.
  • The Line of Business processes are running.
  • The Line of Business services are either running or stopped.
Infrastructure.Tests.ps1
Param(
[Parameter()]
$ConfigFile = '.\InfraData.Tests.psd1'
)
BeforeDiscovery {
$Data = Import-PowerShellDataFile -Path $ConfigFile
#$Data.GetType() - return Hashtable
}
Describe "Infrastructure Operation Validation Test" {
BeforeEach {
If (-Not($Process)){ $SkipProcessTests = $true }
If (-Not($Service)){ $SkipServiceTests = $true }
}
Context "<NodeName> - Server Check" -Foreach $Data{
It "<_.Count> items in each test Object"{
$_.Count | should -BeGreaterThan 1
}
It "IP Address Should be <IPAddress>" -Tag IPAddress{
#This assumes each server is assigned with one IP address.
(Resolve-DnsName -Name $NodeName -Type A).Ip4Address | Should -Be $IPAddress
}
Context "Process Check"{
It "<_> - This PROCESS should be running" -Foreach $($Process) -Skip:($SkipProcessTests) -Tag Process{
[Bool]$(Invoke-Command -ComputerName $NodeName -Command {Get-Process -Name $Using:PSItem }).Name | Should -Be $true
}
}
Context "Service Check"{
It "<Name> - This SERVICE should be: <Status>" -Foreach $($Service) -Skip:($SkipServiceTests) -Tag Service{
(Invoke-Command -ComputerName $NodeName -Command {Get-Service -Name $Using:Name}).Status | Should -Be $Status
}
}
}
}

The pester script above uses the separate data file to actually run the test. Within the data file, you can have the test data for more than one server.

InfraData.Tests.psd1
@(  
@{
NodeName = "Server01"
IPAddress = "192.168.2.1"
Process = @(
"AvidAMSAPIService"
"AvidCaptureClipManagerService"
"AvidFos_Service"
)
Service = @(
@{Name = "Avid AMS API Device Service"; Status = "Running" },
@{Name = "Avid Interplay Capture Clip Manager Service"; Status = "Running"},
@{Name = "AvidFosFS"; Status = "Running" }
)
},
@{
NodeName = "Server02"
IPAddress = "192.168.2.2"
Process = @(
"AvidLookupService64"
"AvidMachineMonitor64"
"AvidNEXISClientManager"
)
Service = @(
@{Name = "Avid Lookup Service"; Status = "Running" },
@{Name = "Avid Machine Monitoring Service"; Status = "Running" },
@{Name = "AvidNEXISObjectAccessModule"; Status = "Stopped" },
@{Name = "nxClientMsgGateway"; Status = "Running" }
)
}
)

Once you've gathered enough test data in a powershell data file, put the test script and the data file in a folder. From PowerShell console, navigate to the folder and run the pester test as below.

Invoke-Pester .\Infrastructure.Tests.ps1 -Output Detailed
info

For further reading on Pester Data driven test and pester container