111 lines
4.0 KiB
PowerShell
111 lines
4.0 KiB
PowerShell
|
. $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 "Test Get-DnsByIP" {
|
||
|
|
||
|
BeforeAll {
|
||
|
#create a module for unavailable commands
|
||
|
$testModule = New-Module -Name DnsServer -ScriptBlock {
|
||
|
function Get-DnsServerZone (){"Get-DnsServerZone"}
|
||
|
function Get-DnsServerResourceRecord (){"Get-DnsServerResourceRecord"}
|
||
|
}
|
||
|
|
||
|
$testModule | Import-Module
|
||
|
}
|
||
|
|
||
|
Mock -CommandName Get-LogLeadName -ModuleName $moduleForMock -MockWith { return 'Get-DnsByIP.tests' }
|
||
|
Mock -CommandName Write-Error -ModuleName $moduleForMock -MockWith { return $true }
|
||
|
# Pretend that the Get-DnsServerZone cmdlet is ALWAYS available
|
||
|
Mock -CommandName Get-Command -ModuleName $moduleForMock -MockWith { return $true }
|
||
|
Mock -CommandName Get-DnsServerZone -ModuleName $moduleForMock -MockWith {return [pscustomobject]@{"ZoneName" = "TestZone"}}
|
||
|
# Provide a set of fake objects to check against at the end of the function
|
||
|
$mockRecords = {
|
||
|
$records = @()
|
||
|
$records += [PSCustomObject]@{
|
||
|
Name = 'VALID'
|
||
|
RecordData = @{
|
||
|
IPv4Address = @{
|
||
|
IPAddressToString = '192.168.4.3'
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
$records += [PSCustomObject]@{
|
||
|
Name = 'NOT-VALID'
|
||
|
RecordData = @{
|
||
|
IPv4Address = @{
|
||
|
IPAddressToString = '192.168.4.7'
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return $records
|
||
|
}
|
||
|
|
||
|
Mock -CommandName Get-DnsServerResourceRecord -ModuleName $moduleForMock -MockWith $mockRecords
|
||
|
|
||
|
Context "Testing" {
|
||
|
|
||
|
It "Executes properly when Get-Command is not found" {
|
||
|
|
||
|
Mock -CommandName Get-Command -ModuleName $moduleForMock -MockWith { return $false }
|
||
|
|
||
|
Get-DnsByIP -TargetIP 192.168.4.55 -DnsServer 'dc314212.fh.local'
|
||
|
|
||
|
Assert-MockCalled -CommandName Get-Command -Times 1 -Exactly -Scope It
|
||
|
|
||
|
}
|
||
|
|
||
|
It "Calls cmdlets the correct amount of times with the -DnsServer parameter" {
|
||
|
|
||
|
Mock -CommandName Get-Command -ModuleName $moduleForMock -MockWith { return $true }
|
||
|
|
||
|
Get-DnsByIP -TargetIP 192.168.4.3 -DnsServer 'dc314212.fh.local'
|
||
|
|
||
|
Assert-MockCalled -CommandName Get-DnsServerResourceRecord -Times 1 -Exactly -Scope It
|
||
|
Assert-MockCalled -CommandName Get-DnsServerZone -Times 1 -Exactly -Scope It
|
||
|
|
||
|
}
|
||
|
|
||
|
It "Calls cmdlets properly without the -DnsServer parameter" {
|
||
|
|
||
|
Get-DnsByIP -TargetIP 192.168.4.55
|
||
|
|
||
|
Assert-MockCalled -CommandName Get-DnsServerZone -Times 1 -Exactly -Scope It
|
||
|
Assert-MockCalled -CommandName "Get-DnsServerResourceRecord" -Times 1 -Exactly -Scope It
|
||
|
|
||
|
}
|
||
|
|
||
|
It "Properly finds only the right resource records" {
|
||
|
|
||
|
$result = Get-DnsByIP -TargetIP 192.168.4.3
|
||
|
|
||
|
$result.RecordData.IPv4Address.IPAddressToString | Should -Be '192.168.4.3'
|
||
|
|
||
|
}
|
||
|
|
||
|
It "Returns an empty array if no matches are found, remote DNS Server specified" {
|
||
|
$result = Get-DnsByIP -DNSServer "ADC.local" -IPAddress "192.168.4.55"
|
||
|
|
||
|
$result | Should -Be @()
|
||
|
Assert-MockCalled -CommandName Get-DnsServerZone -Times 1 -Exactly -Scope It
|
||
|
Assert-MockCalled -CommandName Get-DnsServerResourceRecord -Times 1 -Exactly -Scope It
|
||
|
|
||
|
}
|
||
|
|
||
|
It "Returns an empty array if no matches are found, remote DNS Server not specified" {
|
||
|
$result = Get-DnsByIP -TargetIP "192.168.4.55"
|
||
|
|
||
|
$result | Should -Be @()
|
||
|
Assert-MockCalled -CommandName Get-DnsServerZone -Times 1 -Exactly -Scope It
|
||
|
Assert-MockCalled -CommandName Get-DnsServerResourceRecord -Times 1 -Exactly -Scope It
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|