. $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 = "" Describe "Get-InstanceHostname" { # Load up AWSPowerShell for mocking if available $awsPowerShellLoaded = $false if ($null -ne (Get-Module -ListAvailable AWSPowerShell)) { Import-AWSModule # EC2 $awsPowerShellLoaded = $true } else { Write-Warning "AWSPowerShell Module *NOT* installed. Some tests will not execute." } Context "Parameter Validation" { It "Null Instance Should Throw" { if (!($awsPowerShellLoaded)) { Set-ItResult -Inconclusive -Because "AWSPowerShell Not Installed" continue; } { Get-InstanceHostname -Instance $null } | Should Throw } It "Invalid Parameter Type Should Throw" { if (!($awsPowerShellLoaded)) { Set-ItResult -Inconclusive -Because "AWSPowerShell Not Installed" continue; } { Get-InstanceHostname -Instance 'test' } | Should Throw } } Context "Hostname Determined By Instance Tag" { Mock -CommandName Write-Warning -MockWith {} -ModuleName $moduleForMock It "Writes Warning When Hostname Tag Does Not Exist" { if (!($awsPowerShellLoaded)) { Set-ItResult -Inconclusive -Because "AWSPowerShell Not Installed" continue; } $testInstance = (New-Object Amazon.EC2.Model.Instance) $testInstance.InstanceId = 'Test' Get-InstanceHostname -Instance $testInstance | Out-Null Assert-MockCalled -CommandName Write-Warning -Times 1 -Exactly -Scope It ` -ModuleName $moduleForMock -ParameterFilter { $Message -match "No hostname found" } } It "Returns Null When Hostname Tag Does Not Exist" { if (!($awsPowerShellLoaded)) { Set-ItResult -Inconclusive -Because "AWSPowerShell Not Installed" continue; } $testInstance = (New-Object Amazon.EC2.Model.Instance) $testInstance.InstanceId = 'Test' ( Get-InstanceHostname -Instance $testInstance ) | Should -BeNull } It "Writes Warning When Hostname Tag Is Null" { if (!($awsPowerShellLoaded)) { Set-ItResult -Inconclusive -Because "AWSPowerShell Not Installed" continue; } $testTag = (New-Object Amazon.EC2.Model.Tag($Global:AlkamiTagKeyHostName, $null)) $testInstance = (New-Object Amazon.EC2.Model.Instance) $testInstance.Tags.Add($testTag) $testInstance.InstanceId = 'Test' Get-InstanceHostname -Instance $testInstance | Out-Null Assert-MockCalled -CommandName Write-Warning -Times 1 -Exactly -Scope It ` -ModuleName $moduleForMock -ParameterFilter { $Message -match "No hostname found" } } It "Returns Null When Hostname Tag Is Null" { if (!($awsPowerShellLoaded)) { Set-ItResult -Inconclusive -Because "AWSPowerShell Not Installed" continue; } $testTag = (New-Object Amazon.EC2.Model.Tag($Global:AlkamiTagKeyHostName, $null)) $testInstance = (New-Object Amazon.EC2.Model.Instance) $testInstance.Tags.Add($testTag) $testInstance.InstanceId = 'Test' ( Get-InstanceHostname -Instance $testInstance ) | Should -BeNull } It "Writes Warning When Hostname Tag Is Null" { if (!($awsPowerShellLoaded)) { Set-ItResult -Inconclusive -Because "AWSPowerShell Not Installed" continue; } $testTag = (New-Object Amazon.EC2.Model.Tag($Global:AlkamiTagKeyHostName, '')) $testInstance = (New-Object Amazon.EC2.Model.Instance) $testInstance.Tags.Add($testTag) $testInstance.InstanceId = 'Test' Get-InstanceHostname -Instance $testInstance | Out-Null Assert-MockCalled -CommandName Write-Warning -Times 1 -Exactly -Scope It ` -ModuleName $moduleForMock -ParameterFilter { $Message -match "No hostname found" } } It "Returns Null When Hostname Tag Is Null" { if (!($awsPowerShellLoaded)) { Set-ItResult -Inconclusive -Because "AWSPowerShell Not Installed" continue; } $testTag = (New-Object Amazon.EC2.Model.Tag($Global:AlkamiTagKeyHostName, '')) $testInstance = (New-Object Amazon.EC2.Model.Instance) $testInstance.Tags.Add($testTag) $testInstance.InstanceId = 'Test' ( Get-InstanceHostname -Instance $testInstance ) | Should -BeNull } It "Does Not Write Warning When Hostname Tag Is Valid" { if (!($awsPowerShellLoaded)) { Set-ItResult -Inconclusive -Because "AWSPowerShell Not Installed" continue; } $testTag = (New-Object Amazon.EC2.Model.Tag($Global:AlkamiTagKeyHostName, 'Test')) $testInstance = (New-Object Amazon.EC2.Model.Instance) $testInstance.Tags.Add($testTag) $testInstance.InstanceId = 'Test' Get-InstanceHostname -Instance $testInstance | Out-Null Assert-MockCalled -CommandName Write-Warning -Times 0 -Exactly -Scope It ` -ModuleName $moduleForMock } It "Returns Uppercased Hostname Tag Value When Hostname Tag Is Valid" { if (!($awsPowerShellLoaded)) { Set-ItResult -Inconclusive -Because "AWSPowerShell Not Installed" continue; } $testTag = (New-Object Amazon.EC2.Model.Tag($Global:AlkamiTagKeyHostName, 'Test')) $testInstance = (New-Object Amazon.EC2.Model.Instance) $testInstance.Tags.Add($testTag) $testInstance.InstanceId = 'Test' ( Get-InstanceHostname -Instance $testInstance ) | Should -BeExactly 'TEST' } } }