ps/Modules/Alkami.DevOps.Installation/Public/Set-NewRelicDeployment.tests.ps1
2023-05-30 22:51:22 -07:00

194 lines
10 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 "Set-NewRelicDeployment" {
# by using variables for everything, we can eliminate typos and ensure we still match the appropriate/expected actions
#region variable setup
$EnvironmentKey = "Unit Test Environment"
$AlternateEnvironmentKey = "Fake Pod 12"
$TestUser = "TestUser"
$SampleWcfApplicationId = 1234
$SamplePositiveMicroservice1Id = 5678
$SamplePositiveMicroservice2Id = 1010
$SampleNegativeMicroservice1Id = 9876
$SampleNegativeMicroservice2Id = 8765
$SampleMicroserviceName1 = "Alkami.Microservice Test 1"
$SampleMicroserviceName2 = "Alkami.Microservice Test 2"
$SampleMicroserviceName3 = "Alkami.Microservice Test 3"
$SampleMicroserviceName4 = "Alkami.Microservice Test 4"
#endregion variable setup
#region mock setup
Mock -ModuleName $moduleForMock -CommandName Get-LogLeadName -MockWith { return "[UUT]" }
Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith { }
Mock -ModuleName $moduleForMock -CommandName Write-Verbose -MockWith { }
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith { }
Mock -ModuleName $moduleForMock -CommandName Get-NewRelicAccountDetails -MockWith { return @{
EnvironmentRegex = $EnvironmentKey
APIKey = "Unit Test Api Key"
LicenseKey = "Unit Test License Key"
} }
Mock -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -MockWith { Write-warning "$applicationVersion"; return $true }
Mock -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -ParameterFilter { $ApplicationId -eq $SampleWcfApplicationId } -MockWith { return $true }
Mock -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -ParameterFilter { $ApplicationId -eq $SamplePositiveMicroservice1Id } -MockWith { return $true }
Mock -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -ParameterFilter { $ApplicationId -eq $SamplePositiveMicroservice2Id } -MockWith { return $true }
Mock -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -ParameterFilter { $ApplicationId -eq $SampleNegativeMicroservice1Id } -MockWith { return $true }
Mock -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -ParameterFilter { $ApplicationId -eq $SampleNegativeMicroservice2Id } -MockWith { return $false }
Mock -ModuleName $moduleForMock -CommandName Write-Warning -ParameterFilter { $message.IndexOf("$SampleMicroserviceName3 was specified to be updated in New Relic") -gt -1 } -MockWith { }
Mock -ModuleName $moduleForMock -CommandName Get-NewRelicObjects -MockWith {
return @{ applications = @(
@{
id = $SampleWcfApplicationId;
name = "$EnvironmentKey Sample WCF Application";
},
@{
id = $SamplePositiveMicroservice1Id;
name = "$EnvironmentKey $SampleMicroserviceName1";
},
@{
id = $SamplePositiveMicroservice2Id;
name = "$EnvironmentKey $SampleMicroserviceName2";
},
@{
id = $SampleNegativeMicroservice1Id;
name = "$AlternateEnvironmentKey $SampleMicroserviceName3";
},
@{
id = $SampleNegativeMicroservice2Id;
name = "$AlternateEnvironmentKey.1 $SampleMicroserviceName4";
}
)};
}
#endregion mock setup
Context "When Microservices Are Not Provided As A Parameter, Post For Services" {
Set-NewRelicDeployment -EnvironmentKey $EnvironmentKey -AppVersion "1.1" -DeployUser $TestUser
It "Posts to non-MS Which Do Exist In New Relic" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -Exactly -Times 1 -Scope Context -ParameterFilter { $ApplicationId -eq $SampleWcfApplicationId }
}
It "Does Not Post to MS Which Do Exist In New Relic" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -Exactly -Times 0 -Scope Context -ParameterFilter { $ApplicationId -eq $SamplePositiveMicroservice1Id }
}
# We already asserted above that it posts to the one we want, now ensure it only posted the expected amount of times
It "Does Not Post An Update To Additional Services Which Do Exist In New Relic" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -Exactly -Times 1 -Scope Context
}
}
Context "When Making the Application Query" {
It "Throws if AppVersion is not supplied" {
{ Set-NewRelicDeployment -EnvironmentKey $EnvironmentKey -DeployUser $TestUser } | Should -throw
}
It "Does not throw when required parameters are provided" {
{
Set-NewRelicDeployment -EnvironmentKey $EnvironmentKey -AppVersion "1.1" -DeployUser $TestUser
} | Should -Not -Throw
}
It "Throws when too many parameters are provided for input" {
$microservices = @{
$SampleMicroserviceName1 = "1.1"
$SampleMicroserviceName2 = "1.1"
}
{
Set-NewRelicDeployment -EnvironmentKey $EnvironmentKey -AppVersion "1.1" -DeployUser $TestUser -microservices $microservices
} | Should -Throw
}
}
Context "When 2 Microservices Are Provided As A Parameter and all belong to the environment" {
$microservices = @{
$SampleMicroserviceName1 = "1.1"
$SampleMicroserviceName2 = "1.1"
}
It "Does not throw when MS are provided for input" {
{
Set-NewRelicDeployment -EnvironmentKey $EnvironmentKey -DeployUser $TestUser -microservices $microservices
} | Should -Not -Throw
}
It "Does Not Report a Warning To Provided Microservices Which Exist In New Relic - 1" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -Exactly -Times 1 -Scope Context -ParameterFilter { $ApplicationId -eq $SamplePositiveMicroservice1Id }
}
It "Does Not Report a Warning To Provided Microservices Which Exist In New Relic - 2" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -Exactly -Times 1 -Scope Context -ParameterFilter { $ApplicationId -eq $SamplePositiveMicroservice2Id }
}
It "Does Not Post An Update To Non-Microservices Which Exist In New Relic" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -Exactly -Times 0 -Scope Context -ParameterFilter { $ApplicationId -eq $SampleWcfApplicationId }
}
}
Context "When 4 Microservices Are Provided As A Parameter but some don't belong to the environment" {
$microservices = @{
$SampleMicroserviceName1 = "1.1"
$SampleMicroserviceName2 = "1.1"
$SampleMicroserviceName3 = "1.1"
$SampleMicroserviceName4 = "1.1"
}
It "Does not throw when proper arguments are provided" {
{
Set-NewRelicDeployment -EnvironmentKey $EnvironmentKey -DeployUser $TestUser -Microservices $microservices
} | Should -Not -Throw
}
It "Posts An Update To Provided Microservices Which Exist In New Relic - 1" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -Exactly -Times 1 -Scope Context -ParameterFilter { $ApplicationId -eq $SamplePositiveMicroservice1Id }
}
It "Posts An Update To Provided Microservices Which Exist In New Relic - 2" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -Exactly -Times 1 -Scope Context -ParameterFilter { $ApplicationId -eq $SamplePositiveMicroservice2Id }
}
It "Does Not Post An Update To Provided Microservices Which Do Not Exist In New Relic" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -Exactly -Times 0 -Scope Context -ParameterFilter { $ApplicationId -eq $SampleNegativeMicroservice1Id }
}
It "Reports A Warning For Provided Microservices Which Do Not Exist In New Relic - 1" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -Exactly -Times 0 -Scope Context -ParameterFilter { $ApplicationId -eq $SampleNegativeMicroservice1Id }
}
It "Reports A Warning For Provided Microservices Which Do Not Exist In New Relic - 2" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Exactly -Times 1 -Scope Context -ParameterFilter { $message.IndexOf("$SampleMicroserviceName3 was specified to be updated in New Relic") -gt -1 }
}
}
Context "When 4 microservices are provided as a parameter but some don't belong to the (alternate) environment" {
$microservices = @{
$SampleMicroserviceName1 = "1.1"
$SampleMicroserviceName2 = "1.1"
$SampleMicroserviceName3 = "1.1"
$SampleMicroserviceName4 = "1.1"
}
It "Does not throw when proper arguments are provided" {
{
Set-NewRelicDeployment -EnvironmentKey $AlternateEnvironmentKey -DeployUser $TestUser -microservices $microservices
} | Should -Not -Throw
}
It "Only posts to the specified environment - 1" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -Exactly -Times 1 -Scope Context -ParameterFilter { $ApplicationId -eq $SampleNegativeMicroservice1Id }
}
It "Only posts to the specified environment - 2" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Submit-DeploymentToNewRelic -Exactly -Times 0 -Scope Context -ParameterFilter { $ApplicationId -eq $SampleNegativeMicroservice2Id }
}
}
}