. $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 = @' '@ 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 '' } It "Sets explainsEnabled to False" { $modifiedXml.OuterXml.ToString() | Should -Match '' } It "Sets SlowSql to False" { $modifiedXml.OuterXml.ToString() | Should -Match '' } It "Sets applicationLogging to False" { $modifiedXml.OuterXml.ToString() | Should -Match '' } It "Sets applicationLoggingforwarding to False" { $modifiedXml.OuterXml.ToString() | Should -Match ' # # # # #'@ $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 '' } } Context "When the transactionTracer Node is Missing" { $global:modifiedXml = $null # XML With Missing Slow SQL Node [xml]$global:nrXmlNoSlowSql = @' '@ # 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 '' } } Context "When the applicationLogging Node is Missing" { $global:modifiedXml = $null # XML With Missing Slow SQL Node [xml]$nrXmlNoSlowSql = @' '@ # 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 '' } } Context "When the applicationLoggingforwarding Node is Missing" { $global:modifiedXml = $null # XML With Missing Slow SQL Node [xml]$nrXmlNoSlowSql = @' '@ # 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 ' '@ # 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 ' '@ # 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 '' } } 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 = @' '@ # 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 = @' '@ 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