ps/Modules/Alkami.PowerShell.Common/Public/Get-SupportedPlatformAPMVersion.tests.ps1
2023-05-30 22:51:22 -07:00

132 lines
5.5 KiB
PowerShell

. $PSScriptRoot\..\..\Load-PesterModules.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.'
$functionPath = Join-Path -Path $here -ChildPath $sut
Write-Host "Overriding SUT: $functionPath"
Import-Module $functionPath -Force
$moduleForMock = ""
Describe "Get-SupportedPlatformAPMVersion" {
Context "Unhappy Path" {
It "Writes a Warning and Returns Null if More Than One APM Version Match Found" {
Mock -ModuleName $moduleForMock -CommandName Get-SupportedPlatformAPMVersionMap -MockWith {
return @(
@{
APMVersion = "9.1.1.0";
PlatformMinimumVersion = "0.0.0.0";
PlatformMaximumVersion = "2.0.0.0";
},
@{
APMVersion = "9.2.2.0";
PlatformMinimumVersion = "0.0.0.0";
PlatformMaximumVersion = "2.0.0.0";
}
)
}
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {}
Get-SupportedPlatformAPMVersion -PlatformVersion "1.0.0.0" | Should -BeNullOrEmpty
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Scope It -Times 1 -ParameterFilter {
$Message -match "More than one NewRelic APM version matched"
}
}
It "Writes a Warning and Returns Null if Remote Version Retrieval Fails" {
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Invoke-Command -MockWith { return $null }
Get-SupportedPlatformAPMVersion -ComputerName "iDontExist.nowhere.org" | Should -BeNullOrEmpty
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Scope It -Times 1 -ParameterFilter {
$Message -match "Could not retrieve Alkami Platform version"
}
}
It "Throws if a User Specifies Both Remote Retrieval and Platform Version" {
{ Get-SupportedPlatformAPMVersion -PlatformVersion "1.0.0.0" -ComputerName "fakemachine.lulwut.com" } | Should -Throw
}
}
Context "Happy Path" {
Mock -ModuleName $moduleForMock -CommandName Get-SupportedPlatformAPMVersionMap -MockWith {
return @(
@{
APMVersion = "8.6.7.5309";
PlatformMinimumVersion = "0.0.0.0";
PlatformMaximumVersion = "2.0.0.0";
},
@{
APMVersion = "192.168.1.1";
PlatformMinimumVersion = "2.0.0.1";
PlatformMaximumVersion = "2.9.9.9";
},
@{
APMVersion = "8.8.8.8";
PlatformMinimumVersion = "3.0.0.0";
PlatformMaximumVersion = $null;
}
)
}
$happyPathTestCases = @(
@{ version = "0.0.0.0"; apmVersion = "8.6.7.5309"; },
@{ version = "0.0.0.1"; apmVersion = "8.6.7.5309"; },
@{ version = "1.0.0.0"; apmVersion = "8.6.7.5309"; },
@{ version = "1.1.1.1"; apmVersion = "8.6.7.5309"; },
@{ version = "2.0.0.0"; apmVersion = "8.6.7.5309"; },
@{ version = "2.0.0.1"; apmVersion = "192.168.1.1"; },
@{ version = "2.1.3.4"; apmVersion = "192.168.1.1"; },
@{ version = "2.9.9.9"; apmVersion = "192.168.1.1"; },
@{ version = "3.0.0.0"; apmVersion = "8.8.8.8"; },
@{ version = "99.99.99.99"; apmVersion = "8.8.8.8"; }
)
It "Returns Expected Values: (<apmVersion>)" -TestCases $happyPathTestCases {
param ($version, $apmVersion)
Get-SupportedPlatformAPMVersion -PlatformVersion $version | Should -Be $apmVersion
}
$zeroPaddingTestCases = @(
@{ version = "0.0"; apmVersion = "8.6.7.5309"; },
@{ version = "0.0.0"; apmVersion = "8.6.7.5309"; },
@{ version = "2.1"; apmVersion = "192.168.1.1"; },
@{ version = "2.1.0"; apmVersion = "192.168.1.1"; }
)
It "Pads Short Versions and Returns Expected Values: (<apmVersion>)" -TestCases $zeroPaddingTestCases {
param ($version, $apmVersion)
Get-SupportedPlatformAPMVersion -PlatformVersion $version | Should -Be $apmVersion
}
}
Context "Parameter Handling" {
It "Does Not Attempt Remote Connection if Platform Version Provided" {
Mock -ModuleName $moduleForMock -CommandName Invoke-Command -MockWith {}
Get-SupportedPlatformAPMVersion -PlatformVersion "1.2.3.4"
Assert-MockCalled -ModuleName $moduleForMock -CommandName Invoke-Command -Times 0 -Exactly -Scope It
}
It "Looks Up the Local ORB Version if No Parameters Provided" {
Mock -ModuleName $moduleForMock -CommandName Invoke-Command -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Get-ORBVersion -MockWith { return "1.1.1.1" }
Get-SupportedPlatformAPMVersion
Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-ORBVersion -Times 1 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Invoke-Command -Times 0 -Exactly -Scope It
}
}
}