From 05dccb316a38fe5b577fbade158a2027237dc8b1 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Date: Thu, 6 Jun 2024 15:36:41 -0700 Subject: [PATCH] [AzureMonitorDistro] Conditionally skip failing tests (#44423) * disable tests * Enable only DistroLogForwarderIsAdded * Enable only PublicLogForwarderIsAdded * Enable only SelfDiagnosticsIsDisabled * test conditionally skip * preprocessor --- ...itor.OpenTelemetry.AspNetCore.Tests.csproj | 1 + .../AzureSdkLoggingTests.cs | 13 ++++ .../CustomXunitAttributes.cs | 72 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/CommonTestFramework/CustomXunitAttributes.cs diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests.csproj b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests.csproj index b11f909daab51..7a6fd8cc1633b 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests.csproj +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests.csproj @@ -23,6 +23,7 @@ + diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/AzureSdkLoggingTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/AzureSdkLoggingTests.cs index 5c3489736bb44..d2b336bb29325 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/AzureSdkLoggingTests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/AzureSdkLoggingTests.cs @@ -16,12 +16,17 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; using Xunit; +using static Xunit.CustomXunitAttributes; namespace Azure.Monitor.OpenTelemetry.AspNetCore.Tests { public class AzureSdkLoggingTests { +#if NET6_0 + [ConditionallySkipOSTheory(platformToSkip: "macos", reason: "This test consistently exceeds 1 hour runtime limit when running on MacOS & Net60")] +#else [Theory] +#endif [InlineData(LogLevel.Information, "TestInfoEvent: hello")] [InlineData(LogLevel.Warning, "TestWarningEvent: hello")] [InlineData(LogLevel.Debug, null)] @@ -49,7 +54,11 @@ public async Task DistroLogForwarderIsAdded(LogLevel eventLevel, string expected } } +#if NET6_0 + [ConditionallySkipOSTheory(platformToSkip: "macos", reason: "This test consistently exceeds 1 hour runtime limit when running on MacOS & Net60")] +#else [Theory] +#endif [InlineData(LogLevel.Information, "TestInfoEvent: hello")] [InlineData(LogLevel.Warning, "TestWarningEvent: hello")] [InlineData(LogLevel.Debug, null)] @@ -86,7 +95,11 @@ public async Task PublicLogForwarderIsAdded(LogLevel eventLevel, string expected } } +#if NET6_0 + [ConditionallySkipOSFact(platformToSkip: "macos", reason: "This test consistently exceeds 1 hour runtime limit when running on MacOS & Net60")] +#else [Fact] +#endif public async Task SelfDiagnosticsIsDisabled() { var enableLevel = LogLevel.Debug; diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/CommonTestFramework/CustomXunitAttributes.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/CommonTestFramework/CustomXunitAttributes.cs new file mode 100644 index 0000000000000..8cf987512ae8e --- /dev/null +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/CommonTestFramework/CustomXunitAttributes.cs @@ -0,0 +1,72 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Runtime.InteropServices; + +namespace Xunit +{ + public class CustomXunitAttributes + { + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public class ConditionallySkipOSFactAttribute : FactAttribute + { + private readonly string _platformToSkip; + private readonly string _reason; + + public ConditionallySkipOSFactAttribute(string platformToSkip, string reason) + { + _platformToSkip = platformToSkip; + _reason = reason; + } + + public override string Skip + { + get => IsCurrentOS(_platformToSkip) + ? $"Test skipped on {_platformToSkip}. {_reason}" + : base.Skip; + set => base.Skip = value; + } + } + + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public class ConditionallySkipOSTheoryAttribute : TheoryAttribute + { + private readonly string _platformToSkip; + private readonly string _reason; + + public ConditionallySkipOSTheoryAttribute(string platformToSkip, string reason) + { + _platformToSkip = platformToSkip; + _reason = reason; + } + + public override string Skip + { + get + { + return IsCurrentOS(_platformToSkip) + ? $"Test skipped on {_platformToSkip}. {_reason}" + : base.Skip; + } + set => base.Skip = value; + } + } + + private static bool IsCurrentOS(string osName) + { + switch (osName.ToLowerInvariant()) + { + case "windows": + return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + case "linux": + return RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + case "osx": + case "macos": + return RuntimeInformation.IsOSPlatform(OSPlatform.OSX); + default: + throw new ArgumentException($"Unsupported OS name: {osName}"); + } + } + } +}