. $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 = "" $originalMachineName = $env:COMPUTERNAME Describe "Test-IsServiceFabricServer" { Context "Logic" { It "Returns True and Does Not Query AWS if the Server Role Environment Variable is fabric" { Mock -ModuleName $moduleForMock -CommandName Get-ServerRoleEnvironmentalVariable ` -MockWith {return "fabric" } Mock -ModuleName $moduleForMock -CommandName Test-IsAws ` -MockWith { return $true } Mock -ModuleName $moduleForMock -CommandName Get-CurrentInstanceTags ` -MockWith { return "InvalidTagValue" } Test-IsServiceFabricServer | Should -BeTrue Assert-MockCalled -CommandName Get-CurrentInstanceTags -ModuleName $moduleForMock -Times 0 -Exactly -Scope It } $nonMicEnvVars = @("Web", "App", "Microservice") foreach ($Global:envVar in $nonMicEnvVars) { It "Returns False and Does Not Query AWS if the Server Role Environment Variable is $envVar" { Mock -ModuleName $moduleForMock -CommandName Get-ServerRoleEnvironmentalVariable ` -MockWith {return $Global:envVar } Mock -ModuleName $moduleForMock -CommandName Test-IsAws ` -MockWith { return $true } Mock -ModuleName $moduleForMock -CommandName Get-CurrentInstanceTags ` -MockWith { return "InvalidTagValue" } Test-IsServiceFabricServer | Should -BeFalse Assert-MockCalled -CommandName Get-CurrentInstanceTags -ModuleName $moduleForMock -Times 0 -Exactly -Scope It } } It "Returns True and Does Not Query AWS if the Machine Name Starts with FAB" { Mock -ModuleName $moduleForMock -CommandName Get-ServerRoleEnvironmentalVariable ` -MockWith {return "InvalidRole" } Mock -ModuleName $moduleForMock -CommandName Test-IsAws ` -MockWith { return $true } Mock -ModuleName $moduleForMock -CommandName Get-CurrentInstanceTags ` -MockWith { return "InvalidTagValue" } $env:COMPUTERNAME = "FAB123456" Test-IsServiceFabricServer | Should -BeTrue Assert-MockCalled -CommandName Get-CurrentInstanceTags -ModuleName $moduleForMock -Times 0 -Exactly -Scope It $env:COMPUTERNAME = $originalMachineName } It "Returns False and Does Not Query AWS if the Machine Name Does Not Start with MIC and Test-IsAws Is False" { Mock -ModuleName $moduleForMock -CommandName Get-ServerRoleEnvironmentalVariable ` -MockWith {return "InvalidRole" } Mock -ModuleName $moduleForMock -CommandName Test-IsAws ` -MockWith { return $false } Mock -ModuleName $moduleForMock -CommandName Get-CurrentInstanceTags ` -MockWith { return "app:fab" } $env:COMPUTERNAME = "WEB123456" Test-IsServiceFabricServer | Should -BeFalse Assert-MockCalled -CommandName Get-CurrentInstanceTags -ModuleName $moduleForMock -Times 0 -Exactly -Scope It $env:COMPUTERNAME = $originalMachineName } It "Returns True if the AWS Tag app:role has a Value of app:fab" { Mock -ModuleName $moduleForMock -CommandName Get-ServerRoleEnvironmentalVariable ` -MockWith {return "InvalidRole" } Mock -ModuleName $moduleForMock -CommandName Test-IsAws ` -MockWith { return $true } Mock -ModuleName $moduleForMock -CommandName Get-CurrentInstanceTags ` -MockWith { return "app:fab" } $env:COMPUTERNAME = "WEB123456" Test-IsServiceFabricServer | Should -BeTrue Assert-MockCalled -CommandName Get-CurrentInstanceTags -ModuleName $moduleForMock -Times 1 -Exactly -Scope It ` -ParameterFilter { $tagName -eq "alk:role" } $env:COMPUTERNAME = $originalMachineName } It "Returns False if the AWS Tag app:role has a Value Other Than app:fab" { Mock -ModuleName $moduleForMock -CommandName Get-ServerRoleEnvironmentalVariable ` -MockWith {return "InvalidRole" } Mock -ModuleName $moduleForMock -CommandName Test-IsAws ` -MockWith { return $true } Mock -ModuleName $moduleForMock -CommandName Get-CurrentInstanceTags ` -MockWith { return "InvalidTagValue" } $env:COMPUTERNAME = "WEB123456" Test-IsServiceFabricServer | Should -BeFalse Assert-MockCalled -CommandName Get-CurrentInstanceTags -ModuleName $moduleForMock -Times 1 -Exactly -Scope It ` -ParameterFilter { $tagName -eq "alk:role" } $env:COMPUTERNAME = $originalMachineName } } } $env:COMPUTERNAME = $originalMachineName