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

207 lines
9.7 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-NewRelicApmInstallData" {
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Verbose -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Debug -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Get-LogLeadName -MockWith {}
# Assume running commands on localhost unless overridden withing a Context or It
Mock -ModuleName $moduleForMock -CommandName Compare-StringToLocalMachineIdentifiers -MockWith {return $true}
Mock -ModuleName $moduleForMock -CommandName Invoke-CommandWithRetry -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Get-OrbVersion -MockWith { }
Mock -ModuleName $moduleForMock -CommandName Get-SupportedPlatformAPMVersion -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Get-ChocoState -MockWith {}
$validFakeChocoSources = @(
@{
Name = "ChocoFake1"
Source = "https://packagerepo.orb.alkamitech.com/nuget/choco.fake1"
IsSDK = $false
Disabled = $false
},
@{
Name = "ChocoFake2"
Source = "https://packagerepo.orb.alkamitech.com/nuget/choco.fake2"
IsSDK = $false
Disabled = $false
}
)
Mock -ModuleName $moduleForMock -CommandName Get-ChocolateySources -MockWith { return $validFakeChocoSources }
# Technically, I should mock this, but... it's being used multiple times to check
# values I have control over, so I'm going to let it go until I hit something I
# don't control. For now.
#Mock -ModuleName $moduleForMock -CommandName Test-StringIsNullOrEmpty -MockWith {}
Context "Happy Path" {
# This test is also the "Check most of the Assert-Mocks" test for the happy path
It "No supplied Version, no ORB, APM exists on a feed, return Take-action Upgrade" {
$nrLocal = @{
Version = "1.0.0.0"
Name = "newrelic-dotnet"
Feed = $null
}
$nrRemote = @{
Version = "2.0.0.0"
Name = "newrelic-dotnet"
Feed = $null
}
# LOCAL Get-ChocoState
Mock -ModuleName $moduleForMock -CommandName Get-ChocoState -MockWith { return $nrLocal} -ParameterFilter { $Exact -eq $true -and $LocalOnly -eq $true }
# REMOTE Get-ChocoState
Mock -ModuleName $moduleForMock -CommandName Get-ChocoState -MockWith { return $nrRemote } -ParameterFilter { $Source -in $validFakeChocoSources.Source }
$nrApmInstallData = Get-NewRelicApmUpgradeData -Verbose
#region Verify a ton of mocks in execution order
Assert-MockCalled -CommandName Get-OrbVersion -Times 1 -Exactly -Scope It -ModuleName $moduleForMock
Assert-MockCalled -CommandName Get-SupportedPlatformAPMVersion -Times 0 -Exactly -Scope It -ModuleName $moduleForMock
Assert-MockCalled -CommandName Write-Host -Times 1 -Scope It -ParameterFilter { $Object -match "Could not determine the appropriate target APM version. The latest version in the configured feeds will be used" }
Assert-MockCalled -CommandName Get-ChocoState -Times 1 -ModuleName $moduleForMock -ParameterFilter { $Exact -eq $true -and $LocalOnly -eq $true } -Scope It
Assert-MockCalled -CommandName Write-Host -Times 1 -ModuleName $moduleForMock -ParameterFilter { $Object -match "Detected local NewRelic APM choco package version: \[$($nrLocal.Version)\]" } -Scope It
Assert-MockCalled -CommandName Write-Warning -Times 0 -ModuleName $moduleForMock -ParameterFilter { $Message -match " Could not find a configured feed which matches the requirements for NewRelic APM. Evaluation cannot continue" } -Scope It
Assert-MockCalled -CommandName Write-Verbose -Times 1 -ModuleName $moduleForMock -ParameterFilter { $Message -match "Checking source with name \[$($validFakeChocoSources[0].Name)\] and Feed URL \[$($validFakeChocoSources[0].Source)\]" } -Scope It
Assert-MockCalled -CommandName Get-ChocoState -Times 1 -ModuleName $moduleForMock -ParameterFilter { $Source -in $validFakeChocoSources.Source } -Scope It
#endregion Verify a ton of mocks in execution order
$nrApmInstallData.ActionRequired | Should -BeTrue
}
# This test is also in the catch-all baseline above, but it is also a
# separate bullet in the story. It warrants its own test for visibility
It "No supplied Version, determine the correct version based on the ORB " {
Mock -ModuleName $moduleForMock -CommandName Get-OrbVersion -MockWith { [System.Version]"1.0.0.0" }
$nrApmInstallData = Get-NewRelicApmUpgradeData
Assert-MockCalled -CommandName Get-OrbVersion -Times 1 -Exactly -Scope It -ModuleName $moduleForMock
Assert-MockCalled -CommandName Get-SupportedPlatformAPMVersion -Times 1 -Exactly -Scope It -ModuleName $moduleForMock
# This is purely to make the "variable is declared and never used" squiggles go away
$nrApmInstallData | Should -Not -BeNullOrEmpty
}
It "APM Version supplied, APM Version on feed, Different APM version local, No ORB, return Take-Action Upgrade" {
$nrLocal = @{
Version = "1.0.0.0"
Name = "newrelic-dotnet"
Feed = $null
}
$nrRemote = @{
Version = "2.0.0.0"
Name = "newrelic-dotnet"
Feed = $null
}
# LOCAL Get-ChocoState
Mock -ModuleName $moduleForMock -CommandName Get-ChocoState -MockWith { return $nrLocal } -ParameterFilter { $Exact -eq $true -and $LocalOnly -eq $true }
# REMOTE Get-ChocoState
Mock -ModuleName $moduleForMock -CommandName Get-ChocoState -MockWith { return $nrRemote } -ParameterFilter { $Source -in $validFakeChocoSources.Source }
$nrApmInstallData = Get-NewRelicApmUpgradeData -DesiredAPMVersion 2.0.0.0
$nrApmInstallData.ActionRequired | Should -BeTrue
$nrApmInstallData.ActionType | Should -Be "Upgrade"
}
It "No supplied version, ORB, Supported version on feed, Lower than installed version, return Take-Action Downgrade" {
$nrLocal = @{
Version = "2.0.0.0"
Name = "newrelic-dotnet"
Feed = $null
}
$nrRemote = @{
Version = "1.0.0.0"
Name = "newrelic-dotnet"
Feed = $null
}
# LOCAL Get-ChocoState
Mock -ModuleName $moduleForMock -CommandName Get-ChocoState -MockWith { return $nrLocal } -ParameterFilter { $Exact -eq $true -and $LocalOnly -eq $true }
# REMOTE Get-ChocoState
Mock -ModuleName $moduleForMock -CommandName Get-ChocoState -MockWith { return $nrRemote } -ParameterFilter { $Source -in $validFakeChocoSources.Source }
Mock -ModuleName $moduleForMock -CommandName Get-OrbVersion -MockWith { return [System.Version]"1.0.0.0" }
Mock -ModuleName $moduleForMock -CommandName Get-SupportedPlatformAPMVersion -MockWith { return "1.0.0.0" }
$nrApmInstallData = Get-NewRelicApmUpgradeData
$nrApmInstallData.ActionRequired | Should -BeTrue
$nrApmInstallData.ActionType | Should -Be "Downgrade"
}
}
Context "Unappy Path" {
It "NO valid Feeds Warns, Returns No-Action" {
$nonValidFakeChocoSources = @(
@{
Name = "BadChocoFake1"
Source = "https://packagerepo.orb.alkamitech.com/nuget/choco.sre1"
IsSDK = $false
Disabled = $false
}
)
Mock -ModuleName $moduleForMock -CommandName Get-ChocolateySources -MockWith { return $nonValidFakeChocoSources }
$nrApmInstallData = Get-NewRelicApmUpgradeData
Assert-MockCalled -CommandName Write-Warning -Times 1 -ModuleName $moduleForMock -ParameterFilter { $Message -match " Could not find a configured feed which matches the requirements for NewRelic APM. Evaluation cannot continue" }
$nrApmInstallData.ActionRequired | Should -BeFalse
}
It "No APM on valid feed, No local APM, ORB installed, returns No-Action" {
$nrRemote = @{}
$nrLocal = @{}
Mock -ModuleName $moduleForMock -CommandName Get-OrbVersion -MockWith { return [System.Version]"1.0.0.0" }
Mock -ModuleName $moduleForMock -CommandName Get-SupportedPlatformAPMVersion -MockWith { return "1.0.0.0" }
# LOCAL Get-ChocoState
Mock -ModuleName $moduleForMock -CommandName Get-ChocoState -MockWith { return $nrLocal } -ParameterFilter { $Exact -eq $true -and $LocalOnly -eq $true }
# REMOTE Get-ChocoState
Mock -ModuleName $moduleForMock -CommandName Get-ChocoState -MockWith { return $nrRemote } -ParameterFilter { $Source -in $validFakeChocoSources.Source }
$nrApmInstallData = Get-NewRelicApmUpgradeData
$nrApmInstallData.ActionRequired | Should -BeFalse
}
}
}