From eb1301f28d74a47ef657b1445c43f15037b26c4b Mon Sep 17 00:00:00 2001 From: Nate McMaster Date: Tue, 25 Apr 2017 14:33:06 -0700 Subject: [PATCH] Skip HostName binding test when network is unreachable --- .../AddressRegistrationTests.cs | 2 +- .../DnsIsReachableConditionAttribute.cs | 40 -------------- .../NetworkIsReachableAttribute.cs | 52 +++++++++++++++++++ 3 files changed, 53 insertions(+), 41 deletions(-) delete mode 100644 test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestHelpers/DnsIsReachableConditionAttribute.cs create mode 100644 test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestHelpers/NetworkIsReachableAttribute.cs diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs index 9d717d3935..abd6ec612c 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs @@ -34,7 +34,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests public AddressRegistrationTests(ITestOutputHelper output) => _logger = new LoggerFactory().AddXunit(output); [ConditionalFact] - [DnsHostNameIsResolvable] + [NetworkIsReachable] public async Task RegisterAddresses_HostName_Success() { var hostName = Dns.GetHostName(); diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestHelpers/DnsIsReachableConditionAttribute.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestHelpers/DnsIsReachableConditionAttribute.cs deleted file mode 100644 index 76bf6dfa41..0000000000 --- a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestHelpers/DnsIsReachableConditionAttribute.cs +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Linq; -using System.Net; -using Microsoft.AspNetCore.Testing.xunit; - -namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests -{ - [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] - public class DnsHostNameIsResolvableAttribute : Attribute, ITestCondition - { - private string _hostname; - - public bool IsMet - { - get - { - try - { - _hostname = Dns.GetHostName(); - var addresses = Dns.GetHostAddresses(_hostname); - if (addresses.Any(i => !IPAddress.IsLoopback(i))) - { - return true; - } - } - catch - { } - - return false; - } - } - - public string SkipReason => _hostname != null - ? $"Could not resolve any non-loopback IP address(es) for hostname '{_hostname}'" - : "Could not determine hostname for current test machine"; - } -} diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestHelpers/NetworkIsReachableAttribute.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestHelpers/NetworkIsReachableAttribute.cs new file mode 100644 index 0000000000..a17a321cba --- /dev/null +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/TestHelpers/NetworkIsReachableAttribute.cs @@ -0,0 +1,52 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using Microsoft.AspNetCore.Testing; +using Microsoft.AspNetCore.Testing.xunit; + +namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests +{ + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public class NetworkIsReachableAttribute : Attribute, ITestCondition + { + private string _hostname; + private string _error; + + public bool IsMet + { + get + { + try + { + _hostname = Dns.GetHostName(); + + // if the network is unreachable on macOS, throws with SocketError.NetworkUnreachable + // if the network device is not configured, throws with SocketError.HostNotFound + // if the network is reachable, throws with SocketError.ConnectionRefused or succeeds + HttpClientSlim.GetStringAsync($"http://{_hostname}").GetAwaiter().GetResult(); + } + catch (SocketException ex) when ( + ex.SocketErrorCode == SocketError.NetworkUnreachable + || ex.SocketErrorCode == SocketError.HostNotFound) + { + _error = ex.Message; + return false; + } + catch + { + // Swallow other errors. Allows the test to throw the failures instead + } + + return true; + } + } + + public string SkipReason => _hostname != null + ? $"Test cannot run when network is unreachable. Socket exception: '{_error}'" + : "Could not determine hostname for current test machine"; + } +}