|
1 | | -# Copyright (c) Microsoft Corporation. All rights reserved. |
| 1 | +# Copyright (c) Microsoft Corporation. |
2 | 2 | # Licensed under the MIT License. |
3 | 3 |
|
4 | 4 | Import-Module HelpersCommon |
5 | 5 |
|
| 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 | + |
6 | 35 | Describe "Test-Connection" -tags "CI" { |
7 | 36 | BeforeAll { |
8 | 37 | $hostName = [System.Net.Dns]::GetHostName() |
| 38 | + $gatewayAddress = GetGatewayAddress |
| 39 | + $publicHostAddress = GetExternalHostAddress -HostName $hostName |
| 40 | + |
| 41 | + $testAddress = if ($publicHostAddress) { $publicHostAddress } else { $gatewayAddress } |
| 42 | + |
9 | 43 | $targetName = "localhost" |
10 | 44 | $targetAddress = "127.0.0.1" |
11 | 45 | $targetAddressIPv6 = "::1" |
12 | 46 | $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 } |
19 | 47 | # under some environments, we can't round trip this and retrieve the real name from the address |
20 | 48 | # in this case we will simply use the hostname |
21 | 49 | $jobContinues = Start-Job { Test-Connection $using:targetAddress -Repeat } |
@@ -27,17 +55,17 @@ Describe "Test-Connection" -tags "CI" { |
27 | 55 | $pingResults.Count | Should -Be 4 |
28 | 56 |
|
29 | 57 | $result = $pingResults | |
30 | | - Where-Object Status -eq 'Success' | |
| 58 | + Where-Object Status -EQ 'Success' | |
31 | 59 | Select-Object -First 1 |
32 | 60 |
|
33 | | - $result | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus" |
| 61 | + $result | Should -BeOfType Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus |
34 | 62 | $result.Ping | Should -Be 1 |
35 | 63 | $result.Source | Should -BeExactly $hostName |
36 | 64 | $result.Destination | Should -BeExactly $targetName |
37 | 65 | $result.Address | Should -BeIn @($targetAddress, $targetAddressIPv6) |
38 | 66 | $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 |
41 | 69 | $result.BufferSize | Should -Be 32 |
42 | 70 | } |
43 | 71 |
|
@@ -60,7 +88,6 @@ Describe "Test-Connection" -tags "CI" { |
60 | 88 | } |
61 | 89 |
|
62 | 90 | It "Ping fake host" { |
63 | | - |
64 | 91 | { $result = Test-Connection "fakeHost" -Count 1 -Quiet -ErrorAction Stop } | |
65 | 92 | Should -Throw -ErrorId "TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand" |
66 | 93 | # Error code = 11001 - Host not found. |
@@ -76,35 +103,35 @@ Describe "Test-Connection" -tags "CI" { |
76 | 103 | $error[0].Exception.InnerException.ErrorCode | Should -Be $code |
77 | 104 | } |
78 | 105 |
|
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 |
82 | 108 |
|
83 | | - $result[0].Address | Should -BeExactly $realAddress |
| 109 | + $result[0].Address | Should -BeExactly $testAddress |
84 | 110 | $result[0].Reply.Options.Ttl | Should -BeLessOrEqual 128 |
85 | 111 | if ($IsWindows) { |
86 | 112 | $result[0].Reply.Options.DontFragment | Should -BeFalse |
87 | 113 | } |
88 | 114 | } |
89 | 115 |
|
90 | 116 | # 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 |
94 | 119 |
|
95 | 120 | # explicitly go to google dns. this test will pass even if the destination is unreachable |
96 | 121 | # it's more about breaking out of the loop |
97 | 122 | $result2 = Test-Connection 8.8.8.8 -Count 1 -IPv4 -MaxHops 1 -DontFragment |
98 | 123 |
|
99 | | - $result1.Address | Should -BeExactly $realAddress |
| 124 | + $result1.Address | Should -BeExactly $testAddress |
100 | 125 | $result1.Reply.Options.Ttl | Should -BeLessOrEqual 128 |
101 | 126 |
|
102 | 127 | if (!$IsWindows) { |
103 | 128 | $result1.Reply.Options.DontFragment | Should -BeFalse |
104 | 129 | # Depending on the network configuration any of the following should be returned |
105 | 130 | $result2.Status | Should -BeIn "TtlExpired", "TimedOut", "Success" |
106 | 131 | } 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 | + |
108 | 135 | # We expect 'TtlExpired' but if a router don't reply we get `TimedOut` |
109 | 136 | # AzPipelines returns $null |
110 | 137 | $result2.Status | Should -BeIn "TtlExpired", "TimedOut", $null |
@@ -117,7 +144,7 @@ Describe "Test-Connection" -tags "CI" { |
117 | 144 | # be a lack of or inconsistent support for IPv6 in CI environments. |
118 | 145 | It "Allows us to Force IPv6" -Pending { |
119 | 146 | $result = Test-Connection $targetName -IPv6 -Count 4 | |
120 | | - Where-Object Status -eq Success | |
| 147 | + Where-Object Status -EQ Success | |
121 | 148 | Select-Object -First 1 |
122 | 149 |
|
123 | 150 | $result.Address | Should -BeExactly $targetAddressIPv6 |
@@ -126,7 +153,7 @@ Describe "Test-Connection" -tags "CI" { |
126 | 153 |
|
127 | 154 | It 'can convert IPv6 addresses to IPv4 with -IPv4 parameter' -Pending { |
128 | 155 | $result = Test-Connection '2001:4860:4860::8888' -IPv4 -Count 4 | |
129 | | - Where-Object Status -eq Success | |
| 156 | + Where-Object Status -EQ Success | |
130 | 157 | Select-Object -First 1 |
131 | 158 | # Google's DNS can resolve to either address. |
132 | 159 | $result.Address.IPAddressToString | Should -BeIn @('8.8.8.8', '8.8.4.4') |
@@ -135,7 +162,7 @@ Describe "Test-Connection" -tags "CI" { |
135 | 162 |
|
136 | 163 | It 'can convert IPv4 addresses to IPv6 with -IPv6 parameter' -Pending { |
137 | 164 | $result = Test-Connection '8.8.8.8' -IPv6 -Count 4 | |
138 | | - Where-Object Status -eq Success | |
| 165 | + Where-Object Status -EQ Success | |
139 | 166 | Select-Object -First 1 |
140 | 167 | # Google's DNS can resolve to either address. |
141 | 168 | $result.Address.IPAddressToString | Should -BeIn @('2001:4860:4860::8888', '2001:4860:4860::8844') |
@@ -231,34 +258,33 @@ Describe "Test-Connection" -tags "CI" { |
231 | 258 | # We skip the MtuSize detection tests when in containers, as the environments throw raw exceptions |
232 | 259 | # instead of returning a PacketTooBig response cleanly. |
233 | 260 | It "MTUSizeDetect works" -Pending:($env:__INCONTAINER -eq 1) { |
234 | | - $result = Test-Connection $hostName -MtuSize |
| 261 | + $result = Test-Connection $testAddress -MtuSize |
235 | 262 |
|
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 |
238 | 265 | $result.Status | Should -BeExactly "Success" |
239 | 266 | $result.MtuSize | Should -BeGreaterThan 0 |
240 | 267 | } |
241 | 268 |
|
242 | 269 | It "Quiet works" -Pending:($env:__INCONTAINER -eq 1) { |
243 | | - $result = Test-Connection $hostName -MtuSize -Quiet |
| 270 | + $result = Test-Connection $gatewayAddress -MtuSize -Quiet |
244 | 271 |
|
245 | | - $result | Should -BeOfType "Int32" |
| 272 | + $result | Should -BeOfType Int32 |
246 | 273 | $result | Should -BeGreaterThan 0 |
247 | 274 | } |
248 | 275 | } |
249 | 276 |
|
250 | 277 | Context "TraceRoute" { |
251 | | - # Mark it as pending due to instability in Az DevOps |
252 | | - It "TraceRoute works" -Pending:($IsMacOS) { |
| 278 | + It "TraceRoute works" { |
253 | 279 | # real address is an ipv4 address, so force IPv4 |
254 | | - $result = Test-Connection $hostName -TraceRoute -IPv4 |
| 280 | + $result = Test-Connection $testAddress -TraceRoute -IPv4 |
255 | 281 |
|
256 | | - $result[0] | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus" |
| 282 | + $result[0] | Should -BeOfType Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus |
257 | 283 | $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 |
260 | 286 | $result[0].Hop | Should -Be 1 |
261 | | - $result[0].HopAddress | Should -BeExactly $realAddress |
| 287 | + $result[0].HopAddress.IPAddressToString | Should -BeExactly $testAddress |
262 | 288 | $result[0].Status | Should -BeExactly "Success" |
263 | 289 | if (!$IsWindows) { |
264 | 290 | $result[0].Reply.Buffer.Count | Should -Match '^0$|^32$' |
@@ -268,7 +294,7 @@ Describe "Test-Connection" -tags "CI" { |
268 | 294 | } |
269 | 295 |
|
270 | 296 | It "Quiet works" { |
271 | | - $result = Test-Connection $hostName -TraceRoute -Quiet |
| 297 | + $result = Test-Connection localhost -TraceRoute -Quiet |
272 | 298 |
|
273 | 299 | $result | Should -BeTrue |
274 | 300 | } |
|