Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,47 @@

Import-Module HelpersCommon

function GetGatewayAddress
{
return [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() |
Where-Object { $_.OperationalStatus -eq 'Up' -and $_.NetworkInterfaceType -ne 'Loopback' } |
ForEach-Object { $_.GetIPProperties().GatewayAddresses } |
Select-Object -First 1 |
ForEach-Object { $_.Address.IPAddressToString }
}

function GetExternalHostAddress([string]$HostName)
{
if (-not $HostName)
{
return
}

try
{
return [System.Net.Dns]::GetHostEntry($HostName).AddressList |
Where-Object { $_.AddressFamily -eq 'InterNetwork' } |
Select-Object -First 1 |
ForEach-Object { $_.IPAddressToString }
}
catch
{
return
}
}

Describe "Test-Connection" -tags "CI" {
BeforeAll {
$hostName = [System.Net.Dns]::GetHostName()
$gatewayAddress = GetGatewayAddress
$publicHostAddress = GetExternalHostAddress -HostName $hostName

$testAddress = if ($publicHostAddress) { $publicHostAddress } else { $gatewayAddress }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking, but could have used the null conditional assignment operator here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to port back to 6.2 -- deliberately removed new operators in most recent commit


$targetName = "localhost"
$targetAddress = "127.0.0.1"
$targetAddressIPv6 = "::1"
$UnreachableAddress = "10.11.12.13"
# this resolves to an actual IP rather than 127.0.0.1
# this can also include both IPv4 and IPv6, so select InterNetwork rather than InterNetworkV6
$realAddress = [System.Net.Dns]::GetHostEntry($hostName).AddressList |
Where-Object { $_.AddressFamily -eq "InterNetwork" } |
Select-Object -First 1 |
ForEach-Object { $_.IPAddressToString }
# under some environments, we can't round trip this and retrieve the real name from the address
# in this case we will simply use the hostname
$jobContinues = Start-Job { Test-Connection $using:targetAddress -Repeat }
Expand Down Expand Up @@ -77,35 +105,35 @@ Describe "Test-Connection" -tags "CI" {
$error[0].Exception.InnerException.ErrorCode | Should -Be $code
}

# In VSTS, address is 0.0.0.0. Making pending due to instability in Az DevOps.
It "Force IPv4 with implicit PingOptions" -Pending:($IsMacOS) {
$result = Test-Connection $hostName -Count 1 -IPv4
It "Force IPv4 with implicit PingOptions" {
$result = Test-Connection $testAddress -Count 1 -IPv4

$result[0].Address | Should -BeExactly $realAddress
$result[0].Address | Should -BeExactly $testAddress
$result[0].Reply.Options.Ttl | Should -BeLessOrEqual 128
if ($IsWindows) {
$result[0].Reply.Options.DontFragment | Should -BeFalse
}
}

# In VSTS, address is 0.0.0.0
# This test is marked as PENDING as .NET Core does not return correct PingOptions from ping request
It "Force IPv4 with explicit PingOptions" -Pending {
$result1 = Test-Connection $hostName -Count 1 -IPv4 -MaxHops 10 -DontFragment
It "Force IPv4 with explicit PingOptions" {
$result1 = Test-Connection $testAddress -Count 1 -IPv4 -MaxHops 10 -DontFragment

# explicitly go to google dns. this test will pass even if the destination is unreachable
# it's more about breaking out of the loop
$result2 = Test-Connection 8.8.8.8 -Count 1 -IPv4 -MaxHops 1 -DontFragment

$result1.Address | Should -BeExactly $realAddress
$result1.Address | Should -BeExactly $testAddress
$result1.Reply.Options.Ttl | Should -BeLessOrEqual 128

if (!$IsWindows) {
$result1.Reply.Options.DontFragment | Should -BeFalse
# Depending on the network configuration any of the following should be returned
$result2.Status | Should -BeIn "TtlExpired", "TimedOut", "Success"
} else {
$result1.Reply.Options.DontFragment | Should -BeTrue
# This assertion currently fails, see https://github.com/PowerShell/PowerShell/issues/12967
#$result1.Reply.Options.DontFragment | Should -BeTrue
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #12967 for this


# We expect 'TtlExpired' but if a router don't reply we get `TimedOut`
# AzPipelines returns $null
$result2.Status | Should -BeIn "TtlExpired", "TimedOut", $null
Expand Down Expand Up @@ -232,34 +260,33 @@ Describe "Test-Connection" -tags "CI" {
# We skip the MtuSize detection tests when in containers, as the environments throw raw exceptions
# instead of returning a PacketTooBig response cleanly.
It "MTUSizeDetect works" -Pending:($env:__INCONTAINER -eq 1) {
$result = Test-Connection $hostName -MtuSize
$result = Test-Connection $testAddress -MtuSize

$result | Should -BeOfType Microsoft.PowerShell.Commands.TestConnectionCommand+PingMtuStatus
$result.Destination | Should -BeExactly $hostName
$result.Destination | Should -BeExactly $testAddress
$result.Status | Should -BeExactly "Success"
$result.MtuSize | Should -BeGreaterThan 0
}

It "Quiet works" -Pending:($env:__INCONTAINER -eq 1) {
$result = Test-Connection $hostName -MtuSize -Quiet
$result = Test-Connection $gatewayAddress -MtuSize -Quiet

$result | Should -BeOfType Int32
$result | Should -BeGreaterThan 0
}
}

Context "TraceRoute" {
# Mark it as pending due to instability in Az DevOps
It "TraceRoute works" -Pending:($IsMacOS) {
It "TraceRoute works" {
# real address is an ipv4 address, so force IPv4
$result = Test-Connection $hostName -TraceRoute -IPv4
$result = Test-Connection $testAddress -TraceRoute -IPv4

$result[0] | Should -BeOfType Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus
$result[0].Source | Should -BeExactly $hostName
$result[0].TargetAddress | Should -BeExactly $realAddress
$result[0].Target | Should -BeExactly $hostName
$result[0].TargetAddress | Should -BeExactly $testAddress
$result[0].Target | Should -BeExactly $testAddress
$result[0].Hop | Should -Be 1
$result[0].HopAddress | Should -BeExactly $realAddress
$result[0].HopAddress.IPAddressToString | Should -BeExactly $testAddress
$result[0].Status | Should -BeExactly "Success"
if (!$IsWindows) {
$result[0].Reply.Buffer.Count | Should -Match '^0$|^32$'
Expand Down