. $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-FileBeats Describe "Set-FileBeats" { # Variables for the Tests # Output for Validation Purposes [array]$global:OutFile = @() # Legit Ports to Pass to the Tests [array]$validPorts = @(5045, 5046, 5047, 5048) # Temp file to write content to $tempPath = [System.IO.Path]::GetTempFileName() Write-Warning ("Using temp path: $tempPath for tests") # Sample File Content to Update $sampleFile = @" #================================ Outputs ===================================== #----------------------------- Logstash output -------------------------------- output.logstash: # The Logstash hosts # This value should be modified during install to point to the logstash dns, with the port for the appropriate environment hosts: ["orb.os-logstash.haystack.prod.alkami.net:PORT"] # This is Custom Testing Stuff. "@ #TODO: Actually fake this instead of the redirection for $fileBeatsDefaultSourceConfigPath #TODO: Mock Get-Content with ParamFilter and use one to return $sampleFile and $templateFile(TBD) #TODO: Abandon the filesystem entirely. Either Out-File/Write-AllLines() works or it doesn't # Either way, it's out of scope and mocking/faking will suit our needs AND be faster #$newTempFile = [System.IO.Path]::GetTempFileName() #$FAKE_SourceConfigurationPath = (Get-ChildItem $newTempFile).Directory.FullName #$templateFile(TBD) | Out-File (Join-Path $FAKE_SourceConfigurationPath "filebeat.yml") -Force $hereParent = Split-Path -Path $here -Parent $fileBeatsDefaultSourceConfigPath = Join-Path -Path $hereParent -ChildPath "FileBeatConfiguration" Mock -ModuleName $moduleForMock -CommandName Test-IsMicroServer { return $false } Mock -ModuleName $moduleForMock -CommandName Test-IsAws { return $true } # Clean the TempPath File as Needed function Clean-TempPath { if (Test-Path $tempPath) { Remove-Item $tempPath -Force } } Context "When a Port is Specified" { It "Writes a Warning if the Port Doesn't Match an Environment" { { (Set-FileBeats -Port 9999 3>&1) -match "Could Not Find an Environment Matching Port 9999" } | Should -BeTrue } It "Uses the Specified Port in the Configuration" { $sampleFile | Out-File $tempPath -Force $global:OutFile = $null Mock -ModuleName $moduleForMock -CommandName Out-File { $global:OutFile = $_ } foreach ($testPort in $validPorts) { Set-FileBeats -Port $testPort -ConfigFilePath $tempPath -SourceConfigurationPath $fileBeatsDefaultSourceConfigPath $global:OutFile -match "orb.os-logstash.haystack.prod.alkami.net:$testPort" | Should -BeTrue } } } Context "When a ConfigPath is Specified" { # Must use explicit PARAM for "SourceConfigurationPath" because ps1 path is deeper than psm1 path It "Should Throw if the File Does Not Exist" { { Set-FileBeats -Port $validPorts[0] -TargetConfigPath "aisjdfioajsofijasdoifasd" } | Should -Throw } It "Should Throw if the Parameter is a Path" { { Set-FileBeats -Port $validPorts[0] -TargetConfigPath "C:\Windows" } | Should -Throw } It "Should Use the Specified Path" { Clean-TempPath $sampleFile | Out-File $tempPath -Force Set-FileBeats -Port $validPorts[0] -TargetConfigPath $tempPath -SourceConfigurationPath $fileBeatsDefaultSourceConfigPath $tempPath | Should -FileContentMatch "orb.os-logstash.haystack.prod.alkami.net:$($validPorts[0])" } } Context "When a Custom Source Configuration File is Provided" { It "Should Throw if the File Doesn't Exist" { $nonExistantFile = [System.IO.Path]::GetTempFileName() { Set-FileBeats -Port $validPorts[0] -SourceConfigurationPath $nonExistantFile } | Should -Throw } It "Should Use the Custom Source File" { $newTempFile = [System.IO.Path]::GetTempFileName() $tempConfigPath = (Get-ChildItem $newTempFile).Directory.FullName $sampleFile | Out-File (Join-Path $tempConfigPath "filebeat.yml") -Force Set-FileBeats -Port $validPorts[0] -SourceConfigurationPath $tempConfigPath -TargetConfigPath $newTempFile $newTempFile | Should -FileContentMatch "This is Custom Testing Stuff" } } Context "Server Role Instrumentation" { It "Should Use the Parameter as Provided from the Command Line" { Clean-TempPath $sampleFile | Out-File $tempPath -Force Set-FileBeats -Port $validPorts[0] -ServerRole "Foo" -TargetConfigPath $tempPath -SourceConfigurationPath $fileBeatsDefaultSourceConfigPath $tempPath | Should -FileContentMatch "serverRole: Foo" } It "Should Use 'Web' if IfWebServer is True and No Parameter is Provided" { Mock -ModuleName $moduleForMock -CommandName Test-IsAppServer { return $false } Mock -ModuleName $moduleForMock -CommandName Test-IsWebServer { return $true } Clean-TempPath $sampleFile | Out-File $tempPath -Force Set-FileBeats -Port $validPorts[0] -TargetConfigPath $tempPath -SourceConfigurationPath $fileBeatsDefaultSourceConfigPath $tempPath | Should -FileContentMatch "serverRole: Web" } It "Should Use 'App' if IfAppServer is True and No Parameter is Provided" { Mock -ModuleName $moduleForMock -CommandName Test-IsAppServer { return $true } Mock -ModuleName $moduleForMock -CommandName Test-IsWebServer { return $false } Clean-TempPath $sampleFile | Out-File $tempPath -Force Set-FileBeats -Port $validPorts[0] -TargetConfigPath $tempPath -SourceConfigurationPath $fileBeatsDefaultSourceConfigPath $tempPath | Should -FileContentMatch "serverRole: App" } It "Should Use 'Unknown' if IfAppServer is false and Test-IsWebServer is false and No Parameter is Provided" { Mock -ModuleName $moduleForMock -CommandName Test-IsAppServer { return $false } Mock -ModuleName $moduleForMock -CommandName Test-IsWebServer { return $false } Clean-TempPath $sampleFile | Out-File $tempPath -Force Set-FileBeats -Port $validPorts[0] -TargetConfigPath $tempPath -SourceConfigurationPath $fileBeatsDefaultSourceConfigPath $tempPath | Should -FileContentMatch "serverRole: Unknown" } It "Should Use 'Unknown' if IfAppServer / Test-IsWebServer throws and No Parameter is Provided" { Clean-TempPath $sampleFile | Out-File $tempPath -Force Set-FileBeats -Port $validPorts[0] -TargetConfigPath $tempPath -SourceConfigurationPath $fileBeatsDefaultSourceConfigPath $tempPath | Should -FileContentMatch "serverRole: Unknown" } } Context "Server Group Instrumentation" { It "Should Use the Parameter as Provided from the Command Line" { Clean-TempPath $sampleFile | Out-File $tempPath -Force Set-FileBeats -Port $validPorts[0] -ServerGroup "Foo" -TargetConfigPath $tempPath -SourceConfigurationPath $fileBeatsDefaultSourceConfigPath $tempPath | Should -FileContentMatch "serverGroup: 'Foo'" } It "Should Use the Environmental Variable POD if Present" { # REQUIRES Admin - WILL FAIL IN VSCODE [Environment]::SetEnvironmentVariable("POD", "FooBar", [System.EnvironmentVariableTarget]::Machine) $sampleFile | Out-File $tempPath -Force Set-FileBeats -Port $validPorts[0] -TargetConfigPath $tempPath -SourceConfigurationPath $fileBeatsDefaultSourceConfigPath # REQUIRES Admin - WILL FAIL IN VSCODE [Environment]::SetEnvironmentVariable("POD", $null, [System.EnvironmentVariableTarget]::Machine) $tempPath | Should -FileContentMatch "serverGroup: 'FooBar'" } it "Should use 'unknown' if no parameter as provided and the env variable is not set" { Clean-TempPath $samplefile | Out-File $temppath -Force Set-FileBeats -Port $validports[0] -TargetConfigPath $temppath -SourceConfigurationPath $fileBeatsDefaultSourceConfigPath $tempPath | Should -FileContentMatch "serverGroup: 'Unknown'" } } Context "Server Timezone Instrumentation" { It "Should Use the Parameter as Provided from the Command Line" { Clean-TempPath $sampleFile | Out-File $tempPath -Force Set-FileBeats -Port $validPorts[0] -Timezone "Foo" -TargetConfigPath $tempPath -SourceConfigurationPath $fileBeatsDefaultSourceConfigPath $tempPath | Should -FileContentMatch "timezone: Foo" } It "Should Be Chicago if not in AWS" { Mock -ModuleName $moduleForMock -CommandName Test-IsAws { $false } Clean-TempPath $sampleFile | Out-File $tempPath -Force Set-FileBeats -Port $validPorts[0] -TargetConfigPath $tempPath -SourceConfigurationPath $fileBeatsDefaultSourceConfigPath $tempPath | Should -FileContentMatch "timezone: America/Chicago" } It "Should Be UTC if in AWS" { Mock -ModuleName $moduleForMock -CommandName Test-IsAws { $True } Clean-TempPath $sampleFile | Out-File $tempPath -Force Set-FileBeats -Port $validPorts[0] -TargetConfigPath $tempPath -SourceConfigurationPath $fileBeatsDefaultSourceConfigPath $tempPath | Should -FileContentMatch "timeZone: UTC" } } } #endregion Set-FileBeats