. $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 Get-LocalNlbIp # Handle the AWS Native Functions When AWSPowerShell Not Available if ($null -eq (Get-Command "Get-EC2NetworkInterface" -ErrorAction SilentlyContinue)) { function Get-EC2NetworkInterface { throw "This Should Never Be Actually Called" } } Describe "Get-LocalNlbIp" { Mock -ModuleName $moduleForMock Test-IsAws { return $true } Mock -ModuleName $moduleForMock Get-EC2NetworkInterface { return $null } Mock -ModuleName $moduleForMock Get-CurrentInstance { $placementMock = New-Object PSObject -Property @{ "AvailabilityZone" = "us-east-1a"; } $pod6Instance = New-Object PSObject -Property @{ Placement = $placementMock; Tag = @( @{Key = $Global:AlkamiTagKeyRole; Value = $Global:AlkamiTagValueRoleWeb}, @{Key = $Global:AlkamiTagKeyEnvironment; Value = $Global:AlkamiTagValueEnvironmentProd}, @{Key = "alk:pod"; Value = "6"} ); } return $pod6Instance } Context "When Environment Names Overlap" { Mock -ModuleName $moduleForMock Get-EC2NetworkInterface { # args[1] is an ec2 filter based on the AvailabilityZone from the current instance # https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/EC2/TFilter.html $ec2Filter = $args[1] Write-Warning "Using EC2 Filter Value: $($ec2Filter.Values)" $possibleReturns = @( @{ "Description"="ELB net/12.6-prod-nlb-something"; Id="Test"; "AvailabilityZone"="us-east-1a"; "PrivateIpAddress"="8.8.8.8"; InterfaceType="network_load_balancer"}, @{ "Description"="ELB net/6-prod-nlb-something"; Id="Test"; "AvailabilityZone"="us-east-1a"; "PrivateIpAddress"="100.100.100.100"; InterfaceType="network_load_balancer"} ) return $possibleReturns | Where-Object {$_.AvailabilityZone -like $ec2Filter.Values} } It "Returns the Correct IP When Searching for POD 6 and 12.6 is Available" { Get-LocalNlbIp | Should -Be "100.100.100.100" } } Context "When the Environment is an App Server" { Mock -ModuleName $moduleForMock Get-CurrentInstance { $placementMock = New-Object PSObject -Property @{ "AvailabilityZone" = "us-east-1a"; } $pod6Instance = New-Object PSObject -Property @{ Placement = $placementMock; Tag = @( @{Key = $Global:AlkamiTagKeyRole; Value = $Global:AlkamiTagValueRoleApp}, @{Key = $Global:AlkamiTagKeyEnvironment; Value = $Global:AlkamiTagValueEnvironmentProd}, @{Key = "alk:pod"; Value = "6"} ); } return $pod6Instance } It "Writes a Warning" { ( Get-LocalNlbIp 3>&1 ) -match "This is currently running on an app server." | Should -Be $true } } Context "Environment Tag Switch" { $validEnvironments = @("Prod", "DR") foreach ($Global:podBasedEnvironment in $validEnvironments) { It "Uses the alk:pod tag when the environment is $podBasedEnvironment" { Mock -ModuleName $moduleForMock Get-CurrentInstance { $placementMock = New-Object PSObject -Property @{ "AvailabilityZone" = "us-east-1a"; } $pod6Instance = New-Object PSObject -Property @{ Placement = $placementMock; Tag = @( @{Key = $Global:AlkamiTagKeyRole; Value = $Global:AlkamiTagValueRoleWeb}, @{Key = $Global:AlkamiTagKeyEnvironment; Value = "$podBasedEnvironment"}, @{Key = "alk:pod"; Value = "Pass"}, @{Key = "alk:lane"; Value = "Fail"} ); } return $pod6Instance } Mock -ModuleName $moduleForMock Get-EC2NetworkInterface { # args[1] is an ec2 filter based on the NLB name constructed from the current instance # https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/EC2/TFilter.html $ec2Filter = $args[1] Write-Warning "Using EC2 Filter Value: $($ec2Filter.Values)" $possibleReturns = @( @{ "Description"="ELB net/Pass-$podBasedEnvironment-nlb-something"; Id="Test"; "AvailabilityZone"="us-east-1a"; "PrivateIpAddress"="100.100.100.100"; InterfaceType="network_load_balancer"}, @{ "Description"="ELB net/Fail-$podBasedEnvironment-nlb-something"; Id="Test"; "AvailabilityZone"="us-east-1a"; "PrivateIpAddress"="9.9.9.9"; InterfaceType="network_load_balancer"}, @{ "Description"="ELB net/Fail-staging-nlb-something"; Id="Test"; "AvailabilityZone"="us-east-1a"; "PrivateIpAddress"="8.8.8.8"; InterfaceType="network_load_balancer"} ) return $possibleReturns | Where-Object {$_.AvailabilityZone -like $ec2Filter.Values} } Get-LocalNlbIp | Should -Be 100.100.100.100 } } It "Uses the alk:lane tag when the environment is Staging" { Mock -ModuleName $moduleForMock Get-CurrentInstance { $placementMock = New-Object PSObject -Property @{ "AvailabilityZone" = "us-east-1a"; } $pod6Instance = New-Object PSObject -Property @{ Placement = $placementMock; Tag = @( @{Key = $Global:AlkamiTagKeyRole; Value = $Global:AlkamiTagValueRoleWeb}, @{Key = $Global:AlkamiTagKeyEnvironment; Value = $Global:AlkamiTagValueEnvironmentStaging}, @{Key = "alk:pod"; Value = "Fail"}, @{Key = "alk:lane"; Value = "Pass"} ); } return $pod6Instance } Mock -ModuleName $moduleForMock Get-EC2NetworkInterface { # args[1] is an ec2 filter based on the NLB name constructed from the current instance # https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/EC2/TFilter.html $ec2Filter = $args[1] Write-Warning "Using EC2 Filter Value: $($ec2Filter.Values)" $possibleReturns = @( @{ "Description"="ELB net/Pass-staging-nlb-something"; Id="Test"; "AvailabilityZone"="us-east-1a"; "PrivateIpAddress"="100.100.100.100"; InterfaceType="network_load_balancer"}, @{ "Description"="ELB net/Fail-staging-nlb-something"; Id="Test"; "AvailabilityZone"="us-east-1a"; "PrivateIpAddress"="9.9.9.9"; InterfaceType="network_load_balancer"} @{ "Description"="ELB net/Fail-prod-nlb-something"; Id="Test"; "AvailabilityZone"="us-east-1a"; "PrivateIpAddress"="8.8.8.8"; InterfaceType="network_load_balancer"} ) return $possibleReturns | Where-Object {$_.AvailabilityZone -like $ec2Filter.Values} } Get-LocalNlbIp | Should -Be 100.100.100.100 } $validEnvironments = @("Dev", "QA") foreach ($Global:designationBasedEnvironment in $validEnvironments) { It "Uses the alk:designation tag when the environment is $designationBasedEnvironment" { Mock -ModuleName $moduleForMock Get-CurrentInstance { $placementMock = New-Object PSObject -Property @{ "AvailabilityZone" = "us-east-1a"; } $instance = New-Object PSObject -Property @{ Placement = $placementMock; Tag = @( @{Key = $Global:AlkamiTagKeyRole; Value = $Global:AlkamiTagValueRoleWeb}, @{Key = $Global:AlkamiTagKeyEnvironment; Value = "$designationBasedEnvironment"}, @{Key = "alk:pod"; Value = "Fail"}, @{Key = "alk:lane"; Value = "Fail"}, @{Key = "alk:designation"; Value = "Pass"} ); } return $instance } Mock -ModuleName $moduleForMock Get-EC2NetworkInterface { # args[1] is an ec2 filter based on the NLB name constructed from the current instance # https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/EC2/TFilter.html $ec2Filter = $args[1] Write-Warning "Using EC2 Filter Value: $($ec2Filter.Values)" $possibleReturns = @( @{ "Description"="ELB net/Pass-$designationBasedEnvironment-nlb-something"; Id="Test"; "AvailabilityZone"="us-east-1a"; "PrivateIpAddress"="100.100.100.100"; InterfaceType="network_load_balancer"}, @{ "Description"="ELB net/Fail-$designationBasedEnvironment-nlb-something"; Id="Test"; "AvailabilityZone"="us-east-1a"; "PrivateIpAddress"="9.9.9.9"; InterfaceType="network_load_balancer"} @{ "Description"="ELB net/Fail-prod-nlb-something"; Id="Test"; "AvailabilityZone"="us-east-1a"; "PrivateIpAddress"="8.8.8.8"; InterfaceType="network_load_balancer"} ) return $possibleReturns | Where-Object {$_.AvailabilityZone -like $ec2Filter.Values} } Get-LocalNlbIp | Should -Be 100.100.100.100 } } } Context "Error Scenarios" { Mock -ModuleName $moduleForMock Get-EC2NetworkInterface { # args[1] is an ec2 filter based on the NLB name constructed from the current instance # https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/EC2/TFilter.html $ec2Filter = $args[1] Write-Warning "Using EC2 Filter Value: $($ec2Filter.Values)" $possibleReturns = @( @{ "Description"="ELB net/Pass-$podBasedEnvironment-nlb-something"; Id="Test"; "AvailabilityZone"="us-east-1a"; "PrivateIpAddress"="100.100.100.100"; InterfaceType="network_load_balancer"}, @{ "Description"="ELB net/Pass-$podBasedEnvironment-nlb-something"; Id="Test"; "AvailabilityZone"="us-east-1a"; "PrivateIpAddress"="111.111.111.111"; InterfaceType="network_load_balancer"} ) return $possibleReturns | Where-Object {$_.AvailabilityZone -like $ec2Filter.Values} } It "Writes a Warning and Returns Null When No Match Found" { Mock -ModuleName $moduleForMock Get-CurrentInstance { $placementMock = New-Object PSObject -Property @{ "AvailabilityZone" = "us-east-999"; } $pod6Instance = New-Object PSObject -Property @{ Placement = $placementMock; Tag = @( @{Key = $Global:AlkamiTagKeyRole; Value = $Global:AlkamiTagValueRoleWeb}, @{Key = $Global:AlkamiTagKeyEnvironment; Value = "$podBasedEnvironment"}, @{Key = "alk:pod"; Value = "Pass"}, @{Key = "alk:lane"; Value = "Fail"} ); } return $pod6Instance } Mock -CommandName Write-Warning -MockWith {} -ModuleName $moduleForMock Get-LocalNlbIp | Should -BeNullOrEmpty Assert-MockCalled -CommandName Write-Warning -Times 1 -Exactly -Scope It ` -ModuleName $moduleForMock -ParameterFilter { $Message -match "No ENIs found with Description" } } It "Writes a Warning and Returns Null When More Than One Match Found" { Mock -ModuleName $moduleForMock Get-CurrentInstance { $placementMock = New-Object PSObject -Property @{ "AvailabilityZone" = "us-east-1a"; } $pod6Instance = New-Object PSObject -Property @{ Placement = $placementMock; Tag = @( @{Key = $Global:AlkamiTagKeyRole; Value = $Global:AlkamiTagValueRoleWeb}, @{Key = $Global:AlkamiTagKeyEnvironment; Value = "$podBasedEnvironment"}, @{Key = "alk:pod"; Value = "Pass"}, @{Key = "alk:lane"; Value = "Fail"} ); } return $pod6Instance } Mock -CommandName Write-Warning -MockWith {} -ModuleName $moduleForMock Get-LocalNlbIp | Should -BeNullOrEmpty Assert-MockCalled -CommandName Write-Warning -Times 1 -Exactly -Scope It ` -ModuleName $moduleForMock -ParameterFilter { $Message -match "2 ENIs found with Description" } } } } #endregion Get-LocalNlbIp