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

290 lines
9.3 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 = ""
#region Set-NewRelicConfigurationValues
Describe "Set-NewRelicConfigurationValues" {
$global:modifiedXml = $null
Mock -ModuleName $moduleForMock Save-XmlFile {
Param(
[string]$path,
[xml]$xml
)
$global:modifiedXml = $xml
}
Mock -ModuleName $moduleForMock Write-Host
Mock -ModuleName $moduleForMock Write-Warning
# XML With Wrong Values for both Log Levels and Slow SQL
[xml]$global:nrXmlWrongValues = @'
<?xml version="1.0"?>
<configuration xmlns="urn:newrelic-config" agentEnabled="true">
<log level="debug" />
<transactionTracer enabled="true" transactionThreshold="apdex_f" stackTraceThreshold="500" recordSql="obfuscated" explainEnabled="true" explainThreshold="500" />
<slowSql enabled="true" />
<applicationLogging enabled="true">
<forwarding enabled="true" maxSamplesStored="10000" />
<localDecorating enabled="true" />
</applicationLogging>
</configuration>
'@
Context "When the Values are Not Alkami Defaults" {
# Mock Read-XmlFile for the Test
Mock -ModuleName $moduleForMock Read-XmlFile {
return $global:nrXmlWrongValues
}
Set-NewRelicConfigurationValues
It "Sets the Log Level to Error" {
Assert-MockCalled Read-XmlFile -ModuleName $moduleForMock -times 1
$modifiedXml.OuterXml.ToString() | Should -Match '<log level="ERROR" />'
}
It "Sets explainsEnabled to False" {
$modifiedXml.OuterXml.ToString() | Should -Match '<transactionTracer enabled="true" transactionThreshold="apdex_f" stackTraceThreshold="500" recordSql="obfuscated" explainEnabled="false" explainThreshold="500" />'
}
It "Sets SlowSql to False" {
$modifiedXml.OuterXml.ToString() | Should -Match '<slowSql enabled="FALSE" />'
}
It "Sets applicationLogging to False" {
$modifiedXml.OuterXml.ToString() | Should -Match '<applicationLogging enabled="false">'
}
It "Sets applicationLoggingforwarding to False" {
$modifiedXml.OuterXml.ToString() | Should -Match '<forwarding enabled="false"'
}
It "Sets applicationLogginglocalDecorating to False" {
$modifiedXml.OuterXml.ToString() | Should -Match '<localDecorating enabled="false"'
}
}
Context "When a Specific Log Level is Provided" {
# # XML With Wrong Values for both Log Levels and Slow SQL
# [xml]$global:nrXmlWrongValues = @'
#<?xml version="1.0"?>
#<configuration xmlns="urn:newrelic-config" agentEnabled="true">
# <log level="debug" />
# <slowSql enabled="false" />
#</configuration>
#'@
$global:modifiedXml = $null
# Mock Read-XmlFile for the Test
Mock -ModuleName $moduleForMock Read-XmlFile { return $nrXmlWrongValues }
Set-NewRelicConfigurationValues -LogLevel "FATAL"
It "Sets the Log Level to the Provided Value" {
$modifiedXml.OuterXml.ToString() | Should -Match '<log level="FATAL" />'
}
}
Context "When the transactionTracer Node is Missing" {
$global:modifiedXml = $null
# XML With Missing Slow SQL Node
[xml]$global:nrXmlNoSlowSql = @'
<?xml version="1.0"?>
<configuration xmlns="urn:newrelic-config" agentEnabled="true">
<log level="error" />
<applicationLogging enabled="true">
<forwarding enabled="true" maxSamplesStored="10000" />
<localDecorating enabled="true" />
</applicationLogging>
</configuration>
'@
# Mock Read-XmlFile for the Test
Mock -ModuleName $moduleForMock Read-XmlFile { return $nrXmlNoSlowSql }
Set-NewRelicConfigurationValues
It "Adds the Missing transactionTracer Node" {
$modifiedXml.OuterXml.ToString() | Should -Match '<transactionTracer enabled="true" transactionThreshold="apdex_f" stackTraceThreshold="500" recordSql="obfuscated" explainEnabled="false" explainThreshold="500" />'
}
}
Context "When the applicationLogging Node is Missing" {
$global:modifiedXml = $null
# XML With Missing Slow SQL Node
[xml]$nrXmlNoSlowSql = @'
<?xml version="1.0"?>
<configuration xmlns="urn:newrelic-config" agentEnabled="true">
<log level="error" />
</configuration>
'@
# Mock Read-XmlFile for the Test
Mock -ModuleName $moduleForMock Read-XmlFile { return $nrXmlNoSlowSql }
Set-NewRelicConfigurationValues
It "Adds the Missing applicationLogging Node" {
$modifiedXml.OuterXml.ToString() | Should -Match '<applicationLogging enabled="false">'
}
}
Context "When the applicationLoggingforwarding Node is Missing" {
$global:modifiedXml = $null
# XML With Missing Slow SQL Node
[xml]$nrXmlNoSlowSql = @'
<?xml version="1.0"?>
<configuration xmlns="urn:newrelic-config" agentEnabled="true">
<log level="error" />
</configuration>
'@
# Mock Read-XmlFile for the Test
Mock -ModuleName $moduleForMock Read-XmlFile { return $nrXmlNoSlowSql }
Set-NewRelicConfigurationValues
It "Adds the Missing applicationLoggingforwarding Node" {
$modifiedXml.OuterXml.ToString() | Should -Match '<forwarding enabled="false"'
}
}
Context "When the applicationLogginglocalDecorating Node is Missing" {
$global:modifiedXml = $null
# XML With Missing Slow SQL Node
[xml]$nrXmlNoSlowSql = @'
<?xml version="1.0"?>
<configuration xmlns="urn:newrelic-config" agentEnabled="true">
<log level="error" />
</configuration>
'@
# Mock Read-XmlFile for the Test
Mock -ModuleName $moduleForMock Read-XmlFile { return $nrXmlNoSlowSql }
Set-NewRelicConfigurationValues
It "Adds the Missing applicationLogginglocalDecorating Node" {
$modifiedXml.OuterXml.ToString() | Should -Match '<localDecorating enabled="false"'
}
}
Context "When the SlowSql Node is Missing" {
$global:modifiedXml = $null
# XML With Missing Slow SQL Node
[xml]$nrXmlNoSlowSql = @'
<?xml version="1.0"?>
<configuration xmlns="urn:newrelic-config" agentEnabled="true">
<log level="error" />
<applicationLogging enabled="true">
<forwarding enabled="true" maxSamplesStored="10000" />
<localDecorating enabled="true" />
</applicationLogging>
</configuration>
'@
# Mock Read-XmlFile for the Test
Mock -ModuleName $moduleForMock Read-XmlFile { return $nrXmlNoSlowSql }
Set-NewRelicConfigurationValues
It "Adds the Missing SlowSql Node" {
$modifiedXml.OuterXml.ToString() | Should -Match '<slowSql enabled="FALSE" />'
}
}
Context "When the NewRelic Configuration File Is Invalid" {
$tempPath = [System.IO.Path]::GetTempFileName()
It "Writes a Warning When the File Is Not Found" {
{ (Set-NewRelicConfigurationValues -NewRelicConfigPath $tempPath 3>&1) -match "The New Relic Config File Could Not be Found"
} | Should Be $true
}
It "Writes a Warning When the Config is Invalid XML" {
$global:badXml = "Hello World!"
Mock -ModuleName $moduleForMock Read-XmlFile { return $global:badXml }
{ (Set-NewRelicConfigurationValues 3>&1) -match "The New Relic Config File Could Not be Read Or Is Invalid" } | Should Be $true
}
It "Writes a Warning When the Logging Node Does Not Exist" {
$global:modifiedXml = $null
# XML With Missing Slow SQL Node
[xml]$nrXmlNoLogging = @'
<?xml version="1.0"?>
<configuration xmlns="urn:newrelic-config" agentEnabled="true">
<notReallyTheLogSection level="error" />
</configuration>
'@
# Mock Read-XmlFile for the Test
Mock -ModuleName $moduleForMock Read-XmlFile { return $nrXmlNoLogging }
{ (Set-NewRelicConfigurationValues 3>&1) -match "Log Node was not found" } | Should Be $true
}
}
Context "When No Changes Are Required" {
[xml]$nrNoChangesNeeded = @'
<?xml version="1.0"?>
<configuration xmlns="urn:newrelic-config" agentEnabled="true">
<log level="error" />
<transactionTracer enabled="true" transactionThreshold="apdex_f" stackTraceThreshold="500" recordSql="obfuscated" explainEnabled="false" explainThreshold="500" />
<slowSql enabled="false" />
<applicationLogging enabled="false">
<forwarding enabled="false" maxSamplesStored="10000" />
<localDecorating enabled="false" />
</applicationLogging>
</configuration>
'@
It "Does Not Save the Configuration File" {
# Mock Read-XmlFile for the Test
Mock -ModuleName $moduleForMock Read-XmlFile { return $nrNoChangesNeeded }
# Mock Save-XmlFile for the Test
Mock -ModuleName $moduleForMock Save-XmlFile { }
Set-NewRelicConfigurationValues
Assert-MockCalled Save-XmlFile -ModuleName $moduleForMock -Times 0 -Exactly -Scope It
}
}
}
#endregion Set-NewRelicConfigurationValues