GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merged PR 12007: Remove dependency on DNS for `Test-Connection` tests… · PowerShell/PowerShell@408dbaf

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • test/powershell/Modules/Microsoft.PowerShell.Management
    • Test-Connection.Tests.ps1
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1‎

Copy file name to clipboardExpand all lines: test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1
+63-37Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,49 @@
1-
# Copyright (c) Microsoft Corporation. All rights reserved.
1+
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

44
Import-Module HelpersCommon
55

6+
function GetGatewayAddress
7+
{
8+
return [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() |
9+
Where-Object { $_.OperationalStatus -eq 'Up' -and $_.NetworkInterfaceType -ne 'Loopback' } |
10+
ForEach-Object { $_.GetIPProperties().GatewayAddresses } |
11+
Select-Object -First 1 |
12+
ForEach-Object { $_.Address.IPAddressToString }
13+
}
14+
15+
function GetExternalHostAddress([string]$HostName)
16+
{
17+
if (-not $HostName)
18+
{
19+
return
20+
}
21+
22+
try
23+
{
24+
return [System.Net.Dns]::GetHostEntry($HostName).AddressList |
25+
Where-Object { $_.AddressFamily -eq 'InterNetwork' } |
26+
Select-Object -First 1 |
27+
ForEach-Object { $_.IPAddressToString }
28+
}
29+
catch
30+
{
31+
return
32+
}
33+
}
34+
635
Describe "Test-Connection" -tags "CI" {
736
BeforeAll {
837
$hostName = [System.Net.Dns]::GetHostName()
38+
$gatewayAddress = GetGatewayAddress
39+
$publicHostAddress = GetExternalHostAddress -HostName $hostName
40+
41+
$testAddress = if ($publicHostAddress) { $publicHostAddress } else { $gatewayAddress }
42+
943
$targetName = "localhost"
1044
$targetAddress = "127.0.0.1"
1145
$targetAddressIPv6 = "::1"
1246
$UnreachableAddress = "10.11.12.13"
13-
# this resolves to an actual IP rather than 127.0.0.1
14-
# this can also include both IPv4 and IPv6, so select InterNetwork rather than InterNetworkV6
15-
$realAddress = [System.Net.Dns]::GetHostEntry($hostName).AddressList |
16-
Where-Object { $_.AddressFamily -eq "InterNetwork" } |
17-
Select-Object -First 1 |
18-
ForEach-Object { $_.IPAddressToString }
1947
# under some environments, we can't round trip this and retrieve the real name from the address
2048
# in this case we will simply use the hostname
2149
$jobContinues = Start-Job { Test-Connection $using:targetAddress -Repeat }
@@ -27,17 +55,17 @@ Describe "Test-Connection" -tags "CI" {
2755
$pingResults.Count | Should -Be 4
2856

2957
$result = $pingResults |
30-
Where-Object Status -eq 'Success' |
58+
Where-Object Status -EQ 'Success' |
3159
Select-Object -First 1
3260

33-
$result | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus"
61+
$result | Should -BeOfType Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus
3462
$result.Ping | Should -Be 1
3563
$result.Source | Should -BeExactly $hostName
3664
$result.Destination | Should -BeExactly $targetName
3765
$result.Address | Should -BeIn @($targetAddress, $targetAddressIPv6)
3866
$result.Status | Should -BeExactly "Success"
39-
$result.Latency | Should -BeOfType "long"
40-
$result.Reply | Should -BeOfType "System.Net.NetworkInformation.PingReply"
67+
$result.Latency | Should -BeOfType long
68+
$result.Reply | Should -BeOfType System.Net.NetworkInformation.PingReply
4169
$result.BufferSize | Should -Be 32
4270
}
4371

@@ -60,7 +88,6 @@ Describe "Test-Connection" -tags "CI" {
6088
}
6189

6290
It "Ping fake host" {
63-
6491
{ $result = Test-Connection "fakeHost" -Count 1 -Quiet -ErrorAction Stop } |
6592
Should -Throw -ErrorId "TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand"
6693
# Error code = 11001 - Host not found.
@@ -76,35 +103,35 @@ Describe "Test-Connection" -tags "CI" {
76103
$error[0].Exception.InnerException.ErrorCode | Should -Be $code
77104
}
78105

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

83-
$result[0].Address | Should -BeExactly $realAddress
109+
$result[0].Address | Should -BeExactly $testAddress
84110
$result[0].Reply.Options.Ttl | Should -BeLessOrEqual 128
85111
if ($IsWindows) {
86112
$result[0].Reply.Options.DontFragment | Should -BeFalse
87113
}
88114
}
89115

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

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

99-
$result1.Address | Should -BeExactly $realAddress
124+
$result1.Address | Should -BeExactly $testAddress
100125
$result1.Reply.Options.Ttl | Should -BeLessOrEqual 128
101126

102127
if (!$IsWindows) {
103128
$result1.Reply.Options.DontFragment | Should -BeFalse
104129
# Depending on the network configuration any of the following should be returned
105130
$result2.Status | Should -BeIn "TtlExpired", "TimedOut", "Success"
106131
} else {
107-
$result1.Reply.Options.DontFragment | Should -BeTrue
132+
# This assertion currently fails, see https://github.com/PowerShell/PowerShell/issues/12967
133+
#$result1.Reply.Options.DontFragment | Should -BeTrue
134+
108135
# We expect 'TtlExpired' but if a router don't reply we get `TimedOut`
109136
# AzPipelines returns $null
110137
$result2.Status | Should -BeIn "TtlExpired", "TimedOut", $null
@@ -117,7 +144,7 @@ Describe "Test-Connection" -tags "CI" {
117144
# be a lack of or inconsistent support for IPv6 in CI environments.
118145
It "Allows us to Force IPv6" -Pending {
119146
$result = Test-Connection $targetName -IPv6 -Count 4 |
120-
Where-Object Status -eq Success |
147+
Where-Object Status -EQ Success |
121148
Select-Object -First 1
122149

123150
$result.Address | Should -BeExactly $targetAddressIPv6
@@ -126,7 +153,7 @@ Describe "Test-Connection" -tags "CI" {
126153

127154
It 'can convert IPv6 addresses to IPv4 with -IPv4 parameter' -Pending {
128155
$result = Test-Connection '2001:4860:4860::8888' -IPv4 -Count 4 |
129-
Where-Object Status -eq Success |
156+
Where-Object Status -EQ Success |
130157
Select-Object -First 1
131158
# Google's DNS can resolve to either address.
132159
$result.Address.IPAddressToString | Should -BeIn @('8.8.8.8', '8.8.4.4')
@@ -135,7 +162,7 @@ Describe "Test-Connection" -tags "CI" {
135162

136163
It 'can convert IPv4 addresses to IPv6 with -IPv6 parameter' -Pending {
137164
$result = Test-Connection '8.8.8.8' -IPv6 -Count 4 |
138-
Where-Object Status -eq Success |
165+
Where-Object Status -EQ Success |
139166
Select-Object -First 1
140167
# Google's DNS can resolve to either address.
141168
$result.Address.IPAddressToString | Should -BeIn @('2001:4860:4860::8888', '2001:4860:4860::8844')
@@ -231,34 +258,33 @@ Describe "Test-Connection" -tags "CI" {
231258
# We skip the MtuSize detection tests when in containers, as the environments throw raw exceptions
232259
# instead of returning a PacketTooBig response cleanly.
233260
It "MTUSizeDetect works" -Pending:($env:__INCONTAINER -eq 1) {
234-
$result = Test-Connection $hostName -MtuSize
261+
$result = Test-Connection $testAddress -MtuSize
235262

236-
$result | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+PingMtuStatus"
237-
$result.Destination | Should -BeExactly $hostName
263+
$result | Should -BeOfType Microsoft.PowerShell.Commands.TestConnectionCommand+PingMtuStatus
264+
$result.Destination | Should -BeExactly $testAddress
238265
$result.Status | Should -BeExactly "Success"
239266
$result.MtuSize | Should -BeGreaterThan 0
240267
}
241268

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

245-
$result | Should -BeOfType "Int32"
272+
$result | Should -BeOfType Int32
246273
$result | Should -BeGreaterThan 0
247274
}
248275
}
249276

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

256-
$result[0] | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus"
282+
$result[0] | Should -BeOfType Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus
257283
$result[0].Source | Should -BeExactly $hostName
258-
$result[0].TargetAddress | Should -BeExactly $realAddress
259-
$result[0].Target | Should -BeExactly $hostName
284+
$result[0].TargetAddress | Should -BeExactly $testAddress
285+
$result[0].Target | Should -BeExactly $testAddress
260286
$result[0].Hop | Should -Be 1
261-
$result[0].HopAddress | Should -BeExactly $realAddress
287+
$result[0].HopAddress.IPAddressToString | Should -BeExactly $testAddress
262288
$result[0].Status | Should -BeExactly "Success"
263289
if (!$IsWindows) {
264290
$result[0].Reply.Buffer.Count | Should -Match '^0$|^32$'
@@ -268,7 +294,7 @@ Describe "Test-Connection" -tags "CI" {
268294
}
269295

270296
It "Quiet works" {
271-
$result = Test-Connection $hostName -TraceRoute -Quiet
297+
$result = Test-Connection localhost -TraceRoute -Quiet
272298

273299
$result | Should -BeTrue
274300
}

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.